blob: ec244c9a6dd6c697e7ea0329ffa0d5cad451d241 [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.
Nithin Nayak Sujirb681b652013-01-06 12:51:10 +00007 * Copyright (C) 2005-2013 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>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000047#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000048#include <linux/hwmon.h>
49#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030052#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000056#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Matt Carlsonbe947302012-12-03 19:36:57 +000058#include <uapi/linux/net_tstamp.h>
59#include <linux/ptp_clock_kernel.h>
60
David S. Miller49b6e95f2007-03-29 01:38:42 -070061#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070063#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
Matt Carlson63532392008-11-03 16:49:57 -080066#define BAR_0 0
67#define BAR_2 2
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include "tg3.h"
70
Joe Perches63c3a662011-04-26 08:12:10 +000071/* Functions & macros to verify TG3_FLAGS types */
72
73static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 return test_bit(flag, bits);
76}
77
78static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 set_bit(flag, bits);
81}
82
83static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
84{
85 clear_bit(flag, bits);
86}
87
88#define tg3_flag(tp, flag) \
89 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
90#define tg3_flag_set(tp, flag) \
91 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
92#define tg3_flag_clear(tp, flag) \
93 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000096#define TG3_MAJ_NUM 3
Nithin Sujirc2bba062013-05-21 12:57:33 +000097#define TG3_MIN_NUM 132
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Sujirc2bba062013-05-21 12:57:33 +0000100#define DRV_MODULE_RELDATE "May 21, 2013"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000102#define RESET_KIND_SHUTDOWN 0
103#define RESET_KIND_INIT 1
104#define RESET_KIND_SUSPEND 2
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_MODE 0
107#define TG3_DEF_TX_MODE 0
108#define TG3_DEF_MSG_ENABLE \
109 (NETIF_MSG_DRV | \
110 NETIF_MSG_PROBE | \
111 NETIF_MSG_LINK | \
112 NETIF_MSG_TIMER | \
113 NETIF_MSG_IFDOWN | \
114 NETIF_MSG_IFUP | \
115 NETIF_MSG_RX_ERR | \
116 NETIF_MSG_TX_ERR)
117
Matt Carlson520b2752011-06-13 13:39:02 +0000118#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* length of time before we decide the hardware is borked,
121 * and dev->tx_timeout() should be called to fix the problem
122 */
Joe Perches63c3a662011-04-26 08:12:10 +0000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_TX_TIMEOUT (5 * HZ)
125
126/* hardware minimum and maximum for a single frame's data payload */
127#define TG3_MIN_MTU 60
128#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* These numbers seem to be hard coded in the NIC firmware somehow.
132 * You can't change the ring sizes, but you can change where you place
133 * them in the NIC onboard memory.
134 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000135#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000137 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000139#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000140 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000141 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define TG3_DEF_RX_JUMBO_RING_PENDING 100
143
144/* Do not place this n-ring entries value into the tp struct itself,
145 * we really want to expose these constants to GCC so that modulo et
146 * al. operations are done with shifts and masks instead of with
147 * hw multiply/modulo instructions. Another solution would be to
148 * replace things like '% foo' with '& (foo - 1)'.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define TG3_TX_RING_SIZE 512
152#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
153
Matt Carlson2c49a442010-09-30 10:34:35 +0000154#define TG3_RX_STD_RING_BYTES(tp) \
155 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
156#define TG3_RX_JMB_RING_BYTES(tp) \
157 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
158#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000159 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
161 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
163
Matt Carlson287be122009-08-28 13:58:46 +0000164#define TG3_DMA_BYTE_ENAB 64
165
166#define TG3_RX_STD_DMA_SZ 1536
167#define TG3_RX_JMB_DMA_SZ 9046
168
169#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
170
171#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
172#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Matt Carlson2c49a442010-09-30 10:34:35 +0000174#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
175 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000176
Matt Carlson2c49a442010-09-30 10:34:35 +0000177#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
178 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000179
Matt Carlsond2757fc2010-04-12 06:58:27 +0000180/* Due to a hardware bug, the 5701 can only DMA to memory addresses
181 * that are at least dword aligned when used in PCIX mode. The driver
182 * works around this bug by double copying the packet. This workaround
183 * is built into the normal double copy length check for efficiency.
184 *
185 * However, the double copy is only necessary on those architectures
186 * where unaligned memory accesses are inefficient. For those architectures
187 * where unaligned memory accesses incur little penalty, we can reintegrate
188 * the 5701 in the normal rx path. Doing so saves a device structure
189 * dereference by hardcoding the double copy threshold in place.
190 */
191#define TG3_RX_COPY_THRESHOLD 256
192#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
193 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
194#else
195 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
196#endif
197
Matt Carlson81389f52011-08-31 11:44:49 +0000198#if (NET_IP_ALIGN != 0)
199#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
200#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000201#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000202#endif
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000205#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000206#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000207#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Matt Carlsonad829262008-11-21 17:16:16 -0800209#define TG3_RAW_IP_ALIGN 2
210
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000211#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000212#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000213
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800214#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000215#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800216#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
217#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
218
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500219static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000220 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
223MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
224MODULE_LICENSE("GPL");
225MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800226MODULE_FIRMWARE(FIRMWARE_TG3);
227MODULE_FIRMWARE(FIRMWARE_TG3TSO);
228MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
231module_param(tg3_debug, int, 0);
232MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
233
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000234#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
235#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
236
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000237static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
257 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
258 TG3_DRV_DATA_FLAG_5705_10_100},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
260 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
261 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
264 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
265 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
272 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
278 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000286 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
287 PCI_VENDOR_ID_LENOVO,
288 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
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_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
292 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000311 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
312 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
313 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
314 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
315 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
316 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800317 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
320 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
330 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
332 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700340 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
341 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
342 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
343 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
344 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
345 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
346 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000347 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700348 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
351MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
352
Andreas Mohr50da8592006-08-14 23:54:30 -0700353static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000355} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 { "rx_octets" },
357 { "rx_fragments" },
358 { "rx_ucast_packets" },
359 { "rx_mcast_packets" },
360 { "rx_bcast_packets" },
361 { "rx_fcs_errors" },
362 { "rx_align_errors" },
363 { "rx_xon_pause_rcvd" },
364 { "rx_xoff_pause_rcvd" },
365 { "rx_mac_ctrl_rcvd" },
366 { "rx_xoff_entered" },
367 { "rx_frame_too_long_errors" },
368 { "rx_jabbers" },
369 { "rx_undersize_packets" },
370 { "rx_in_length_errors" },
371 { "rx_out_length_errors" },
372 { "rx_64_or_less_octet_packets" },
373 { "rx_65_to_127_octet_packets" },
374 { "rx_128_to_255_octet_packets" },
375 { "rx_256_to_511_octet_packets" },
376 { "rx_512_to_1023_octet_packets" },
377 { "rx_1024_to_1522_octet_packets" },
378 { "rx_1523_to_2047_octet_packets" },
379 { "rx_2048_to_4095_octet_packets" },
380 { "rx_4096_to_8191_octet_packets" },
381 { "rx_8192_to_9022_octet_packets" },
382
383 { "tx_octets" },
384 { "tx_collisions" },
385
386 { "tx_xon_sent" },
387 { "tx_xoff_sent" },
388 { "tx_flow_control" },
389 { "tx_mac_errors" },
390 { "tx_single_collisions" },
391 { "tx_mult_collisions" },
392 { "tx_deferred" },
393 { "tx_excessive_collisions" },
394 { "tx_late_collisions" },
395 { "tx_collide_2times" },
396 { "tx_collide_3times" },
397 { "tx_collide_4times" },
398 { "tx_collide_5times" },
399 { "tx_collide_6times" },
400 { "tx_collide_7times" },
401 { "tx_collide_8times" },
402 { "tx_collide_9times" },
403 { "tx_collide_10times" },
404 { "tx_collide_11times" },
405 { "tx_collide_12times" },
406 { "tx_collide_13times" },
407 { "tx_collide_14times" },
408 { "tx_collide_15times" },
409 { "tx_ucast_packets" },
410 { "tx_mcast_packets" },
411 { "tx_bcast_packets" },
412 { "tx_carrier_sense_errors" },
413 { "tx_discards" },
414 { "tx_errors" },
415
416 { "dma_writeq_full" },
417 { "dma_write_prioq_full" },
418 { "rxbds_empty" },
419 { "rx_discards" },
420 { "rx_errors" },
421 { "rx_threshold_hit" },
422
423 { "dma_readq_full" },
424 { "dma_read_prioq_full" },
425 { "tx_comp_queue_full" },
426
427 { "ring_set_send_prod_index" },
428 { "ring_status_update" },
429 { "nic_irqs" },
430 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000431 { "nic_tx_threshold_hit" },
432
433 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
435
Matt Carlson48fa55a2011-04-13 11:05:06 +0000436#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000437#define TG3_NVRAM_TEST 0
438#define TG3_LINK_TEST 1
439#define TG3_REGISTER_TEST 2
440#define TG3_MEMORY_TEST 3
441#define TG3_MAC_LOOPB_TEST 4
442#define TG3_PHY_LOOPB_TEST 5
443#define TG3_EXT_LOOPB_TEST 6
444#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000445
446
Andreas Mohr50da8592006-08-14 23:54:30 -0700447static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700448 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000449} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000450 [TG3_NVRAM_TEST] = { "nvram test (online) " },
451 [TG3_LINK_TEST] = { "link test (online) " },
452 [TG3_REGISTER_TEST] = { "register test (offline)" },
453 [TG3_MEMORY_TEST] = { "memory test (offline)" },
454 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
455 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
456 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
457 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700458};
459
Matt Carlson48fa55a2011-04-13 11:05:06 +0000460#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
461
462
Michael Chanb401e9e2005-12-19 16:27:04 -0800463static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
464{
465 writel(val, tp->regs + off);
466}
467
468static u32 tg3_read32(struct tg3 *tp, u32 off)
469{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000470 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800471}
472
Matt Carlson0d3031d2007-10-10 18:02:43 -0700473static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
474{
475 writel(val, tp->aperegs + off);
476}
477
478static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
479{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000480 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
484{
Michael Chan68929142005-08-09 20:17:14 -0700485 unsigned long flags;
486
487 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700488 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
489 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700490 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700491}
492
493static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
494{
495 writel(val, tp->regs + off);
496 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Michael Chan68929142005-08-09 20:17:14 -0700499static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
500{
501 unsigned long flags;
502 u32 val;
503
504 spin_lock_irqsave(&tp->indirect_lock, flags);
505 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
506 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
507 spin_unlock_irqrestore(&tp->indirect_lock, flags);
508 return val;
509}
510
511static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
512{
513 unsigned long flags;
514
515 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
516 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
517 TG3_64BIT_REG_LOW, val);
518 return;
519 }
Matt Carlson66711e62009-11-13 13:03:49 +0000520 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700521 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
522 TG3_64BIT_REG_LOW, val);
523 return;
524 }
525
526 spin_lock_irqsave(&tp->indirect_lock, flags);
527 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
528 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
529 spin_unlock_irqrestore(&tp->indirect_lock, flags);
530
531 /* In indirect mode when disabling interrupts, we also need
532 * to clear the interrupt bit in the GRC local ctrl register.
533 */
534 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
535 (val == 0x1)) {
536 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
537 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
538 }
539}
540
541static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
542{
543 unsigned long flags;
544 u32 val;
545
546 spin_lock_irqsave(&tp->indirect_lock, flags);
547 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
548 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
549 spin_unlock_irqrestore(&tp->indirect_lock, flags);
550 return val;
551}
552
Michael Chanb401e9e2005-12-19 16:27:04 -0800553/* usec_wait specifies the wait time in usec when writing to certain registers
554 * where it is unsafe to read back the register without some delay.
555 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
556 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
557 */
558static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Joe Perches63c3a662011-04-26 08:12:10 +0000560 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800561 /* Non-posted methods */
562 tp->write32(tp, off, val);
563 else {
564 /* Posted method */
565 tg3_write32(tp, off, val);
566 if (usec_wait)
567 udelay(usec_wait);
568 tp->read32(tp, off);
569 }
570 /* Wait again after the read for the posted method to guarantee that
571 * the wait time is met.
572 */
573 if (usec_wait)
574 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Michael Chan09ee9292005-08-09 20:17:00 -0700577static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
578{
579 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000580 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
581 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
582 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700583 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700584}
585
Michael Chan20094932005-08-09 20:16:32 -0700586static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 void __iomem *mbox = tp->regs + off;
589 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000590 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000592 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
593 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 readl(mbox);
595}
596
Michael Chanb5d37722006-09-27 16:06:21 -0700597static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
598{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000599 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700600}
601
602static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
603{
604 writel(val, tp->regs + off + GRCMBOX_BASE);
605}
606
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000607#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700608#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000609#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
610#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
611#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700612
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000613#define tw32(reg, val) tp->write32(tp, reg, val)
614#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
615#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
616#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
619{
Michael Chan68929142005-08-09 20:17:14 -0700620 unsigned long flags;
621
Joe Perches41535772013-02-16 11:20:04 +0000622 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700623 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
624 return;
625
Michael Chan68929142005-08-09 20:17:14 -0700626 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000627 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700628 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
629 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Michael Chanbbadf502006-04-06 21:46:34 -0700631 /* Always leave this as zero. */
632 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
633 } else {
634 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
635 tw32_f(TG3PCI_MEM_WIN_DATA, val);
636
637 /* Always leave this as zero. */
638 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
639 }
Michael Chan68929142005-08-09 20:17:14 -0700640 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
644{
Michael Chan68929142005-08-09 20:17:14 -0700645 unsigned long flags;
646
Joe Perches41535772013-02-16 11:20:04 +0000647 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700648 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
649 *val = 0;
650 return;
651 }
652
Michael Chan68929142005-08-09 20:17:14 -0700653 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000654 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700655 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
656 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Michael Chanbbadf502006-04-06 21:46:34 -0700658 /* Always leave this as zero. */
659 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
660 } else {
661 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
662 *val = tr32(TG3PCI_MEM_WIN_DATA);
663
664 /* Always leave this as zero. */
665 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
666 }
Michael Chan68929142005-08-09 20:17:14 -0700667 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Matt Carlson0d3031d2007-10-10 18:02:43 -0700670static void tg3_ape_lock_init(struct tg3 *tp)
671{
672 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000673 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674
Joe Perches41535772013-02-16 11:20:04 +0000675 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000676 regbase = TG3_APE_LOCK_GRANT;
677 else
678 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700679
680 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000681 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
682 switch (i) {
683 case TG3_APE_LOCK_PHY0:
684 case TG3_APE_LOCK_PHY1:
685 case TG3_APE_LOCK_PHY2:
686 case TG3_APE_LOCK_PHY3:
687 bit = APE_LOCK_GRANT_DRIVER;
688 break;
689 default:
690 if (!tp->pci_fn)
691 bit = APE_LOCK_GRANT_DRIVER;
692 else
693 bit = 1 << tp->pci_fn;
694 }
695 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 }
697
Matt Carlson0d3031d2007-10-10 18:02:43 -0700698}
699
700static int tg3_ape_lock(struct tg3 *tp, int locknum)
701{
702 int i, off;
703 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000704 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700705
Joe Perches63c3a662011-04-26 08:12:10 +0000706 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707 return 0;
708
709 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000710 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000711 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000713 case TG3_APE_LOCK_GRC:
714 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000715 if (!tp->pci_fn)
716 bit = APE_LOCK_REQ_DRIVER;
717 else
718 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000719 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000720 case TG3_APE_LOCK_PHY0:
721 case TG3_APE_LOCK_PHY1:
722 case TG3_APE_LOCK_PHY2:
723 case TG3_APE_LOCK_PHY3:
724 bit = APE_LOCK_REQ_DRIVER;
725 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000726 default:
727 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700728 }
729
Joe Perches41535772013-02-16 11:20:04 +0000730 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000731 req = TG3_APE_LOCK_REQ;
732 gnt = TG3_APE_LOCK_GRANT;
733 } else {
734 req = TG3_APE_PER_LOCK_REQ;
735 gnt = TG3_APE_PER_LOCK_GRANT;
736 }
737
Matt Carlson0d3031d2007-10-10 18:02:43 -0700738 off = 4 * locknum;
739
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000740 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700741
742 /* Wait for up to 1 millisecond to acquire lock. */
743 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000744 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000745 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800747 if (pci_channel_offline(tp->pdev))
748 break;
749
Matt Carlson0d3031d2007-10-10 18:02:43 -0700750 udelay(10);
751 }
752
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000753 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700754 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000755 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700756 ret = -EBUSY;
757 }
758
759 return ret;
760}
761
762static void tg3_ape_unlock(struct tg3 *tp, int locknum)
763{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000764 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700765
Joe Perches63c3a662011-04-26 08:12:10 +0000766 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700767 return;
768
769 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000770 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000771 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000772 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000773 case TG3_APE_LOCK_GRC:
774 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000775 if (!tp->pci_fn)
776 bit = APE_LOCK_GRANT_DRIVER;
777 else
778 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000779 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000780 case TG3_APE_LOCK_PHY0:
781 case TG3_APE_LOCK_PHY1:
782 case TG3_APE_LOCK_PHY2:
783 case TG3_APE_LOCK_PHY3:
784 bit = APE_LOCK_GRANT_DRIVER;
785 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000786 default:
787 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700788 }
789
Joe Perches41535772013-02-16 11:20:04 +0000790 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000791 gnt = TG3_APE_LOCK_GRANT;
792 else
793 gnt = TG3_APE_PER_LOCK_GRANT;
794
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000795 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700796}
797
Matt Carlsonb65a3722012-07-16 16:24:00 +0000798static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000799{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000800 u32 apedata;
801
Matt Carlsonb65a3722012-07-16 16:24:00 +0000802 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000803 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000804 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000805
806 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000807 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
808 break;
809
Matt Carlsonb65a3722012-07-16 16:24:00 +0000810 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
811
812 udelay(10);
813 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000814 }
815
Matt Carlsonb65a3722012-07-16 16:24:00 +0000816 return timeout_us ? 0 : -EBUSY;
817}
818
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000819static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
820{
821 u32 i, apedata;
822
823 for (i = 0; i < timeout_us / 10; i++) {
824 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
825
826 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
827 break;
828
829 udelay(10);
830 }
831
832 return i == timeout_us / 10;
833}
834
Michael Chan86449942012-10-02 20:31:14 -0700835static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
836 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000837{
838 int err;
839 u32 i, bufoff, msgoff, maxlen, apedata;
840
841 if (!tg3_flag(tp, APE_HAS_NCSI))
842 return 0;
843
844 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
845 if (apedata != APE_SEG_SIG_MAGIC)
846 return -ENODEV;
847
848 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
849 if (!(apedata & APE_FW_STATUS_READY))
850 return -EAGAIN;
851
852 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
853 TG3_APE_SHMEM_BASE;
854 msgoff = bufoff + 2 * sizeof(u32);
855 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
856
857 while (len) {
858 u32 length;
859
860 /* Cap xfer sizes to scratchpad limits. */
861 length = (len > maxlen) ? maxlen : len;
862 len -= length;
863
864 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
865 if (!(apedata & APE_FW_STATUS_READY))
866 return -EAGAIN;
867
868 /* Wait for up to 1 msec for APE to service previous event. */
869 err = tg3_ape_event_lock(tp, 1000);
870 if (err)
871 return err;
872
873 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
874 APE_EVENT_STATUS_SCRTCHPD_READ |
875 APE_EVENT_STATUS_EVENT_PENDING;
876 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
877
878 tg3_ape_write32(tp, bufoff, base_off);
879 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
880
881 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
882 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
883
884 base_off += length;
885
886 if (tg3_ape_wait_for_event(tp, 30000))
887 return -EAGAIN;
888
889 for (i = 0; length; i += 4, length -= 4) {
890 u32 val = tg3_ape_read32(tp, msgoff + i);
891 memcpy(data, &val, sizeof(u32));
892 data++;
893 }
894 }
895
896 return 0;
897}
898
Matt Carlsonb65a3722012-07-16 16:24:00 +0000899static int tg3_ape_send_event(struct tg3 *tp, u32 event)
900{
901 int err;
902 u32 apedata;
903
904 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
905 if (apedata != APE_SEG_SIG_MAGIC)
906 return -EAGAIN;
907
908 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
909 if (!(apedata & APE_FW_STATUS_READY))
910 return -EAGAIN;
911
912 /* Wait for up to 1 millisecond for APE to service previous event. */
913 err = tg3_ape_event_lock(tp, 1000);
914 if (err)
915 return err;
916
917 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
918 event | APE_EVENT_STATUS_EVENT_PENDING);
919
920 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
921 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
922
923 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000924}
925
926static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
927{
928 u32 event;
929 u32 apedata;
930
931 if (!tg3_flag(tp, ENABLE_APE))
932 return;
933
934 switch (kind) {
935 case RESET_KIND_INIT:
936 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
937 APE_HOST_SEG_SIG_MAGIC);
938 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
939 APE_HOST_SEG_LEN_MAGIC);
940 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
941 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
942 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
943 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
944 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
945 APE_HOST_BEHAV_NO_PHYLOCK);
946 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
947 TG3_APE_HOST_DRVR_STATE_START);
948
949 event = APE_EVENT_STATUS_STATE_START;
950 break;
951 case RESET_KIND_SHUTDOWN:
952 /* With the interface we are currently using,
953 * APE does not track driver state. Wiping
954 * out the HOST SEGMENT SIGNATURE forces
955 * the APE to assume OS absent status.
956 */
957 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
958
959 if (device_may_wakeup(&tp->pdev->dev) &&
960 tg3_flag(tp, WOL_ENABLE)) {
961 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
962 TG3_APE_HOST_WOL_SPEED_AUTO);
963 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
964 } else
965 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
966
967 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
968
969 event = APE_EVENT_STATUS_STATE_UNLOAD;
970 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000971 default:
972 return;
973 }
974
975 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
976
977 tg3_ape_send_event(tp, event);
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static void tg3_disable_ints(struct tg3 *tp)
981{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000982 int i;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 tw32(TG3PCI_MISC_HOST_CTRL,
985 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000986 for (i = 0; i < tp->irq_max; i++)
987 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990static void tg3_enable_ints(struct tg3 *tp)
991{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000992 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000993
Michael Chanbbe832c2005-06-24 20:20:04 -0700994 tp->irq_sync = 0;
995 wmb();
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 tw32(TG3PCI_MISC_HOST_CTRL,
998 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000999
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001000 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001 for (i = 0; i < tp->irq_cnt; i++) {
1002 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001003
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001004 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001005 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001006 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1007
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001008 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001010
1011 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001012 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001013 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1014 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1015 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001016 tw32(HOSTCC_MODE, tp->coal_now);
1017
1018 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Matt Carlson17375d22009-08-28 14:02:18 +00001021static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001022{
Matt Carlson17375d22009-08-28 14:02:18 +00001023 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001024 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001025 unsigned int work_exists = 0;
1026
1027 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001028 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001029 if (sblk->status & SD_STATUS_LINK_CHG)
1030 work_exists = 1;
1031 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001032
1033 /* check for TX work to do */
1034 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1035 work_exists = 1;
1036
1037 /* check for RX work to do */
1038 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001039 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001040 work_exists = 1;
1041
1042 return work_exists;
1043}
1044
Matt Carlson17375d22009-08-28 14:02:18 +00001045/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001046 * similar to tg3_enable_ints, but it accurately determines whether there
1047 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001048 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Matt Carlson17375d22009-08-28 14:02:18 +00001050static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Matt Carlson17375d22009-08-28 14:02:18 +00001052 struct tg3 *tp = tnapi->tp;
1053
Matt Carlson898a56f2009-08-28 14:02:40 +00001054 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 mmiowb();
1056
David S. Millerfac9b832005-05-18 22:46:34 -07001057 /* When doing tagged status, this work check is unnecessary.
1058 * The last_tag we write above tells the chip which piece of
1059 * work we've completed.
1060 */
Joe Perches63c3a662011-04-26 08:12:10 +00001061 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001062 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001063 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066static void tg3_switch_clocks(struct tg3 *tp)
1067{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001068 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 u32 orig_clock_ctrl;
1070
Joe Perches63c3a662011-04-26 08:12:10 +00001071 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001072 return;
1073
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001074 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 orig_clock_ctrl = clock_ctrl;
1077 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1078 CLOCK_CTRL_CLKRUN_OENABLE |
1079 0x1f);
1080 tp->pci_clock_ctrl = clock_ctrl;
1081
Joe Perches63c3a662011-04-26 08:12:10 +00001082 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001084 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1085 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001088 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1089 clock_ctrl |
1090 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1091 40);
1092 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1093 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1094 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001096 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099#define PHY_BUSY_LOOPS 5000
1100
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001101static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1102 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 u32 frame_val;
1105 unsigned int loops;
1106 int ret;
1107
1108 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1109 tw32_f(MAC_MI_MODE,
1110 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1111 udelay(80);
1112 }
1113
Michael Chan8151ad52012-07-29 19:15:41 +00001114 tg3_ape_lock(tp, tp->phy_ape_lock);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *val = 0x0;
1117
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001118 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 MI_COM_PHY_ADDR_MASK);
1120 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1121 MI_COM_REG_ADDR_MASK);
1122 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 tw32_f(MAC_MI_COM, frame_val);
1125
1126 loops = PHY_BUSY_LOOPS;
1127 while (loops != 0) {
1128 udelay(10);
1129 frame_val = tr32(MAC_MI_COM);
1130
1131 if ((frame_val & MI_COM_BUSY) == 0) {
1132 udelay(5);
1133 frame_val = tr32(MAC_MI_COM);
1134 break;
1135 }
1136 loops -= 1;
1137 }
1138
1139 ret = -EBUSY;
1140 if (loops != 0) {
1141 *val = frame_val & MI_COM_DATA_MASK;
1142 ret = 0;
1143 }
1144
1145 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1146 tw32_f(MAC_MI_MODE, tp->mi_mode);
1147 udelay(80);
1148 }
1149
Michael Chan8151ad52012-07-29 19:15:41 +00001150 tg3_ape_unlock(tp, tp->phy_ape_lock);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return ret;
1153}
1154
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001155static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1156{
1157 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1158}
1159
1160static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1161 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 u32 frame_val;
1164 unsigned int loops;
1165 int ret;
1166
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001167 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001168 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001169 return 0;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1172 tw32_f(MAC_MI_MODE,
1173 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1174 udelay(80);
1175 }
1176
Michael Chan8151ad52012-07-29 19:15:41 +00001177 tg3_ape_lock(tp, tp->phy_ape_lock);
1178
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001179 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 MI_COM_PHY_ADDR_MASK);
1181 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1182 MI_COM_REG_ADDR_MASK);
1183 frame_val |= (val & MI_COM_DATA_MASK);
1184 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 tw32_f(MAC_MI_COM, frame_val);
1187
1188 loops = PHY_BUSY_LOOPS;
1189 while (loops != 0) {
1190 udelay(10);
1191 frame_val = tr32(MAC_MI_COM);
1192 if ((frame_val & MI_COM_BUSY) == 0) {
1193 udelay(5);
1194 frame_val = tr32(MAC_MI_COM);
1195 break;
1196 }
1197 loops -= 1;
1198 }
1199
1200 ret = -EBUSY;
1201 if (loops != 0)
1202 ret = 0;
1203
1204 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1205 tw32_f(MAC_MI_MODE, tp->mi_mode);
1206 udelay(80);
1207 }
1208
Michael Chan8151ad52012-07-29 19:15:41 +00001209 tg3_ape_unlock(tp, tp->phy_ape_lock);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return ret;
1212}
1213
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001214static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1215{
1216 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1217}
1218
Matt Carlsonb0988c12011-04-20 07:57:39 +00001219static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1220{
1221 int err;
1222
1223 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1224 if (err)
1225 goto done;
1226
1227 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1228 if (err)
1229 goto done;
1230
1231 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1232 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1233 if (err)
1234 goto done;
1235
1236 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1237
1238done:
1239 return err;
1240}
1241
1242static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1243{
1244 int err;
1245
1246 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1247 if (err)
1248 goto done;
1249
1250 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1251 if (err)
1252 goto done;
1253
1254 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1255 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1256 if (err)
1257 goto done;
1258
1259 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1260
1261done:
1262 return err;
1263}
1264
1265static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1266{
1267 int err;
1268
1269 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1270 if (!err)
1271 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1272
1273 return err;
1274}
1275
1276static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1277{
1278 int err;
1279
1280 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1281 if (!err)
1282 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1283
1284 return err;
1285}
1286
Matt Carlson15ee95c2011-04-20 07:57:40 +00001287static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1288{
1289 int err;
1290
1291 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1292 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1293 MII_TG3_AUXCTL_SHDWSEL_MISC);
1294 if (!err)
1295 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1296
1297 return err;
1298}
1299
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001300static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1301{
1302 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1303 set |= MII_TG3_AUXCTL_MISC_WREN;
1304
1305 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1306}
1307
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001308static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1309{
1310 u32 val;
1311 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001312
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001313 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1314
1315 if (err)
1316 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001317
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001318 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001319 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1320 else
1321 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1322
1323 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1324 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1325
1326 return err;
1327}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001328
Matt Carlson95e28692008-05-25 23:44:14 -07001329static int tg3_bmcr_reset(struct tg3 *tp)
1330{
1331 u32 phy_control;
1332 int limit, err;
1333
1334 /* OK, reset it, and poll the BMCR_RESET bit until it
1335 * clears or we time out.
1336 */
1337 phy_control = BMCR_RESET;
1338 err = tg3_writephy(tp, MII_BMCR, phy_control);
1339 if (err != 0)
1340 return -EBUSY;
1341
1342 limit = 5000;
1343 while (limit--) {
1344 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1345 if (err != 0)
1346 return -EBUSY;
1347
1348 if ((phy_control & BMCR_RESET) == 0) {
1349 udelay(40);
1350 break;
1351 }
1352 udelay(10);
1353 }
Roel Kluind4675b52009-02-12 16:33:27 -08001354 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001355 return -EBUSY;
1356
1357 return 0;
1358}
1359
Matt Carlson158d7ab2008-05-29 01:37:54 -07001360static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1361{
Francois Romieu3d165432009-01-19 16:56:50 -08001362 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001363 u32 val;
1364
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001365 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001366
1367 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001368 val = -EIO;
1369
1370 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001371
1372 return val;
1373}
1374
1375static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1376{
Francois Romieu3d165432009-01-19 16:56:50 -08001377 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001378 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001379
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001380 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001381
1382 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001383 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001384
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001385 spin_unlock_bh(&tp->lock);
1386
1387 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001388}
1389
1390static int tg3_mdio_reset(struct mii_bus *bp)
1391{
1392 return 0;
1393}
1394
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001395static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001396{
1397 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001398 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001399
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001400 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001401 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001402 case PHY_ID_BCM50610:
1403 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001404 val = MAC_PHYCFG2_50610_LED_MODES;
1405 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001406 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001407 val = MAC_PHYCFG2_AC131_LED_MODES;
1408 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001409 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001410 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1411 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001412 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001413 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1414 break;
1415 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001416 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001417 }
1418
1419 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1420 tw32(MAC_PHYCFG2, val);
1421
1422 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001423 val &= ~(MAC_PHYCFG1_RGMII_INT |
1424 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1425 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001426 tw32(MAC_PHYCFG1, val);
1427
1428 return;
1429 }
1430
Joe Perches63c3a662011-04-26 08:12:10 +00001431 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001432 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1433 MAC_PHYCFG2_FMODE_MASK_MASK |
1434 MAC_PHYCFG2_GMODE_MASK_MASK |
1435 MAC_PHYCFG2_ACT_MASK_MASK |
1436 MAC_PHYCFG2_QUAL_MASK_MASK |
1437 MAC_PHYCFG2_INBAND_ENABLE;
1438
1439 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001440
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001441 val = tr32(MAC_PHYCFG1);
1442 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1443 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001444 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1445 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001446 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001447 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001448 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1449 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001450 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1451 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1452 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001453
Matt Carlsona9daf362008-05-25 23:49:44 -07001454 val = tr32(MAC_EXT_RGMII_MODE);
1455 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1456 MAC_RGMII_MODE_RX_QUALITY |
1457 MAC_RGMII_MODE_RX_ACTIVITY |
1458 MAC_RGMII_MODE_RX_ENG_DET |
1459 MAC_RGMII_MODE_TX_ENABLE |
1460 MAC_RGMII_MODE_TX_LOWPWR |
1461 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001462 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1463 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001464 val |= MAC_RGMII_MODE_RX_INT_B |
1465 MAC_RGMII_MODE_RX_QUALITY |
1466 MAC_RGMII_MODE_RX_ACTIVITY |
1467 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001468 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001469 val |= MAC_RGMII_MODE_TX_ENABLE |
1470 MAC_RGMII_MODE_TX_LOWPWR |
1471 MAC_RGMII_MODE_TX_RESET;
1472 }
1473 tw32(MAC_EXT_RGMII_MODE, val);
1474}
1475
Matt Carlson158d7ab2008-05-29 01:37:54 -07001476static void tg3_mdio_start(struct tg3 *tp)
1477{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001478 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1479 tw32_f(MAC_MI_MODE, tp->mi_mode);
1480 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001481
Joe Perches63c3a662011-04-26 08:12:10 +00001482 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001483 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001484 tg3_mdio_config_5785(tp);
1485}
1486
1487static int tg3_mdio_init(struct tg3 *tp)
1488{
1489 int i;
1490 u32 reg;
1491 struct phy_device *phydev;
1492
Joe Perches63c3a662011-04-26 08:12:10 +00001493 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001494 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001495
Matt Carlson69f11c92011-07-13 09:27:30 +00001496 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001497
Joe Perches41535772013-02-16 11:20:04 +00001498 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001499 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1500 else
1501 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1502 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001503 if (is_serdes)
1504 tp->phy_addr += 7;
1505 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001506 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001507
Matt Carlson158d7ab2008-05-29 01:37:54 -07001508 tg3_mdio_start(tp);
1509
Joe Perches63c3a662011-04-26 08:12:10 +00001510 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001511 return 0;
1512
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001513 tp->mdio_bus = mdiobus_alloc();
1514 if (tp->mdio_bus == NULL)
1515 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001516
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001517 tp->mdio_bus->name = "tg3 mdio bus";
1518 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001519 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001520 tp->mdio_bus->priv = tp;
1521 tp->mdio_bus->parent = &tp->pdev->dev;
1522 tp->mdio_bus->read = &tg3_mdio_read;
1523 tp->mdio_bus->write = &tg3_mdio_write;
1524 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001525 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001526 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001527
1528 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001529 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001530
1531 /* The bus registration will look for all the PHYs on the mdio bus.
1532 * Unfortunately, it does not ensure the PHY is powered up before
1533 * accessing the PHY ID registers. A chip reset is the
1534 * quickest way to bring the device back to an operational state..
1535 */
1536 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1537 tg3_bmcr_reset(tp);
1538
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001539 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001540 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001541 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001542 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001543 return i;
1544 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001545
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001546 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001547
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001548 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001549 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001550 mdiobus_unregister(tp->mdio_bus);
1551 mdiobus_free(tp->mdio_bus);
1552 return -ENODEV;
1553 }
1554
1555 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001556 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001557 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001558 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001559 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001560 case PHY_ID_BCM50610:
1561 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001562 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001563 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001564 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001565 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001566 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001567 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001568 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001569 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001570 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001571 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001572 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001573 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001574 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001575 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001576 case PHY_ID_RTL8201E:
1577 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001578 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001579 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001580 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001581 break;
1582 }
1583
Joe Perches63c3a662011-04-26 08:12:10 +00001584 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001585
Joe Perches41535772013-02-16 11:20:04 +00001586 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001587 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001588
1589 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001590}
1591
1592static void tg3_mdio_fini(struct tg3 *tp)
1593{
Joe Perches63c3a662011-04-26 08:12:10 +00001594 if (tg3_flag(tp, MDIOBUS_INITED)) {
1595 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001596 mdiobus_unregister(tp->mdio_bus);
1597 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001598 }
1599}
1600
Matt Carlson95e28692008-05-25 23:44:14 -07001601/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001602static inline void tg3_generate_fw_event(struct tg3 *tp)
1603{
1604 u32 val;
1605
1606 val = tr32(GRC_RX_CPU_EVENT);
1607 val |= GRC_RX_CPU_DRIVER_EVENT;
1608 tw32_f(GRC_RX_CPU_EVENT, val);
1609
1610 tp->last_event_jiffies = jiffies;
1611}
1612
1613#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1614
1615/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001616static void tg3_wait_for_event_ack(struct tg3 *tp)
1617{
1618 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001619 unsigned int delay_cnt;
1620 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001621
Matt Carlson4ba526c2008-08-15 14:10:04 -07001622 /* If enough time has passed, no wait is necessary. */
1623 time_remain = (long)(tp->last_event_jiffies + 1 +
1624 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1625 (long)jiffies;
1626 if (time_remain < 0)
1627 return;
1628
1629 /* Check if we can shorten the wait time. */
1630 delay_cnt = jiffies_to_usecs(time_remain);
1631 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1632 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1633 delay_cnt = (delay_cnt >> 3) + 1;
1634
1635 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001636 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1637 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001638 if (pci_channel_offline(tp->pdev))
1639 break;
1640
Matt Carlson4ba526c2008-08-15 14:10:04 -07001641 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001642 }
1643}
1644
1645/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001646static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001647{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001648 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001649
1650 val = 0;
1651 if (!tg3_readphy(tp, MII_BMCR, &reg))
1652 val = reg << 16;
1653 if (!tg3_readphy(tp, MII_BMSR, &reg))
1654 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001655 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001656
1657 val = 0;
1658 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1659 val = reg << 16;
1660 if (!tg3_readphy(tp, MII_LPA, &reg))
1661 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001662 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001663
1664 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001665 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001666 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1667 val = reg << 16;
1668 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1669 val |= (reg & 0xffff);
1670 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001671 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001672
1673 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1674 val = reg << 16;
1675 else
1676 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001677 *data++ = val;
1678}
1679
1680/* tp->lock is held. */
1681static void tg3_ump_link_report(struct tg3 *tp)
1682{
1683 u32 data[4];
1684
1685 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1686 return;
1687
1688 tg3_phy_gather_ump_data(tp, data);
1689
1690 tg3_wait_for_event_ack(tp);
1691
1692 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1693 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1694 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1695 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1696 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1697 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001698
Matt Carlson4ba526c2008-08-15 14:10:04 -07001699 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001700}
1701
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001702/* tp->lock is held. */
1703static void tg3_stop_fw(struct tg3 *tp)
1704{
1705 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1706 /* Wait for RX cpu to ACK the previous event. */
1707 tg3_wait_for_event_ack(tp);
1708
1709 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1710
1711 tg3_generate_fw_event(tp);
1712
1713 /* Wait for RX cpu to ACK this event. */
1714 tg3_wait_for_event_ack(tp);
1715 }
1716}
1717
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001718/* tp->lock is held. */
1719static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1720{
1721 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1722 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1723
1724 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1725 switch (kind) {
1726 case RESET_KIND_INIT:
1727 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1728 DRV_STATE_START);
1729 break;
1730
1731 case RESET_KIND_SHUTDOWN:
1732 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1733 DRV_STATE_UNLOAD);
1734 break;
1735
1736 case RESET_KIND_SUSPEND:
1737 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1738 DRV_STATE_SUSPEND);
1739 break;
1740
1741 default:
1742 break;
1743 }
1744 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001745}
1746
1747/* tp->lock is held. */
1748static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1749{
1750 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1751 switch (kind) {
1752 case RESET_KIND_INIT:
1753 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1754 DRV_STATE_START_DONE);
1755 break;
1756
1757 case RESET_KIND_SHUTDOWN:
1758 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1759 DRV_STATE_UNLOAD_DONE);
1760 break;
1761
1762 default:
1763 break;
1764 }
1765 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001766}
1767
1768/* tp->lock is held. */
1769static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1770{
1771 if (tg3_flag(tp, ENABLE_ASF)) {
1772 switch (kind) {
1773 case RESET_KIND_INIT:
1774 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1775 DRV_STATE_START);
1776 break;
1777
1778 case RESET_KIND_SHUTDOWN:
1779 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1780 DRV_STATE_UNLOAD);
1781 break;
1782
1783 case RESET_KIND_SUSPEND:
1784 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1785 DRV_STATE_SUSPEND);
1786 break;
1787
1788 default:
1789 break;
1790 }
1791 }
1792}
1793
1794static int tg3_poll_fw(struct tg3 *tp)
1795{
1796 int i;
1797 u32 val;
1798
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001799 if (tg3_flag(tp, NO_FWARE_REPORTED))
1800 return 0;
1801
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001802 if (tg3_flag(tp, IS_SSB_CORE)) {
1803 /* We don't use firmware. */
1804 return 0;
1805 }
1806
Joe Perches41535772013-02-16 11:20:04 +00001807 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001808 /* Wait up to 20ms for init done. */
1809 for (i = 0; i < 200; i++) {
1810 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1811 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001812 if (pci_channel_offline(tp->pdev))
1813 return -ENODEV;
1814
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001815 udelay(100);
1816 }
1817 return -ENODEV;
1818 }
1819
1820 /* Wait for firmware initialization to complete. */
1821 for (i = 0; i < 100000; i++) {
1822 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1823 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1824 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001825 if (pci_channel_offline(tp->pdev)) {
1826 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1827 tg3_flag_set(tp, NO_FWARE_REPORTED);
1828 netdev_info(tp->dev, "No firmware running\n");
1829 }
1830
1831 break;
1832 }
1833
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001834 udelay(10);
1835 }
1836
1837 /* Chip might not be fitted with firmware. Some Sun onboard
1838 * parts are configured like that. So don't signal the timeout
1839 * of the above loop as an error, but do report the lack of
1840 * running firmware once.
1841 */
1842 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1843 tg3_flag_set(tp, NO_FWARE_REPORTED);
1844
1845 netdev_info(tp->dev, "No firmware running\n");
1846 }
1847
Joe Perches41535772013-02-16 11:20:04 +00001848 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001849 /* The 57765 A0 needs a little more
1850 * time to do some important work.
1851 */
1852 mdelay(10);
1853 }
1854
1855 return 0;
1856}
1857
Matt Carlson95e28692008-05-25 23:44:14 -07001858static void tg3_link_report(struct tg3 *tp)
1859{
1860 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001861 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001862 tg3_ump_link_report(tp);
1863 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001864 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1865 (tp->link_config.active_speed == SPEED_1000 ?
1866 1000 :
1867 (tp->link_config.active_speed == SPEED_100 ?
1868 100 : 10)),
1869 (tp->link_config.active_duplex == DUPLEX_FULL ?
1870 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001871
Joe Perches05dbe002010-02-17 19:44:19 +00001872 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1873 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1874 "on" : "off",
1875 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1876 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001877
1878 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1879 netdev_info(tp->dev, "EEE is %s\n",
1880 tp->setlpicnt ? "enabled" : "disabled");
1881
Matt Carlson95e28692008-05-25 23:44:14 -07001882 tg3_ump_link_report(tp);
1883 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001884
1885 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001886}
1887
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001888static u32 tg3_decode_flowctrl_1000T(u32 adv)
1889{
1890 u32 flowctrl = 0;
1891
1892 if (adv & ADVERTISE_PAUSE_CAP) {
1893 flowctrl |= FLOW_CTRL_RX;
1894 if (!(adv & ADVERTISE_PAUSE_ASYM))
1895 flowctrl |= FLOW_CTRL_TX;
1896 } else if (adv & ADVERTISE_PAUSE_ASYM)
1897 flowctrl |= FLOW_CTRL_TX;
1898
1899 return flowctrl;
1900}
1901
Matt Carlson95e28692008-05-25 23:44:14 -07001902static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1903{
1904 u16 miireg;
1905
Steve Glendinninge18ce342008-12-16 02:00:00 -08001906 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001907 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001908 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001909 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001910 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001911 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1912 else
1913 miireg = 0;
1914
1915 return miireg;
1916}
1917
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001918static u32 tg3_decode_flowctrl_1000X(u32 adv)
1919{
1920 u32 flowctrl = 0;
1921
1922 if (adv & ADVERTISE_1000XPAUSE) {
1923 flowctrl |= FLOW_CTRL_RX;
1924 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1925 flowctrl |= FLOW_CTRL_TX;
1926 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1927 flowctrl |= FLOW_CTRL_TX;
1928
1929 return flowctrl;
1930}
1931
Matt Carlson95e28692008-05-25 23:44:14 -07001932static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1933{
1934 u8 cap = 0;
1935
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001936 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1937 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1938 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1939 if (lcladv & ADVERTISE_1000XPAUSE)
1940 cap = FLOW_CTRL_RX;
1941 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001942 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001943 }
1944
1945 return cap;
1946}
1947
Matt Carlsonf51f3562008-05-25 23:45:08 -07001948static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001949{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001950 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001951 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001952 u32 old_rx_mode = tp->rx_mode;
1953 u32 old_tx_mode = tp->tx_mode;
1954
Joe Perches63c3a662011-04-26 08:12:10 +00001955 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001956 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001957 else
1958 autoneg = tp->link_config.autoneg;
1959
Joe Perches63c3a662011-04-26 08:12:10 +00001960 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001961 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001962 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001963 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001964 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001965 } else
1966 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001967
Matt Carlsonf51f3562008-05-25 23:45:08 -07001968 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001969
Steve Glendinninge18ce342008-12-16 02:00:00 -08001970 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001971 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1972 else
1973 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1974
Matt Carlsonf51f3562008-05-25 23:45:08 -07001975 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001976 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001977
Steve Glendinninge18ce342008-12-16 02:00:00 -08001978 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001979 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1980 else
1981 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1982
Matt Carlsonf51f3562008-05-25 23:45:08 -07001983 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001984 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001985}
1986
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001987static void tg3_adjust_link(struct net_device *dev)
1988{
1989 u8 oldflowctrl, linkmesg = 0;
1990 u32 mac_mode, lcl_adv, rmt_adv;
1991 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001992 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001993
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001994 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001995
1996 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1997 MAC_MODE_HALF_DUPLEX);
1998
1999 oldflowctrl = tp->link_config.active_flowctrl;
2000
2001 if (phydev->link) {
2002 lcl_adv = 0;
2003 rmt_adv = 0;
2004
2005 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2006 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002007 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002008 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002009 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002010 else
2011 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002012
2013 if (phydev->duplex == DUPLEX_HALF)
2014 mac_mode |= MAC_MODE_HALF_DUPLEX;
2015 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002016 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002017 tp->link_config.flowctrl);
2018
2019 if (phydev->pause)
2020 rmt_adv = LPA_PAUSE_CAP;
2021 if (phydev->asym_pause)
2022 rmt_adv |= LPA_PAUSE_ASYM;
2023 }
2024
2025 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2026 } else
2027 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2028
2029 if (mac_mode != tp->mac_mode) {
2030 tp->mac_mode = mac_mode;
2031 tw32_f(MAC_MODE, tp->mac_mode);
2032 udelay(40);
2033 }
2034
Joe Perches41535772013-02-16 11:20:04 +00002035 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002036 if (phydev->speed == SPEED_10)
2037 tw32(MAC_MI_STAT,
2038 MAC_MI_STAT_10MBPS_MODE |
2039 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2040 else
2041 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2042 }
2043
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002044 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2045 tw32(MAC_TX_LENGTHS,
2046 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2047 (6 << TX_LENGTHS_IPG_SHIFT) |
2048 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2049 else
2050 tw32(MAC_TX_LENGTHS,
2051 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2052 (6 << TX_LENGTHS_IPG_SHIFT) |
2053 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2054
Matt Carlson34655ad2012-02-22 12:35:18 +00002055 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002056 phydev->speed != tp->link_config.active_speed ||
2057 phydev->duplex != tp->link_config.active_duplex ||
2058 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002059 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002060
Matt Carlson34655ad2012-02-22 12:35:18 +00002061 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002062 tp->link_config.active_speed = phydev->speed;
2063 tp->link_config.active_duplex = phydev->duplex;
2064
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002065 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002066
2067 if (linkmesg)
2068 tg3_link_report(tp);
2069}
2070
2071static int tg3_phy_init(struct tg3 *tp)
2072{
2073 struct phy_device *phydev;
2074
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002075 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002076 return 0;
2077
2078 /* Bring the PHY back to a known state. */
2079 tg3_bmcr_reset(tp);
2080
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002081 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002082
2083 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002084 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2085 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002086 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002087 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002088 return PTR_ERR(phydev);
2089 }
2090
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002091 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002092 switch (phydev->interface) {
2093 case PHY_INTERFACE_MODE_GMII:
2094 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002095 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002096 phydev->supported &= (PHY_GBIT_FEATURES |
2097 SUPPORTED_Pause |
2098 SUPPORTED_Asym_Pause);
2099 break;
2100 }
2101 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002102 case PHY_INTERFACE_MODE_MII:
2103 phydev->supported &= (PHY_BASIC_FEATURES |
2104 SUPPORTED_Pause |
2105 SUPPORTED_Asym_Pause);
2106 break;
2107 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002108 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002109 return -EINVAL;
2110 }
2111
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002112 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002113
2114 phydev->advertising = phydev->supported;
2115
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002116 return 0;
2117}
2118
2119static void tg3_phy_start(struct tg3 *tp)
2120{
2121 struct phy_device *phydev;
2122
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002123 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002124 return;
2125
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002126 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002127
Matt Carlson80096062010-08-02 11:26:06 +00002128 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2129 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002130 phydev->speed = tp->link_config.speed;
2131 phydev->duplex = tp->link_config.duplex;
2132 phydev->autoneg = tp->link_config.autoneg;
2133 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002134 }
2135
2136 phy_start(phydev);
2137
2138 phy_start_aneg(phydev);
2139}
2140
2141static void tg3_phy_stop(struct tg3 *tp)
2142{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002143 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002144 return;
2145
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002146 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002147}
2148
2149static void tg3_phy_fini(struct tg3 *tp)
2150{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002151 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002152 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002153 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002154 }
2155}
2156
Matt Carlson941ec902011-08-19 13:58:23 +00002157static int tg3_phy_set_extloopbk(struct tg3 *tp)
2158{
2159 int err;
2160 u32 val;
2161
2162 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2163 return 0;
2164
2165 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2166 /* Cannot do read-modify-write on 5401 */
2167 err = tg3_phy_auxctl_write(tp,
2168 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2169 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2170 0x4c20);
2171 goto done;
2172 }
2173
2174 err = tg3_phy_auxctl_read(tp,
2175 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2176 if (err)
2177 return err;
2178
2179 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2180 err = tg3_phy_auxctl_write(tp,
2181 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2182
2183done:
2184 return err;
2185}
2186
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002187static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2188{
2189 u32 phytest;
2190
2191 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2192 u32 phy;
2193
2194 tg3_writephy(tp, MII_TG3_FET_TEST,
2195 phytest | MII_TG3_FET_SHADOW_EN);
2196 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2197 if (enable)
2198 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2199 else
2200 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2201 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2202 }
2203 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2204 }
2205}
2206
Matt Carlson6833c042008-11-21 17:18:59 -08002207static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2208{
2209 u32 reg;
2210
Joe Perches63c3a662011-04-26 08:12:10 +00002211 if (!tg3_flag(tp, 5705_PLUS) ||
2212 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002213 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002214 return;
2215
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002216 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002217 tg3_phy_fet_toggle_apd(tp, enable);
2218 return;
2219 }
2220
Matt Carlson6833c042008-11-21 17:18:59 -08002221 reg = MII_TG3_MISC_SHDW_WREN |
2222 MII_TG3_MISC_SHDW_SCR5_SEL |
2223 MII_TG3_MISC_SHDW_SCR5_LPED |
2224 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2225 MII_TG3_MISC_SHDW_SCR5_SDTL |
2226 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002227 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002228 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2229
2230 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2231
2232
2233 reg = MII_TG3_MISC_SHDW_WREN |
2234 MII_TG3_MISC_SHDW_APD_SEL |
2235 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2236 if (enable)
2237 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2238
2239 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2240}
2241
Joe Perches953c96e2013-04-09 10:18:14 +00002242static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002243{
2244 u32 phy;
2245
Joe Perches63c3a662011-04-26 08:12:10 +00002246 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002247 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002248 return;
2249
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002250 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002251 u32 ephy;
2252
Matt Carlson535ef6e2009-08-25 10:09:36 +00002253 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2254 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2255
2256 tg3_writephy(tp, MII_TG3_FET_TEST,
2257 ephy | MII_TG3_FET_SHADOW_EN);
2258 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002259 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002260 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002261 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002262 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2263 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002264 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002265 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002266 }
2267 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002268 int ret;
2269
2270 ret = tg3_phy_auxctl_read(tp,
2271 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2272 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002273 if (enable)
2274 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2275 else
2276 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002277 tg3_phy_auxctl_write(tp,
2278 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002279 }
2280 }
2281}
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283static void tg3_phy_set_wirespeed(struct tg3 *tp)
2284{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002285 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 u32 val;
2287
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002288 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return;
2290
Matt Carlson15ee95c2011-04-20 07:57:40 +00002291 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2292 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002293 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2294 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002297static void tg3_phy_apply_otp(struct tg3 *tp)
2298{
2299 u32 otp, phy;
2300
2301 if (!tp->phy_otp)
2302 return;
2303
2304 otp = tp->phy_otp;
2305
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002306 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002307 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002308
2309 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2310 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2311 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2312
2313 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2314 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2315 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2316
2317 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2318 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2319 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2320
2321 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2322 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2323
2324 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2325 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2326
2327 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2328 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2329 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2330
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002331 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002332}
2333
Nithin Sujir400dfba2013-05-18 06:26:53 +00002334static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2335{
2336 u32 val;
2337 struct ethtool_eee *dest = &tp->eee;
2338
2339 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2340 return;
2341
2342 if (eee)
2343 dest = eee;
2344
2345 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2346 return;
2347
2348 /* Pull eee_active */
2349 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2350 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2351 dest->eee_active = 1;
2352 } else
2353 dest->eee_active = 0;
2354
2355 /* Pull lp advertised settings */
2356 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2357 return;
2358 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2359
2360 /* Pull advertised and eee_enabled settings */
2361 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2362 return;
2363 dest->eee_enabled = !!val;
2364 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2365
2366 /* Pull tx_lpi_enabled */
2367 val = tr32(TG3_CPMU_EEE_MODE);
2368 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2369
2370 /* Pull lpi timer value */
2371 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2372}
2373
Joe Perches953c96e2013-04-09 10:18:14 +00002374static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002375{
2376 u32 val;
2377
2378 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2379 return;
2380
2381 tp->setlpicnt = 0;
2382
2383 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002384 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002385 tp->link_config.active_duplex == DUPLEX_FULL &&
2386 (tp->link_config.active_speed == SPEED_100 ||
2387 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002388 u32 eeectl;
2389
2390 if (tp->link_config.active_speed == SPEED_1000)
2391 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2392 else
2393 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2394
2395 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2396
Nithin Sujir400dfba2013-05-18 06:26:53 +00002397 tg3_eee_pull_config(tp, NULL);
2398 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002399 tp->setlpicnt = 2;
2400 }
2401
2402 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002403 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002404 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002405 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002406 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002407 }
2408
Matt Carlson52b02d02010-10-14 10:37:41 +00002409 val = tr32(TG3_CPMU_EEE_MODE);
2410 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2411 }
2412}
2413
Matt Carlsonb0c59432011-05-19 12:12:48 +00002414static void tg3_phy_eee_enable(struct tg3 *tp)
2415{
2416 u32 val;
2417
2418 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002419 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2420 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002421 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002422 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002423 val = MII_TG3_DSP_TAP26_ALNOKO |
2424 MII_TG3_DSP_TAP26_RMRXSTO;
2425 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002426 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002427 }
2428
2429 val = tr32(TG3_CPMU_EEE_MODE);
2430 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2431}
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433static int tg3_wait_macro_done(struct tg3 *tp)
2434{
2435 int limit = 100;
2436
2437 while (limit--) {
2438 u32 tmp32;
2439
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002440 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if ((tmp32 & 0x1000) == 0)
2442 break;
2443 }
2444 }
Roel Kluind4675b52009-02-12 16:33:27 -08002445 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return -EBUSY;
2447
2448 return 0;
2449}
2450
2451static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2452{
2453 static const u32 test_pat[4][6] = {
2454 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2455 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2456 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2457 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2458 };
2459 int chan;
2460
2461 for (chan = 0; chan < 4; chan++) {
2462 int i;
2463
2464 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2465 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002466 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 for (i = 0; i < 6; i++)
2469 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2470 test_pat[chan][i]);
2471
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002472 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (tg3_wait_macro_done(tp)) {
2474 *resetp = 1;
2475 return -EBUSY;
2476 }
2477
2478 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2479 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002480 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (tg3_wait_macro_done(tp)) {
2482 *resetp = 1;
2483 return -EBUSY;
2484 }
2485
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002486 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (tg3_wait_macro_done(tp)) {
2488 *resetp = 1;
2489 return -EBUSY;
2490 }
2491
2492 for (i = 0; i < 6; i += 2) {
2493 u32 low, high;
2494
2495 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2496 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2497 tg3_wait_macro_done(tp)) {
2498 *resetp = 1;
2499 return -EBUSY;
2500 }
2501 low &= 0x7fff;
2502 high &= 0x000f;
2503 if (low != test_pat[chan][i] ||
2504 high != test_pat[chan][i+1]) {
2505 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2506 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2507 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2508
2509 return -EBUSY;
2510 }
2511 }
2512 }
2513
2514 return 0;
2515}
2516
2517static int tg3_phy_reset_chanpat(struct tg3 *tp)
2518{
2519 int chan;
2520
2521 for (chan = 0; chan < 4; chan++) {
2522 int i;
2523
2524 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2525 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002526 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 for (i = 0; i < 6; i++)
2528 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002529 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (tg3_wait_macro_done(tp))
2531 return -EBUSY;
2532 }
2533
2534 return 0;
2535}
2536
2537static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2538{
2539 u32 reg32, phy9_orig;
2540 int retries, do_phy_reset, err;
2541
2542 retries = 10;
2543 do_phy_reset = 1;
2544 do {
2545 if (do_phy_reset) {
2546 err = tg3_bmcr_reset(tp);
2547 if (err)
2548 return err;
2549 do_phy_reset = 0;
2550 }
2551
2552 /* Disable transmitter and interrupt. */
2553 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2554 continue;
2555
2556 reg32 |= 0x3000;
2557 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2558
2559 /* Set full-duplex, 1000 mbps. */
2560 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002561 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002564 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 continue;
2566
Matt Carlson221c5632011-06-13 13:39:01 +00002567 tg3_writephy(tp, MII_CTRL1000,
2568 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002570 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002571 if (err)
2572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002575 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2578 if (!err)
2579 break;
2580 } while (--retries);
2581
2582 err = tg3_phy_reset_chanpat(tp);
2583 if (err)
2584 return err;
2585
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002586 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002589 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002591 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Matt Carlson221c5632011-06-13 13:39:01 +00002593 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2596 reg32 &= ~0x3000;
2597 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2598 } else if (!err)
2599 err = -EBUSY;
2600
2601 return err;
2602}
2603
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002604static void tg3_carrier_off(struct tg3 *tp)
2605{
2606 netif_carrier_off(tp->dev);
2607 tp->link_up = false;
2608}
2609
Nithin Sujirce20f162013-04-09 08:48:04 +00002610static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2611{
2612 if (tg3_flag(tp, ENABLE_ASF))
2613 netdev_warn(tp->dev,
2614 "Management side-band traffic will be interrupted during phy settings change\n");
2615}
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617/* This will reset the tigon3 PHY if there is no valid
2618 * link unless the FORCE argument is non-zero.
2619 */
2620static int tg3_phy_reset(struct tg3 *tp)
2621{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002622 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 int err;
2624
Joe Perches41535772013-02-16 11:20:04 +00002625 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002626 val = tr32(GRC_MISC_CFG);
2627 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2628 udelay(40);
2629 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002630 err = tg3_readphy(tp, MII_BMSR, &val);
2631 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (err != 0)
2633 return -EBUSY;
2634
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002635 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002636 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002637 tg3_link_report(tp);
2638 }
2639
Joe Perches41535772013-02-16 11:20:04 +00002640 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2641 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2642 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 err = tg3_phy_reset_5703_4_5(tp);
2644 if (err)
2645 return err;
2646 goto out;
2647 }
2648
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002649 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002650 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2651 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002652 cpmuctrl = tr32(TG3_CPMU_CTRL);
2653 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2654 tw32(TG3_CPMU_CTRL,
2655 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2656 }
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 err = tg3_bmcr_reset(tp);
2659 if (err)
2660 return err;
2661
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002662 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002663 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2664 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002665
2666 tw32(TG3_CPMU_CTRL, cpmuctrl);
2667 }
2668
Joe Perches41535772013-02-16 11:20:04 +00002669 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2670 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002671 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2672 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2673 CPMU_LSPD_1000MB_MACCLK_12_5) {
2674 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2675 udelay(40);
2676 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2677 }
2678 }
2679
Joe Perches63c3a662011-04-26 08:12:10 +00002680 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002681 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002682 return 0;
2683
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002684 tg3_phy_apply_otp(tp);
2685
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002686 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002687 tg3_phy_toggle_apd(tp, true);
2688 else
2689 tg3_phy_toggle_apd(tp, false);
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002692 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002693 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002694 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2695 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002696 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002698
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002699 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002700 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2701 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002703
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002704 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002705 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002706 tg3_phydsp_write(tp, 0x000a, 0x310b);
2707 tg3_phydsp_write(tp, 0x201f, 0x9506);
2708 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002709 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002710 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002711 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002712 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002713 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2714 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2715 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2716 tg3_writephy(tp, MII_TG3_TEST1,
2717 MII_TG3_TEST1_TRIM_EN | 0x4);
2718 } else
2719 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2720
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002721 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002722 }
Michael Chanc424cb22006-04-29 18:56:34 -07002723 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* Set Extended packet length bit (bit 14) on all chips that */
2726 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002727 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002729 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002730 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002732 err = tg3_phy_auxctl_read(tp,
2733 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2734 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002735 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2736 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
2738
2739 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2740 * jumbo frames transmission.
2741 */
Joe Perches63c3a662011-04-26 08:12:10 +00002742 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002743 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002744 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002745 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
2747
Joe Perches41535772013-02-16 11:20:04 +00002748 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002749 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002750 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002751 }
2752
Joe Perches41535772013-02-16 11:20:04 +00002753 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002754 tg3_phydsp_write(tp, 0xffb, 0x4000);
2755
Joe Perches953c96e2013-04-09 10:18:14 +00002756 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 tg3_phy_set_wirespeed(tp);
2758 return 0;
2759}
2760
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002761#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2762#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2763#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2764 TG3_GPIO_MSG_NEED_VAUX)
2765#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2766 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2767 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2768 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2769 (TG3_GPIO_MSG_DRVR_PRES << 12))
2770
2771#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2772 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2773 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2774 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2775 (TG3_GPIO_MSG_NEED_VAUX << 12))
2776
2777static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2778{
2779 u32 status, shift;
2780
Joe Perches41535772013-02-16 11:20:04 +00002781 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2782 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002783 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2784 else
2785 status = tr32(TG3_CPMU_DRV_STATUS);
2786
2787 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2788 status &= ~(TG3_GPIO_MSG_MASK << shift);
2789 status |= (newstat << shift);
2790
Joe Perches41535772013-02-16 11:20:04 +00002791 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2792 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002793 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2794 else
2795 tw32(TG3_CPMU_DRV_STATUS, status);
2796
2797 return status >> TG3_APE_GPIO_MSG_SHIFT;
2798}
2799
Matt Carlson520b2752011-06-13 13:39:02 +00002800static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2801{
2802 if (!tg3_flag(tp, IS_NIC))
2803 return 0;
2804
Joe Perches41535772013-02-16 11:20:04 +00002805 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2806 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2807 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002808 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2809 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002810
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002811 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2812
2813 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2814 TG3_GRC_LCLCTL_PWRSW_DELAY);
2815
2816 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2817 } else {
2818 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2819 TG3_GRC_LCLCTL_PWRSW_DELAY);
2820 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002821
Matt Carlson520b2752011-06-13 13:39:02 +00002822 return 0;
2823}
2824
2825static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2826{
2827 u32 grc_local_ctrl;
2828
2829 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002830 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2831 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002832 return;
2833
2834 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2835
2836 tw32_wait_f(GRC_LOCAL_CTRL,
2837 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2838 TG3_GRC_LCLCTL_PWRSW_DELAY);
2839
2840 tw32_wait_f(GRC_LOCAL_CTRL,
2841 grc_local_ctrl,
2842 TG3_GRC_LCLCTL_PWRSW_DELAY);
2843
2844 tw32_wait_f(GRC_LOCAL_CTRL,
2845 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2846 TG3_GRC_LCLCTL_PWRSW_DELAY);
2847}
2848
2849static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2850{
2851 if (!tg3_flag(tp, IS_NIC))
2852 return;
2853
Joe Perches41535772013-02-16 11:20:04 +00002854 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2855 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002856 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2857 (GRC_LCLCTRL_GPIO_OE0 |
2858 GRC_LCLCTRL_GPIO_OE1 |
2859 GRC_LCLCTRL_GPIO_OE2 |
2860 GRC_LCLCTRL_GPIO_OUTPUT0 |
2861 GRC_LCLCTRL_GPIO_OUTPUT1),
2862 TG3_GRC_LCLCTL_PWRSW_DELAY);
2863 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2864 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2865 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2866 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2867 GRC_LCLCTRL_GPIO_OE1 |
2868 GRC_LCLCTRL_GPIO_OE2 |
2869 GRC_LCLCTRL_GPIO_OUTPUT0 |
2870 GRC_LCLCTRL_GPIO_OUTPUT1 |
2871 tp->grc_local_ctrl;
2872 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2873 TG3_GRC_LCLCTL_PWRSW_DELAY);
2874
2875 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2876 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2877 TG3_GRC_LCLCTL_PWRSW_DELAY);
2878
2879 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2880 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2881 TG3_GRC_LCLCTL_PWRSW_DELAY);
2882 } else {
2883 u32 no_gpio2;
2884 u32 grc_local_ctrl = 0;
2885
2886 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002887 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002888 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2889 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2890 grc_local_ctrl,
2891 TG3_GRC_LCLCTL_PWRSW_DELAY);
2892 }
2893
2894 /* On 5753 and variants, GPIO2 cannot be used. */
2895 no_gpio2 = tp->nic_sram_data_cfg &
2896 NIC_SRAM_DATA_CFG_NO_GPIO2;
2897
2898 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2899 GRC_LCLCTRL_GPIO_OE1 |
2900 GRC_LCLCTRL_GPIO_OE2 |
2901 GRC_LCLCTRL_GPIO_OUTPUT1 |
2902 GRC_LCLCTRL_GPIO_OUTPUT2;
2903 if (no_gpio2) {
2904 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2905 GRC_LCLCTRL_GPIO_OUTPUT2);
2906 }
2907 tw32_wait_f(GRC_LOCAL_CTRL,
2908 tp->grc_local_ctrl | grc_local_ctrl,
2909 TG3_GRC_LCLCTL_PWRSW_DELAY);
2910
2911 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2912
2913 tw32_wait_f(GRC_LOCAL_CTRL,
2914 tp->grc_local_ctrl | grc_local_ctrl,
2915 TG3_GRC_LCLCTL_PWRSW_DELAY);
2916
2917 if (!no_gpio2) {
2918 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2919 tw32_wait_f(GRC_LOCAL_CTRL,
2920 tp->grc_local_ctrl | grc_local_ctrl,
2921 TG3_GRC_LCLCTL_PWRSW_DELAY);
2922 }
2923 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002924}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002925
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002926static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002927{
2928 u32 msg = 0;
2929
2930 /* Serialize power state transitions */
2931 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2932 return;
2933
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002934 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002935 msg = TG3_GPIO_MSG_NEED_VAUX;
2936
2937 msg = tg3_set_function_status(tp, msg);
2938
2939 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2940 goto done;
2941
2942 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2943 tg3_pwrsrc_switch_to_vaux(tp);
2944 else
2945 tg3_pwrsrc_die_with_vmain(tp);
2946
2947done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002948 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002949}
2950
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002951static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Matt Carlson683644b2011-03-09 16:58:23 +00002953 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Matt Carlson334355a2010-01-20 16:58:10 +00002955 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002956 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return;
2958
Joe Perches41535772013-02-16 11:20:04 +00002959 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2960 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2961 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002962 tg3_frob_aux_power_5717(tp, include_wol ?
2963 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002964 return;
2965 }
2966
2967 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002968 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002970 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002971
Michael Chanbc1c7562006-03-20 17:48:03 -08002972 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002973 if (dev_peer) {
2974 struct tg3 *tp_peer = netdev_priv(dev_peer);
2975
Joe Perches63c3a662011-04-26 08:12:10 +00002976 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002977 return;
2978
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002979 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002980 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002981 need_vaux = true;
2982 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002985 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2986 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002987 need_vaux = true;
2988
Matt Carlson520b2752011-06-13 13:39:02 +00002989 if (need_vaux)
2990 tg3_pwrsrc_switch_to_vaux(tp);
2991 else
2992 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002995static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2996{
2997 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2998 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002999 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003000 if (speed != SPEED_10)
3001 return 1;
3002 } else if (speed == SPEED_10)
3003 return 1;
3004
3005 return 0;
3006}
3007
Nithin Sujir44f3b502013-05-13 11:04:15 +00003008static bool tg3_phy_power_bug(struct tg3 *tp)
3009{
3010 switch (tg3_asic_rev(tp)) {
3011 case ASIC_REV_5700:
3012 case ASIC_REV_5704:
3013 return true;
3014 case ASIC_REV_5780:
3015 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3016 return true;
3017 return false;
3018 case ASIC_REV_5717:
3019 if (!tp->pci_fn)
3020 return true;
3021 return false;
3022 case ASIC_REV_5719:
3023 case ASIC_REV_5720:
3024 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3025 !tp->pci_fn)
3026 return true;
3027 return false;
3028 }
3029
3030 return false;
3031}
3032
Matt Carlson0a459aa2008-11-03 16:54:15 -08003033static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003034{
Matt Carlsonce057f02007-11-12 21:08:03 -08003035 u32 val;
3036
Nithin Sujir942d1af2013-04-09 08:48:07 +00003037 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3038 return;
3039
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003040 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003041 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003042 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3043 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3044
3045 sg_dig_ctrl |=
3046 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3047 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3048 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3049 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003050 return;
Michael Chan51297242007-02-13 12:17:57 -08003051 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003052
Joe Perches41535772013-02-16 11:20:04 +00003053 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003054 tg3_bmcr_reset(tp);
3055 val = tr32(GRC_MISC_CFG);
3056 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3057 udelay(40);
3058 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003059 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003060 u32 phytest;
3061 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3062 u32 phy;
3063
3064 tg3_writephy(tp, MII_ADVERTISE, 0);
3065 tg3_writephy(tp, MII_BMCR,
3066 BMCR_ANENABLE | BMCR_ANRESTART);
3067
3068 tg3_writephy(tp, MII_TG3_FET_TEST,
3069 phytest | MII_TG3_FET_SHADOW_EN);
3070 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3071 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3072 tg3_writephy(tp,
3073 MII_TG3_FET_SHDW_AUXMODE4,
3074 phy);
3075 }
3076 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3077 }
3078 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003079 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07003080 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3081 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003082
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003083 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3084 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3085 MII_TG3_AUXCTL_PCTL_VREG_11V;
3086 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003087 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003088
Michael Chan15c3b692006-03-22 01:06:52 -08003089 /* The PHY should not be powered down on some chips because
3090 * of bugs.
3091 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003092 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003093 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003094
Joe Perches41535772013-02-16 11:20:04 +00003095 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3096 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003097 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3098 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3099 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3100 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3101 }
3102
Michael Chan15c3b692006-03-22 01:06:52 -08003103 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3104}
3105
Matt Carlson3f007892008-11-03 16:51:36 -08003106/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003107static int tg3_nvram_lock(struct tg3 *tp)
3108{
Joe Perches63c3a662011-04-26 08:12:10 +00003109 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003110 int i;
3111
3112 if (tp->nvram_lock_cnt == 0) {
3113 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3114 for (i = 0; i < 8000; i++) {
3115 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3116 break;
3117 udelay(20);
3118 }
3119 if (i == 8000) {
3120 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3121 return -ENODEV;
3122 }
3123 }
3124 tp->nvram_lock_cnt++;
3125 }
3126 return 0;
3127}
3128
3129/* tp->lock is held. */
3130static void tg3_nvram_unlock(struct tg3 *tp)
3131{
Joe Perches63c3a662011-04-26 08:12:10 +00003132 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003133 if (tp->nvram_lock_cnt > 0)
3134 tp->nvram_lock_cnt--;
3135 if (tp->nvram_lock_cnt == 0)
3136 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3137 }
3138}
3139
3140/* tp->lock is held. */
3141static void tg3_enable_nvram_access(struct tg3 *tp)
3142{
Joe Perches63c3a662011-04-26 08:12:10 +00003143 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003144 u32 nvaccess = tr32(NVRAM_ACCESS);
3145
3146 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3147 }
3148}
3149
3150/* tp->lock is held. */
3151static void tg3_disable_nvram_access(struct tg3 *tp)
3152{
Joe Perches63c3a662011-04-26 08:12:10 +00003153 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003154 u32 nvaccess = tr32(NVRAM_ACCESS);
3155
3156 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3157 }
3158}
3159
3160static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3161 u32 offset, u32 *val)
3162{
3163 u32 tmp;
3164 int i;
3165
3166 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3167 return -EINVAL;
3168
3169 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3170 EEPROM_ADDR_DEVID_MASK |
3171 EEPROM_ADDR_READ);
3172 tw32(GRC_EEPROM_ADDR,
3173 tmp |
3174 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3175 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3176 EEPROM_ADDR_ADDR_MASK) |
3177 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3178
3179 for (i = 0; i < 1000; i++) {
3180 tmp = tr32(GRC_EEPROM_ADDR);
3181
3182 if (tmp & EEPROM_ADDR_COMPLETE)
3183 break;
3184 msleep(1);
3185 }
3186 if (!(tmp & EEPROM_ADDR_COMPLETE))
3187 return -EBUSY;
3188
Matt Carlson62cedd12009-04-20 14:52:29 -07003189 tmp = tr32(GRC_EEPROM_DATA);
3190
3191 /*
3192 * The data will always be opposite the native endian
3193 * format. Perform a blind byteswap to compensate.
3194 */
3195 *val = swab32(tmp);
3196
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003197 return 0;
3198}
3199
3200#define NVRAM_CMD_TIMEOUT 10000
3201
3202static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3203{
3204 int i;
3205
3206 tw32(NVRAM_CMD, nvram_cmd);
3207 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3208 udelay(10);
3209 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3210 udelay(10);
3211 break;
3212 }
3213 }
3214
3215 if (i == NVRAM_CMD_TIMEOUT)
3216 return -EBUSY;
3217
3218 return 0;
3219}
3220
3221static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3222{
Joe Perches63c3a662011-04-26 08:12:10 +00003223 if (tg3_flag(tp, NVRAM) &&
3224 tg3_flag(tp, NVRAM_BUFFERED) &&
3225 tg3_flag(tp, FLASH) &&
3226 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003227 (tp->nvram_jedecnum == JEDEC_ATMEL))
3228
3229 addr = ((addr / tp->nvram_pagesize) <<
3230 ATMEL_AT45DB0X1B_PAGE_POS) +
3231 (addr % tp->nvram_pagesize);
3232
3233 return addr;
3234}
3235
3236static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3237{
Joe Perches63c3a662011-04-26 08:12:10 +00003238 if (tg3_flag(tp, NVRAM) &&
3239 tg3_flag(tp, NVRAM_BUFFERED) &&
3240 tg3_flag(tp, FLASH) &&
3241 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003242 (tp->nvram_jedecnum == JEDEC_ATMEL))
3243
3244 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3245 tp->nvram_pagesize) +
3246 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3247
3248 return addr;
3249}
3250
Matt Carlsone4f34112009-02-25 14:25:00 +00003251/* NOTE: Data read in from NVRAM is byteswapped according to
3252 * the byteswapping settings for all other register accesses.
3253 * tg3 devices are BE devices, so on a BE machine, the data
3254 * returned will be exactly as it is seen in NVRAM. On a LE
3255 * machine, the 32-bit value will be byteswapped.
3256 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003257static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3258{
3259 int ret;
3260
Joe Perches63c3a662011-04-26 08:12:10 +00003261 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003262 return tg3_nvram_read_using_eeprom(tp, offset, val);
3263
3264 offset = tg3_nvram_phys_addr(tp, offset);
3265
3266 if (offset > NVRAM_ADDR_MSK)
3267 return -EINVAL;
3268
3269 ret = tg3_nvram_lock(tp);
3270 if (ret)
3271 return ret;
3272
3273 tg3_enable_nvram_access(tp);
3274
3275 tw32(NVRAM_ADDR, offset);
3276 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3277 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3278
3279 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003280 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003281
3282 tg3_disable_nvram_access(tp);
3283
3284 tg3_nvram_unlock(tp);
3285
3286 return ret;
3287}
3288
Matt Carlsona9dc5292009-02-25 14:25:30 +00003289/* Ensures NVRAM data is in bytestream format. */
3290static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003291{
3292 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003293 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003294 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003295 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003296 return res;
3297}
3298
Matt Carlsondbe9b922012-02-13 10:20:09 +00003299static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3300 u32 offset, u32 len, u8 *buf)
3301{
3302 int i, j, rc = 0;
3303 u32 val;
3304
3305 for (i = 0; i < len; i += 4) {
3306 u32 addr;
3307 __be32 data;
3308
3309 addr = offset + i;
3310
3311 memcpy(&data, buf + i, 4);
3312
3313 /*
3314 * The SEEPROM interface expects the data to always be opposite
3315 * the native endian format. We accomplish this by reversing
3316 * all the operations that would have been performed on the
3317 * data from a call to tg3_nvram_read_be32().
3318 */
3319 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3320
3321 val = tr32(GRC_EEPROM_ADDR);
3322 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3323
3324 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3325 EEPROM_ADDR_READ);
3326 tw32(GRC_EEPROM_ADDR, val |
3327 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3328 (addr & EEPROM_ADDR_ADDR_MASK) |
3329 EEPROM_ADDR_START |
3330 EEPROM_ADDR_WRITE);
3331
3332 for (j = 0; j < 1000; j++) {
3333 val = tr32(GRC_EEPROM_ADDR);
3334
3335 if (val & EEPROM_ADDR_COMPLETE)
3336 break;
3337 msleep(1);
3338 }
3339 if (!(val & EEPROM_ADDR_COMPLETE)) {
3340 rc = -EBUSY;
3341 break;
3342 }
3343 }
3344
3345 return rc;
3346}
3347
3348/* offset and length are dword aligned */
3349static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3350 u8 *buf)
3351{
3352 int ret = 0;
3353 u32 pagesize = tp->nvram_pagesize;
3354 u32 pagemask = pagesize - 1;
3355 u32 nvram_cmd;
3356 u8 *tmp;
3357
3358 tmp = kmalloc(pagesize, GFP_KERNEL);
3359 if (tmp == NULL)
3360 return -ENOMEM;
3361
3362 while (len) {
3363 int j;
3364 u32 phy_addr, page_off, size;
3365
3366 phy_addr = offset & ~pagemask;
3367
3368 for (j = 0; j < pagesize; j += 4) {
3369 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3370 (__be32 *) (tmp + j));
3371 if (ret)
3372 break;
3373 }
3374 if (ret)
3375 break;
3376
3377 page_off = offset & pagemask;
3378 size = pagesize;
3379 if (len < size)
3380 size = len;
3381
3382 len -= size;
3383
3384 memcpy(tmp + page_off, buf, size);
3385
3386 offset = offset + (pagesize - page_off);
3387
3388 tg3_enable_nvram_access(tp);
3389
3390 /*
3391 * Before we can erase the flash page, we need
3392 * to issue a special "write enable" command.
3393 */
3394 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3395
3396 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3397 break;
3398
3399 /* Erase the target page */
3400 tw32(NVRAM_ADDR, phy_addr);
3401
3402 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3403 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3404
3405 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3406 break;
3407
3408 /* Issue another write enable to start the write. */
3409 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3410
3411 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3412 break;
3413
3414 for (j = 0; j < pagesize; j += 4) {
3415 __be32 data;
3416
3417 data = *((__be32 *) (tmp + j));
3418
3419 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3420
3421 tw32(NVRAM_ADDR, phy_addr + j);
3422
3423 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3424 NVRAM_CMD_WR;
3425
3426 if (j == 0)
3427 nvram_cmd |= NVRAM_CMD_FIRST;
3428 else if (j == (pagesize - 4))
3429 nvram_cmd |= NVRAM_CMD_LAST;
3430
3431 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3432 if (ret)
3433 break;
3434 }
3435 if (ret)
3436 break;
3437 }
3438
3439 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3440 tg3_nvram_exec_cmd(tp, nvram_cmd);
3441
3442 kfree(tmp);
3443
3444 return ret;
3445}
3446
3447/* offset and length are dword aligned */
3448static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3449 u8 *buf)
3450{
3451 int i, ret = 0;
3452
3453 for (i = 0; i < len; i += 4, offset += 4) {
3454 u32 page_off, phy_addr, nvram_cmd;
3455 __be32 data;
3456
3457 memcpy(&data, buf + i, 4);
3458 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3459
3460 page_off = offset % tp->nvram_pagesize;
3461
3462 phy_addr = tg3_nvram_phys_addr(tp, offset);
3463
Matt Carlsondbe9b922012-02-13 10:20:09 +00003464 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3465
3466 if (page_off == 0 || i == 0)
3467 nvram_cmd |= NVRAM_CMD_FIRST;
3468 if (page_off == (tp->nvram_pagesize - 4))
3469 nvram_cmd |= NVRAM_CMD_LAST;
3470
3471 if (i == (len - 4))
3472 nvram_cmd |= NVRAM_CMD_LAST;
3473
Matt Carlson42278222012-02-13 15:20:11 +00003474 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3475 !tg3_flag(tp, FLASH) ||
3476 !tg3_flag(tp, 57765_PLUS))
3477 tw32(NVRAM_ADDR, phy_addr);
3478
Joe Perches41535772013-02-16 11:20:04 +00003479 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003480 !tg3_flag(tp, 5755_PLUS) &&
3481 (tp->nvram_jedecnum == JEDEC_ST) &&
3482 (nvram_cmd & NVRAM_CMD_FIRST)) {
3483 u32 cmd;
3484
3485 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3486 ret = tg3_nvram_exec_cmd(tp, cmd);
3487 if (ret)
3488 break;
3489 }
3490 if (!tg3_flag(tp, FLASH)) {
3491 /* We always do complete word writes to eeprom. */
3492 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3493 }
3494
3495 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3496 if (ret)
3497 break;
3498 }
3499 return ret;
3500}
3501
3502/* offset and length are dword aligned */
3503static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3504{
3505 int ret;
3506
3507 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3508 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3509 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3510 udelay(40);
3511 }
3512
3513 if (!tg3_flag(tp, NVRAM)) {
3514 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3515 } else {
3516 u32 grc_mode;
3517
3518 ret = tg3_nvram_lock(tp);
3519 if (ret)
3520 return ret;
3521
3522 tg3_enable_nvram_access(tp);
3523 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3524 tw32(NVRAM_WRITE1, 0x406);
3525
3526 grc_mode = tr32(GRC_MODE);
3527 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3528
3529 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3530 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3531 buf);
3532 } else {
3533 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3534 buf);
3535 }
3536
3537 grc_mode = tr32(GRC_MODE);
3538 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3539
3540 tg3_disable_nvram_access(tp);
3541 tg3_nvram_unlock(tp);
3542 }
3543
3544 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3545 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3546 udelay(40);
3547 }
3548
3549 return ret;
3550}
3551
Matt Carlson997b4f12011-08-31 11:44:53 +00003552#define RX_CPU_SCRATCH_BASE 0x30000
3553#define RX_CPU_SCRATCH_SIZE 0x04000
3554#define TX_CPU_SCRATCH_BASE 0x34000
3555#define TX_CPU_SCRATCH_SIZE 0x04000
3556
3557/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003558static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003559{
3560 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003561 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003562
Nithin Sujir837c45b2013-03-06 17:02:30 +00003563 for (i = 0; i < iters; i++) {
3564 tw32(cpu_base + CPU_STATE, 0xffffffff);
3565 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3566 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3567 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003568 if (pci_channel_offline(tp->pdev))
3569 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003570 }
3571
3572 return (i == iters) ? -EBUSY : 0;
3573}
3574
3575/* tp->lock is held. */
3576static int tg3_rxcpu_pause(struct tg3 *tp)
3577{
3578 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3579
3580 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3581 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3582 udelay(10);
3583
3584 return rc;
3585}
3586
3587/* tp->lock is held. */
3588static int tg3_txcpu_pause(struct tg3 *tp)
3589{
3590 return tg3_pause_cpu(tp, TX_CPU_BASE);
3591}
3592
3593/* tp->lock is held. */
3594static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3595{
3596 tw32(cpu_base + CPU_STATE, 0xffffffff);
3597 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3598}
3599
3600/* tp->lock is held. */
3601static void tg3_rxcpu_resume(struct tg3 *tp)
3602{
3603 tg3_resume_cpu(tp, RX_CPU_BASE);
3604}
3605
3606/* tp->lock is held. */
3607static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3608{
3609 int rc;
3610
3611 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003612
Joe Perches41535772013-02-16 11:20:04 +00003613 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003614 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3615
3616 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3617 return 0;
3618 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003619 if (cpu_base == RX_CPU_BASE) {
3620 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003621 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003622 /*
3623 * There is only an Rx CPU for the 5750 derivative in the
3624 * BCM4785.
3625 */
3626 if (tg3_flag(tp, IS_SSB_CORE))
3627 return 0;
3628
Nithin Sujir837c45b2013-03-06 17:02:30 +00003629 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003630 }
3631
Nithin Sujir837c45b2013-03-06 17:02:30 +00003632 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003633 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003634 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003635 return -ENODEV;
3636 }
3637
3638 /* Clear firmware's nvram arbitration. */
3639 if (tg3_flag(tp, NVRAM))
3640 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3641 return 0;
3642}
3643
Nithin Sujir31f11a92013-03-06 17:02:33 +00003644static int tg3_fw_data_len(struct tg3 *tp,
3645 const struct tg3_firmware_hdr *fw_hdr)
3646{
3647 int fw_len;
3648
3649 /* Non fragmented firmware have one firmware header followed by a
3650 * contiguous chunk of data to be written. The length field in that
3651 * header is not the length of data to be written but the complete
3652 * length of the bss. The data length is determined based on
3653 * tp->fw->size minus headers.
3654 *
3655 * Fragmented firmware have a main header followed by multiple
3656 * fragments. Each fragment is identical to non fragmented firmware
3657 * with a firmware header followed by a contiguous chunk of data. In
3658 * the main header, the length field is unused and set to 0xffffffff.
3659 * In each fragment header the length is the entire size of that
3660 * fragment i.e. fragment data + header length. Data length is
3661 * therefore length field in the header minus TG3_FW_HDR_LEN.
3662 */
3663 if (tp->fw_len == 0xffffffff)
3664 fw_len = be32_to_cpu(fw_hdr->len);
3665 else
3666 fw_len = tp->fw->size;
3667
3668 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3669}
3670
Matt Carlson997b4f12011-08-31 11:44:53 +00003671/* tp->lock is held. */
3672static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3673 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003674 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003675{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003676 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003677 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003678 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003679
3680 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3681 netdev_err(tp->dev,
3682 "%s: Trying to load TX cpu firmware which is 5705\n",
3683 __func__);
3684 return -EINVAL;
3685 }
3686
Nithin Sujirc4dab502013-03-06 17:02:34 +00003687 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003688 write_op = tg3_write_mem;
3689 else
3690 write_op = tg3_write_indirect_reg32;
3691
Nithin Sujirc4dab502013-03-06 17:02:34 +00003692 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3693 /* It is possible that bootcode is still loading at this point.
3694 * Get the nvram lock first before halting the cpu.
3695 */
3696 int lock_err = tg3_nvram_lock(tp);
3697 err = tg3_halt_cpu(tp, cpu_base);
3698 if (!lock_err)
3699 tg3_nvram_unlock(tp);
3700 if (err)
3701 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003702
Nithin Sujirc4dab502013-03-06 17:02:34 +00003703 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3704 write_op(tp, cpu_scratch_base + i, 0);
3705 tw32(cpu_base + CPU_STATE, 0xffffffff);
3706 tw32(cpu_base + CPU_MODE,
3707 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3708 } else {
3709 /* Subtract additional main header for fragmented firmware and
3710 * advance to the first fragment
3711 */
3712 total_len -= TG3_FW_HDR_LEN;
3713 fw_hdr++;
3714 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003715
Nithin Sujir31f11a92013-03-06 17:02:33 +00003716 do {
3717 u32 *fw_data = (u32 *)(fw_hdr + 1);
3718 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3719 write_op(tp, cpu_scratch_base +
3720 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3721 (i * sizeof(u32)),
3722 be32_to_cpu(fw_data[i]));
3723
3724 total_len -= be32_to_cpu(fw_hdr->len);
3725
3726 /* Advance to next fragment */
3727 fw_hdr = (struct tg3_firmware_hdr *)
3728 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3729 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003730
3731 err = 0;
3732
3733out:
3734 return err;
3735}
3736
3737/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003738static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3739{
3740 int i;
3741 const int iters = 5;
3742
3743 tw32(cpu_base + CPU_STATE, 0xffffffff);
3744 tw32_f(cpu_base + CPU_PC, pc);
3745
3746 for (i = 0; i < iters; i++) {
3747 if (tr32(cpu_base + CPU_PC) == pc)
3748 break;
3749 tw32(cpu_base + CPU_STATE, 0xffffffff);
3750 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3751 tw32_f(cpu_base + CPU_PC, pc);
3752 udelay(1000);
3753 }
3754
3755 return (i == iters) ? -EBUSY : 0;
3756}
3757
3758/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003759static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3760{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003761 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003762 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003763
Nithin Sujir77997ea2013-03-06 17:02:32 +00003764 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003765
3766 /* Firmware blob starts with version numbers, followed by
3767 start address and length. We are setting complete length.
3768 length = end_address_of_bss - start_address_of_text.
3769 Remainder is the blob to be loaded contiguously
3770 from start address. */
3771
Matt Carlson997b4f12011-08-31 11:44:53 +00003772 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3773 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003774 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003775 if (err)
3776 return err;
3777
3778 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3779 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003780 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003781 if (err)
3782 return err;
3783
3784 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003785 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3786 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003787 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003788 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3789 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003790 tr32(RX_CPU_BASE + CPU_PC),
3791 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003792 return -ENODEV;
3793 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003794
3795 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003796
3797 return 0;
3798}
3799
Nithin Sujirc4dab502013-03-06 17:02:34 +00003800static int tg3_validate_rxcpu_state(struct tg3 *tp)
3801{
3802 const int iters = 1000;
3803 int i;
3804 u32 val;
3805
3806 /* Wait for boot code to complete initialization and enter service
3807 * loop. It is then safe to download service patches
3808 */
3809 for (i = 0; i < iters; i++) {
3810 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3811 break;
3812
3813 udelay(10);
3814 }
3815
3816 if (i == iters) {
3817 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3818 return -EBUSY;
3819 }
3820
3821 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3822 if (val & 0xff) {
3823 netdev_warn(tp->dev,
3824 "Other patches exist. Not downloading EEE patch\n");
3825 return -EEXIST;
3826 }
3827
3828 return 0;
3829}
3830
3831/* tp->lock is held. */
3832static void tg3_load_57766_firmware(struct tg3 *tp)
3833{
3834 struct tg3_firmware_hdr *fw_hdr;
3835
3836 if (!tg3_flag(tp, NO_NVRAM))
3837 return;
3838
3839 if (tg3_validate_rxcpu_state(tp))
3840 return;
3841
3842 if (!tp->fw)
3843 return;
3844
3845 /* This firmware blob has a different format than older firmware
3846 * releases as given below. The main difference is we have fragmented
3847 * data to be written to non-contiguous locations.
3848 *
3849 * In the beginning we have a firmware header identical to other
3850 * firmware which consists of version, base addr and length. The length
3851 * here is unused and set to 0xffffffff.
3852 *
3853 * This is followed by a series of firmware fragments which are
3854 * individually identical to previous firmware. i.e. they have the
3855 * firmware header and followed by data for that fragment. The version
3856 * field of the individual fragment header is unused.
3857 */
3858
3859 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3860 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3861 return;
3862
3863 if (tg3_rxcpu_pause(tp))
3864 return;
3865
3866 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3867 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3868
3869 tg3_rxcpu_resume(tp);
3870}
3871
Matt Carlson997b4f12011-08-31 11:44:53 +00003872/* tp->lock is held. */
3873static int tg3_load_tso_firmware(struct tg3 *tp)
3874{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003875 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003876 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003877 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003878
Matt Carlson1caf13e2013-03-06 17:02:29 +00003879 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003880 return 0;
3881
Nithin Sujir77997ea2013-03-06 17:02:32 +00003882 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003883
3884 /* Firmware blob starts with version numbers, followed by
3885 start address and length. We are setting complete length.
3886 length = end_address_of_bss - start_address_of_text.
3887 Remainder is the blob to be loaded contiguously
3888 from start address. */
3889
Matt Carlson997b4f12011-08-31 11:44:53 +00003890 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003891
Joe Perches41535772013-02-16 11:20:04 +00003892 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003893 cpu_base = RX_CPU_BASE;
3894 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3895 } else {
3896 cpu_base = TX_CPU_BASE;
3897 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3898 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3899 }
3900
3901 err = tg3_load_firmware_cpu(tp, cpu_base,
3902 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003903 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003904 if (err)
3905 return err;
3906
3907 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003908 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3909 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003910 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003911 netdev_err(tp->dev,
3912 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003913 __func__, tr32(cpu_base + CPU_PC),
3914 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003915 return -ENODEV;
3916 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003917
3918 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003919 return 0;
3920}
3921
3922
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003923/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003924static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003925{
3926 u32 addr_high, addr_low;
3927 int i;
3928
3929 addr_high = ((tp->dev->dev_addr[0] << 8) |
3930 tp->dev->dev_addr[1]);
3931 addr_low = ((tp->dev->dev_addr[2] << 24) |
3932 (tp->dev->dev_addr[3] << 16) |
3933 (tp->dev->dev_addr[4] << 8) |
3934 (tp->dev->dev_addr[5] << 0));
3935 for (i = 0; i < 4; i++) {
3936 if (i == 1 && skip_mac_1)
3937 continue;
3938 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3939 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3940 }
3941
Joe Perches41535772013-02-16 11:20:04 +00003942 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3943 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003944 for (i = 0; i < 12; i++) {
3945 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3946 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3947 }
3948 }
3949
3950 addr_high = (tp->dev->dev_addr[0] +
3951 tp->dev->dev_addr[1] +
3952 tp->dev->dev_addr[2] +
3953 tp->dev->dev_addr[3] +
3954 tp->dev->dev_addr[4] +
3955 tp->dev->dev_addr[5]) &
3956 TX_BACKOFF_SEED_MASK;
3957 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3958}
3959
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003960static void tg3_enable_register_access(struct tg3 *tp)
3961{
3962 /*
3963 * Make sure register accesses (indirect or otherwise) will function
3964 * correctly.
3965 */
3966 pci_write_config_dword(tp->pdev,
3967 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3968}
3969
3970static int tg3_power_up(struct tg3 *tp)
3971{
Matt Carlsonbed98292011-07-13 09:27:29 +00003972 int err;
3973
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003974 tg3_enable_register_access(tp);
3975
Matt Carlsonbed98292011-07-13 09:27:29 +00003976 err = pci_set_power_state(tp->pdev, PCI_D0);
3977 if (!err) {
3978 /* Switch out of Vaux if it is a NIC */
3979 tg3_pwrsrc_switch_to_vmain(tp);
3980 } else {
3981 netdev_err(tp->dev, "Transition to D0 failed\n");
3982 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003983
Matt Carlsonbed98292011-07-13 09:27:29 +00003984 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003985}
3986
Joe Perches953c96e2013-04-09 10:18:14 +00003987static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00003988
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003989static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
3991 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003992 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003994 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003995
3996 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003997 if (tg3_flag(tp, CLKREQ_BUG))
3998 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3999 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4002 tw32(TG3PCI_MISC_HOST_CTRL,
4003 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4004
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004005 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004006 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004007
Joe Perches63c3a662011-04-26 08:12:10 +00004008 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004009 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004010 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00004011 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004012 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004013 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004014
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00004015 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004016
Matt Carlson80096062010-08-02 11:26:06 +00004017 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004018
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004019 tp->link_config.speed = phydev->speed;
4020 tp->link_config.duplex = phydev->duplex;
4021 tp->link_config.autoneg = phydev->autoneg;
4022 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004023
4024 advertising = ADVERTISED_TP |
4025 ADVERTISED_Pause |
4026 ADVERTISED_Autoneg |
4027 ADVERTISED_10baseT_Half;
4028
Joe Perches63c3a662011-04-26 08:12:10 +00004029 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4030 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004031 advertising |=
4032 ADVERTISED_100baseT_Half |
4033 ADVERTISED_100baseT_Full |
4034 ADVERTISED_10baseT_Full;
4035 else
4036 advertising |= ADVERTISED_10baseT_Full;
4037 }
4038
4039 phydev->advertising = advertising;
4040
4041 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004042
4043 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004044 if (phyid != PHY_ID_BCMAC131) {
4045 phyid &= PHY_BCM_OUI_MASK;
4046 if (phyid == PHY_BCM_OUI_1 ||
4047 phyid == PHY_BCM_OUI_2 ||
4048 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004049 do_low_power = true;
4050 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004051 }
Matt Carlsondd477002008-05-25 23:45:58 -07004052 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004053 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004054
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004055 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004056 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Matt Carlson2855b9f2012-02-13 15:20:14 +00004058 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004059 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
4061
Joe Perches41535772013-02-16 11:20:04 +00004062 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004063 u32 val;
4064
4065 val = tr32(GRC_VCPU_EXT_CTRL);
4066 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004067 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004068 int i;
4069 u32 val;
4070
4071 for (i = 0; i < 200; i++) {
4072 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4073 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4074 break;
4075 msleep(1);
4076 }
4077 }
Joe Perches63c3a662011-04-26 08:12:10 +00004078 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004079 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4080 WOL_DRV_STATE_SHUTDOWN |
4081 WOL_DRV_WOL |
4082 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004083
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004084 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 u32 mac_mode;
4086
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004087 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004088 if (do_low_power &&
4089 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4090 tg3_phy_auxctl_write(tp,
4091 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4092 MII_TG3_AUXCTL_PCTL_WOL_EN |
4093 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4094 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004095 udelay(40);
4096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004098 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004099 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004100 else if (tp->phy_flags &
4101 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4102 if (tp->link_config.active_speed == SPEED_1000)
4103 mac_mode = MAC_MODE_PORT_MODE_GMII;
4104 else
4105 mac_mode = MAC_MODE_PORT_MODE_MII;
4106 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004107 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004109 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004110 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004111 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004112 SPEED_100 : SPEED_10;
4113 if (tg3_5700_link_polarity(tp, speed))
4114 mac_mode |= MAC_MODE_LINK_POLARITY;
4115 else
4116 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 } else {
4119 mac_mode = MAC_MODE_PORT_MODE_TBI;
4120 }
4121
Joe Perches63c3a662011-04-26 08:12:10 +00004122 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 tw32(MAC_LED_CTRL, tp->led_ctrl);
4124
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004125 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004126 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4127 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004128 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Joe Perches63c3a662011-04-26 08:12:10 +00004130 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004131 mac_mode |= MAC_MODE_APE_TX_EN |
4132 MAC_MODE_APE_RX_EN |
4133 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 tw32_f(MAC_MODE, mac_mode);
4136 udelay(100);
4137
4138 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4139 udelay(10);
4140 }
4141
Joe Perches63c3a662011-04-26 08:12:10 +00004142 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004143 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4144 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 u32 base_val;
4146
4147 base_val = tp->pci_clock_ctrl;
4148 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4149 CLOCK_CTRL_TXCLK_DISABLE);
4150
Michael Chanb401e9e2005-12-19 16:27:04 -08004151 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4152 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004153 } else if (tg3_flag(tp, 5780_CLASS) ||
4154 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004155 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004156 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004157 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 u32 newbits1, newbits2;
4159
Joe Perches41535772013-02-16 11:20:04 +00004160 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4161 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4163 CLOCK_CTRL_TXCLK_DISABLE |
4164 CLOCK_CTRL_ALTCLK);
4165 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004166 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 newbits1 = CLOCK_CTRL_625_CORE;
4168 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4169 } else {
4170 newbits1 = CLOCK_CTRL_ALTCLK;
4171 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4172 }
4173
Michael Chanb401e9e2005-12-19 16:27:04 -08004174 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4175 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Michael Chanb401e9e2005-12-19 16:27:04 -08004177 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4178 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
Joe Perches63c3a662011-04-26 08:12:10 +00004180 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 u32 newbits3;
4182
Joe Perches41535772013-02-16 11:20:04 +00004183 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4184 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4186 CLOCK_CTRL_TXCLK_DISABLE |
4187 CLOCK_CTRL_44MHZ_CORE);
4188 } else {
4189 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4190 }
4191
Michael Chanb401e9e2005-12-19 16:27:04 -08004192 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4193 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 }
4195 }
4196
Joe Perches63c3a662011-04-26 08:12:10 +00004197 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004198 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004199
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004200 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
4202 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004203 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004204 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4205 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 u32 val = tr32(0x7d00);
4207
4208 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4209 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004210 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004211 int err;
4212
4213 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004215 if (!err)
4216 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
4219
Michael Chanbbadf502006-04-06 21:46:34 -07004220 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4221
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004222 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4223
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 return 0;
4225}
4226
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004227static void tg3_power_down(struct tg3 *tp)
4228{
4229 tg3_power_down_prepare(tp);
4230
Joe Perches63c3a662011-04-26 08:12:10 +00004231 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004232 pci_set_power_state(tp->pdev, PCI_D3hot);
4233}
4234
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4236{
4237 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4238 case MII_TG3_AUX_STAT_10HALF:
4239 *speed = SPEED_10;
4240 *duplex = DUPLEX_HALF;
4241 break;
4242
4243 case MII_TG3_AUX_STAT_10FULL:
4244 *speed = SPEED_10;
4245 *duplex = DUPLEX_FULL;
4246 break;
4247
4248 case MII_TG3_AUX_STAT_100HALF:
4249 *speed = SPEED_100;
4250 *duplex = DUPLEX_HALF;
4251 break;
4252
4253 case MII_TG3_AUX_STAT_100FULL:
4254 *speed = SPEED_100;
4255 *duplex = DUPLEX_FULL;
4256 break;
4257
4258 case MII_TG3_AUX_STAT_1000HALF:
4259 *speed = SPEED_1000;
4260 *duplex = DUPLEX_HALF;
4261 break;
4262
4263 case MII_TG3_AUX_STAT_1000FULL:
4264 *speed = SPEED_1000;
4265 *duplex = DUPLEX_FULL;
4266 break;
4267
4268 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004269 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004270 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4271 SPEED_10;
4272 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4273 DUPLEX_HALF;
4274 break;
4275 }
Matt Carlsone7405222012-02-13 15:20:16 +00004276 *speed = SPEED_UNKNOWN;
4277 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
Matt Carlson42b64a42011-05-19 12:12:49 +00004282static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
Matt Carlson42b64a42011-05-19 12:12:49 +00004284 int err = 0;
4285 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Matt Carlson42b64a42011-05-19 12:12:49 +00004287 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004288 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004289 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Matt Carlson42b64a42011-05-19 12:12:49 +00004291 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4292 if (err)
4293 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Matt Carlson4f272092011-12-14 11:09:57 +00004295 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4296 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004297
Joe Perches41535772013-02-16 11:20:04 +00004298 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4299 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004300 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004301
Matt Carlson4f272092011-12-14 11:09:57 +00004302 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4303 if (err)
4304 goto done;
4305 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004306
Matt Carlson42b64a42011-05-19 12:12:49 +00004307 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4308 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Matt Carlson42b64a42011-05-19 12:12:49 +00004310 tw32(TG3_CPMU_EEE_MODE,
4311 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004312
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004313 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004314 if (!err) {
4315 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004316
Matt Carlsona6b68da2010-12-06 08:28:52 +00004317 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004318 /* Advertise 100-BaseTX EEE ability */
4319 if (advertise & ADVERTISED_100baseT_Full)
4320 val |= MDIO_AN_EEE_ADV_100TX;
4321 /* Advertise 1000-BaseT EEE ability */
4322 if (advertise & ADVERTISED_1000baseT_Full)
4323 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004324
4325 if (!tp->eee.eee_enabled) {
4326 val = 0;
4327 tp->eee.advertised = 0;
4328 } else {
4329 tp->eee.advertised = advertise &
4330 (ADVERTISED_100baseT_Full |
4331 ADVERTISED_1000baseT_Full);
4332 }
4333
Matt Carlson42b64a42011-05-19 12:12:49 +00004334 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004335 if (err)
4336 val = 0;
4337
Joe Perches41535772013-02-16 11:20:04 +00004338 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004339 case ASIC_REV_5717:
4340 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004341 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004342 case ASIC_REV_5719:
4343 /* If we advertised any eee advertisements above... */
4344 if (val)
4345 val = MII_TG3_DSP_TAP26_ALNOKO |
4346 MII_TG3_DSP_TAP26_RMRXSTO |
4347 MII_TG3_DSP_TAP26_OPCSINPT;
4348 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4349 /* Fall through */
4350 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004351 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004352 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4353 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4354 MII_TG3_DSP_CH34TP2_HIBW01);
4355 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004356
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004357 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004358 if (!err)
4359 err = err2;
4360 }
4361
4362done:
4363 return err;
4364}
4365
4366static void tg3_phy_copper_begin(struct tg3 *tp)
4367{
Matt Carlsond13ba512012-02-22 12:35:19 +00004368 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4369 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4370 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004371
Nithin Sujir942d1af2013-04-09 08:48:07 +00004372 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4373 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004374 adv = ADVERTISED_10baseT_Half |
4375 ADVERTISED_10baseT_Full;
4376 if (tg3_flag(tp, WOL_SPEED_100MB))
4377 adv |= ADVERTISED_100baseT_Half |
4378 ADVERTISED_100baseT_Full;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004379 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK)
4380 adv |= ADVERTISED_1000baseT_Half |
4381 ADVERTISED_1000baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004382
Matt Carlsond13ba512012-02-22 12:35:19 +00004383 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004384 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004385 adv = tp->link_config.advertising;
4386 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4387 adv &= ~(ADVERTISED_1000baseT_Half |
4388 ADVERTISED_1000baseT_Full);
4389
4390 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004391 }
4392
Matt Carlsond13ba512012-02-22 12:35:19 +00004393 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004394
Nithin Sujir942d1af2013-04-09 08:48:07 +00004395 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4396 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4397 /* Normally during power down we want to autonegotiate
4398 * the lowest possible speed for WOL. However, to avoid
4399 * link flap, we leave it untouched.
4400 */
4401 return;
4402 }
4403
Matt Carlsond13ba512012-02-22 12:35:19 +00004404 tg3_writephy(tp, MII_BMCR,
4405 BMCR_ANENABLE | BMCR_ANRESTART);
4406 } else {
4407 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 u32 bmcr, orig_bmcr;
4409
4410 tp->link_config.active_speed = tp->link_config.speed;
4411 tp->link_config.active_duplex = tp->link_config.duplex;
4412
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004413 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4414 /* With autoneg disabled, 5715 only links up when the
4415 * advertisement register has the configured speed
4416 * enabled.
4417 */
4418 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4419 }
4420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 bmcr = 0;
4422 switch (tp->link_config.speed) {
4423 default:
4424 case SPEED_10:
4425 break;
4426
4427 case SPEED_100:
4428 bmcr |= BMCR_SPEED100;
4429 break;
4430
4431 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004432 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 if (tp->link_config.duplex == DUPLEX_FULL)
4437 bmcr |= BMCR_FULLDPLX;
4438
4439 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4440 (bmcr != orig_bmcr)) {
4441 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4442 for (i = 0; i < 1500; i++) {
4443 u32 tmp;
4444
4445 udelay(10);
4446 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4447 tg3_readphy(tp, MII_BMSR, &tmp))
4448 continue;
4449 if (!(tmp & BMSR_LSTATUS)) {
4450 udelay(40);
4451 break;
4452 }
4453 }
4454 tg3_writephy(tp, MII_BMCR, bmcr);
4455 udelay(40);
4456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
4458}
4459
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004460static int tg3_phy_pull_config(struct tg3 *tp)
4461{
4462 int err;
4463 u32 val;
4464
4465 err = tg3_readphy(tp, MII_BMCR, &val);
4466 if (err)
4467 goto done;
4468
4469 if (!(val & BMCR_ANENABLE)) {
4470 tp->link_config.autoneg = AUTONEG_DISABLE;
4471 tp->link_config.advertising = 0;
4472 tg3_flag_clear(tp, PAUSE_AUTONEG);
4473
4474 err = -EIO;
4475
4476 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4477 case 0:
4478 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4479 goto done;
4480
4481 tp->link_config.speed = SPEED_10;
4482 break;
4483 case BMCR_SPEED100:
4484 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4485 goto done;
4486
4487 tp->link_config.speed = SPEED_100;
4488 break;
4489 case BMCR_SPEED1000:
4490 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4491 tp->link_config.speed = SPEED_1000;
4492 break;
4493 }
4494 /* Fall through */
4495 default:
4496 goto done;
4497 }
4498
4499 if (val & BMCR_FULLDPLX)
4500 tp->link_config.duplex = DUPLEX_FULL;
4501 else
4502 tp->link_config.duplex = DUPLEX_HALF;
4503
4504 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4505
4506 err = 0;
4507 goto done;
4508 }
4509
4510 tp->link_config.autoneg = AUTONEG_ENABLE;
4511 tp->link_config.advertising = ADVERTISED_Autoneg;
4512 tg3_flag_set(tp, PAUSE_AUTONEG);
4513
4514 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4515 u32 adv;
4516
4517 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4518 if (err)
4519 goto done;
4520
4521 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4522 tp->link_config.advertising |= adv | ADVERTISED_TP;
4523
4524 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4525 } else {
4526 tp->link_config.advertising |= ADVERTISED_FIBRE;
4527 }
4528
4529 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4530 u32 adv;
4531
4532 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4533 err = tg3_readphy(tp, MII_CTRL1000, &val);
4534 if (err)
4535 goto done;
4536
4537 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4538 } else {
4539 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4540 if (err)
4541 goto done;
4542
4543 adv = tg3_decode_flowctrl_1000X(val);
4544 tp->link_config.flowctrl = adv;
4545
4546 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4547 adv = mii_adv_to_ethtool_adv_x(val);
4548 }
4549
4550 tp->link_config.advertising |= adv;
4551 }
4552
4553done:
4554 return err;
4555}
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557static int tg3_init_5401phy_dsp(struct tg3 *tp)
4558{
4559 int err;
4560
4561 /* Turn off tap power management. */
4562 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004563 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004565 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4566 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4567 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4568 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4569 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
4571 udelay(40);
4572
4573 return err;
4574}
4575
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004576static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4577{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004578 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004579
4580 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4581 return true;
4582
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004583 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004584
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004585 if (tp->eee.eee_enabled) {
4586 if (tp->eee.advertised != eee.advertised ||
4587 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4588 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4589 return false;
4590 } else {
4591 /* EEE is disabled but we're advertising */
4592 if (eee.advertised)
4593 return false;
4594 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004595
4596 return true;
4597}
4598
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004599static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004601 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004602
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004603 advertising = tp->link_config.advertising;
4604 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004606 advmsk = ADVERTISE_ALL;
4607 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004608 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004609 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004612 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4613 return false;
4614
4615 if ((*lcladv & advmsk) != tgtadv)
4616 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004617
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004618 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 u32 tg3_ctrl;
4620
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004621 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004622
Matt Carlson221c5632011-06-13 13:39:01 +00004623 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004624 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Matt Carlson3198e072012-02-13 15:20:10 +00004626 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004627 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4628 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004629 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4630 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4631 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4632 } else {
4633 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4634 }
4635
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004636 if (tg3_ctrl != tgtadv)
4637 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004639
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004640 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004641}
4642
Matt Carlson859edb22011-12-08 14:40:16 +00004643static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4644{
4645 u32 lpeth = 0;
4646
4647 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4648 u32 val;
4649
4650 if (tg3_readphy(tp, MII_STAT1000, &val))
4651 return false;
4652
4653 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4654 }
4655
4656 if (tg3_readphy(tp, MII_LPA, rmtadv))
4657 return false;
4658
4659 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4660 tp->link_config.rmt_adv = lpeth;
4661
4662 return true;
4663}
4664
Joe Perches953c96e2013-04-09 10:18:14 +00004665static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004666{
4667 if (curr_link_up != tp->link_up) {
4668 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004669 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004670 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004671 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004672 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4673 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4674 }
4675
4676 tg3_link_report(tp);
4677 return true;
4678 }
4679
4680 return false;
4681}
4682
Michael Chan3310e242013-04-09 08:48:05 +00004683static void tg3_clear_mac_status(struct tg3 *tp)
4684{
4685 tw32(MAC_EVENT, 0);
4686
4687 tw32_f(MAC_STATUS,
4688 MAC_STATUS_SYNC_CHANGED |
4689 MAC_STATUS_CFG_CHANGED |
4690 MAC_STATUS_MI_COMPLETION |
4691 MAC_STATUS_LNKSTATE_CHANGED);
4692 udelay(40);
4693}
4694
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004695static void tg3_setup_eee(struct tg3 *tp)
4696{
4697 u32 val;
4698
4699 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4700 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4701 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4702 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4703
4704 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4705
4706 tw32_f(TG3_CPMU_EEE_CTRL,
4707 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4708
4709 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4710 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4711 TG3_CPMU_EEEMD_LPI_IN_RX |
4712 TG3_CPMU_EEEMD_EEE_ENABLE;
4713
4714 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4715 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4716
4717 if (tg3_flag(tp, ENABLE_APE))
4718 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4719
4720 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4721
4722 tw32_f(TG3_CPMU_EEE_DBTMR1,
4723 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4724 (tp->eee.tx_lpi_timer & 0xffff));
4725
4726 tw32_f(TG3_CPMU_EEE_DBTMR2,
4727 TG3_CPMU_DBTMR2_APE_TX_2047US |
4728 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4729}
4730
Joe Perches953c96e2013-04-09 10:18:14 +00004731static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732{
Joe Perches953c96e2013-04-09 10:18:14 +00004733 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004734 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004735 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 u16 current_speed;
4737 u8 current_duplex;
4738 int i, err;
4739
Michael Chan3310e242013-04-09 08:48:05 +00004740 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Matt Carlson8ef21422008-05-02 16:47:53 -07004742 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4743 tw32_f(MAC_MI_MODE,
4744 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4745 udelay(80);
4746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004748 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 /* Some third-party PHYs need to be reset on link going
4751 * down.
4752 */
Joe Perches41535772013-02-16 11:20:04 +00004753 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4754 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4755 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004756 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 tg3_readphy(tp, MII_BMSR, &bmsr);
4758 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4759 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004760 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 }
4762 if (force_reset)
4763 tg3_phy_reset(tp);
4764
Matt Carlson79eb6902010-02-17 15:17:03 +00004765 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 tg3_readphy(tp, MII_BMSR, &bmsr);
4767 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004768 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 bmsr = 0;
4770
4771 if (!(bmsr & BMSR_LSTATUS)) {
4772 err = tg3_init_5401phy_dsp(tp);
4773 if (err)
4774 return err;
4775
4776 tg3_readphy(tp, MII_BMSR, &bmsr);
4777 for (i = 0; i < 1000; i++) {
4778 udelay(10);
4779 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4780 (bmsr & BMSR_LSTATUS)) {
4781 udelay(40);
4782 break;
4783 }
4784 }
4785
Matt Carlson79eb6902010-02-17 15:17:03 +00004786 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4787 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 !(bmsr & BMSR_LSTATUS) &&
4789 tp->link_config.active_speed == SPEED_1000) {
4790 err = tg3_phy_reset(tp);
4791 if (!err)
4792 err = tg3_init_5401phy_dsp(tp);
4793 if (err)
4794 return err;
4795 }
4796 }
Joe Perches41535772013-02-16 11:20:04 +00004797 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4798 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 /* 5701 {A0,B0} CRC bug workaround */
4800 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004801 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4802 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4803 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
4805
4806 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004807 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4808 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004810 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004812 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4814
Joe Perches41535772013-02-16 11:20:04 +00004815 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4816 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4818 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4819 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4820 else
4821 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4822 }
4823
Joe Perches953c96e2013-04-09 10:18:14 +00004824 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004825 current_speed = SPEED_UNKNOWN;
4826 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004827 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004828 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004830 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004831 err = tg3_phy_auxctl_read(tp,
4832 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4833 &val);
4834 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004835 tg3_phy_auxctl_write(tp,
4836 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4837 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 goto relink;
4839 }
4840 }
4841
4842 bmsr = 0;
4843 for (i = 0; i < 100; i++) {
4844 tg3_readphy(tp, MII_BMSR, &bmsr);
4845 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4846 (bmsr & BMSR_LSTATUS))
4847 break;
4848 udelay(40);
4849 }
4850
4851 if (bmsr & BMSR_LSTATUS) {
4852 u32 aux_stat, bmcr;
4853
4854 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4855 for (i = 0; i < 2000; i++) {
4856 udelay(10);
4857 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4858 aux_stat)
4859 break;
4860 }
4861
4862 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4863 &current_speed,
4864 &current_duplex);
4865
4866 bmcr = 0;
4867 for (i = 0; i < 200; i++) {
4868 tg3_readphy(tp, MII_BMCR, &bmcr);
4869 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4870 continue;
4871 if (bmcr && bmcr != 0x7fff)
4872 break;
4873 udelay(10);
4874 }
4875
Matt Carlsonef167e22007-12-20 20:10:01 -08004876 lcl_adv = 0;
4877 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
Matt Carlsonef167e22007-12-20 20:10:01 -08004879 tp->link_config.active_speed = current_speed;
4880 tp->link_config.active_duplex = current_duplex;
4881
4882 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004883 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4884
Matt Carlsonef167e22007-12-20 20:10:01 -08004885 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004886 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004887 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004888 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004889 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004890
4891 /* EEE settings changes take effect only after a phy
4892 * reset. If we have skipped a reset due to Link Flap
4893 * Avoidance being enabled, do it now.
4894 */
4895 if (!eee_config_ok &&
4896 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004897 !force_reset) {
4898 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004899 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 } else {
4902 if (!(bmcr & BMCR_ANENABLE) &&
4903 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004904 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004905 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 }
4907 }
4908
Joe Perches953c96e2013-04-09 10:18:14 +00004909 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004910 tp->link_config.active_duplex == DUPLEX_FULL) {
4911 u32 reg, bit;
4912
4913 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4914 reg = MII_TG3_FET_GEN_STAT;
4915 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4916 } else {
4917 reg = MII_TG3_EXT_STAT;
4918 bit = MII_TG3_EXT_STAT_MDIX;
4919 }
4920
4921 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4922 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4923
Matt Carlsonef167e22007-12-20 20:10:01 -08004924 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 }
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004929 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 tg3_phy_copper_begin(tp);
4931
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004932 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004933 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004934 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4935 current_speed = SPEED_1000;
4936 current_duplex = DUPLEX_FULL;
4937 tp->link_config.active_speed = current_speed;
4938 tp->link_config.active_duplex = current_duplex;
4939 }
4940
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004941 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004942 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4943 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004944 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 }
4946
4947 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004948 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (tp->link_config.active_speed == SPEED_100 ||
4950 tp->link_config.active_speed == SPEED_10)
4951 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4952 else
4953 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004954 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004955 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4956 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4958
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004959 /* In order for the 5750 core in BCM4785 chip to work properly
4960 * in RGMII mode, the Led Control Register must be set up.
4961 */
4962 if (tg3_flag(tp, RGMII_MODE)) {
4963 u32 led_ctrl = tr32(MAC_LED_CTRL);
4964 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4965
4966 if (tp->link_config.active_speed == SPEED_10)
4967 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4968 else if (tp->link_config.active_speed == SPEED_100)
4969 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4970 LED_CTRL_100MBPS_ON);
4971 else if (tp->link_config.active_speed == SPEED_1000)
4972 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4973 LED_CTRL_1000MBPS_ON);
4974
4975 tw32(MAC_LED_CTRL, led_ctrl);
4976 udelay(40);
4977 }
4978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4980 if (tp->link_config.active_duplex == DUPLEX_HALF)
4981 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4982
Joe Perches41535772013-02-16 11:20:04 +00004983 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00004984 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004985 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004987 else
4988 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 }
4990
4991 /* ??? Without this setting Netgear GA302T PHY does not
4992 * ??? send/receive packets...
4993 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004994 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004995 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4997 tw32_f(MAC_MI_MODE, tp->mi_mode);
4998 udelay(80);
4999 }
5000
5001 tw32_f(MAC_MODE, tp->mac_mode);
5002 udelay(40);
5003
Matt Carlson52b02d02010-10-14 10:37:41 +00005004 tg3_phy_eee_adjust(tp, current_link_up);
5005
Joe Perches63c3a662011-04-26 08:12:10 +00005006 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 /* Polled via timer. */
5008 tw32_f(MAC_EVENT, 0);
5009 } else {
5010 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5011 }
5012 udelay(40);
5013
Joe Perches41535772013-02-16 11:20:04 +00005014 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005015 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005017 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 udelay(120);
5019 tw32_f(MAC_STATUS,
5020 (MAC_STATUS_SYNC_CHANGED |
5021 MAC_STATUS_CFG_CHANGED));
5022 udelay(40);
5023 tg3_write_mem(tp,
5024 NIC_SRAM_FIRMWARE_MBOX,
5025 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5026 }
5027
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005028 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005029 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005030 if (tp->link_config.active_speed == SPEED_100 ||
5031 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005032 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5033 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005034 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005035 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5036 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005037 }
5038
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005039 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
5041 return 0;
5042}
5043
5044struct tg3_fiber_aneginfo {
5045 int state;
5046#define ANEG_STATE_UNKNOWN 0
5047#define ANEG_STATE_AN_ENABLE 1
5048#define ANEG_STATE_RESTART_INIT 2
5049#define ANEG_STATE_RESTART 3
5050#define ANEG_STATE_DISABLE_LINK_OK 4
5051#define ANEG_STATE_ABILITY_DETECT_INIT 5
5052#define ANEG_STATE_ABILITY_DETECT 6
5053#define ANEG_STATE_ACK_DETECT_INIT 7
5054#define ANEG_STATE_ACK_DETECT 8
5055#define ANEG_STATE_COMPLETE_ACK_INIT 9
5056#define ANEG_STATE_COMPLETE_ACK 10
5057#define ANEG_STATE_IDLE_DETECT_INIT 11
5058#define ANEG_STATE_IDLE_DETECT 12
5059#define ANEG_STATE_LINK_OK 13
5060#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5061#define ANEG_STATE_NEXT_PAGE_WAIT 15
5062
5063 u32 flags;
5064#define MR_AN_ENABLE 0x00000001
5065#define MR_RESTART_AN 0x00000002
5066#define MR_AN_COMPLETE 0x00000004
5067#define MR_PAGE_RX 0x00000008
5068#define MR_NP_LOADED 0x00000010
5069#define MR_TOGGLE_TX 0x00000020
5070#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5071#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5072#define MR_LP_ADV_SYM_PAUSE 0x00000100
5073#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5074#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5075#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5076#define MR_LP_ADV_NEXT_PAGE 0x00001000
5077#define MR_TOGGLE_RX 0x00002000
5078#define MR_NP_RX 0x00004000
5079
5080#define MR_LINK_OK 0x80000000
5081
5082 unsigned long link_time, cur_time;
5083
5084 u32 ability_match_cfg;
5085 int ability_match_count;
5086
5087 char ability_match, idle_match, ack_match;
5088
5089 u32 txconfig, rxconfig;
5090#define ANEG_CFG_NP 0x00000080
5091#define ANEG_CFG_ACK 0x00000040
5092#define ANEG_CFG_RF2 0x00000020
5093#define ANEG_CFG_RF1 0x00000010
5094#define ANEG_CFG_PS2 0x00000001
5095#define ANEG_CFG_PS1 0x00008000
5096#define ANEG_CFG_HD 0x00004000
5097#define ANEG_CFG_FD 0x00002000
5098#define ANEG_CFG_INVAL 0x00001f06
5099
5100};
5101#define ANEG_OK 0
5102#define ANEG_DONE 1
5103#define ANEG_TIMER_ENAB 2
5104#define ANEG_FAILED -1
5105
5106#define ANEG_STATE_SETTLE_TIME 10000
5107
5108static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5109 struct tg3_fiber_aneginfo *ap)
5110{
Matt Carlson5be73b42007-12-20 20:09:29 -08005111 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 unsigned long delta;
5113 u32 rx_cfg_reg;
5114 int ret;
5115
5116 if (ap->state == ANEG_STATE_UNKNOWN) {
5117 ap->rxconfig = 0;
5118 ap->link_time = 0;
5119 ap->cur_time = 0;
5120 ap->ability_match_cfg = 0;
5121 ap->ability_match_count = 0;
5122 ap->ability_match = 0;
5123 ap->idle_match = 0;
5124 ap->ack_match = 0;
5125 }
5126 ap->cur_time++;
5127
5128 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5129 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5130
5131 if (rx_cfg_reg != ap->ability_match_cfg) {
5132 ap->ability_match_cfg = rx_cfg_reg;
5133 ap->ability_match = 0;
5134 ap->ability_match_count = 0;
5135 } else {
5136 if (++ap->ability_match_count > 1) {
5137 ap->ability_match = 1;
5138 ap->ability_match_cfg = rx_cfg_reg;
5139 }
5140 }
5141 if (rx_cfg_reg & ANEG_CFG_ACK)
5142 ap->ack_match = 1;
5143 else
5144 ap->ack_match = 0;
5145
5146 ap->idle_match = 0;
5147 } else {
5148 ap->idle_match = 1;
5149 ap->ability_match_cfg = 0;
5150 ap->ability_match_count = 0;
5151 ap->ability_match = 0;
5152 ap->ack_match = 0;
5153
5154 rx_cfg_reg = 0;
5155 }
5156
5157 ap->rxconfig = rx_cfg_reg;
5158 ret = ANEG_OK;
5159
Matt Carlson33f401a2010-04-05 10:19:27 +00005160 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 case ANEG_STATE_UNKNOWN:
5162 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5163 ap->state = ANEG_STATE_AN_ENABLE;
5164
5165 /* fallthru */
5166 case ANEG_STATE_AN_ENABLE:
5167 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5168 if (ap->flags & MR_AN_ENABLE) {
5169 ap->link_time = 0;
5170 ap->cur_time = 0;
5171 ap->ability_match_cfg = 0;
5172 ap->ability_match_count = 0;
5173 ap->ability_match = 0;
5174 ap->idle_match = 0;
5175 ap->ack_match = 0;
5176
5177 ap->state = ANEG_STATE_RESTART_INIT;
5178 } else {
5179 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5180 }
5181 break;
5182
5183 case ANEG_STATE_RESTART_INIT:
5184 ap->link_time = ap->cur_time;
5185 ap->flags &= ~(MR_NP_LOADED);
5186 ap->txconfig = 0;
5187 tw32(MAC_TX_AUTO_NEG, 0);
5188 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5189 tw32_f(MAC_MODE, tp->mac_mode);
5190 udelay(40);
5191
5192 ret = ANEG_TIMER_ENAB;
5193 ap->state = ANEG_STATE_RESTART;
5194
5195 /* fallthru */
5196 case ANEG_STATE_RESTART:
5197 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005198 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005200 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 break;
5203
5204 case ANEG_STATE_DISABLE_LINK_OK:
5205 ret = ANEG_DONE;
5206 break;
5207
5208 case ANEG_STATE_ABILITY_DETECT_INIT:
5209 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005210 ap->txconfig = ANEG_CFG_FD;
5211 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5212 if (flowctrl & ADVERTISE_1000XPAUSE)
5213 ap->txconfig |= ANEG_CFG_PS1;
5214 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5215 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5217 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5218 tw32_f(MAC_MODE, tp->mac_mode);
5219 udelay(40);
5220
5221 ap->state = ANEG_STATE_ABILITY_DETECT;
5222 break;
5223
5224 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005225 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 break;
5228
5229 case ANEG_STATE_ACK_DETECT_INIT:
5230 ap->txconfig |= ANEG_CFG_ACK;
5231 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5232 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5233 tw32_f(MAC_MODE, tp->mac_mode);
5234 udelay(40);
5235
5236 ap->state = ANEG_STATE_ACK_DETECT;
5237
5238 /* fallthru */
5239 case ANEG_STATE_ACK_DETECT:
5240 if (ap->ack_match != 0) {
5241 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5242 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5243 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5244 } else {
5245 ap->state = ANEG_STATE_AN_ENABLE;
5246 }
5247 } else if (ap->ability_match != 0 &&
5248 ap->rxconfig == 0) {
5249 ap->state = ANEG_STATE_AN_ENABLE;
5250 }
5251 break;
5252
5253 case ANEG_STATE_COMPLETE_ACK_INIT:
5254 if (ap->rxconfig & ANEG_CFG_INVAL) {
5255 ret = ANEG_FAILED;
5256 break;
5257 }
5258 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5259 MR_LP_ADV_HALF_DUPLEX |
5260 MR_LP_ADV_SYM_PAUSE |
5261 MR_LP_ADV_ASYM_PAUSE |
5262 MR_LP_ADV_REMOTE_FAULT1 |
5263 MR_LP_ADV_REMOTE_FAULT2 |
5264 MR_LP_ADV_NEXT_PAGE |
5265 MR_TOGGLE_RX |
5266 MR_NP_RX);
5267 if (ap->rxconfig & ANEG_CFG_FD)
5268 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5269 if (ap->rxconfig & ANEG_CFG_HD)
5270 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5271 if (ap->rxconfig & ANEG_CFG_PS1)
5272 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5273 if (ap->rxconfig & ANEG_CFG_PS2)
5274 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5275 if (ap->rxconfig & ANEG_CFG_RF1)
5276 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5277 if (ap->rxconfig & ANEG_CFG_RF2)
5278 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5279 if (ap->rxconfig & ANEG_CFG_NP)
5280 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5281
5282 ap->link_time = ap->cur_time;
5283
5284 ap->flags ^= (MR_TOGGLE_TX);
5285 if (ap->rxconfig & 0x0008)
5286 ap->flags |= MR_TOGGLE_RX;
5287 if (ap->rxconfig & ANEG_CFG_NP)
5288 ap->flags |= MR_NP_RX;
5289 ap->flags |= MR_PAGE_RX;
5290
5291 ap->state = ANEG_STATE_COMPLETE_ACK;
5292 ret = ANEG_TIMER_ENAB;
5293 break;
5294
5295 case ANEG_STATE_COMPLETE_ACK:
5296 if (ap->ability_match != 0 &&
5297 ap->rxconfig == 0) {
5298 ap->state = ANEG_STATE_AN_ENABLE;
5299 break;
5300 }
5301 delta = ap->cur_time - ap->link_time;
5302 if (delta > ANEG_STATE_SETTLE_TIME) {
5303 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5304 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5305 } else {
5306 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5307 !(ap->flags & MR_NP_RX)) {
5308 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5309 } else {
5310 ret = ANEG_FAILED;
5311 }
5312 }
5313 }
5314 break;
5315
5316 case ANEG_STATE_IDLE_DETECT_INIT:
5317 ap->link_time = ap->cur_time;
5318 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5319 tw32_f(MAC_MODE, tp->mac_mode);
5320 udelay(40);
5321
5322 ap->state = ANEG_STATE_IDLE_DETECT;
5323 ret = ANEG_TIMER_ENAB;
5324 break;
5325
5326 case ANEG_STATE_IDLE_DETECT:
5327 if (ap->ability_match != 0 &&
5328 ap->rxconfig == 0) {
5329 ap->state = ANEG_STATE_AN_ENABLE;
5330 break;
5331 }
5332 delta = ap->cur_time - ap->link_time;
5333 if (delta > ANEG_STATE_SETTLE_TIME) {
5334 /* XXX another gem from the Broadcom driver :( */
5335 ap->state = ANEG_STATE_LINK_OK;
5336 }
5337 break;
5338
5339 case ANEG_STATE_LINK_OK:
5340 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5341 ret = ANEG_DONE;
5342 break;
5343
5344 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5345 /* ??? unimplemented */
5346 break;
5347
5348 case ANEG_STATE_NEXT_PAGE_WAIT:
5349 /* ??? unimplemented */
5350 break;
5351
5352 default:
5353 ret = ANEG_FAILED;
5354 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
5357 return ret;
5358}
5359
Matt Carlson5be73b42007-12-20 20:09:29 -08005360static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361{
5362 int res = 0;
5363 struct tg3_fiber_aneginfo aninfo;
5364 int status = ANEG_FAILED;
5365 unsigned int tick;
5366 u32 tmp;
5367
5368 tw32_f(MAC_TX_AUTO_NEG, 0);
5369
5370 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5371 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5372 udelay(40);
5373
5374 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5375 udelay(40);
5376
5377 memset(&aninfo, 0, sizeof(aninfo));
5378 aninfo.flags |= MR_AN_ENABLE;
5379 aninfo.state = ANEG_STATE_UNKNOWN;
5380 aninfo.cur_time = 0;
5381 tick = 0;
5382 while (++tick < 195000) {
5383 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5384 if (status == ANEG_DONE || status == ANEG_FAILED)
5385 break;
5386
5387 udelay(1);
5388 }
5389
5390 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5391 tw32_f(MAC_MODE, tp->mac_mode);
5392 udelay(40);
5393
Matt Carlson5be73b42007-12-20 20:09:29 -08005394 *txflags = aninfo.txconfig;
5395 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
5397 if (status == ANEG_DONE &&
5398 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5399 MR_LP_ADV_FULL_DUPLEX)))
5400 res = 1;
5401
5402 return res;
5403}
5404
5405static void tg3_init_bcm8002(struct tg3 *tp)
5406{
5407 u32 mac_status = tr32(MAC_STATUS);
5408 int i;
5409
5410 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005411 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 !(mac_status & MAC_STATUS_PCS_SYNCED))
5413 return;
5414
5415 /* Set PLL lock range. */
5416 tg3_writephy(tp, 0x16, 0x8007);
5417
5418 /* SW reset */
5419 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5420
5421 /* Wait for reset to complete. */
5422 /* XXX schedule_timeout() ... */
5423 for (i = 0; i < 500; i++)
5424 udelay(10);
5425
5426 /* Config mode; select PMA/Ch 1 regs. */
5427 tg3_writephy(tp, 0x10, 0x8411);
5428
5429 /* Enable auto-lock and comdet, select txclk for tx. */
5430 tg3_writephy(tp, 0x11, 0x0a10);
5431
5432 tg3_writephy(tp, 0x18, 0x00a0);
5433 tg3_writephy(tp, 0x16, 0x41ff);
5434
5435 /* Assert and deassert POR. */
5436 tg3_writephy(tp, 0x13, 0x0400);
5437 udelay(40);
5438 tg3_writephy(tp, 0x13, 0x0000);
5439
5440 tg3_writephy(tp, 0x11, 0x0a50);
5441 udelay(40);
5442 tg3_writephy(tp, 0x11, 0x0a10);
5443
5444 /* Wait for signal to stabilize */
5445 /* XXX schedule_timeout() ... */
5446 for (i = 0; i < 15000; i++)
5447 udelay(10);
5448
5449 /* Deselect the channel register so we can read the PHYID
5450 * later.
5451 */
5452 tg3_writephy(tp, 0x10, 0x8011);
5453}
5454
Joe Perches953c96e2013-04-09 10:18:14 +00005455static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005457 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005458 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 u32 sg_dig_ctrl, sg_dig_status;
5460 u32 serdes_cfg, expected_sg_dig_ctrl;
5461 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
5463 serdes_cfg = 0;
5464 expected_sg_dig_ctrl = 0;
5465 workaround = 0;
5466 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005467 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Joe Perches41535772013-02-16 11:20:04 +00005469 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5470 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 workaround = 1;
5472 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5473 port_a = 0;
5474
5475 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5476 /* preserve bits 20-23 for voltage regulator */
5477 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5478 }
5479
5480 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5481
5482 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005483 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 if (workaround) {
5485 u32 val = serdes_cfg;
5486
5487 if (port_a)
5488 val |= 0xc010000;
5489 else
5490 val |= 0x4010000;
5491 tw32_f(MAC_SERDES_CFG, val);
5492 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005493
5494 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 }
5496 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5497 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005498 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 }
5500 goto out;
5501 }
5502
5503 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005504 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Matt Carlson82cd3d12007-12-20 20:09:00 -08005506 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5507 if (flowctrl & ADVERTISE_1000XPAUSE)
5508 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5509 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5510 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
5512 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005513 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005514 tp->serdes_counter &&
5515 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5516 MAC_STATUS_RCVD_CFG)) ==
5517 MAC_STATUS_PCS_SYNCED)) {
5518 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005519 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005520 goto out;
5521 }
5522restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 if (workaround)
5524 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005525 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 udelay(5);
5527 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5528
Michael Chan3d3ebe72006-09-27 15:59:15 -07005529 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005530 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5532 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005533 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 mac_status = tr32(MAC_STATUS);
5535
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005536 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005538 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
Matt Carlson82cd3d12007-12-20 20:09:00 -08005540 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5541 local_adv |= ADVERTISE_1000XPAUSE;
5542 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5543 local_adv |= ADVERTISE_1000XPSE_ASYM;
5544
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005545 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005546 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005547 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005548 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
Matt Carlson859edb22011-12-08 14:40:16 +00005550 tp->link_config.rmt_adv =
5551 mii_adv_to_ethtool_adv_x(remote_adv);
5552
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005554 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005555 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005556 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005557 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005558 if (tp->serdes_counter)
5559 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 else {
5561 if (workaround) {
5562 u32 val = serdes_cfg;
5563
5564 if (port_a)
5565 val |= 0xc010000;
5566 else
5567 val |= 0x4010000;
5568
5569 tw32_f(MAC_SERDES_CFG, val);
5570 }
5571
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005572 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 udelay(40);
5574
5575 /* Link parallel detection - link is up */
5576 /* only if we have PCS_SYNC and not */
5577 /* receiving config code words */
5578 mac_status = tr32(MAC_STATUS);
5579 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5580 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5581 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005582 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005583 tp->phy_flags |=
5584 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005585 tp->serdes_counter =
5586 SERDES_PARALLEL_DET_TIMEOUT;
5587 } else
5588 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 }
5590 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005591 } else {
5592 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005593 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 }
5595
5596out:
5597 return current_link_up;
5598}
5599
Joe Perches953c96e2013-04-09 10:18:14 +00005600static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
Joe Perches953c96e2013-04-09 10:18:14 +00005602 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005604 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
5607 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005608 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005610
Matt Carlson5be73b42007-12-20 20:09:29 -08005611 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5612 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Matt Carlson5be73b42007-12-20 20:09:29 -08005614 if (txflags & ANEG_CFG_PS1)
5615 local_adv |= ADVERTISE_1000XPAUSE;
5616 if (txflags & ANEG_CFG_PS2)
5617 local_adv |= ADVERTISE_1000XPSE_ASYM;
5618
5619 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5620 remote_adv |= LPA_1000XPAUSE;
5621 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5622 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Matt Carlson859edb22011-12-08 14:40:16 +00005624 tp->link_config.rmt_adv =
5625 mii_adv_to_ethtool_adv_x(remote_adv);
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 tg3_setup_flow_control(tp, local_adv, remote_adv);
5628
Joe Perches953c96e2013-04-09 10:18:14 +00005629 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 }
5631 for (i = 0; i < 30; i++) {
5632 udelay(20);
5633 tw32_f(MAC_STATUS,
5634 (MAC_STATUS_SYNC_CHANGED |
5635 MAC_STATUS_CFG_CHANGED));
5636 udelay(40);
5637 if ((tr32(MAC_STATUS) &
5638 (MAC_STATUS_SYNC_CHANGED |
5639 MAC_STATUS_CFG_CHANGED)) == 0)
5640 break;
5641 }
5642
5643 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005644 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5646 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005647 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005649 tg3_setup_flow_control(tp, 0, 0);
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005652 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
5654 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5655 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005656
5657 tw32_f(MAC_MODE, tp->mac_mode);
5658 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 }
5660
5661out:
5662 return current_link_up;
5663}
5664
Joe Perches953c96e2013-04-09 10:18:14 +00005665static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666{
5667 u32 orig_pause_cfg;
5668 u16 orig_active_speed;
5669 u8 orig_active_duplex;
5670 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005671 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 int i;
5673
Matt Carlson8d018622007-12-20 20:05:44 -08005674 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 orig_active_speed = tp->link_config.active_speed;
5676 orig_active_duplex = tp->link_config.active_duplex;
5677
Joe Perches63c3a662011-04-26 08:12:10 +00005678 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005679 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005680 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 mac_status = tr32(MAC_STATUS);
5682 mac_status &= (MAC_STATUS_PCS_SYNCED |
5683 MAC_STATUS_SIGNAL_DET |
5684 MAC_STATUS_CFG_CHANGED |
5685 MAC_STATUS_RCVD_CFG);
5686 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5687 MAC_STATUS_SIGNAL_DET)) {
5688 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5689 MAC_STATUS_CFG_CHANGED));
5690 return 0;
5691 }
5692 }
5693
5694 tw32_f(MAC_TX_AUTO_NEG, 0);
5695
5696 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5697 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5698 tw32_f(MAC_MODE, tp->mac_mode);
5699 udelay(40);
5700
Matt Carlson79eb6902010-02-17 15:17:03 +00005701 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 tg3_init_bcm8002(tp);
5703
5704 /* Enable link change event even when serdes polling. */
5705 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5706 udelay(40);
5707
Joe Perches953c96e2013-04-09 10:18:14 +00005708 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005709 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 mac_status = tr32(MAC_STATUS);
5711
Joe Perches63c3a662011-04-26 08:12:10 +00005712 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5714 else
5715 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5716
Matt Carlson898a56f2009-08-28 14:02:40 +00005717 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005719 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 for (i = 0; i < 100; i++) {
5722 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5723 MAC_STATUS_CFG_CHANGED));
5724 udelay(5);
5725 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005726 MAC_STATUS_CFG_CHANGED |
5727 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 break;
5729 }
5730
5731 mac_status = tr32(MAC_STATUS);
5732 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005733 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005734 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5735 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 tw32_f(MAC_MODE, (tp->mac_mode |
5737 MAC_MODE_SEND_CONFIGS));
5738 udelay(1);
5739 tw32_f(MAC_MODE, tp->mac_mode);
5740 }
5741 }
5742
Joe Perches953c96e2013-04-09 10:18:14 +00005743 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 tp->link_config.active_speed = SPEED_1000;
5745 tp->link_config.active_duplex = DUPLEX_FULL;
5746 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5747 LED_CTRL_LNKLED_OVERRIDE |
5748 LED_CTRL_1000MBPS_ON));
5749 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005750 tp->link_config.active_speed = SPEED_UNKNOWN;
5751 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5753 LED_CTRL_LNKLED_OVERRIDE |
5754 LED_CTRL_TRAFFIC_OVERRIDE));
5755 }
5756
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005757 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005758 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 if (orig_pause_cfg != now_pause_cfg ||
5760 orig_active_speed != tp->link_config.active_speed ||
5761 orig_active_duplex != tp->link_config.active_duplex)
5762 tg3_link_report(tp);
5763 }
5764
5765 return 0;
5766}
5767
Joe Perches953c96e2013-04-09 10:18:14 +00005768static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005769{
Joe Perches953c96e2013-04-09 10:18:14 +00005770 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005771 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005772 u16 current_speed = SPEED_UNKNOWN;
5773 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005774 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005775 u32 local_adv, remote_adv, sgsr;
5776
5777 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5778 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5779 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5780 (sgsr & SERDES_TG3_SGMII_MODE)) {
5781
5782 if (force_reset)
5783 tg3_phy_reset(tp);
5784
5785 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5786
5787 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5788 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5789 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005790 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005791 if (sgsr & SERDES_TG3_SPEED_1000) {
5792 current_speed = SPEED_1000;
5793 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5794 } else if (sgsr & SERDES_TG3_SPEED_100) {
5795 current_speed = SPEED_100;
5796 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5797 } else {
5798 current_speed = SPEED_10;
5799 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5800 }
5801
5802 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5803 current_duplex = DUPLEX_FULL;
5804 else
5805 current_duplex = DUPLEX_HALF;
5806 }
5807
5808 tw32_f(MAC_MODE, tp->mac_mode);
5809 udelay(40);
5810
5811 tg3_clear_mac_status(tp);
5812
5813 goto fiber_setup_done;
5814 }
Michael Chan747e8f82005-07-25 12:33:22 -07005815
5816 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5817 tw32_f(MAC_MODE, tp->mac_mode);
5818 udelay(40);
5819
Michael Chan3310e242013-04-09 08:48:05 +00005820 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005821
5822 if (force_reset)
5823 tg3_phy_reset(tp);
5824
Matt Carlson859edb22011-12-08 14:40:16 +00005825 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005826
5827 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5828 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005829 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005830 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5831 bmsr |= BMSR_LSTATUS;
5832 else
5833 bmsr &= ~BMSR_LSTATUS;
5834 }
Michael Chan747e8f82005-07-25 12:33:22 -07005835
5836 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5837
5838 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005839 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005840 /* do nothing, just check for link up at the end */
5841 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005842 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005843
5844 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005845 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5846 ADVERTISE_1000XPAUSE |
5847 ADVERTISE_1000XPSE_ASYM |
5848 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005849
Matt Carlson28011cf2011-11-16 18:36:59 -05005850 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005851 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005852
Matt Carlson28011cf2011-11-16 18:36:59 -05005853 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5854 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005855 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5856 tg3_writephy(tp, MII_BMCR, bmcr);
5857
5858 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005859 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005860 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005861
5862 return err;
5863 }
5864 } else {
5865 u32 new_bmcr;
5866
5867 bmcr &= ~BMCR_SPEED1000;
5868 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5869
5870 if (tp->link_config.duplex == DUPLEX_FULL)
5871 new_bmcr |= BMCR_FULLDPLX;
5872
5873 if (new_bmcr != bmcr) {
5874 /* BMCR_SPEED1000 is a reserved bit that needs
5875 * to be set on write.
5876 */
5877 new_bmcr |= BMCR_SPEED1000;
5878
5879 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005880 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005881 u32 adv;
5882
5883 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5884 adv &= ~(ADVERTISE_1000XFULL |
5885 ADVERTISE_1000XHALF |
5886 ADVERTISE_SLCT);
5887 tg3_writephy(tp, MII_ADVERTISE, adv);
5888 tg3_writephy(tp, MII_BMCR, bmcr |
5889 BMCR_ANRESTART |
5890 BMCR_ANENABLE);
5891 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005892 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005893 }
5894 tg3_writephy(tp, MII_BMCR, new_bmcr);
5895 bmcr = new_bmcr;
5896 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5897 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005898 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005899 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5900 bmsr |= BMSR_LSTATUS;
5901 else
5902 bmsr &= ~BMSR_LSTATUS;
5903 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005904 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005905 }
5906 }
5907
5908 if (bmsr & BMSR_LSTATUS) {
5909 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005910 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005911 if (bmcr & BMCR_FULLDPLX)
5912 current_duplex = DUPLEX_FULL;
5913 else
5914 current_duplex = DUPLEX_HALF;
5915
Matt Carlsonef167e22007-12-20 20:10:01 -08005916 local_adv = 0;
5917 remote_adv = 0;
5918
Michael Chan747e8f82005-07-25 12:33:22 -07005919 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005920 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005921
5922 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5923 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5924 common = local_adv & remote_adv;
5925 if (common & (ADVERTISE_1000XHALF |
5926 ADVERTISE_1000XFULL)) {
5927 if (common & ADVERTISE_1000XFULL)
5928 current_duplex = DUPLEX_FULL;
5929 else
5930 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005931
5932 tp->link_config.rmt_adv =
5933 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005934 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005935 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005936 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005937 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005938 }
Michael Chan747e8f82005-07-25 12:33:22 -07005939 }
5940 }
5941
Michael Chan85730a62013-04-09 08:48:06 +00005942fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005943 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005944 tg3_setup_flow_control(tp, local_adv, remote_adv);
5945
Michael Chan747e8f82005-07-25 12:33:22 -07005946 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5947 if (tp->link_config.active_duplex == DUPLEX_HALF)
5948 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5949
5950 tw32_f(MAC_MODE, tp->mac_mode);
5951 udelay(40);
5952
5953 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5954
5955 tp->link_config.active_speed = current_speed;
5956 tp->link_config.active_duplex = current_duplex;
5957
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005958 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005959 return err;
5960}
5961
5962static void tg3_serdes_parallel_detect(struct tg3 *tp)
5963{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005964 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005965 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005966 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005967 return;
5968 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005969
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005970 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005971 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5972 u32 bmcr;
5973
5974 tg3_readphy(tp, MII_BMCR, &bmcr);
5975 if (bmcr & BMCR_ANENABLE) {
5976 u32 phy1, phy2;
5977
5978 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005979 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5980 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005981
5982 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005983 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5984 MII_TG3_DSP_EXP1_INT_STAT);
5985 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5986 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005987
5988 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5989 /* We have signal detect and not receiving
5990 * config code words, link is up by parallel
5991 * detection.
5992 */
5993
5994 bmcr &= ~BMCR_ANENABLE;
5995 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5996 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005997 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005998 }
5999 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006000 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006001 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006002 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006003 u32 phy2;
6004
6005 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006006 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6007 MII_TG3_DSP_EXP1_INT_STAT);
6008 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006009 if (phy2 & 0x20) {
6010 u32 bmcr;
6011
6012 /* Config code words received, turn on autoneg. */
6013 tg3_readphy(tp, MII_BMCR, &bmcr);
6014 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6015
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006016 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006017
6018 }
6019 }
6020}
6021
Joe Perches953c96e2013-04-09 10:18:14 +00006022static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006024 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 int err;
6026
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006027 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006029 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006030 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006031 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
Joe Perches41535772013-02-16 11:20:04 +00006034 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006035 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006036
6037 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6038 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6039 scale = 65;
6040 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6041 scale = 6;
6042 else
6043 scale = 12;
6044
6045 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6046 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6047 tw32(GRC_MISC_CFG, val);
6048 }
6049
Matt Carlsonf2096f92011-04-05 14:22:48 +00006050 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6051 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006052 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6053 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006054 val |= tr32(MAC_TX_LENGTHS) &
6055 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6056 TX_LENGTHS_CNT_DWN_VAL_MSK);
6057
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 if (tp->link_config.active_speed == SPEED_1000 &&
6059 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006060 tw32(MAC_TX_LENGTHS, val |
6061 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006063 tw32(MAC_TX_LENGTHS, val |
6064 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
Joe Perches63c3a662011-04-26 08:12:10 +00006066 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006067 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006069 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 } else {
6071 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6072 }
6073 }
6074
Joe Perches63c3a662011-04-26 08:12:10 +00006075 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006076 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006077 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006078 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6079 tp->pwrmgmt_thresh;
6080 else
6081 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6082 tw32(PCIE_PWR_MGMT_THRESH, val);
6083 }
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 return err;
6086}
6087
Matt Carlsonbe947302012-12-03 19:36:57 +00006088/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006089static u64 tg3_refclk_read(struct tg3 *tp)
6090{
6091 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6092 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6093}
6094
6095/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006096static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6097{
6098 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
6099 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6100 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
6101 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
6102}
6103
Matt Carlson7d41e492012-12-03 19:36:58 +00006104static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6105static inline void tg3_full_unlock(struct tg3 *tp);
6106static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6107{
6108 struct tg3 *tp = netdev_priv(dev);
6109
6110 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6111 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006112 SOF_TIMESTAMPING_SOFTWARE;
6113
6114 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006115 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006116 SOF_TIMESTAMPING_RX_HARDWARE |
6117 SOF_TIMESTAMPING_RAW_HARDWARE;
6118 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006119
6120 if (tp->ptp_clock)
6121 info->phc_index = ptp_clock_index(tp->ptp_clock);
6122 else
6123 info->phc_index = -1;
6124
6125 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6126
6127 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6128 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6129 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6130 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6131 return 0;
6132}
6133
6134static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6135{
6136 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6137 bool neg_adj = false;
6138 u32 correction = 0;
6139
6140 if (ppb < 0) {
6141 neg_adj = true;
6142 ppb = -ppb;
6143 }
6144
6145 /* Frequency adjustment is performed using hardware with a 24 bit
6146 * accumulator and a programmable correction value. On each clk, the
6147 * correction value gets added to the accumulator and when it
6148 * overflows, the time counter is incremented/decremented.
6149 *
6150 * So conversion from ppb to correction value is
6151 * ppb * (1 << 24) / 1000000000
6152 */
6153 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6154 TG3_EAV_REF_CLK_CORRECT_MASK;
6155
6156 tg3_full_lock(tp, 0);
6157
6158 if (correction)
6159 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6160 TG3_EAV_REF_CLK_CORRECT_EN |
6161 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6162 else
6163 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6164
6165 tg3_full_unlock(tp);
6166
6167 return 0;
6168}
6169
6170static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6171{
6172 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6173
6174 tg3_full_lock(tp, 0);
6175 tp->ptp_adjust += delta;
6176 tg3_full_unlock(tp);
6177
6178 return 0;
6179}
6180
6181static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6182{
6183 u64 ns;
6184 u32 remainder;
6185 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6186
6187 tg3_full_lock(tp, 0);
6188 ns = tg3_refclk_read(tp);
6189 ns += tp->ptp_adjust;
6190 tg3_full_unlock(tp);
6191
6192 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6193 ts->tv_nsec = remainder;
6194
6195 return 0;
6196}
6197
6198static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6199 const struct timespec *ts)
6200{
6201 u64 ns;
6202 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6203
6204 ns = timespec_to_ns(ts);
6205
6206 tg3_full_lock(tp, 0);
6207 tg3_refclk_write(tp, ns);
6208 tp->ptp_adjust = 0;
6209 tg3_full_unlock(tp);
6210
6211 return 0;
6212}
6213
6214static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6215 struct ptp_clock_request *rq, int on)
6216{
6217 return -EOPNOTSUPP;
6218}
6219
6220static const struct ptp_clock_info tg3_ptp_caps = {
6221 .owner = THIS_MODULE,
6222 .name = "tg3 clock",
6223 .max_adj = 250000000,
6224 .n_alarm = 0,
6225 .n_ext_ts = 0,
6226 .n_per_out = 0,
6227 .pps = 0,
6228 .adjfreq = tg3_ptp_adjfreq,
6229 .adjtime = tg3_ptp_adjtime,
6230 .gettime = tg3_ptp_gettime,
6231 .settime = tg3_ptp_settime,
6232 .enable = tg3_ptp_enable,
6233};
6234
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006235static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6236 struct skb_shared_hwtstamps *timestamp)
6237{
6238 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6239 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6240 tp->ptp_adjust);
6241}
6242
Matt Carlsonbe947302012-12-03 19:36:57 +00006243/* tp->lock must be held */
6244static void tg3_ptp_init(struct tg3 *tp)
6245{
6246 if (!tg3_flag(tp, PTP_CAPABLE))
6247 return;
6248
6249 /* Initialize the hardware clock to the system time. */
6250 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6251 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006252 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006253}
6254
6255/* tp->lock must be held */
6256static void tg3_ptp_resume(struct tg3 *tp)
6257{
6258 if (!tg3_flag(tp, PTP_CAPABLE))
6259 return;
6260
6261 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6262 tp->ptp_adjust = 0;
6263}
6264
6265static void tg3_ptp_fini(struct tg3 *tp)
6266{
6267 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6268 return;
6269
Matt Carlson7d41e492012-12-03 19:36:58 +00006270 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006271 tp->ptp_clock = NULL;
6272 tp->ptp_adjust = 0;
6273}
6274
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006275static inline int tg3_irq_sync(struct tg3 *tp)
6276{
6277 return tp->irq_sync;
6278}
6279
Matt Carlson97bd8e42011-04-13 11:05:04 +00006280static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6281{
6282 int i;
6283
6284 dst = (u32 *)((u8 *)dst + off);
6285 for (i = 0; i < len; i += sizeof(u32))
6286 *dst++ = tr32(off + i);
6287}
6288
6289static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6290{
6291 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6292 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6293 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6294 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6295 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6296 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6297 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6298 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6299 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6300 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6301 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6302 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6303 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6304 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6305 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6306 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6307 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6308 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6309 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6310
Joe Perches63c3a662011-04-26 08:12:10 +00006311 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006312 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6313
6314 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6315 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6316 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6317 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6318 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6319 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6320 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6321 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6322
Joe Perches63c3a662011-04-26 08:12:10 +00006323 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006324 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6325 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6326 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6327 }
6328
6329 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6330 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6331 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6332 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6333 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6334
Joe Perches63c3a662011-04-26 08:12:10 +00006335 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006336 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6337}
6338
6339static void tg3_dump_state(struct tg3 *tp)
6340{
6341 int i;
6342 u32 *regs;
6343
6344 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006345 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006346 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006347
Joe Perches63c3a662011-04-26 08:12:10 +00006348 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006349 /* Read up to but not including private PCI registers */
6350 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6351 regs[i / sizeof(u32)] = tr32(i);
6352 } else
6353 tg3_dump_legacy_regs(tp, regs);
6354
6355 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6356 if (!regs[i + 0] && !regs[i + 1] &&
6357 !regs[i + 2] && !regs[i + 3])
6358 continue;
6359
6360 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6361 i * 4,
6362 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6363 }
6364
6365 kfree(regs);
6366
6367 for (i = 0; i < tp->irq_cnt; i++) {
6368 struct tg3_napi *tnapi = &tp->napi[i];
6369
6370 /* SW status block */
6371 netdev_err(tp->dev,
6372 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6373 i,
6374 tnapi->hw_status->status,
6375 tnapi->hw_status->status_tag,
6376 tnapi->hw_status->rx_jumbo_consumer,
6377 tnapi->hw_status->rx_consumer,
6378 tnapi->hw_status->rx_mini_consumer,
6379 tnapi->hw_status->idx[0].rx_producer,
6380 tnapi->hw_status->idx[0].tx_consumer);
6381
6382 netdev_err(tp->dev,
6383 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6384 i,
6385 tnapi->last_tag, tnapi->last_irq_tag,
6386 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6387 tnapi->rx_rcb_ptr,
6388 tnapi->prodring.rx_std_prod_idx,
6389 tnapi->prodring.rx_std_cons_idx,
6390 tnapi->prodring.rx_jmb_prod_idx,
6391 tnapi->prodring.rx_jmb_cons_idx);
6392 }
6393}
6394
Michael Chandf3e6542006-05-26 17:48:07 -07006395/* This is called whenever we suspect that the system chipset is re-
6396 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6397 * is bogus tx completions. We try to recover by setting the
6398 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6399 * in the workqueue.
6400 */
6401static void tg3_tx_recover(struct tg3 *tp)
6402{
Joe Perches63c3a662011-04-26 08:12:10 +00006403 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006404 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6405
Matt Carlson5129c3a2010-04-05 10:19:23 +00006406 netdev_warn(tp->dev,
6407 "The system may be re-ordering memory-mapped I/O "
6408 "cycles to the network device, attempting to recover. "
6409 "Please report the problem to the driver maintainer "
6410 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006411
Joe Perches63c3a662011-04-26 08:12:10 +00006412 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006413}
6414
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006415static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006416{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006417 /* Tell compiler to fetch tx indices from memory. */
6418 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006419 return tnapi->tx_pending -
6420 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006421}
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423/* Tigon3 never reports partial packet sends. So we do not
6424 * need special logic to handle SKBs that have not had all
6425 * of their frags sent yet, like SunGEM does.
6426 */
Matt Carlson17375d22009-08-28 14:02:18 +00006427static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428{
Matt Carlson17375d22009-08-28 14:02:18 +00006429 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006430 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006431 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006432 struct netdev_queue *txq;
6433 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006434 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006435
Joe Perches63c3a662011-04-26 08:12:10 +00006436 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006437 index--;
6438
6439 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
6441 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006442 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006444 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445
Michael Chandf3e6542006-05-26 17:48:07 -07006446 if (unlikely(skb == NULL)) {
6447 tg3_tx_recover(tp);
6448 return;
6449 }
6450
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006451 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6452 struct skb_shared_hwtstamps timestamp;
6453 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6454 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6455
6456 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6457
6458 skb_tstamp_tx(skb, &timestamp);
6459 }
6460
Alexander Duyckf4188d82009-12-02 16:48:38 +00006461 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006462 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006463 skb_headlen(skb),
6464 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465
6466 ri->skb = NULL;
6467
Matt Carlsone01ee142011-07-27 14:20:50 +00006468 while (ri->fragmented) {
6469 ri->fragmented = false;
6470 sw_idx = NEXT_TX(sw_idx);
6471 ri = &tnapi->tx_buffers[sw_idx];
6472 }
6473
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 sw_idx = NEXT_TX(sw_idx);
6475
6476 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006477 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006478 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6479 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006480
6481 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006482 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006483 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006484 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006485
6486 while (ri->fragmented) {
6487 ri->fragmented = false;
6488 sw_idx = NEXT_TX(sw_idx);
6489 ri = &tnapi->tx_buffers[sw_idx];
6490 }
6491
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 sw_idx = NEXT_TX(sw_idx);
6493 }
6494
Tom Herbert298376d2011-11-28 16:33:30 +00006495 pkts_compl++;
6496 bytes_compl += skb->len;
6497
David S. Millerf47c11e2005-06-24 20:18:35 -07006498 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006499
6500 if (unlikely(tx_bug)) {
6501 tg3_tx_recover(tp);
6502 return;
6503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 }
6505
Tom Herbert5cb917b2012-03-05 19:53:50 +00006506 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006507
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006508 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
Michael Chan1b2a7202006-08-07 21:46:02 -07006510 /* Need to make the tx_cons update visible to tg3_start_xmit()
6511 * before checking for netif_queue_stopped(). Without the
6512 * memory barrier, there is a small possibility that tg3_start_xmit()
6513 * will miss it and cause the queue to be stopped forever.
6514 */
6515 smp_mb();
6516
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006517 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006518 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006519 __netif_tx_lock(txq, smp_processor_id());
6520 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006521 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006522 netif_tx_wake_queue(txq);
6523 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525}
6526
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006527static void tg3_frag_free(bool is_frag, void *data)
6528{
6529 if (is_frag)
6530 put_page(virt_to_head_page(data));
6531 else
6532 kfree(data);
6533}
6534
Eric Dumazet9205fd92011-11-18 06:47:01 +00006535static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006536{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006537 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6538 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6539
Eric Dumazet9205fd92011-11-18 06:47:01 +00006540 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006541 return;
6542
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006543 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006544 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006545 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006546 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006547}
6548
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006549
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550/* Returns size of skb allocated or < 0 on error.
6551 *
6552 * We only need to fill in the address because the other members
6553 * of the RX descriptor are invariant, see tg3_init_rings.
6554 *
6555 * Note the purposeful assymetry of cpu vs. chip accesses. For
6556 * posting buffers we only dirty the first cache line of the RX
6557 * descriptor (containing the address). Whereas for the RX status
6558 * buffers the cpu only reads the last cacheline of the RX descriptor
6559 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6560 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006561static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006562 u32 opaque_key, u32 dest_idx_unmasked,
6563 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564{
6565 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006566 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006567 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006569 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 switch (opaque_key) {
6572 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006573 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006574 desc = &tpr->rx_std[dest_idx];
6575 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006576 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 break;
6578
6579 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006580 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006581 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006582 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006583 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 break;
6585
6586 default:
6587 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589
6590 /* Do not overwrite any of the map or rp information
6591 * until we are sure we can commit to a new buffer.
6592 *
6593 * Callers depend upon this behavior and assume that
6594 * we leave everything unchanged if we fail.
6595 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006596 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6597 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006598 if (skb_size <= PAGE_SIZE) {
6599 data = netdev_alloc_frag(skb_size);
6600 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006601 } else {
6602 data = kmalloc(skb_size, GFP_ATOMIC);
6603 *frag_size = 0;
6604 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006605 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 return -ENOMEM;
6607
Eric Dumazet9205fd92011-11-18 06:47:01 +00006608 mapping = pci_map_single(tp->pdev,
6609 data + TG3_RX_OFFSET(tp),
6610 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006612 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006613 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006614 return -EIO;
6615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616
Eric Dumazet9205fd92011-11-18 06:47:01 +00006617 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006618 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 desc->addr_hi = ((u64)mapping >> 32);
6621 desc->addr_lo = ((u64)mapping & 0xffffffff);
6622
Eric Dumazet9205fd92011-11-18 06:47:01 +00006623 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624}
6625
6626/* We only need to move over in the address because the other
6627 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006628 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 */
Matt Carlsona3896162009-11-13 13:03:44 +00006630static void tg3_recycle_rx(struct tg3_napi *tnapi,
6631 struct tg3_rx_prodring_set *dpr,
6632 u32 opaque_key, int src_idx,
6633 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634{
Matt Carlson17375d22009-08-28 14:02:18 +00006635 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6637 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006638 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006639 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
6641 switch (opaque_key) {
6642 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006643 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006644 dest_desc = &dpr->rx_std[dest_idx];
6645 dest_map = &dpr->rx_std_buffers[dest_idx];
6646 src_desc = &spr->rx_std[src_idx];
6647 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 break;
6649
6650 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006651 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006652 dest_desc = &dpr->rx_jmb[dest_idx].std;
6653 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6654 src_desc = &spr->rx_jmb[src_idx].std;
6655 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 break;
6657
6658 default:
6659 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661
Eric Dumazet9205fd92011-11-18 06:47:01 +00006662 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006663 dma_unmap_addr_set(dest_map, mapping,
6664 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 dest_desc->addr_hi = src_desc->addr_hi;
6666 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006667
6668 /* Ensure that the update to the skb happens after the physical
6669 * addresses have been transferred to the new BD location.
6670 */
6671 smp_wmb();
6672
Eric Dumazet9205fd92011-11-18 06:47:01 +00006673 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674}
6675
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676/* The RX ring scheme is composed of multiple rings which post fresh
6677 * buffers to the chip, and one special ring the chip uses to report
6678 * status back to the host.
6679 *
6680 * The special ring reports the status of received packets to the
6681 * host. The chip does not write into the original descriptor the
6682 * RX buffer was obtained from. The chip simply takes the original
6683 * descriptor as provided by the host, updates the status and length
6684 * field, then writes this into the next status ring entry.
6685 *
6686 * Each ring the host uses to post buffers to the chip is described
6687 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6688 * it is first placed into the on-chip ram. When the packet's length
6689 * is known, it walks down the TG3_BDINFO entries to select the ring.
6690 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6691 * which is within the range of the new packet's length is chosen.
6692 *
6693 * The "separate ring for rx status" scheme may sound queer, but it makes
6694 * sense from a cache coherency perspective. If only the host writes
6695 * to the buffer post rings, and only the chip writes to the rx status
6696 * rings, then cache lines never move beyond shared-modified state.
6697 * If both the host and chip were to write into the same ring, cache line
6698 * eviction could occur since both entities want it in an exclusive state.
6699 */
Matt Carlson17375d22009-08-28 14:02:18 +00006700static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701{
Matt Carlson17375d22009-08-28 14:02:18 +00006702 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006703 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006704 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006705 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006706 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006708 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006710 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 /*
6712 * We need to order the read of hw_idx and the read of
6713 * the opaque cookie.
6714 */
6715 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 work_mask = 0;
6717 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006718 std_prod_idx = tpr->rx_std_prod_idx;
6719 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006721 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006722 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 unsigned int len;
6724 struct sk_buff *skb;
6725 dma_addr_t dma_addr;
6726 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006727 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006728 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
6730 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6731 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6732 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006733 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006734 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006735 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006736 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006737 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006739 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006740 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006741 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006742 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006743 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
6746 work_mask |= opaque_key;
6747
6748 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6749 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6750 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006751 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 desc_idx, *post_ptr);
6753 drop_it_no_recycle:
6754 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006755 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 goto next_pkt;
6757 }
6758
Eric Dumazet9205fd92011-11-18 06:47:01 +00006759 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006760 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6761 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006763 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6764 RXD_FLAG_PTPSTAT_PTPV1 ||
6765 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6766 RXD_FLAG_PTPSTAT_PTPV2) {
6767 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6768 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6769 }
6770
Matt Carlsond2757fc2010-04-12 06:58:27 +00006771 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006773 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774
Eric Dumazet9205fd92011-11-18 06:47:01 +00006775 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006776 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 if (skb_size < 0)
6778 goto drop_it;
6779
Matt Carlson287be122009-08-28 13:58:46 +00006780 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 PCI_DMA_FROMDEVICE);
6782
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006783 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006784 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006785 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006786 goto drop_it_no_recycle;
6787 }
6788 skb_reserve(skb, TG3_RX_OFFSET(tp));
6789 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006790 * after the usage of the old DMA mapping.
6791 */
6792 smp_wmb();
6793
Eric Dumazet9205fd92011-11-18 06:47:01 +00006794 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006795
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006797 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 desc_idx, *post_ptr);
6799
Eric Dumazet9205fd92011-11-18 06:47:01 +00006800 skb = netdev_alloc_skb(tp->dev,
6801 len + TG3_RAW_IP_ALIGN);
6802 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 goto drop_it_no_recycle;
6804
Eric Dumazet9205fd92011-11-18 06:47:01 +00006805 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006807 memcpy(skb->data,
6808 data + TG3_RX_OFFSET(tp),
6809 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 }
6812
Eric Dumazet9205fd92011-11-18 06:47:01 +00006813 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006814 if (tstamp)
6815 tg3_hwclock_to_timestamp(tp, tstamp,
6816 skb_hwtstamps(skb));
6817
Michał Mirosławdc668912011-04-07 03:35:07 +00006818 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6820 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6821 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6822 skb->ip_summed = CHECKSUM_UNNECESSARY;
6823 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006824 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
6826 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006827
6828 if (len > (tp->dev->mtu + ETH_HLEN) &&
6829 skb->protocol != htons(ETH_P_8021Q)) {
6830 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006831 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006832 }
6833
Matt Carlson9dc7a112010-04-12 06:58:28 +00006834 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006835 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006836 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006837 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006838
Matt Carlsonbf933c82011-01-25 15:58:49 +00006839 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 received++;
6842 budget--;
6843
6844next_pkt:
6845 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006846
6847 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006848 tpr->rx_std_prod_idx = std_prod_idx &
6849 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006850 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6851 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006852 work_mask &= ~RXD_OPAQUE_RING_STD;
6853 rx_std_posted = 0;
6854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006856 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006857 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006858
6859 /* Refresh hw_idx to see if there is new work */
6860 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006861 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006862 rmb();
6863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 }
6865
6866 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006867 tnapi->rx_rcb_ptr = sw_idx;
6868 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
6870 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006871 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006872 /* Sync BD data before updating mailbox */
6873 wmb();
6874
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006875 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006876 tpr->rx_std_prod_idx = std_prod_idx &
6877 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006878 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6879 tpr->rx_std_prod_idx);
6880 }
6881 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006882 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6883 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006884 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6885 tpr->rx_jmb_prod_idx);
6886 }
6887 mmiowb();
6888 } else if (work_mask) {
6889 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6890 * updated before the producer indices can be updated.
6891 */
6892 smp_wmb();
6893
Matt Carlson2c49a442010-09-30 10:34:35 +00006894 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6895 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006896
Michael Chan7ae52892012-03-21 15:38:33 +00006897 if (tnapi != &tp->napi[1]) {
6898 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006899 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
6903 return received;
6904}
6905
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006906static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006909 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006910 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6911
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 if (sblk->status & SD_STATUS_LINK_CHG) {
6913 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006914 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006915 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006916 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006917 tw32_f(MAC_STATUS,
6918 (MAC_STATUS_SYNC_CHANGED |
6919 MAC_STATUS_CFG_CHANGED |
6920 MAC_STATUS_MI_COMPLETION |
6921 MAC_STATUS_LNKSTATE_CHANGED));
6922 udelay(40);
6923 } else
Joe Perches953c96e2013-04-09 10:18:14 +00006924 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07006925 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 }
6927 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006928}
6929
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006930static int tg3_rx_prodring_xfer(struct tg3 *tp,
6931 struct tg3_rx_prodring_set *dpr,
6932 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006933{
6934 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006935 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006936
6937 while (1) {
6938 src_prod_idx = spr->rx_std_prod_idx;
6939
6940 /* Make sure updates to the rx_std_buffers[] entries and the
6941 * standard producer index are seen in the correct order.
6942 */
6943 smp_rmb();
6944
6945 if (spr->rx_std_cons_idx == src_prod_idx)
6946 break;
6947
6948 if (spr->rx_std_cons_idx < src_prod_idx)
6949 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6950 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006951 cpycnt = tp->rx_std_ring_mask + 1 -
6952 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006953
Matt Carlson2c49a442010-09-30 10:34:35 +00006954 cpycnt = min(cpycnt,
6955 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006956
6957 si = spr->rx_std_cons_idx;
6958 di = dpr->rx_std_prod_idx;
6959
Matt Carlsone92967b2010-02-12 14:47:06 +00006960 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006961 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006962 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006963 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006964 break;
6965 }
6966 }
6967
6968 if (!cpycnt)
6969 break;
6970
6971 /* Ensure that updates to the rx_std_buffers ring and the
6972 * shadowed hardware producer ring from tg3_recycle_skb() are
6973 * ordered correctly WRT the skb check above.
6974 */
6975 smp_rmb();
6976
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006977 memcpy(&dpr->rx_std_buffers[di],
6978 &spr->rx_std_buffers[si],
6979 cpycnt * sizeof(struct ring_info));
6980
6981 for (i = 0; i < cpycnt; i++, di++, si++) {
6982 struct tg3_rx_buffer_desc *sbd, *dbd;
6983 sbd = &spr->rx_std[si];
6984 dbd = &dpr->rx_std[di];
6985 dbd->addr_hi = sbd->addr_hi;
6986 dbd->addr_lo = sbd->addr_lo;
6987 }
6988
Matt Carlson2c49a442010-09-30 10:34:35 +00006989 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6990 tp->rx_std_ring_mask;
6991 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6992 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006993 }
6994
6995 while (1) {
6996 src_prod_idx = spr->rx_jmb_prod_idx;
6997
6998 /* Make sure updates to the rx_jmb_buffers[] entries and
6999 * the jumbo producer index are seen in the correct order.
7000 */
7001 smp_rmb();
7002
7003 if (spr->rx_jmb_cons_idx == src_prod_idx)
7004 break;
7005
7006 if (spr->rx_jmb_cons_idx < src_prod_idx)
7007 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7008 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007009 cpycnt = tp->rx_jmb_ring_mask + 1 -
7010 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007011
7012 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007013 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007014
7015 si = spr->rx_jmb_cons_idx;
7016 di = dpr->rx_jmb_prod_idx;
7017
Matt Carlsone92967b2010-02-12 14:47:06 +00007018 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007019 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007020 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007021 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007022 break;
7023 }
7024 }
7025
7026 if (!cpycnt)
7027 break;
7028
7029 /* Ensure that updates to the rx_jmb_buffers ring and the
7030 * shadowed hardware producer ring from tg3_recycle_skb() are
7031 * ordered correctly WRT the skb check above.
7032 */
7033 smp_rmb();
7034
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007035 memcpy(&dpr->rx_jmb_buffers[di],
7036 &spr->rx_jmb_buffers[si],
7037 cpycnt * sizeof(struct ring_info));
7038
7039 for (i = 0; i < cpycnt; i++, di++, si++) {
7040 struct tg3_rx_buffer_desc *sbd, *dbd;
7041 sbd = &spr->rx_jmb[si].std;
7042 dbd = &dpr->rx_jmb[di].std;
7043 dbd->addr_hi = sbd->addr_hi;
7044 dbd->addr_lo = sbd->addr_lo;
7045 }
7046
Matt Carlson2c49a442010-09-30 10:34:35 +00007047 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7048 tp->rx_jmb_ring_mask;
7049 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7050 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007051 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007052
7053 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007054}
7055
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007056static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7057{
7058 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059
7060 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007061 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007062 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007063 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007064 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 }
7066
Matt Carlsonf891ea12012-04-24 13:37:01 +00007067 if (!tnapi->rx_rcb_prod_idx)
7068 return work_done;
7069
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 /* run RX thread, within the bounds set by NAPI.
7071 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007072 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007074 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007075 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076
Joe Perches63c3a662011-04-26 08:12:10 +00007077 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007078 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007079 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007080 u32 std_prod_idx = dpr->rx_std_prod_idx;
7081 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007082
Michael Chan7ae52892012-03-21 15:38:33 +00007083 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007084 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007085 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007086 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007087
7088 wmb();
7089
Matt Carlsone4af1af2010-02-12 14:47:05 +00007090 if (std_prod_idx != dpr->rx_std_prod_idx)
7091 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7092 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007093
Matt Carlsone4af1af2010-02-12 14:47:05 +00007094 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7095 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7096 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007097
7098 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007099
7100 if (err)
7101 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007102 }
7103
David S. Miller6f535762007-10-11 18:08:29 -07007104 return work_done;
7105}
David S. Millerf7383c22005-05-18 22:50:53 -07007106
Matt Carlsondb219972011-11-04 09:15:03 +00007107static inline void tg3_reset_task_schedule(struct tg3 *tp)
7108{
7109 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7110 schedule_work(&tp->reset_task);
7111}
7112
7113static inline void tg3_reset_task_cancel(struct tg3 *tp)
7114{
7115 cancel_work_sync(&tp->reset_task);
7116 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007117 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007118}
7119
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007120static int tg3_poll_msix(struct napi_struct *napi, int budget)
7121{
7122 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7123 struct tg3 *tp = tnapi->tp;
7124 int work_done = 0;
7125 struct tg3_hw_status *sblk = tnapi->hw_status;
7126
7127 while (1) {
7128 work_done = tg3_poll_work(tnapi, work_done, budget);
7129
Joe Perches63c3a662011-04-26 08:12:10 +00007130 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007131 goto tx_recovery;
7132
7133 if (unlikely(work_done >= budget))
7134 break;
7135
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007136 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007137 * to tell the hw how much work has been processed,
7138 * so we must read it before checking for more work.
7139 */
7140 tnapi->last_tag = sblk->status_tag;
7141 tnapi->last_irq_tag = tnapi->last_tag;
7142 rmb();
7143
7144 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007145 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7146 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007147
7148 /* This test here is not race free, but will reduce
7149 * the number of interrupts by looping again.
7150 */
7151 if (tnapi == &tp->napi[1] && tp->rx_refill)
7152 continue;
7153
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007154 napi_complete(napi);
7155 /* Reenable interrupts. */
7156 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007157
7158 /* This test here is synchronized by napi_schedule()
7159 * and napi_complete() to close the race condition.
7160 */
7161 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7162 tw32(HOSTCC_MODE, tp->coalesce_mode |
7163 HOSTCC_MODE_ENABLE |
7164 tnapi->coal_now);
7165 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007166 mmiowb();
7167 break;
7168 }
7169 }
7170
7171 return work_done;
7172
7173tx_recovery:
7174 /* work_done is guaranteed to be less than budget. */
7175 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007176 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007177 return work_done;
7178}
7179
Matt Carlsone64de4e2011-04-13 11:05:05 +00007180static void tg3_process_error(struct tg3 *tp)
7181{
7182 u32 val;
7183 bool real_error = false;
7184
Joe Perches63c3a662011-04-26 08:12:10 +00007185 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007186 return;
7187
7188 /* Check Flow Attention register */
7189 val = tr32(HOSTCC_FLOW_ATTN);
7190 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7191 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7192 real_error = true;
7193 }
7194
7195 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7196 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7197 real_error = true;
7198 }
7199
7200 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7201 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7202 real_error = true;
7203 }
7204
7205 if (!real_error)
7206 return;
7207
7208 tg3_dump_state(tp);
7209
Joe Perches63c3a662011-04-26 08:12:10 +00007210 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007211 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007212}
7213
David S. Miller6f535762007-10-11 18:08:29 -07007214static int tg3_poll(struct napi_struct *napi, int budget)
7215{
Matt Carlson8ef04422009-08-28 14:01:37 +00007216 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7217 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007218 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007219 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007220
7221 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007222 if (sblk->status & SD_STATUS_ERROR)
7223 tg3_process_error(tp);
7224
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007225 tg3_poll_link(tp);
7226
Matt Carlson17375d22009-08-28 14:02:18 +00007227 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007228
Joe Perches63c3a662011-04-26 08:12:10 +00007229 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007230 goto tx_recovery;
7231
7232 if (unlikely(work_done >= budget))
7233 break;
7234
Joe Perches63c3a662011-04-26 08:12:10 +00007235 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007236 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007237 * to tell the hw how much work has been processed,
7238 * so we must read it before checking for more work.
7239 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007240 tnapi->last_tag = sblk->status_tag;
7241 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007242 rmb();
7243 } else
7244 sblk->status &= ~SD_STATUS_UPDATED;
7245
Matt Carlson17375d22009-08-28 14:02:18 +00007246 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007247 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007248 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007249 break;
7250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 }
7252
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007253 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007254
7255tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007256 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007257 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007258 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007259 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260}
7261
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007262static void tg3_napi_disable(struct tg3 *tp)
7263{
7264 int i;
7265
7266 for (i = tp->irq_cnt - 1; i >= 0; i--)
7267 napi_disable(&tp->napi[i].napi);
7268}
7269
7270static void tg3_napi_enable(struct tg3 *tp)
7271{
7272 int i;
7273
7274 for (i = 0; i < tp->irq_cnt; i++)
7275 napi_enable(&tp->napi[i].napi);
7276}
7277
7278static void tg3_napi_init(struct tg3 *tp)
7279{
7280 int i;
7281
7282 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7283 for (i = 1; i < tp->irq_cnt; i++)
7284 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7285}
7286
7287static void tg3_napi_fini(struct tg3 *tp)
7288{
7289 int i;
7290
7291 for (i = 0; i < tp->irq_cnt; i++)
7292 netif_napi_del(&tp->napi[i].napi);
7293}
7294
7295static inline void tg3_netif_stop(struct tg3 *tp)
7296{
7297 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7298 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007299 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007300 netif_tx_disable(tp->dev);
7301}
7302
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007303/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007304static inline void tg3_netif_start(struct tg3 *tp)
7305{
Matt Carlsonbe947302012-12-03 19:36:57 +00007306 tg3_ptp_resume(tp);
7307
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007308 /* NOTE: unconditional netif_tx_wake_all_queues is only
7309 * appropriate so long as all callers are assured to
7310 * have free tx slots (such as after tg3_init_hw)
7311 */
7312 netif_tx_wake_all_queues(tp->dev);
7313
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007314 if (tp->link_up)
7315 netif_carrier_on(tp->dev);
7316
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007317 tg3_napi_enable(tp);
7318 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7319 tg3_enable_ints(tp);
7320}
7321
David S. Millerf47c11e2005-06-24 20:18:35 -07007322static void tg3_irq_quiesce(struct tg3 *tp)
7323{
Matt Carlson4f125f42009-09-01 12:55:02 +00007324 int i;
7325
David S. Millerf47c11e2005-06-24 20:18:35 -07007326 BUG_ON(tp->irq_sync);
7327
7328 tp->irq_sync = 1;
7329 smp_mb();
7330
Matt Carlson4f125f42009-09-01 12:55:02 +00007331 for (i = 0; i < tp->irq_cnt; i++)
7332 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007333}
7334
David S. Millerf47c11e2005-06-24 20:18:35 -07007335/* Fully shutdown all tg3 driver activity elsewhere in the system.
7336 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7337 * with as well. Most of the time, this is not necessary except when
7338 * shutting down the device.
7339 */
7340static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7341{
Michael Chan46966542007-07-11 19:47:19 -07007342 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007343 if (irq_sync)
7344 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007345}
7346
7347static inline void tg3_full_unlock(struct tg3 *tp)
7348{
David S. Millerf47c11e2005-06-24 20:18:35 -07007349 spin_unlock_bh(&tp->lock);
7350}
7351
Michael Chanfcfa0a32006-03-20 22:28:41 -08007352/* One-shot MSI handler - Chip automatically disables interrupt
7353 * after sending MSI so driver doesn't have to do it.
7354 */
David Howells7d12e782006-10-05 14:55:46 +01007355static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007356{
Matt Carlson09943a12009-08-28 14:01:57 +00007357 struct tg3_napi *tnapi = dev_id;
7358 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007359
Matt Carlson898a56f2009-08-28 14:02:40 +00007360 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007361 if (tnapi->rx_rcb)
7362 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007363
7364 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007365 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007366
7367 return IRQ_HANDLED;
7368}
7369
Michael Chan88b06bc22005-04-21 17:13:25 -07007370/* MSI ISR - No need to check for interrupt sharing and no need to
7371 * flush status block and interrupt mailbox. PCI ordering rules
7372 * guarantee that MSI will arrive after the status block.
7373 */
David Howells7d12e782006-10-05 14:55:46 +01007374static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007375{
Matt Carlson09943a12009-08-28 14:01:57 +00007376 struct tg3_napi *tnapi = dev_id;
7377 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007378
Matt Carlson898a56f2009-08-28 14:02:40 +00007379 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007380 if (tnapi->rx_rcb)
7381 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007382 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007383 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007384 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007385 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007386 * NIC to stop sending us irqs, engaging "in-intr-handler"
7387 * event coalescing.
7388 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007389 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007390 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007391 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007392
Michael Chan88b06bc22005-04-21 17:13:25 -07007393 return IRQ_RETVAL(1);
7394}
7395
David Howells7d12e782006-10-05 14:55:46 +01007396static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397{
Matt Carlson09943a12009-08-28 14:01:57 +00007398 struct tg3_napi *tnapi = dev_id;
7399 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007400 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 unsigned int handled = 1;
7402
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 /* In INTx mode, it is possible for the interrupt to arrive at
7404 * the CPU before the status block posted prior to the interrupt.
7405 * Reading the PCI State register will confirm whether the
7406 * interrupt is ours and will flush the status block.
7407 */
Michael Chand18edcb2007-03-24 20:57:11 -07007408 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007409 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007410 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7411 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007412 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007413 }
Michael Chand18edcb2007-03-24 20:57:11 -07007414 }
7415
7416 /*
7417 * Writing any value to intr-mbox-0 clears PCI INTA# and
7418 * chip-internal interrupt pending events.
7419 * Writing non-zero to intr-mbox-0 additional tells the
7420 * NIC to stop sending us irqs, engaging "in-intr-handler"
7421 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007422 *
7423 * Flush the mailbox to de-assert the IRQ immediately to prevent
7424 * spurious interrupts. The flush impacts performance but
7425 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007426 */
Michael Chanc04cb342007-05-07 00:26:15 -07007427 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007428 if (tg3_irq_sync(tp))
7429 goto out;
7430 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007431 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007432 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007433 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007434 } else {
7435 /* No work, shared interrupt perhaps? re-enable
7436 * interrupts, and flush that PCI write
7437 */
7438 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7439 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007440 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007441out:
David S. Millerfac9b832005-05-18 22:46:34 -07007442 return IRQ_RETVAL(handled);
7443}
7444
David Howells7d12e782006-10-05 14:55:46 +01007445static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007446{
Matt Carlson09943a12009-08-28 14:01:57 +00007447 struct tg3_napi *tnapi = dev_id;
7448 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007449 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007450 unsigned int handled = 1;
7451
David S. Millerfac9b832005-05-18 22:46:34 -07007452 /* In INTx mode, it is possible for the interrupt to arrive at
7453 * the CPU before the status block posted prior to the interrupt.
7454 * Reading the PCI State register will confirm whether the
7455 * interrupt is ours and will flush the status block.
7456 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007457 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007458 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007459 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7460 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 }
Michael Chand18edcb2007-03-24 20:57:11 -07007463 }
7464
7465 /*
7466 * writing any value to intr-mbox-0 clears PCI INTA# and
7467 * chip-internal interrupt pending events.
7468 * writing non-zero to intr-mbox-0 additional tells the
7469 * NIC to stop sending us irqs, engaging "in-intr-handler"
7470 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007471 *
7472 * Flush the mailbox to de-assert the IRQ immediately to prevent
7473 * spurious interrupts. The flush impacts performance but
7474 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007475 */
Michael Chanc04cb342007-05-07 00:26:15 -07007476 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007477
7478 /*
7479 * In a shared interrupt configuration, sometimes other devices'
7480 * interrupts will scream. We record the current status tag here
7481 * so that the above check can report that the screaming interrupts
7482 * are unhandled. Eventually they will be silenced.
7483 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007484 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007485
Michael Chand18edcb2007-03-24 20:57:11 -07007486 if (tg3_irq_sync(tp))
7487 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007488
Matt Carlson72334482009-08-28 14:03:01 +00007489 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007490
Matt Carlson09943a12009-08-28 14:01:57 +00007491 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007492
David S. Millerf47c11e2005-06-24 20:18:35 -07007493out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 return IRQ_RETVAL(handled);
7495}
7496
Michael Chan79381092005-04-21 17:13:59 -07007497/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007498static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007499{
Matt Carlson09943a12009-08-28 14:01:57 +00007500 struct tg3_napi *tnapi = dev_id;
7501 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007502 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007503
Michael Chanf9804dd2005-09-27 12:13:10 -07007504 if ((sblk->status & SD_STATUS_UPDATED) ||
7505 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007506 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007507 return IRQ_RETVAL(1);
7508 }
7509 return IRQ_RETVAL(0);
7510}
7511
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512#ifdef CONFIG_NET_POLL_CONTROLLER
7513static void tg3_poll_controller(struct net_device *dev)
7514{
Matt Carlson4f125f42009-09-01 12:55:02 +00007515 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007516 struct tg3 *tp = netdev_priv(dev);
7517
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007518 if (tg3_irq_sync(tp))
7519 return;
7520
Matt Carlson4f125f42009-09-01 12:55:02 +00007521 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007522 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523}
7524#endif
7525
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526static void tg3_tx_timeout(struct net_device *dev)
7527{
7528 struct tg3 *tp = netdev_priv(dev);
7529
Michael Chanb0408752007-02-13 12:18:30 -08007530 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007531 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007532 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534
Matt Carlsondb219972011-11-04 09:15:03 +00007535 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536}
7537
Michael Chanc58ec932005-09-17 00:46:27 -07007538/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7539static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7540{
7541 u32 base = (u32) mapping & 0xffffffff;
7542
Eric Dumazet807540b2010-09-23 05:40:09 +00007543 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007544}
7545
Michael Chan0f0d1512013-05-13 11:04:16 +00007546/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7547 * of any 4GB boundaries: 4G, 8G, etc
7548 */
7549static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7550 u32 len, u32 mss)
7551{
7552 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7553 u32 base = (u32) mapping & 0xffffffff;
7554
7555 return ((base + len + (mss & 0x3fff)) < base);
7556 }
7557 return 0;
7558}
7559
Michael Chan72f2afb2006-03-06 19:28:35 -08007560/* Test for DMA addresses > 40-bit */
7561static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7562 int len)
7563{
7564#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007565 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007566 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007567 return 0;
7568#else
7569 return 0;
7570#endif
7571}
7572
Matt Carlsond1a3b732011-07-27 14:20:51 +00007573static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007574 dma_addr_t mapping, u32 len, u32 flags,
7575 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007576{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007577 txbd->addr_hi = ((u64) mapping >> 32);
7578 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7579 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7580 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582
Matt Carlson84b67b22011-07-27 14:20:52 +00007583static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007584 dma_addr_t map, u32 len, u32 flags,
7585 u32 mss, u32 vlan)
7586{
7587 struct tg3 *tp = tnapi->tp;
7588 bool hwbug = false;
7589
7590 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007591 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007592
7593 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007594 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007595
Michael Chan0f0d1512013-05-13 11:04:16 +00007596 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7597 hwbug = true;
7598
Matt Carlsond1a3b732011-07-27 14:20:51 +00007599 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007600 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007601
Matt Carlsona4cb4282011-12-14 11:09:58 +00007602 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007603 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007604 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007605 while (len > tp->dma_limit && *budget) {
7606 u32 frag_len = tp->dma_limit;
7607 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007608
Matt Carlsonb9e45482011-11-04 09:14:59 +00007609 /* Avoid the 8byte DMA problem */
7610 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007611 len += tp->dma_limit / 2;
7612 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007613 }
7614
Matt Carlsonb9e45482011-11-04 09:14:59 +00007615 tnapi->tx_buffers[*entry].fragmented = true;
7616
7617 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7618 frag_len, tmp_flag, mss, vlan);
7619 *budget -= 1;
7620 prvidx = *entry;
7621 *entry = NEXT_TX(*entry);
7622
Matt Carlsone31aa982011-07-27 14:20:53 +00007623 map += frag_len;
7624 }
7625
7626 if (len) {
7627 if (*budget) {
7628 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7629 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007630 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007631 *entry = NEXT_TX(*entry);
7632 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007633 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007634 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007635 }
7636 }
7637 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007638 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7639 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007640 *entry = NEXT_TX(*entry);
7641 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007642
7643 return hwbug;
7644}
7645
Matt Carlson0d681b22011-07-27 14:20:49 +00007646static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007647{
7648 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007649 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007650 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007651
Matt Carlson0d681b22011-07-27 14:20:49 +00007652 skb = txb->skb;
7653 txb->skb = NULL;
7654
Matt Carlson432aa7e2011-05-19 12:12:45 +00007655 pci_unmap_single(tnapi->tp->pdev,
7656 dma_unmap_addr(txb, mapping),
7657 skb_headlen(skb),
7658 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007659
7660 while (txb->fragmented) {
7661 txb->fragmented = false;
7662 entry = NEXT_TX(entry);
7663 txb = &tnapi->tx_buffers[entry];
7664 }
7665
Matt Carlsonba1142e2011-11-04 09:15:00 +00007666 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007667 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007668
7669 entry = NEXT_TX(entry);
7670 txb = &tnapi->tx_buffers[entry];
7671
7672 pci_unmap_page(tnapi->tp->pdev,
7673 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007674 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007675
7676 while (txb->fragmented) {
7677 txb->fragmented = false;
7678 entry = NEXT_TX(entry);
7679 txb = &tnapi->tx_buffers[entry];
7680 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007681 }
7682}
7683
Michael Chan72f2afb2006-03-06 19:28:35 -08007684/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007685static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007686 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007687 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007688 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007690 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007691 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007692 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007693 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694
Joe Perches41535772013-02-16 11:20:04 +00007695 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007696 new_skb = skb_copy(skb, GFP_ATOMIC);
7697 else {
7698 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7699
7700 new_skb = skb_copy_expand(skb,
7701 skb_headroom(skb) + more_headroom,
7702 skb_tailroom(skb), GFP_ATOMIC);
7703 }
7704
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007706 ret = -1;
7707 } else {
7708 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007709 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7710 PCI_DMA_TODEVICE);
7711 /* Make sure the mapping succeeded */
7712 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007713 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007714 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007715 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007716 u32 save_entry = *entry;
7717
Matt Carlson92cd3a12011-07-27 14:20:47 +00007718 base_flags |= TXD_FLAG_END;
7719
Matt Carlson84b67b22011-07-27 14:20:52 +00007720 tnapi->tx_buffers[*entry].skb = new_skb;
7721 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007722 mapping, new_addr);
7723
Matt Carlson84b67b22011-07-27 14:20:52 +00007724 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007725 new_skb->len, base_flags,
7726 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007727 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007728 dev_kfree_skb(new_skb);
7729 ret = -1;
7730 }
Michael Chanc58ec932005-09-17 00:46:27 -07007731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 }
7733
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007735 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007736 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737}
7738
Matt Carlson2ffcc982011-05-19 12:12:44 +00007739static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007740
7741/* Use GSO to workaround a rare TSO bug that may be triggered when the
7742 * TSO header is greater than 80 bytes.
7743 */
7744static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7745{
7746 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007747 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007748
7749 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007750 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007751 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007752
7753 /* netif_tx_stop_queue() must be done before checking
7754 * checking tx index in tg3_tx_avail() below, because in
7755 * tg3_tx(), we update tx index before checking for
7756 * netif_tx_queue_stopped().
7757 */
7758 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007759 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007760 return NETDEV_TX_BUSY;
7761
7762 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007763 }
7764
7765 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007766 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007767 goto tg3_tso_bug_end;
7768
7769 do {
7770 nskb = segs;
7771 segs = segs->next;
7772 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007773 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007774 } while (segs);
7775
7776tg3_tso_bug_end:
7777 dev_kfree_skb(skb);
7778
7779 return NETDEV_TX_OK;
7780}
Michael Chan52c0fd82006-06-29 20:15:54 -07007781
Michael Chan5a6f3072006-03-20 22:28:05 -08007782/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007783 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007784 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007785static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007786{
7787 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007788 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007789 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007790 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007791 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007792 struct tg3_napi *tnapi;
7793 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007794 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007795
Matt Carlson24f4efd2009-11-13 13:03:35 +00007796 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7797 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007798 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007799 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800
Matt Carlson84b67b22011-07-27 14:20:52 +00007801 budget = tg3_tx_avail(tnapi);
7802
Michael Chan00b70502006-06-17 21:58:45 -07007803 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007804 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007805 * interrupt. Furthermore, IRQ processing runs lockless so we have
7806 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007808 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007809 if (!netif_tx_queue_stopped(txq)) {
7810 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007811
7812 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007813 netdev_err(dev,
7814 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 return NETDEV_TX_BUSY;
7817 }
7818
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007819 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007821 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007823
Matt Carlsonbe98da62010-07-11 09:31:46 +00007824 mss = skb_shinfo(skb)->gso_size;
7825 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007826 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007827 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828
7829 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007830 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7831 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
Matt Carlson34195c32010-07-11 09:31:42 +00007833 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007834 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835
Eric Dumazeta5a11952012-01-23 01:22:09 +00007836 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007837
Eric Dumazeta5a11952012-01-23 01:22:09 +00007838 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007839 iph->check = 0;
7840 iph->tot_len = htons(mss + hdr_len);
7841 }
7842
Michael Chan52c0fd82006-06-29 20:15:54 -07007843 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007844 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007845 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007846
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7848 TXD_FLAG_CPU_POST_DMA);
7849
Joe Perches63c3a662011-04-26 08:12:10 +00007850 if (tg3_flag(tp, HW_TSO_1) ||
7851 tg3_flag(tp, HW_TSO_2) ||
7852 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007853 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007855 } else
7856 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7857 iph->daddr, 0,
7858 IPPROTO_TCP,
7859 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
Joe Perches63c3a662011-04-26 08:12:10 +00007861 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007862 mss |= (hdr_len & 0xc) << 12;
7863 if (hdr_len & 0x10)
7864 base_flags |= 0x00000010;
7865 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007866 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007867 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007868 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007869 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007870 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 int tsflags;
7872
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007873 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 mss |= (tsflags << 11);
7875 }
7876 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007877 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 int tsflags;
7879
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007880 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 base_flags |= tsflags << 12;
7882 }
7883 }
7884 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007885
Matt Carlson93a700a2011-08-31 11:44:54 +00007886 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7887 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7888 base_flags |= TXD_FLAG_JMB_PKT;
7889
Matt Carlson92cd3a12011-07-27 14:20:47 +00007890 if (vlan_tx_tag_present(skb)) {
7891 base_flags |= TXD_FLAG_VLAN;
7892 vlan = vlan_tx_tag_get(skb);
7893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007895 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7896 tg3_flag(tp, TX_TSTAMP_EN)) {
7897 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7898 base_flags |= TXD_FLAG_HWTSTAMP;
7899 }
7900
Alexander Duyckf4188d82009-12-02 16:48:38 +00007901 len = skb_headlen(skb);
7902
7903 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007904 if (pci_dma_mapping_error(tp->pdev, mapping))
7905 goto drop;
7906
David S. Miller90079ce2008-09-11 04:52:51 -07007907
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007908 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007909 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910
7911 would_hit_hwbug = 0;
7912
Joe Perches63c3a662011-04-26 08:12:10 +00007913 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007914 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
Matt Carlson84b67b22011-07-27 14:20:52 +00007916 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007917 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007918 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007919 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007920 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007921 u32 tmp_mss = mss;
7922
7923 if (!tg3_flag(tp, HW_TSO_1) &&
7924 !tg3_flag(tp, HW_TSO_2) &&
7925 !tg3_flag(tp, HW_TSO_3))
7926 tmp_mss = 0;
7927
Matt Carlsonc5665a52012-02-13 10:20:12 +00007928 /* Now loop through additional data
7929 * fragments, and queue them.
7930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 last = skb_shinfo(skb)->nr_frags - 1;
7932 for (i = 0; i <= last; i++) {
7933 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7934
Eric Dumazet9e903e02011-10-18 21:00:24 +00007935 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007936 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007937 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007939 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007940 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007941 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007942 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007943 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944
Matt Carlsonb9e45482011-11-04 09:14:59 +00007945 if (!budget ||
7946 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007947 len, base_flags |
7948 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007949 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007950 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007951 break;
7952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 }
7954 }
7955
7956 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007957 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958
7959 /* If the workaround fails due to memory/mapping
7960 * failure, silently drop this packet.
7961 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007962 entry = tnapi->tx_prod;
7963 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007964 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007965 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007966 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967 }
7968
Richard Cochrand515b452011-06-19 03:31:41 +00007969 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007970 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007971
Michael Chan6541b802012-03-04 14:48:14 +00007972 /* Sync BD data before updating mailbox */
7973 wmb();
7974
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007976 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007978 tnapi->tx_prod = entry;
7979 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007980 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007981
7982 /* netif_tx_stop_queue() must be done before checking
7983 * checking tx index in tg3_tx_avail() below, because in
7984 * tg3_tx(), we update tx index before checking for
7985 * netif_tx_queue_stopped().
7986 */
7987 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007988 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007989 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007992 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007994
7995dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007996 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007997 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007998drop:
7999 dev_kfree_skb(skb);
8000drop_nofree:
8001 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008002 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003}
8004
Matt Carlson6e01b202011-08-19 13:58:20 +00008005static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8006{
8007 if (enable) {
8008 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8009 MAC_MODE_PORT_MODE_MASK);
8010
8011 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8012
8013 if (!tg3_flag(tp, 5705_PLUS))
8014 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8015
8016 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8017 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8018 else
8019 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8020 } else {
8021 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8022
8023 if (tg3_flag(tp, 5705_PLUS) ||
8024 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008025 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008026 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8027 }
8028
8029 tw32(MAC_MODE, tp->mac_mode);
8030 udelay(40);
8031}
8032
Matt Carlson941ec902011-08-19 13:58:23 +00008033static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008034{
Matt Carlson941ec902011-08-19 13:58:23 +00008035 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008036
8037 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008038 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008039
Matt Carlson941ec902011-08-19 13:58:23 +00008040 if (extlpbk && tg3_phy_set_extloopbk(tp))
8041 return -EIO;
8042
8043 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008044 switch (speed) {
8045 case SPEED_10:
8046 break;
8047 case SPEED_100:
8048 bmcr |= BMCR_SPEED100;
8049 break;
8050 case SPEED_1000:
8051 default:
8052 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8053 speed = SPEED_100;
8054 bmcr |= BMCR_SPEED100;
8055 } else {
8056 speed = SPEED_1000;
8057 bmcr |= BMCR_SPEED1000;
8058 }
8059 }
8060
Matt Carlson941ec902011-08-19 13:58:23 +00008061 if (extlpbk) {
8062 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8063 tg3_readphy(tp, MII_CTRL1000, &val);
8064 val |= CTL1000_AS_MASTER |
8065 CTL1000_ENABLE_MASTER;
8066 tg3_writephy(tp, MII_CTRL1000, val);
8067 } else {
8068 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8069 MII_TG3_FET_PTEST_TRIM_2;
8070 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8071 }
8072 } else
8073 bmcr |= BMCR_LOOPBACK;
8074
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008075 tg3_writephy(tp, MII_BMCR, bmcr);
8076
8077 /* The write needs to be flushed for the FETs */
8078 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8079 tg3_readphy(tp, MII_BMCR, &bmcr);
8080
8081 udelay(40);
8082
8083 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008084 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008085 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008086 MII_TG3_FET_PTEST_FRC_TX_LINK |
8087 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8088
8089 /* The write needs to be flushed for the AC131 */
8090 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8091 }
8092
8093 /* Reset to prevent losing 1st rx packet intermittently */
8094 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8095 tg3_flag(tp, 5780_CLASS)) {
8096 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8097 udelay(10);
8098 tw32_f(MAC_RX_MODE, tp->rx_mode);
8099 }
8100
8101 mac_mode = tp->mac_mode &
8102 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8103 if (speed == SPEED_1000)
8104 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8105 else
8106 mac_mode |= MAC_MODE_PORT_MODE_MII;
8107
Joe Perches41535772013-02-16 11:20:04 +00008108 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008109 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8110
8111 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8112 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8113 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8114 mac_mode |= MAC_MODE_LINK_POLARITY;
8115
8116 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8117 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8118 }
8119
8120 tw32(MAC_MODE, mac_mode);
8121 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008122
8123 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008124}
8125
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008126static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008127{
8128 struct tg3 *tp = netdev_priv(dev);
8129
8130 if (features & NETIF_F_LOOPBACK) {
8131 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8132 return;
8133
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008134 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008135 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008136 netif_carrier_on(tp->dev);
8137 spin_unlock_bh(&tp->lock);
8138 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8139 } else {
8140 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8141 return;
8142
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008143 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008144 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008145 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008146 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008147 spin_unlock_bh(&tp->lock);
8148 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8149 }
8150}
8151
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008152static netdev_features_t tg3_fix_features(struct net_device *dev,
8153 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008154{
8155 struct tg3 *tp = netdev_priv(dev);
8156
Joe Perches63c3a662011-04-26 08:12:10 +00008157 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008158 features &= ~NETIF_F_ALL_TSO;
8159
8160 return features;
8161}
8162
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008163static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008164{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008165 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008166
8167 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8168 tg3_set_loopback(dev, features);
8169
8170 return 0;
8171}
8172
Matt Carlson21f581a2009-08-28 14:00:25 +00008173static void tg3_rx_prodring_free(struct tg3 *tp,
8174 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 int i;
8177
Matt Carlson8fea32b2010-09-15 08:59:58 +00008178 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008179 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008180 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008181 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008182 tp->rx_pkt_map_sz);
8183
Joe Perches63c3a662011-04-26 08:12:10 +00008184 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008185 for (i = tpr->rx_jmb_cons_idx;
8186 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008187 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008188 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008189 TG3_RX_JMB_MAP_SZ);
8190 }
8191 }
8192
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008193 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195
Matt Carlson2c49a442010-09-30 10:34:35 +00008196 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008197 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008198 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199
Joe Perches63c3a662011-04-26 08:12:10 +00008200 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008201 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008202 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008203 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204 }
8205}
8206
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008207/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208 *
8209 * The chip has been shut down and the driver detached from
8210 * the networking, so no interrupts or new tx packets will
8211 * end up in the driver. tp->{tx,}lock are held and thus
8212 * we may not sleep.
8213 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008214static int tg3_rx_prodring_alloc(struct tg3 *tp,
8215 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216{
Matt Carlson287be122009-08-28 13:58:46 +00008217 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008219 tpr->rx_std_cons_idx = 0;
8220 tpr->rx_std_prod_idx = 0;
8221 tpr->rx_jmb_cons_idx = 0;
8222 tpr->rx_jmb_prod_idx = 0;
8223
Matt Carlson8fea32b2010-09-15 08:59:58 +00008224 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008225 memset(&tpr->rx_std_buffers[0], 0,
8226 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008227 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008228 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008229 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008230 goto done;
8231 }
8232
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008234 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235
Matt Carlson287be122009-08-28 13:58:46 +00008236 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008237 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008238 tp->dev->mtu > ETH_DATA_LEN)
8239 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8240 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008241
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242 /* Initialize invariants of the rings, we only set this
8243 * stuff once. This works because the card does not
8244 * write into the rx buffer posting rings.
8245 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008246 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247 struct tg3_rx_buffer_desc *rxd;
8248
Matt Carlson21f581a2009-08-28 14:00:25 +00008249 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008250 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008251 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8252 rxd->opaque = (RXD_OPAQUE_RING_STD |
8253 (i << RXD_OPAQUE_INDEX_SHIFT));
8254 }
8255
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008256 /* Now allocate fresh SKBs for each rx ring. */
8257 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008258 unsigned int frag_size;
8259
8260 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8261 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008262 netdev_warn(tp->dev,
8263 "Using a smaller RX standard ring. Only "
8264 "%d out of %d buffers were allocated "
8265 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008266 if (i == 0)
8267 goto initfail;
8268 tp->rx_pending = i;
8269 break;
8270 }
8271 }
8272
Joe Perches63c3a662011-04-26 08:12:10 +00008273 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008274 goto done;
8275
Matt Carlson2c49a442010-09-30 10:34:35 +00008276 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008277
Joe Perches63c3a662011-04-26 08:12:10 +00008278 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008279 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280
Matt Carlson2c49a442010-09-30 10:34:35 +00008281 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008282 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283
Matt Carlson0d86df82010-02-17 15:17:00 +00008284 rxd = &tpr->rx_jmb[i].std;
8285 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8286 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8287 RXD_FLAG_JUMBO;
8288 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8289 (i << RXD_OPAQUE_INDEX_SHIFT));
8290 }
8291
8292 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008293 unsigned int frag_size;
8294
8295 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8296 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008297 netdev_warn(tp->dev,
8298 "Using a smaller RX jumbo ring. Only %d "
8299 "out of %d buffers were allocated "
8300 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008301 if (i == 0)
8302 goto initfail;
8303 tp->rx_jumbo_pending = i;
8304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 }
8306 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008307
8308done:
Michael Chan32d8c572006-07-25 16:38:29 -07008309 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008310
8311initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008312 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008313 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314}
8315
Matt Carlson21f581a2009-08-28 14:00:25 +00008316static void tg3_rx_prodring_fini(struct tg3 *tp,
8317 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318{
Matt Carlson21f581a2009-08-28 14:00:25 +00008319 kfree(tpr->rx_std_buffers);
8320 tpr->rx_std_buffers = NULL;
8321 kfree(tpr->rx_jmb_buffers);
8322 tpr->rx_jmb_buffers = NULL;
8323 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008324 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8325 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008326 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008328 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008329 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8330 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008331 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008333}
8334
Matt Carlson21f581a2009-08-28 14:00:25 +00008335static int tg3_rx_prodring_init(struct tg3 *tp,
8336 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008337{
Matt Carlson2c49a442010-09-30 10:34:35 +00008338 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8339 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008340 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008341 return -ENOMEM;
8342
Matt Carlson4bae65c2010-11-24 08:31:52 +00008343 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8344 TG3_RX_STD_RING_BYTES(tp),
8345 &tpr->rx_std_mapping,
8346 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008347 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008348 goto err_out;
8349
Joe Perches63c3a662011-04-26 08:12:10 +00008350 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008351 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008352 GFP_KERNEL);
8353 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008354 goto err_out;
8355
Matt Carlson4bae65c2010-11-24 08:31:52 +00008356 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8357 TG3_RX_JMB_RING_BYTES(tp),
8358 &tpr->rx_jmb_mapping,
8359 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008360 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008361 goto err_out;
8362 }
8363
8364 return 0;
8365
8366err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008367 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008368 return -ENOMEM;
8369}
8370
8371/* Free up pending packets in all rx/tx rings.
8372 *
8373 * The chip has been shut down and the driver detached from
8374 * the networking, so no interrupts or new tx packets will
8375 * end up in the driver. tp->{tx,}lock is not held and we are not
8376 * in an interrupt context and thus may sleep.
8377 */
8378static void tg3_free_rings(struct tg3 *tp)
8379{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008380 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008381
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008382 for (j = 0; j < tp->irq_cnt; j++) {
8383 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008384
Matt Carlson8fea32b2010-09-15 08:59:58 +00008385 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008386
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008387 if (!tnapi->tx_buffers)
8388 continue;
8389
Matt Carlson0d681b22011-07-27 14:20:49 +00008390 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8391 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008392
Matt Carlson0d681b22011-07-27 14:20:49 +00008393 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008394 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008395
Matt Carlsonba1142e2011-11-04 09:15:00 +00008396 tg3_tx_skb_unmap(tnapi, i,
8397 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008398
8399 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008400 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008401 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008402 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008403}
8404
8405/* Initialize tx/rx rings for packet processing.
8406 *
8407 * The chip has been shut down and the driver detached from
8408 * the networking, so no interrupts or new tx packets will
8409 * end up in the driver. tp->{tx,}lock are held and thus
8410 * we may not sleep.
8411 */
8412static int tg3_init_rings(struct tg3 *tp)
8413{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008414 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008415
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008416 /* Free up all the SKBs. */
8417 tg3_free_rings(tp);
8418
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008419 for (i = 0; i < tp->irq_cnt; i++) {
8420 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008421
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008422 tnapi->last_tag = 0;
8423 tnapi->last_irq_tag = 0;
8424 tnapi->hw_status->status = 0;
8425 tnapi->hw_status->status_tag = 0;
8426 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8427
8428 tnapi->tx_prod = 0;
8429 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008430 if (tnapi->tx_ring)
8431 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008432
8433 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008434 if (tnapi->rx_rcb)
8435 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008436
Matt Carlson8fea32b2010-09-15 08:59:58 +00008437 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008438 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008439 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008440 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008441 }
Matt Carlson72334482009-08-28 14:03:01 +00008442
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008443 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008444}
8445
Michael Chan49a359e2012-09-28 07:12:37 +00008446static void tg3_mem_tx_release(struct tg3 *tp)
8447{
8448 int i;
8449
8450 for (i = 0; i < tp->irq_max; i++) {
8451 struct tg3_napi *tnapi = &tp->napi[i];
8452
8453 if (tnapi->tx_ring) {
8454 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8455 tnapi->tx_ring, tnapi->tx_desc_mapping);
8456 tnapi->tx_ring = NULL;
8457 }
8458
8459 kfree(tnapi->tx_buffers);
8460 tnapi->tx_buffers = NULL;
8461 }
8462}
8463
8464static int tg3_mem_tx_acquire(struct tg3 *tp)
8465{
8466 int i;
8467 struct tg3_napi *tnapi = &tp->napi[0];
8468
8469 /* If multivector TSS is enabled, vector 0 does not handle
8470 * tx interrupts. Don't allocate any resources for it.
8471 */
8472 if (tg3_flag(tp, ENABLE_TSS))
8473 tnapi++;
8474
8475 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8476 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8477 TG3_TX_RING_SIZE, GFP_KERNEL);
8478 if (!tnapi->tx_buffers)
8479 goto err_out;
8480
8481 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8482 TG3_TX_RING_BYTES,
8483 &tnapi->tx_desc_mapping,
8484 GFP_KERNEL);
8485 if (!tnapi->tx_ring)
8486 goto err_out;
8487 }
8488
8489 return 0;
8490
8491err_out:
8492 tg3_mem_tx_release(tp);
8493 return -ENOMEM;
8494}
8495
8496static void tg3_mem_rx_release(struct tg3 *tp)
8497{
8498 int i;
8499
8500 for (i = 0; i < tp->irq_max; i++) {
8501 struct tg3_napi *tnapi = &tp->napi[i];
8502
8503 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8504
8505 if (!tnapi->rx_rcb)
8506 continue;
8507
8508 dma_free_coherent(&tp->pdev->dev,
8509 TG3_RX_RCB_RING_BYTES(tp),
8510 tnapi->rx_rcb,
8511 tnapi->rx_rcb_mapping);
8512 tnapi->rx_rcb = NULL;
8513 }
8514}
8515
8516static int tg3_mem_rx_acquire(struct tg3 *tp)
8517{
8518 unsigned int i, limit;
8519
8520 limit = tp->rxq_cnt;
8521
8522 /* If RSS is enabled, we need a (dummy) producer ring
8523 * set on vector zero. This is the true hw prodring.
8524 */
8525 if (tg3_flag(tp, ENABLE_RSS))
8526 limit++;
8527
8528 for (i = 0; i < limit; i++) {
8529 struct tg3_napi *tnapi = &tp->napi[i];
8530
8531 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8532 goto err_out;
8533
8534 /* If multivector RSS is enabled, vector 0
8535 * does not handle rx or tx interrupts.
8536 * Don't allocate any resources for it.
8537 */
8538 if (!i && tg3_flag(tp, ENABLE_RSS))
8539 continue;
8540
8541 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8542 TG3_RX_RCB_RING_BYTES(tp),
8543 &tnapi->rx_rcb_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008544 GFP_KERNEL | __GFP_ZERO);
Michael Chan49a359e2012-09-28 07:12:37 +00008545 if (!tnapi->rx_rcb)
8546 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008547 }
8548
8549 return 0;
8550
8551err_out:
8552 tg3_mem_rx_release(tp);
8553 return -ENOMEM;
8554}
8555
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008556/*
8557 * Must not be invoked with interrupt sources disabled and
8558 * the hardware shutdown down.
8559 */
8560static void tg3_free_consistent(struct tg3 *tp)
8561{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008562 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008563
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008564 for (i = 0; i < tp->irq_cnt; i++) {
8565 struct tg3_napi *tnapi = &tp->napi[i];
8566
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008567 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008568 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8569 tnapi->hw_status,
8570 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008571 tnapi->hw_status = NULL;
8572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008574
Michael Chan49a359e2012-09-28 07:12:37 +00008575 tg3_mem_rx_release(tp);
8576 tg3_mem_tx_release(tp);
8577
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008579 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8580 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008581 tp->hw_stats = NULL;
8582 }
8583}
8584
8585/*
8586 * Must not be invoked with interrupt sources disabled and
8587 * the hardware shutdown down. Can sleep.
8588 */
8589static int tg3_alloc_consistent(struct tg3 *tp)
8590{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008591 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008592
Matt Carlson4bae65c2010-11-24 08:31:52 +00008593 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8594 sizeof(struct tg3_hw_stats),
8595 &tp->stats_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008596 GFP_KERNEL | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597 if (!tp->hw_stats)
8598 goto err_out;
8599
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008600 for (i = 0; i < tp->irq_cnt; i++) {
8601 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008602 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008603
Matt Carlson4bae65c2010-11-24 08:31:52 +00008604 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8605 TG3_HW_STATUS_SIZE,
8606 &tnapi->status_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008607 GFP_KERNEL | __GFP_ZERO);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008608 if (!tnapi->hw_status)
8609 goto err_out;
8610
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008611 sblk = tnapi->hw_status;
8612
Michael Chan49a359e2012-09-28 07:12:37 +00008613 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008614 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008615
Michael Chan49a359e2012-09-28 07:12:37 +00008616 /*
8617 * When RSS is enabled, the status block format changes
8618 * slightly. The "rx_jumbo_consumer", "reserved",
8619 * and "rx_mini_consumer" members get mapped to the
8620 * other three rx return ring producer indexes.
8621 */
8622 switch (i) {
8623 case 1:
8624 prodptr = &sblk->idx[0].rx_producer;
8625 break;
8626 case 2:
8627 prodptr = &sblk->rx_jumbo_consumer;
8628 break;
8629 case 3:
8630 prodptr = &sblk->reserved;
8631 break;
8632 case 4:
8633 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008634 break;
8635 }
Michael Chan49a359e2012-09-28 07:12:37 +00008636 tnapi->rx_rcb_prod_idx = prodptr;
8637 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008638 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008639 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008640 }
8641
Michael Chan49a359e2012-09-28 07:12:37 +00008642 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8643 goto err_out;
8644
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 return 0;
8646
8647err_out:
8648 tg3_free_consistent(tp);
8649 return -ENOMEM;
8650}
8651
8652#define MAX_WAIT_CNT 1000
8653
8654/* To stop a block, clear the enable bit and poll till it
8655 * clears. tp->lock is held.
8656 */
Joe Perches953c96e2013-04-09 10:18:14 +00008657static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658{
8659 unsigned int i;
8660 u32 val;
8661
Joe Perches63c3a662011-04-26 08:12:10 +00008662 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663 switch (ofs) {
8664 case RCVLSC_MODE:
8665 case DMAC_MODE:
8666 case MBFREE_MODE:
8667 case BUFMGR_MODE:
8668 case MEMARB_MODE:
8669 /* We can't enable/disable these bits of the
8670 * 5705/5750, just say success.
8671 */
8672 return 0;
8673
8674 default:
8675 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 }
8678
8679 val = tr32(ofs);
8680 val &= ~enable_bit;
8681 tw32_f(ofs, val);
8682
8683 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008684 if (pci_channel_offline(tp->pdev)) {
8685 dev_err(&tp->pdev->dev,
8686 "tg3_stop_block device offline, "
8687 "ofs=%lx enable_bit=%x\n",
8688 ofs, enable_bit);
8689 return -ENODEV;
8690 }
8691
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 udelay(100);
8693 val = tr32(ofs);
8694 if ((val & enable_bit) == 0)
8695 break;
8696 }
8697
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008698 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008699 dev_err(&tp->pdev->dev,
8700 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8701 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 return -ENODEV;
8703 }
8704
8705 return 0;
8706}
8707
8708/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008709static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710{
8711 int i, err;
8712
8713 tg3_disable_ints(tp);
8714
Gavin Shan6d446ec2013-06-25 15:24:32 +08008715 if (pci_channel_offline(tp->pdev)) {
8716 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8717 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8718 err = -ENODEV;
8719 goto err_no_dev;
8720 }
8721
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722 tp->rx_mode &= ~RX_MODE_ENABLE;
8723 tw32_f(MAC_RX_MODE, tp->rx_mode);
8724 udelay(10);
8725
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008726 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8727 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8728 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8729 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8730 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8731 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008733 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8734 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8735 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8736 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8737 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8738 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8739 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740
8741 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8742 tw32_f(MAC_MODE, tp->mac_mode);
8743 udelay(40);
8744
8745 tp->tx_mode &= ~TX_MODE_ENABLE;
8746 tw32_f(MAC_TX_MODE, tp->tx_mode);
8747
8748 for (i = 0; i < MAX_WAIT_CNT; i++) {
8749 udelay(100);
8750 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8751 break;
8752 }
8753 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008754 dev_err(&tp->pdev->dev,
8755 "%s timed out, TX_MODE_ENABLE will not clear "
8756 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008757 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 }
8759
Michael Chane6de8ad2005-05-05 14:42:41 -07008760 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008761 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8762 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763
8764 tw32(FTQ_RESET, 0xffffffff);
8765 tw32(FTQ_RESET, 0x00000000);
8766
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008767 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8768 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769
Gavin Shan6d446ec2013-06-25 15:24:32 +08008770err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008771 for (i = 0; i < tp->irq_cnt; i++) {
8772 struct tg3_napi *tnapi = &tp->napi[i];
8773 if (tnapi->hw_status)
8774 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776
Linus Torvalds1da177e2005-04-16 15:20:36 -07008777 return err;
8778}
8779
Michael Chanee6a99b2007-07-18 21:49:10 -07008780/* Save PCI command register before chip reset */
8781static void tg3_save_pci_state(struct tg3 *tp)
8782{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008783 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008784}
8785
8786/* Restore PCI state after chip reset */
8787static void tg3_restore_pci_state(struct tg3 *tp)
8788{
8789 u32 val;
8790
8791 /* Re-enable indirect register accesses. */
8792 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8793 tp->misc_host_ctrl);
8794
8795 /* Set MAX PCI retry to zero. */
8796 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008797 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008798 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008799 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008800 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008801 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008802 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008803 PCISTATE_ALLOW_APE_SHMEM_WR |
8804 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008805 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8806
Matt Carlson8a6eac92007-10-21 16:17:55 -07008807 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008808
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008809 if (!tg3_flag(tp, PCI_EXPRESS)) {
8810 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8811 tp->pci_cacheline_sz);
8812 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8813 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008814 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008815
Michael Chanee6a99b2007-07-18 21:49:10 -07008816 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008817 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008818 u16 pcix_cmd;
8819
8820 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8821 &pcix_cmd);
8822 pcix_cmd &= ~PCI_X_CMD_ERO;
8823 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8824 pcix_cmd);
8825 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008826
Joe Perches63c3a662011-04-26 08:12:10 +00008827 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008828
8829 /* Chip reset on 5780 will reset MSI enable bit,
8830 * so need to restore it.
8831 */
Joe Perches63c3a662011-04-26 08:12:10 +00008832 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008833 u16 ctrl;
8834
8835 pci_read_config_word(tp->pdev,
8836 tp->msi_cap + PCI_MSI_FLAGS,
8837 &ctrl);
8838 pci_write_config_word(tp->pdev,
8839 tp->msi_cap + PCI_MSI_FLAGS,
8840 ctrl | PCI_MSI_FLAGS_ENABLE);
8841 val = tr32(MSGINT_MODE);
8842 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8843 }
8844 }
8845}
8846
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847/* tp->lock is held. */
8848static int tg3_chip_reset(struct tg3 *tp)
8849{
8850 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008851 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008852 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853
David S. Millerf49639e2006-06-09 11:58:36 -07008854 tg3_nvram_lock(tp);
8855
Matt Carlson77b483f2008-08-15 14:07:24 -07008856 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8857
David S. Millerf49639e2006-06-09 11:58:36 -07008858 /* No matching tg3_nvram_unlock() after this because
8859 * chip reset below will undo the nvram lock.
8860 */
8861 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862
Michael Chanee6a99b2007-07-18 21:49:10 -07008863 /* GRC_MISC_CFG core clock reset will clear the memory
8864 * enable bit in PCI register 4 and the MSI enable bit
8865 * on some chips, so we save relevant registers here.
8866 */
8867 tg3_save_pci_state(tp);
8868
Joe Perches41535772013-02-16 11:20:04 +00008869 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008870 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008871 tw32(GRC_FASTBOOT_PC, 0);
8872
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 /*
8874 * We must avoid the readl() that normally takes place.
8875 * It locks machines, causes machine checks, and other
8876 * fun things. So, temporarily disable the 5701
8877 * hardware workaround, while we do the reset.
8878 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008879 write_op = tp->write32;
8880 if (write_op == tg3_write_flush_reg32)
8881 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882
Michael Chand18edcb2007-03-24 20:57:11 -07008883 /* Prevent the irq handler from reading or writing PCI registers
8884 * during chip reset when the memory enable bit in the PCI command
8885 * register may be cleared. The chip does not generate interrupt
8886 * at this time, but the irq handler may still be called due to irq
8887 * sharing or irqpoll.
8888 */
Joe Perches63c3a662011-04-26 08:12:10 +00008889 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008890 for (i = 0; i < tp->irq_cnt; i++) {
8891 struct tg3_napi *tnapi = &tp->napi[i];
8892 if (tnapi->hw_status) {
8893 tnapi->hw_status->status = 0;
8894 tnapi->hw_status->status_tag = 0;
8895 }
8896 tnapi->last_tag = 0;
8897 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008898 }
Michael Chand18edcb2007-03-24 20:57:11 -07008899 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008900
8901 for (i = 0; i < tp->irq_cnt; i++)
8902 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008903
Joe Perches41535772013-02-16 11:20:04 +00008904 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008905 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8906 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8907 }
8908
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 /* do the reset */
8910 val = GRC_MISC_CFG_CORECLK_RESET;
8911
Joe Perches63c3a662011-04-26 08:12:10 +00008912 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008913 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008914 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008915 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008916 tr32(TG3_PCIE_PHY_TSTCTL) ==
8917 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8918 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8919
Joe Perches41535772013-02-16 11:20:04 +00008920 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 tw32(GRC_MISC_CFG, (1 << 29));
8922 val |= (1 << 29);
8923 }
8924 }
8925
Joe Perches41535772013-02-16 11:20:04 +00008926 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008927 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8928 tw32(GRC_VCPU_EXT_CTRL,
8929 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8930 }
8931
Matt Carlsonf37500d2010-08-02 11:25:59 +00008932 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008933 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008935
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936 tw32(GRC_MISC_CFG, val);
8937
Michael Chan1ee582d2005-08-09 20:16:46 -07008938 /* restore 5701 hardware bug workaround write method */
8939 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940
8941 /* Unfortunately, we have to delay before the PCI read back.
8942 * Some 575X chips even will not respond to a PCI cfg access
8943 * when the reset command is given to the chip.
8944 *
8945 * How do these hardware designers expect things to work
8946 * properly if the PCI write is posted for a long period
8947 * of time? It is always necessary to have some method by
8948 * which a register read back can occur to push the write
8949 * out which does the reset.
8950 *
8951 * For most tg3 variants the trick below was working.
8952 * Ho hum...
8953 */
8954 udelay(120);
8955
8956 /* Flush PCI posted writes. The normal MMIO registers
8957 * are inaccessible at this time so this is the only
8958 * way to make this reliably (actually, this is no longer
8959 * the case, see above). I tried to use indirect
8960 * register read/write but this upset some 5701 variants.
8961 */
8962 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8963
8964 udelay(120);
8965
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008966 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008967 u16 val16;
8968
Joe Perches41535772013-02-16 11:20:04 +00008969 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008970 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 u32 cfg_val;
8972
8973 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008974 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 udelay(100);
8976
8977 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8978 pci_write_config_dword(tp->pdev, 0xc4,
8979 cfg_val | (1 << 15));
8980 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008981
Matt Carlsone7126992009-08-25 10:08:16 +00008982 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008983 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008984 /*
8985 * Older PCIe devices only support the 128 byte
8986 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008987 */
Joe Perches63c3a662011-04-26 08:12:10 +00008988 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008989 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8990 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008991
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008992 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008993 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008994 PCI_EXP_DEVSTA_CED |
8995 PCI_EXP_DEVSTA_NFED |
8996 PCI_EXP_DEVSTA_FED |
8997 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008998 }
8999
Michael Chanee6a99b2007-07-18 21:49:10 -07009000 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009001
Joe Perches63c3a662011-04-26 08:12:10 +00009002 tg3_flag_clear(tp, CHIP_RESETTING);
9003 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009004
Michael Chanee6a99b2007-07-18 21:49:10 -07009005 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009006 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009007 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009008 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009
Joe Perches41535772013-02-16 11:20:04 +00009010 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011 tg3_stop_fw(tp);
9012 tw32(0x5000, 0x400);
9013 }
9014
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009015 if (tg3_flag(tp, IS_SSB_CORE)) {
9016 /*
9017 * BCM4785: In order to avoid repercussions from using
9018 * potentially defective internal ROM, stop the Rx RISC CPU,
9019 * which is not required.
9020 */
9021 tg3_stop_fw(tp);
9022 tg3_halt_cpu(tp, RX_CPU_BASE);
9023 }
9024
Nithin Sujirfb03a432013-05-21 12:57:32 +00009025 err = tg3_poll_fw(tp);
9026 if (err)
9027 return err;
9028
Linus Torvalds1da177e2005-04-16 15:20:36 -07009029 tw32(GRC_MODE, tp->grc_mode);
9030
Joe Perches41535772013-02-16 11:20:04 +00009031 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009032 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033
9034 tw32(0xc4, val | (1 << 15));
9035 }
9036
9037 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009038 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009039 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009040 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9042 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9043 }
9044
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009045 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009046 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009047 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009048 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009049 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009050 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009052 val = 0;
9053
9054 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009055 udelay(40);
9056
Matt Carlson77b483f2008-08-15 14:07:24 -07009057 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9058
Matt Carlson0a9140c2009-08-28 12:27:50 +00009059 tg3_mdio_start(tp);
9060
Joe Perches63c3a662011-04-26 08:12:10 +00009061 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009062 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9063 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009064 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009065 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066
9067 tw32(0x7c00, val | (1 << 25));
9068 }
9069
Joe Perches41535772013-02-16 11:20:04 +00009070 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00009071 val = tr32(TG3_CPMU_CLCK_ORIDE);
9072 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9073 }
9074
Linus Torvalds1da177e2005-04-16 15:20:36 -07009075 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009076 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009077 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9078 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9079
Joe Perches63c3a662011-04-26 08:12:10 +00009080 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009081 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9082 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9083 u32 nic_cfg;
9084
9085 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9086 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009087 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009088 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009089 if (tg3_flag(tp, 5750_PLUS))
9090 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009091
9092 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9093 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9094 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9095 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9096 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097 }
9098 }
9099
9100 return 0;
9101}
9102
Matt Carlson65ec6982012-02-28 23:33:37 +00009103static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9104static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009105
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009107static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108{
9109 int err;
9110
9111 tg3_stop_fw(tp);
9112
Michael Chan944d9802005-05-29 14:57:48 -07009113 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009114
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009115 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116 err = tg3_chip_reset(tp);
9117
Joe Perches953c96e2013-04-09 10:18:14 +00009118 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009119
Michael Chan944d9802005-05-29 14:57:48 -07009120 tg3_write_sig_legacy(tp, kind);
9121 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009122
Matt Carlson92feeab2011-12-08 14:40:14 +00009123 if (tp->hw_stats) {
9124 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009125 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009126 tg3_get_estats(tp, &tp->estats_prev);
9127
9128 /* And make sure the next sample is new data */
9129 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9130 }
9131
Linus Torvalds1da177e2005-04-16 15:20:36 -07009132 if (err)
9133 return err;
9134
9135 return 0;
9136}
9137
Linus Torvalds1da177e2005-04-16 15:20:36 -07009138static int tg3_set_mac_addr(struct net_device *dev, void *p)
9139{
9140 struct tg3 *tp = netdev_priv(dev);
9141 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009142 int err = 0;
9143 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009144
Michael Chanf9804dd2005-09-27 12:13:10 -07009145 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009146 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009147
Linus Torvalds1da177e2005-04-16 15:20:36 -07009148 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9149
Michael Chane75f7c92006-03-20 21:33:26 -08009150 if (!netif_running(dev))
9151 return 0;
9152
Joe Perches63c3a662011-04-26 08:12:10 +00009153 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009154 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009155
Michael Chan986e0ae2007-05-05 12:10:20 -07009156 addr0_high = tr32(MAC_ADDR_0_HIGH);
9157 addr0_low = tr32(MAC_ADDR_0_LOW);
9158 addr1_high = tr32(MAC_ADDR_1_HIGH);
9159 addr1_low = tr32(MAC_ADDR_1_LOW);
9160
9161 /* Skip MAC addr 1 if ASF is using it. */
9162 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9163 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009164 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009165 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009166 spin_lock_bh(&tp->lock);
9167 __tg3_set_mac_addr(tp, skip_mac_1);
9168 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009169
Michael Chanb9ec6c12006-07-25 16:37:27 -07009170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171}
9172
9173/* tp->lock is held. */
9174static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9175 dma_addr_t mapping, u32 maxlen_flags,
9176 u32 nic_addr)
9177{
9178 tg3_write_mem(tp,
9179 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9180 ((u64) mapping >> 32));
9181 tg3_write_mem(tp,
9182 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9183 ((u64) mapping & 0xffffffff));
9184 tg3_write_mem(tp,
9185 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9186 maxlen_flags);
9187
Joe Perches63c3a662011-04-26 08:12:10 +00009188 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189 tg3_write_mem(tp,
9190 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9191 nic_addr);
9192}
9193
Michael Chana489b6d2012-09-28 07:12:39 +00009194
9195static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009196{
Michael Chana489b6d2012-09-28 07:12:39 +00009197 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009198
Joe Perches63c3a662011-04-26 08:12:10 +00009199 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009200 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9201 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9202 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009203 } else {
9204 tw32(HOSTCC_TXCOL_TICKS, 0);
9205 tw32(HOSTCC_TXMAX_FRAMES, 0);
9206 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009207
9208 for (; i < tp->txq_cnt; i++) {
9209 u32 reg;
9210
9211 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9212 tw32(reg, ec->tx_coalesce_usecs);
9213 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9214 tw32(reg, ec->tx_max_coalesced_frames);
9215 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9216 tw32(reg, ec->tx_max_coalesced_frames_irq);
9217 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009218 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009219
Michael Chana489b6d2012-09-28 07:12:39 +00009220 for (; i < tp->irq_max - 1; i++) {
9221 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9222 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9223 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9224 }
9225}
9226
9227static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9228{
9229 int i = 0;
9230 u32 limit = tp->rxq_cnt;
9231
Joe Perches63c3a662011-04-26 08:12:10 +00009232 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009233 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9234 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9235 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009236 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009237 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009238 tw32(HOSTCC_RXCOL_TICKS, 0);
9239 tw32(HOSTCC_RXMAX_FRAMES, 0);
9240 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009241 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009242
Michael Chana489b6d2012-09-28 07:12:39 +00009243 for (; i < limit; i++) {
9244 u32 reg;
9245
9246 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9247 tw32(reg, ec->rx_coalesce_usecs);
9248 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9249 tw32(reg, ec->rx_max_coalesced_frames);
9250 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9251 tw32(reg, ec->rx_max_coalesced_frames_irq);
9252 }
9253
9254 for (; i < tp->irq_max - 1; i++) {
9255 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9256 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9257 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9258 }
9259}
9260
9261static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9262{
9263 tg3_coal_tx_init(tp, ec);
9264 tg3_coal_rx_init(tp, ec);
9265
Joe Perches63c3a662011-04-26 08:12:10 +00009266 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009267 u32 val = ec->stats_block_coalesce_usecs;
9268
Matt Carlsonb6080e12009-09-01 13:12:00 +00009269 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9270 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9271
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009272 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009273 val = 0;
9274
9275 tw32(HOSTCC_STAT_COAL_TICKS, val);
9276 }
9277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278
9279/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009280static void tg3_tx_rcbs_disable(struct tg3 *tp)
9281{
9282 u32 txrcb, limit;
9283
9284 /* Disable all transmit rings but the first. */
9285 if (!tg3_flag(tp, 5705_PLUS))
9286 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9287 else if (tg3_flag(tp, 5717_PLUS))
9288 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9289 else if (tg3_flag(tp, 57765_CLASS) ||
9290 tg3_asic_rev(tp) == ASIC_REV_5762)
9291 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9292 else
9293 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9294
9295 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9296 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9297 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9298 BDINFO_FLAGS_DISABLED);
9299}
9300
9301/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009302static void tg3_tx_rcbs_init(struct tg3 *tp)
9303{
9304 int i = 0;
9305 u32 txrcb = NIC_SRAM_SEND_RCB;
9306
9307 if (tg3_flag(tp, ENABLE_TSS))
9308 i++;
9309
9310 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9311 struct tg3_napi *tnapi = &tp->napi[i];
9312
9313 if (!tnapi->tx_ring)
9314 continue;
9315
9316 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9317 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9318 NIC_SRAM_TX_BUFFER_DESC);
9319 }
9320}
9321
9322/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009323static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9324{
9325 u32 rxrcb, limit;
9326
9327 /* Disable all receive return rings but the first. */
9328 if (tg3_flag(tp, 5717_PLUS))
9329 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9330 else if (!tg3_flag(tp, 5705_PLUS))
9331 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9332 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9333 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9334 tg3_flag(tp, 57765_CLASS))
9335 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9336 else
9337 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9338
9339 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9340 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9341 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9342 BDINFO_FLAGS_DISABLED);
9343}
9344
9345/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009346static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9347{
9348 int i = 0;
9349 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9350
9351 if (tg3_flag(tp, ENABLE_RSS))
9352 i++;
9353
9354 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9355 struct tg3_napi *tnapi = &tp->napi[i];
9356
9357 if (!tnapi->rx_rcb)
9358 continue;
9359
9360 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9361 (tp->rx_ret_ring_mask + 1) <<
9362 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9363 }
9364}
9365
9366/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009367static void tg3_rings_reset(struct tg3 *tp)
9368{
9369 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009370 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009371 struct tg3_napi *tnapi = &tp->napi[0];
9372
Nithin Sujir328947f2013-05-23 11:11:24 +00009373 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009374
Nithin Sujir328947f2013-05-23 11:11:24 +00009375 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009376
9377 /* Disable interrupts */
9378 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009379 tp->napi[0].chk_msi_cnt = 0;
9380 tp->napi[0].last_rx_cons = 0;
9381 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009382
9383 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009384 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009385 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009386 tp->napi[i].tx_prod = 0;
9387 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009388 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009389 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009390 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9391 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009392 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009393 tp->napi[i].last_rx_cons = 0;
9394 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009395 }
Joe Perches63c3a662011-04-26 08:12:10 +00009396 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009397 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009398 } else {
9399 tp->napi[0].tx_prod = 0;
9400 tp->napi[0].tx_cons = 0;
9401 tw32_mailbox(tp->napi[0].prodmbox, 0);
9402 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9403 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009404
9405 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009406 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009407 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9408 for (i = 0; i < 16; i++)
9409 tw32_tx_mbox(mbox + i * 8, 0);
9410 }
9411
Matt Carlson2d31eca2009-09-01 12:53:31 +00009412 /* Clear status block in ram. */
9413 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9414
9415 /* Set status block DMA address */
9416 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9417 ((u64) tnapi->status_mapping >> 32));
9418 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9419 ((u64) tnapi->status_mapping & 0xffffffff));
9420
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009421 stblk = HOSTCC_STATBLCK_RING1;
9422
9423 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9424 u64 mapping = (u64)tnapi->status_mapping;
9425 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9426 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009427 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009428
9429 /* Clear status block in ram. */
9430 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009431 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009432
9433 tg3_tx_rcbs_init(tp);
9434 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009435}
9436
Matt Carlsoneb07a942011-04-20 07:57:36 +00009437static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9438{
9439 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9440
Joe Perches63c3a662011-04-26 08:12:10 +00009441 if (!tg3_flag(tp, 5750_PLUS) ||
9442 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009443 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9444 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009445 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009446 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009447 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9448 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009449 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9450 else
9451 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9452
9453 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9454 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9455
9456 val = min(nic_rep_thresh, host_rep_thresh);
9457 tw32(RCVBDI_STD_THRESH, val);
9458
Joe Perches63c3a662011-04-26 08:12:10 +00009459 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009460 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9461
Joe Perches63c3a662011-04-26 08:12:10 +00009462 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009463 return;
9464
Matt Carlson513aa6e2011-11-21 15:01:18 +00009465 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009466
9467 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9468
9469 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9470 tw32(RCVBDI_JUMBO_THRESH, val);
9471
Joe Perches63c3a662011-04-26 08:12:10 +00009472 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009473 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9474}
9475
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009476static inline u32 calc_crc(unsigned char *buf, int len)
9477{
9478 u32 reg;
9479 u32 tmp;
9480 int j, k;
9481
9482 reg = 0xffffffff;
9483
9484 for (j = 0; j < len; j++) {
9485 reg ^= buf[j];
9486
9487 for (k = 0; k < 8; k++) {
9488 tmp = reg & 0x01;
9489
9490 reg >>= 1;
9491
9492 if (tmp)
9493 reg ^= 0xedb88320;
9494 }
9495 }
9496
9497 return ~reg;
9498}
9499
9500static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9501{
9502 /* accept or reject all multicast frames */
9503 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9504 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9505 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9506 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9507}
9508
9509static void __tg3_set_rx_mode(struct net_device *dev)
9510{
9511 struct tg3 *tp = netdev_priv(dev);
9512 u32 rx_mode;
9513
9514 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9515 RX_MODE_KEEP_VLAN_TAG);
9516
9517#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9518 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9519 * flag clear.
9520 */
9521 if (!tg3_flag(tp, ENABLE_ASF))
9522 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9523#endif
9524
9525 if (dev->flags & IFF_PROMISC) {
9526 /* Promiscuous mode. */
9527 rx_mode |= RX_MODE_PROMISC;
9528 } else if (dev->flags & IFF_ALLMULTI) {
9529 /* Accept all multicast. */
9530 tg3_set_multi(tp, 1);
9531 } else if (netdev_mc_empty(dev)) {
9532 /* Reject all multicast. */
9533 tg3_set_multi(tp, 0);
9534 } else {
9535 /* Accept one or more multicast(s). */
9536 struct netdev_hw_addr *ha;
9537 u32 mc_filter[4] = { 0, };
9538 u32 regidx;
9539 u32 bit;
9540 u32 crc;
9541
9542 netdev_for_each_mc_addr(ha, dev) {
9543 crc = calc_crc(ha->addr, ETH_ALEN);
9544 bit = ~crc & 0x7f;
9545 regidx = (bit & 0x60) >> 5;
9546 bit &= 0x1f;
9547 mc_filter[regidx] |= (1 << bit);
9548 }
9549
9550 tw32(MAC_HASH_REG_0, mc_filter[0]);
9551 tw32(MAC_HASH_REG_1, mc_filter[1]);
9552 tw32(MAC_HASH_REG_2, mc_filter[2]);
9553 tw32(MAC_HASH_REG_3, mc_filter[3]);
9554 }
9555
9556 if (rx_mode != tp->rx_mode) {
9557 tp->rx_mode = rx_mode;
9558 tw32_f(MAC_RX_MODE, rx_mode);
9559 udelay(10);
9560 }
9561}
9562
Michael Chan91024262012-09-28 07:12:38 +00009563static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009564{
9565 int i;
9566
9567 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009568 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009569}
9570
9571static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009572{
9573 int i;
9574
9575 if (!tg3_flag(tp, SUPPORT_MSIX))
9576 return;
9577
Michael Chan0b3ba052012-11-14 14:44:29 +00009578 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009579 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009580 return;
9581 }
9582
9583 /* Validate table against current IRQ count */
9584 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009585 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009586 break;
9587 }
9588
9589 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009590 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009591}
9592
Matt Carlson90415472011-12-16 13:33:23 +00009593static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009594{
9595 int i = 0;
9596 u32 reg = MAC_RSS_INDIR_TBL_0;
9597
9598 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9599 u32 val = tp->rss_ind_tbl[i];
9600 i++;
9601 for (; i % 8; i++) {
9602 val <<= 4;
9603 val |= tp->rss_ind_tbl[i];
9604 }
9605 tw32(reg, val);
9606 reg += 4;
9607 }
9608}
9609
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009610static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9611{
9612 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9613 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9614 else
9615 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9616}
9617
Matt Carlson2d31eca2009-09-01 12:53:31 +00009618/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009619static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009620{
9621 u32 val, rdmac_mode;
9622 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009623 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624
9625 tg3_disable_ints(tp);
9626
9627 tg3_stop_fw(tp);
9628
9629 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9630
Joe Perches63c3a662011-04-26 08:12:10 +00009631 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009632 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009633
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009634 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9635 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9636 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009637 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009638 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9639 }
9640
Nithin Sujir400dfba2013-05-18 06:26:53 +00009641 /* Enable MAC control of LPI */
9642 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9643 tg3_setup_eee(tp);
9644
Matt Carlson603f1172010-02-12 14:47:10 +00009645 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009646 tg3_phy_reset(tp);
9647
Linus Torvalds1da177e2005-04-16 15:20:36 -07009648 err = tg3_chip_reset(tp);
9649 if (err)
9650 return err;
9651
9652 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9653
Joe Perches41535772013-02-16 11:20:04 +00009654 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009655 val = tr32(TG3_CPMU_CTRL);
9656 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9657 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009658
9659 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9660 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9661 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9662 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9663
9664 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9665 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9666 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9667 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9668
9669 val = tr32(TG3_CPMU_HST_ACC);
9670 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9671 val |= CPMU_HST_ACC_MACCLK_6_25;
9672 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009673 }
9674
Joe Perches41535772013-02-16 11:20:04 +00009675 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009676 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9677 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9678 PCIE_PWR_MGMT_L1_THRESH_4MS;
9679 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009680
9681 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9682 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9683
9684 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009685
Matt Carlsonf40386c2009-11-02 14:24:02 +00009686 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9687 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009688 }
9689
Joe Perches63c3a662011-04-26 08:12:10 +00009690 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009691 u32 grc_mode = tr32(GRC_MODE);
9692
9693 /* Access the lower 1K of PL PCIE block registers. */
9694 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9695 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9696
9697 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9698 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9699 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9700
9701 tw32(GRC_MODE, grc_mode);
9702 }
9703
Matt Carlson55086ad2011-12-14 11:09:59 +00009704 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009705 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009706 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009707
Matt Carlson5093eed2010-11-24 08:31:45 +00009708 /* Access the lower 1K of PL PCIE block registers. */
9709 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9710 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009711
Matt Carlson5093eed2010-11-24 08:31:45 +00009712 val = tr32(TG3_PCIE_TLDLPL_PORT +
9713 TG3_PCIE_PL_LO_PHYCTL5);
9714 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9715 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009716
Matt Carlson5093eed2010-11-24 08:31:45 +00009717 tw32(GRC_MODE, grc_mode);
9718 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009719
Joe Perches41535772013-02-16 11:20:04 +00009720 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009721 u32 grc_mode;
9722
9723 /* Fix transmit hangs */
9724 val = tr32(TG3_CPMU_PADRNG_CTL);
9725 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9726 tw32(TG3_CPMU_PADRNG_CTL, val);
9727
9728 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009729
9730 /* Access the lower 1K of DL PCIE block registers. */
9731 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9732 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9733
9734 val = tr32(TG3_PCIE_TLDLPL_PORT +
9735 TG3_PCIE_DL_LO_FTSMAX);
9736 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9737 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9738 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9739
9740 tw32(GRC_MODE, grc_mode);
9741 }
9742
Matt Carlsona977dbe2010-04-12 06:58:26 +00009743 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9744 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9745 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9746 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009747 }
9748
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749 /* This works around an issue with Athlon chipsets on
9750 * B3 tigon3 silicon. This bit has no effect on any
9751 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009752 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753 */
Joe Perches63c3a662011-04-26 08:12:10 +00009754 if (!tg3_flag(tp, CPMU_PRESENT)) {
9755 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009756 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9757 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759
Joe Perches41535772013-02-16 11:20:04 +00009760 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009761 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762 val = tr32(TG3PCI_PCISTATE);
9763 val |= PCISTATE_RETRY_SAME_DMA;
9764 tw32(TG3PCI_PCISTATE, val);
9765 }
9766
Joe Perches63c3a662011-04-26 08:12:10 +00009767 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009768 /* Allow reads and writes to the
9769 * APE register and memory space.
9770 */
9771 val = tr32(TG3PCI_PCISTATE);
9772 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009773 PCISTATE_ALLOW_APE_SHMEM_WR |
9774 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009775 tw32(TG3PCI_PCISTATE, val);
9776 }
9777
Joe Perches41535772013-02-16 11:20:04 +00009778 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009779 /* Enable some hw fixes. */
9780 val = tr32(TG3PCI_MSI_DATA);
9781 val |= (1 << 26) | (1 << 28) | (1 << 29);
9782 tw32(TG3PCI_MSI_DATA, val);
9783 }
9784
9785 /* Descriptor ring init may make accesses to the
9786 * NIC SRAM area to setup the TX descriptors, so we
9787 * can only do this after the hardware has been
9788 * successfully reset.
9789 */
Michael Chan32d8c572006-07-25 16:38:29 -07009790 err = tg3_init_rings(tp);
9791 if (err)
9792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793
Joe Perches63c3a662011-04-26 08:12:10 +00009794 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009795 val = tr32(TG3PCI_DMA_RW_CTRL) &
9796 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009797 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009798 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009799 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009800 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9801 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009802 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009803 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009804 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9805 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009806 /* This value is determined during the probe time DMA
9807 * engine test, tg3_test_dma.
9808 */
9809 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811
9812 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9813 GRC_MODE_4X_NIC_SEND_RINGS |
9814 GRC_MODE_NO_TX_PHDR_CSUM |
9815 GRC_MODE_NO_RX_PHDR_CSUM);
9816 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009817
9818 /* Pseudo-header checksum is done by hardware logic and not
9819 * the offload processers, so make the chip do the pseudo-
9820 * header checksums on receive. For transmit it is more
9821 * convenient to do the pseudo-header checksum in software
9822 * as Linux does that on transmit for us in all cases.
9823 */
9824 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009826 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9827 if (tp->rxptpctl)
9828 tw32(TG3_RX_PTP_CTL,
9829 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9830
9831 if (tg3_flag(tp, PTP_CAPABLE))
9832 val |= GRC_MODE_TIME_SYNC_ENABLE;
9833
9834 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835
9836 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9837 val = tr32(GRC_MISC_CFG);
9838 val &= ~0xff;
9839 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9840 tw32(GRC_MISC_CFG, val);
9841
9842 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009843 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009845 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009847 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9849 else
9850 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9851 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9852 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009853 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854 int fw_len;
9855
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009856 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009857 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9858 tw32(BUFMGR_MB_POOL_ADDR,
9859 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9860 tw32(BUFMGR_MB_POOL_SIZE,
9861 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863
Michael Chan0f893dc2005-07-25 12:30:38 -07009864 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9866 tp->bufmgr_config.mbuf_read_dma_low_water);
9867 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9868 tp->bufmgr_config.mbuf_mac_rx_low_water);
9869 tw32(BUFMGR_MB_HIGH_WATER,
9870 tp->bufmgr_config.mbuf_high_water);
9871 } else {
9872 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9873 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9874 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9875 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9876 tw32(BUFMGR_MB_HIGH_WATER,
9877 tp->bufmgr_config.mbuf_high_water_jumbo);
9878 }
9879 tw32(BUFMGR_DMA_LOW_WATER,
9880 tp->bufmgr_config.dma_low_water);
9881 tw32(BUFMGR_DMA_HIGH_WATER,
9882 tp->bufmgr_config.dma_high_water);
9883
Matt Carlsond309a462010-09-30 10:34:31 +00009884 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009885 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009886 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009887 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9888 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9889 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009890 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009891 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009892 for (i = 0; i < 2000; i++) {
9893 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9894 break;
9895 udelay(10);
9896 }
9897 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009898 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009899 return -ENODEV;
9900 }
9901
Joe Perches41535772013-02-16 11:20:04 +00009902 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009903 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009904
Matt Carlsoneb07a942011-04-20 07:57:36 +00009905 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009906
9907 /* Initialize TG3_BDINFO's at:
9908 * RCVDBDI_STD_BD: standard eth size rx ring
9909 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9910 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9911 *
9912 * like so:
9913 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9914 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9915 * ring attribute flags
9916 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9917 *
9918 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9919 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9920 *
9921 * The size of each ring is fixed in the firmware, but the location is
9922 * configurable.
9923 */
9924 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009925 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009926 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009927 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009928 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009929 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9930 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009932 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009933 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009934 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9935 BDINFO_FLAGS_DISABLED);
9936
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009937 /* Program the jumbo buffer descriptor ring control
9938 * blocks on those devices that have them.
9939 */
Joe Perches41535772013-02-16 11:20:04 +00009940 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009941 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009942
Joe Perches63c3a662011-04-26 08:12:10 +00009943 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009945 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009947 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009948 val = TG3_RX_JMB_RING_SIZE(tp) <<
9949 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009951 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009952 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009953 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009954 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009955 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9956 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009957 } else {
9958 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9959 BDINFO_FLAGS_DISABLED);
9960 }
9961
Joe Perches63c3a662011-04-26 08:12:10 +00009962 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009963 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009964 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9965 val |= (TG3_RX_STD_DMA_SZ << 2);
9966 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009967 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009968 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009969 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009970
9971 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972
Matt Carlson411da642009-11-13 13:03:46 +00009973 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009974 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975
Joe Perches63c3a662011-04-26 08:12:10 +00009976 tpr->rx_jmb_prod_idx =
9977 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009978 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009979
Matt Carlson2d31eca2009-09-01 12:53:31 +00009980 tg3_rings_reset(tp);
9981
Linus Torvalds1da177e2005-04-16 15:20:36 -07009982 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +00009983 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984
9985 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009986 tw32(MAC_RX_MTU_SIZE,
9987 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009988
9989 /* The slot time is changed by tg3_setup_phy if we
9990 * run at gigabit with half duplex.
9991 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009992 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9993 (6 << TX_LENGTHS_IPG_SHIFT) |
9994 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9995
Joe Perches41535772013-02-16 11:20:04 +00009996 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9997 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009998 val |= tr32(MAC_TX_LENGTHS) &
9999 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10000 TX_LENGTHS_CNT_DWN_VAL_MSK);
10001
10002 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010003
10004 /* Receive rules. */
10005 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10006 tw32(RCVLPC_CONFIG, 0x0181);
10007
10008 /* Calculate RDMAC_MODE setting early, we need it to determine
10009 * the RCVLPC_STATE_ENABLE mask.
10010 */
10011 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10012 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10013 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10014 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10015 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010016
Joe Perches41535772013-02-16 11:20:04 +000010017 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010018 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10019
Joe Perches41535772013-02-16 11:20:04 +000010020 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10021 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10022 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010023 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10024 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10025 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10026
Joe Perches41535772013-02-16 11:20:04 +000010027 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10028 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010029 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010030 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010031 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10032 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010033 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10035 }
10036 }
10037
Joe Perches63c3a662011-04-26 08:12:10 +000010038 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010039 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10040
Joe Perches41535772013-02-16 11:20:04 +000010041 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010042 tp->dma_limit = 0;
10043 if (tp->dev->mtu <= ETH_DATA_LEN) {
10044 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10045 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10046 }
10047 }
10048
Joe Perches63c3a662011-04-26 08:12:10 +000010049 if (tg3_flag(tp, HW_TSO_1) ||
10050 tg3_flag(tp, HW_TSO_2) ||
10051 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010052 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10053
Matt Carlson108a6c12011-05-19 12:12:47 +000010054 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010055 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10056 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010057 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058
Joe Perches41535772013-02-16 11:20:04 +000010059 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10060 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010061 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10062
Joe Perches41535772013-02-16 11:20:04 +000010063 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10064 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10065 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10066 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010067 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010068 u32 tgtreg;
10069
Joe Perches41535772013-02-16 11:20:04 +000010070 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010071 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10072 else
10073 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10074
10075 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010076 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10077 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010078 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10079 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10080 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10081 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10082 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10083 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010084 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010085 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010086 }
10087
Joe Perches41535772013-02-16 11:20:04 +000010088 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10089 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10090 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010091 u32 tgtreg;
10092
Joe Perches41535772013-02-16 11:20:04 +000010093 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010094 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10095 else
10096 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10097
10098 val = tr32(tgtreg);
10099 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010100 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10101 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10102 }
10103
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010105 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010106 val = tr32(RCVLPC_STATS_ENABLE);
10107 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10108 tw32(RCVLPC_STATS_ENABLE, val);
10109 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010110 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 val = tr32(RCVLPC_STATS_ENABLE);
10112 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10113 tw32(RCVLPC_STATS_ENABLE, val);
10114 } else {
10115 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10116 }
10117 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10118 tw32(SNDDATAI_STATSENAB, 0xffffff);
10119 tw32(SNDDATAI_STATSCTRL,
10120 (SNDDATAI_SCTRL_ENABLE |
10121 SNDDATAI_SCTRL_FASTUPD));
10122
10123 /* Setup host coalescing engine. */
10124 tw32(HOSTCC_MODE, 0);
10125 for (i = 0; i < 2000; i++) {
10126 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10127 break;
10128 udelay(10);
10129 }
10130
Michael Chand244c892005-07-05 14:42:33 -070010131 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132
Joe Perches63c3a662011-04-26 08:12:10 +000010133 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010134 /* Status/statistics block address. See tg3_timer,
10135 * the tg3_periodic_fetch_stats call there, and
10136 * tg3_get_stats to see how this works for 5705/5750 chips.
10137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10139 ((u64) tp->stats_mapping >> 32));
10140 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10141 ((u64) tp->stats_mapping & 0xffffffff));
10142 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010143
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010145
10146 /* Clear statistics and status block memory areas */
10147 for (i = NIC_SRAM_STATS_BLK;
10148 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10149 i += sizeof(u32)) {
10150 tg3_write_mem(tp, i, 0);
10151 udelay(40);
10152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153 }
10154
10155 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10156
10157 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10158 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010159 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10161
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010162 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10163 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010164 /* reset to prevent losing 1st rx packet intermittently */
10165 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10166 udelay(10);
10167 }
10168
Matt Carlson3bda1252008-08-15 14:08:22 -070010169 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010170 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10171 MAC_MODE_FHDE_ENABLE;
10172 if (tg3_flag(tp, ENABLE_APE))
10173 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010174 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010175 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010176 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010177 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10179 udelay(40);
10180
Michael Chan314fba32005-04-21 17:07:04 -070010181 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010182 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010183 * register to preserve the GPIO settings for LOMs. The GPIOs,
10184 * whether used as inputs or outputs, are set by boot code after
10185 * reset.
10186 */
Joe Perches63c3a662011-04-26 08:12:10 +000010187 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010188 u32 gpio_mask;
10189
Michael Chan9d26e212006-12-07 00:21:14 -080010190 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10191 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10192 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010193
Joe Perches41535772013-02-16 11:20:04 +000010194 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010195 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10196 GRC_LCLCTRL_GPIO_OUTPUT3;
10197
Joe Perches41535772013-02-16 11:20:04 +000010198 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010199 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10200
Gary Zambranoaaf84462007-05-05 11:51:45 -070010201 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010202 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10203
10204 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010205 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010206 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10207 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10210 udelay(100);
10211
Matt Carlsonc3b50032012-01-17 15:27:23 +000010212 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010213 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010214 val |= MSGINT_MODE_ENABLE;
10215 if (tp->irq_cnt > 1)
10216 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010217 if (!tg3_flag(tp, 1SHOT_MSI))
10218 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010219 tw32(MSGINT_MODE, val);
10220 }
10221
Joe Perches63c3a662011-04-26 08:12:10 +000010222 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10224 udelay(40);
10225 }
10226
10227 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10228 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10229 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10230 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10231 WDMAC_MODE_LNGREAD_ENAB);
10232
Joe Perches41535772013-02-16 11:20:04 +000010233 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10234 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010235 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010236 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10237 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238 /* nothing */
10239 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010240 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 val |= WDMAC_MODE_RX_ACCEL;
10242 }
10243 }
10244
Michael Chand9ab5ad12006-03-20 22:27:35 -080010245 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010246 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010247 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010248
Joe Perches41535772013-02-16 11:20:04 +000010249 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010250 val |= WDMAC_MODE_BURST_ALL_DATA;
10251
Linus Torvalds1da177e2005-04-16 15:20:36 -070010252 tw32_f(WDMAC_MODE, val);
10253 udelay(40);
10254
Joe Perches63c3a662011-04-26 08:12:10 +000010255 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010256 u16 pcix_cmd;
10257
10258 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10259 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010260 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010261 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10262 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010263 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010264 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10265 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010266 }
Matt Carlson9974a352007-10-07 23:27:28 -070010267 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10268 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010269 }
10270
10271 tw32_f(RDMAC_MODE, rdmac_mode);
10272 udelay(40);
10273
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010274 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10275 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010276 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10277 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10278 break;
10279 }
10280 if (i < TG3_NUM_RDMA_CHANNELS) {
10281 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010282 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010283 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010284 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010285 }
10286 }
10287
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010289 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010291
Joe Perches41535772013-02-16 11:20:04 +000010292 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010293 tw32(SNDDATAC_MODE,
10294 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10295 else
10296 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10297
Linus Torvalds1da177e2005-04-16 15:20:36 -070010298 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10299 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010300 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010301 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010302 val |= RCVDBDI_MODE_LRG_RING_SZ;
10303 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010304 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010305 if (tg3_flag(tp, HW_TSO_1) ||
10306 tg3_flag(tp, HW_TSO_2) ||
10307 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010308 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010309 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010310 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010311 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10312 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10314
Joe Perches41535772013-02-16 11:20:04 +000010315 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316 err = tg3_load_5701_a0_firmware_fix(tp);
10317 if (err)
10318 return err;
10319 }
10320
Nithin Sujirc4dab502013-03-06 17:02:34 +000010321 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10322 /* Ignore any errors for the firmware download. If download
10323 * fails, the device will operate with EEE disabled
10324 */
10325 tg3_load_57766_firmware(tp);
10326 }
10327
Joe Perches63c3a662011-04-26 08:12:10 +000010328 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010329 err = tg3_load_tso_firmware(tp);
10330 if (err)
10331 return err;
10332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010333
10334 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010335
Joe Perches63c3a662011-04-26 08:12:10 +000010336 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010337 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010338 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010339
Joe Perches41535772013-02-16 11:20:04 +000010340 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10341 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010342 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10343 tp->tx_mode &= ~val;
10344 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10345 }
10346
Linus Torvalds1da177e2005-04-16 15:20:36 -070010347 tw32_f(MAC_TX_MODE, tp->tx_mode);
10348 udelay(100);
10349
Joe Perches63c3a662011-04-26 08:12:10 +000010350 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010351 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010352
10353 /* Setup the "secret" hash key. */
10354 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10355 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10356 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10357 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10358 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10359 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10360 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10361 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10362 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10363 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10364 }
10365
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010367 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010368 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10369
Joe Perches63c3a662011-04-26 08:12:10 +000010370 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010371 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10372 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10373 RX_MODE_RSS_IPV6_HASH_EN |
10374 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10375 RX_MODE_RSS_IPV4_HASH_EN |
10376 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10377
Linus Torvalds1da177e2005-04-16 15:20:36 -070010378 tw32_f(MAC_RX_MODE, tp->rx_mode);
10379 udelay(10);
10380
Linus Torvalds1da177e2005-04-16 15:20:36 -070010381 tw32(MAC_LED_CTRL, tp->led_ctrl);
10382
10383 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010384 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010385 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10386 udelay(10);
10387 }
10388 tw32_f(MAC_RX_MODE, tp->rx_mode);
10389 udelay(10);
10390
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010391 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010392 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10393 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394 /* Set drive transmission level to 1.2V */
10395 /* only if the signal pre-emphasis bit is not set */
10396 val = tr32(MAC_SERDES_CFG);
10397 val &= 0xfffff000;
10398 val |= 0x880;
10399 tw32(MAC_SERDES_CFG, val);
10400 }
Joe Perches41535772013-02-16 11:20:04 +000010401 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010402 tw32(MAC_SERDES_CFG, 0x616000);
10403 }
10404
10405 /* Prevent chip from dropping frames when flow control
10406 * is enabled.
10407 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010408 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010409 val = 1;
10410 else
10411 val = 2;
10412 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413
Joe Perches41535772013-02-16 11:20:04 +000010414 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010415 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010416 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010417 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010418 }
10419
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010420 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010421 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010422 u32 tmp;
10423
10424 tmp = tr32(SERDES_RX_CTRL);
10425 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10426 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10427 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10428 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10429 }
10430
Joe Perches63c3a662011-04-26 08:12:10 +000010431 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010432 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010433 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010434
Joe Perches953c96e2013-04-09 10:18:14 +000010435 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010436 if (err)
10437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010439 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10440 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010441 u32 tmp;
10442
10443 /* Clear CRC stats. */
10444 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10445 tg3_writephy(tp, MII_TG3_TEST1,
10446 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010447 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010449 }
10450 }
10451
10452 __tg3_set_rx_mode(tp->dev);
10453
10454 /* Initialize receive rules. */
10455 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10456 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10457 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10458 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10459
Joe Perches63c3a662011-04-26 08:12:10 +000010460 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461 limit = 8;
10462 else
10463 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010464 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465 limit -= 4;
10466 switch (limit) {
10467 case 16:
10468 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10469 case 15:
10470 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10471 case 14:
10472 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10473 case 13:
10474 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10475 case 12:
10476 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10477 case 11:
10478 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10479 case 10:
10480 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10481 case 9:
10482 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10483 case 8:
10484 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10485 case 7:
10486 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10487 case 6:
10488 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10489 case 5:
10490 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10491 case 4:
10492 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10493 case 3:
10494 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10495 case 2:
10496 case 1:
10497
10498 default:
10499 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010501
Joe Perches63c3a662011-04-26 08:12:10 +000010502 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010503 /* Write our heartbeat update interval to APE. */
10504 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10505 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010506
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10508
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509 return 0;
10510}
10511
10512/* Called at device open time to get the chip ready for
10513 * packet processing. Invoked with tp->lock held.
10514 */
Joe Perches953c96e2013-04-09 10:18:14 +000010515static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010516{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010517 /* Chip may have been just powered on. If so, the boot code may still
10518 * be running initialization. Wait for it to finish to avoid races in
10519 * accessing the hardware.
10520 */
10521 tg3_enable_register_access(tp);
10522 tg3_poll_fw(tp);
10523
Linus Torvalds1da177e2005-04-16 15:20:36 -070010524 tg3_switch_clocks(tp);
10525
10526 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10527
Matt Carlson2f751b62008-08-04 23:17:34 -070010528 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010529}
10530
Michael Chanaed93e02012-07-16 16:24:02 +000010531static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10532{
10533 int i;
10534
10535 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10536 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10537
10538 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10539 off += len;
10540
10541 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10542 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10543 memset(ocir, 0, TG3_OCIR_LEN);
10544 }
10545}
10546
10547/* sysfs attributes for hwmon */
10548static ssize_t tg3_show_temp(struct device *dev,
10549 struct device_attribute *devattr, char *buf)
10550{
10551 struct pci_dev *pdev = to_pci_dev(dev);
10552 struct net_device *netdev = pci_get_drvdata(pdev);
10553 struct tg3 *tp = netdev_priv(netdev);
10554 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10555 u32 temperature;
10556
10557 spin_lock_bh(&tp->lock);
10558 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10559 sizeof(temperature));
10560 spin_unlock_bh(&tp->lock);
10561 return sprintf(buf, "%u\n", temperature);
10562}
10563
10564
10565static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10566 TG3_TEMP_SENSOR_OFFSET);
10567static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10568 TG3_TEMP_CAUTION_OFFSET);
10569static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10570 TG3_TEMP_MAX_OFFSET);
10571
10572static struct attribute *tg3_attributes[] = {
10573 &sensor_dev_attr_temp1_input.dev_attr.attr,
10574 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10575 &sensor_dev_attr_temp1_max.dev_attr.attr,
10576 NULL
10577};
10578
10579static const struct attribute_group tg3_group = {
10580 .attrs = tg3_attributes,
10581};
10582
Michael Chanaed93e02012-07-16 16:24:02 +000010583static void tg3_hwmon_close(struct tg3 *tp)
10584{
Michael Chanaed93e02012-07-16 16:24:02 +000010585 if (tp->hwmon_dev) {
10586 hwmon_device_unregister(tp->hwmon_dev);
10587 tp->hwmon_dev = NULL;
10588 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10589 }
Michael Chanaed93e02012-07-16 16:24:02 +000010590}
10591
10592static void tg3_hwmon_open(struct tg3 *tp)
10593{
Michael Chanaed93e02012-07-16 16:24:02 +000010594 int i, err;
10595 u32 size = 0;
10596 struct pci_dev *pdev = tp->pdev;
10597 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10598
10599 tg3_sd_scan_scratchpad(tp, ocirs);
10600
10601 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10602 if (!ocirs[i].src_data_length)
10603 continue;
10604
10605 size += ocirs[i].src_hdr_length;
10606 size += ocirs[i].src_data_length;
10607 }
10608
10609 if (!size)
10610 return;
10611
10612 /* Register hwmon sysfs hooks */
10613 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10614 if (err) {
10615 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10616 return;
10617 }
10618
10619 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10620 if (IS_ERR(tp->hwmon_dev)) {
10621 tp->hwmon_dev = NULL;
10622 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10623 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10624 }
Michael Chanaed93e02012-07-16 16:24:02 +000010625}
10626
10627
Linus Torvalds1da177e2005-04-16 15:20:36 -070010628#define TG3_STAT_ADD32(PSTAT, REG) \
10629do { u32 __val = tr32(REG); \
10630 (PSTAT)->low += __val; \
10631 if ((PSTAT)->low < __val) \
10632 (PSTAT)->high += 1; \
10633} while (0)
10634
10635static void tg3_periodic_fetch_stats(struct tg3 *tp)
10636{
10637 struct tg3_hw_stats *sp = tp->hw_stats;
10638
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010639 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010640 return;
10641
10642 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10643 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10644 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10645 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10646 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10647 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10648 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10649 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10650 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10651 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10652 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10653 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10654 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010655 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010656 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10657 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10658 u32 val;
10659
10660 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010661 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010662 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010663 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010665
10666 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10667 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10668 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10669 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10670 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10671 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10672 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10673 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10674 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10675 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10676 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10677 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10678 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10679 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010680
10681 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010682 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10683 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10684 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010685 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10686 } else {
10687 u32 val = tr32(HOSTCC_FLOW_ATTN);
10688 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10689 if (val) {
10690 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10691 sp->rx_discards.low += val;
10692 if (sp->rx_discards.low < val)
10693 sp->rx_discards.high += 1;
10694 }
10695 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10696 }
Michael Chan463d3052006-05-22 16:36:27 -070010697 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010698}
10699
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010700static void tg3_chk_missed_msi(struct tg3 *tp)
10701{
10702 u32 i;
10703
10704 for (i = 0; i < tp->irq_cnt; i++) {
10705 struct tg3_napi *tnapi = &tp->napi[i];
10706
10707 if (tg3_has_work(tnapi)) {
10708 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10709 tnapi->last_tx_cons == tnapi->tx_cons) {
10710 if (tnapi->chk_msi_cnt < 1) {
10711 tnapi->chk_msi_cnt++;
10712 return;
10713 }
Matt Carlson7f230732011-08-31 11:44:48 +000010714 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010715 }
10716 }
10717 tnapi->chk_msi_cnt = 0;
10718 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10719 tnapi->last_tx_cons = tnapi->tx_cons;
10720 }
10721}
10722
Linus Torvalds1da177e2005-04-16 15:20:36 -070010723static void tg3_timer(unsigned long __opaque)
10724{
10725 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010726
Matt Carlson5b190622011-11-04 09:15:04 +000010727 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010728 goto restart_timer;
10729
David S. Millerf47c11e2005-06-24 20:18:35 -070010730 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010731
Joe Perches41535772013-02-16 11:20:04 +000010732 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010733 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010734 tg3_chk_missed_msi(tp);
10735
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010736 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10737 /* BCM4785: Flush posted writes from GbE to host memory. */
10738 tr32(HOSTCC_MODE);
10739 }
10740
Joe Perches63c3a662011-04-26 08:12:10 +000010741 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010742 /* All of this garbage is because when using non-tagged
10743 * IRQ status the mailbox/status_block protocol the chip
10744 * uses with the cpu is race prone.
10745 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010746 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010747 tw32(GRC_LOCAL_CTRL,
10748 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10749 } else {
10750 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010751 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010753
David S. Millerfac9b832005-05-18 22:46:34 -070010754 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010755 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010756 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010757 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010759 }
10760
Linus Torvalds1da177e2005-04-16 15:20:36 -070010761 /* This part only runs once per second. */
10762 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010763 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010764 tg3_periodic_fetch_stats(tp);
10765
Matt Carlsonb0c59432011-05-19 12:12:48 +000010766 if (tp->setlpicnt && !--tp->setlpicnt)
10767 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010768
Joe Perches63c3a662011-04-26 08:12:10 +000010769 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010770 u32 mac_stat;
10771 int phy_event;
10772
10773 mac_stat = tr32(MAC_STATUS);
10774
10775 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010776 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010777 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10778 phy_event = 1;
10779 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10780 phy_event = 1;
10781
10782 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010783 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010784 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010785 u32 mac_stat = tr32(MAC_STATUS);
10786 int need_setup = 0;
10787
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010788 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010789 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10790 need_setup = 1;
10791 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010792 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010793 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10794 MAC_STATUS_SIGNAL_DET))) {
10795 need_setup = 1;
10796 }
10797 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010798 if (!tp->serdes_counter) {
10799 tw32_f(MAC_MODE,
10800 (tp->mac_mode &
10801 ~MAC_MODE_PORT_MODE_MASK));
10802 udelay(40);
10803 tw32_f(MAC_MODE, tp->mac_mode);
10804 udelay(40);
10805 }
Joe Perches953c96e2013-04-09 10:18:14 +000010806 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010807 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010808 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010809 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010810 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010812
10813 tp->timer_counter = tp->timer_multiplier;
10814 }
10815
Michael Chan130b8e42006-09-27 16:00:40 -070010816 /* Heartbeat is only sent once every 2 seconds.
10817 *
10818 * The heartbeat is to tell the ASF firmware that the host
10819 * driver is still alive. In the event that the OS crashes,
10820 * ASF needs to reset the hardware to free up the FIFO space
10821 * that may be filled with rx packets destined for the host.
10822 * If the FIFO is full, ASF will no longer function properly.
10823 *
10824 * Unintended resets have been reported on real time kernels
10825 * where the timer doesn't run on time. Netpoll will also have
10826 * same problem.
10827 *
10828 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10829 * to check the ring condition when the heartbeat is expiring
10830 * before doing the reset. This will prevent most unintended
10831 * resets.
10832 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010833 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010834 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010835 tg3_wait_for_event_ack(tp);
10836
Michael Chanbbadf502006-04-06 21:46:34 -070010837 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010838 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010839 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010840 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10841 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010842
10843 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010844 }
10845 tp->asf_counter = tp->asf_multiplier;
10846 }
10847
David S. Millerf47c11e2005-06-24 20:18:35 -070010848 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010849
Michael Chanf475f162006-03-27 23:20:14 -080010850restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010851 tp->timer.expires = jiffies + tp->timer_offset;
10852 add_timer(&tp->timer);
10853}
10854
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010855static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010856{
10857 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010858 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010859 !tg3_flag(tp, 57765_CLASS))
10860 tp->timer_offset = HZ;
10861 else
10862 tp->timer_offset = HZ / 10;
10863
10864 BUG_ON(tp->timer_offset > HZ);
10865
10866 tp->timer_multiplier = (HZ / tp->timer_offset);
10867 tp->asf_multiplier = (HZ / tp->timer_offset) *
10868 TG3_FW_UPDATE_FREQ_SEC;
10869
10870 init_timer(&tp->timer);
10871 tp->timer.data = (unsigned long) tp;
10872 tp->timer.function = tg3_timer;
10873}
10874
10875static void tg3_timer_start(struct tg3 *tp)
10876{
10877 tp->asf_counter = tp->asf_multiplier;
10878 tp->timer_counter = tp->timer_multiplier;
10879
10880 tp->timer.expires = jiffies + tp->timer_offset;
10881 add_timer(&tp->timer);
10882}
10883
10884static void tg3_timer_stop(struct tg3 *tp)
10885{
10886 del_timer_sync(&tp->timer);
10887}
10888
10889/* Restart hardware after configuration changes, self-test, etc.
10890 * Invoked with tp->lock held.
10891 */
Joe Perches953c96e2013-04-09 10:18:14 +000010892static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000010893 __releases(tp->lock)
10894 __acquires(tp->lock)
10895{
10896 int err;
10897
10898 err = tg3_init_hw(tp, reset_phy);
10899 if (err) {
10900 netdev_err(tp->dev,
10901 "Failed to re-initialize device, aborting\n");
10902 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10903 tg3_full_unlock(tp);
10904 tg3_timer_stop(tp);
10905 tp->irq_sync = 0;
10906 tg3_napi_enable(tp);
10907 dev_close(tp->dev);
10908 tg3_full_lock(tp, 0);
10909 }
10910 return err;
10911}
10912
10913static void tg3_reset_task(struct work_struct *work)
10914{
10915 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10916 int err;
10917
10918 tg3_full_lock(tp, 0);
10919
10920 if (!netif_running(tp->dev)) {
10921 tg3_flag_clear(tp, RESET_TASK_PENDING);
10922 tg3_full_unlock(tp);
10923 return;
10924 }
10925
10926 tg3_full_unlock(tp);
10927
10928 tg3_phy_stop(tp);
10929
10930 tg3_netif_stop(tp);
10931
10932 tg3_full_lock(tp, 1);
10933
10934 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10935 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10936 tp->write32_rx_mbox = tg3_write_flush_reg32;
10937 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10938 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10939 }
10940
10941 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000010942 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000010943 if (err)
10944 goto out;
10945
10946 tg3_netif_start(tp);
10947
10948out:
10949 tg3_full_unlock(tp);
10950
10951 if (!err)
10952 tg3_phy_start(tp);
10953
10954 tg3_flag_clear(tp, RESET_TASK_PENDING);
10955}
10956
Matt Carlson4f125f42009-09-01 12:55:02 +000010957static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010958{
David Howells7d12e782006-10-05 14:55:46 +010010959 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010960 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010961 char *name;
10962 struct tg3_napi *tnapi = &tp->napi[irq_num];
10963
10964 if (tp->irq_cnt == 1)
10965 name = tp->dev->name;
10966 else {
10967 name = &tnapi->irq_lbl[0];
10968 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10969 name[IFNAMSIZ-1] = 0;
10970 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010971
Joe Perches63c3a662011-04-26 08:12:10 +000010972 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010973 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010974 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010975 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010976 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010977 } else {
10978 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010979 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010980 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010981 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010982 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010983
10984 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010985}
10986
Michael Chan79381092005-04-21 17:13:59 -070010987static int tg3_test_interrupt(struct tg3 *tp)
10988{
Matt Carlson09943a12009-08-28 14:01:57 +000010989 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010990 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010991 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010992 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010993
Michael Chand4bc3922005-05-29 14:59:20 -070010994 if (!netif_running(dev))
10995 return -ENODEV;
10996
Michael Chan79381092005-04-21 17:13:59 -070010997 tg3_disable_ints(tp);
10998
Matt Carlson4f125f42009-09-01 12:55:02 +000010999 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011000
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011001 /*
11002 * Turn off MSI one shot mode. Otherwise this test has no
11003 * observable way to know whether the interrupt was delivered.
11004 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011005 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011006 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11007 tw32(MSGINT_MODE, val);
11008 }
11009
Matt Carlson4f125f42009-09-01 12:55:02 +000011010 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011011 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011012 if (err)
11013 return err;
11014
Matt Carlson898a56f2009-08-28 14:02:40 +000011015 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011016 tg3_enable_ints(tp);
11017
11018 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011019 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011020
11021 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011022 u32 int_mbox, misc_host_ctrl;
11023
Matt Carlson898a56f2009-08-28 14:02:40 +000011024 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011025 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11026
11027 if ((int_mbox != 0) ||
11028 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11029 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011030 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011031 }
11032
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011033 if (tg3_flag(tp, 57765_PLUS) &&
11034 tnapi->hw_status->status_tag != tnapi->last_tag)
11035 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11036
Michael Chan79381092005-04-21 17:13:59 -070011037 msleep(10);
11038 }
11039
11040 tg3_disable_ints(tp);
11041
Matt Carlson4f125f42009-09-01 12:55:02 +000011042 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011043
Matt Carlson4f125f42009-09-01 12:55:02 +000011044 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011045
11046 if (err)
11047 return err;
11048
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011049 if (intr_ok) {
11050 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011051 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011052 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11053 tw32(MSGINT_MODE, val);
11054 }
Michael Chan79381092005-04-21 17:13:59 -070011055 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011056 }
Michael Chan79381092005-04-21 17:13:59 -070011057
11058 return -EIO;
11059}
11060
11061/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11062 * successfully restored
11063 */
11064static int tg3_test_msi(struct tg3 *tp)
11065{
Michael Chan79381092005-04-21 17:13:59 -070011066 int err;
11067 u16 pci_cmd;
11068
Joe Perches63c3a662011-04-26 08:12:10 +000011069 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011070 return 0;
11071
11072 /* Turn off SERR reporting in case MSI terminates with Master
11073 * Abort.
11074 */
11075 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11076 pci_write_config_word(tp->pdev, PCI_COMMAND,
11077 pci_cmd & ~PCI_COMMAND_SERR);
11078
11079 err = tg3_test_interrupt(tp);
11080
11081 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11082
11083 if (!err)
11084 return 0;
11085
11086 /* other failures */
11087 if (err != -EIO)
11088 return err;
11089
11090 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011091 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11092 "to INTx mode. Please report this failure to the PCI "
11093 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011094
Matt Carlson4f125f42009-09-01 12:55:02 +000011095 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011096
Michael Chan79381092005-04-21 17:13:59 -070011097 pci_disable_msi(tp->pdev);
11098
Joe Perches63c3a662011-04-26 08:12:10 +000011099 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011100 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011101
Matt Carlson4f125f42009-09-01 12:55:02 +000011102 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011103 if (err)
11104 return err;
11105
11106 /* Need to reset the chip because the MSI cycle may have terminated
11107 * with Master Abort.
11108 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011109 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011110
Michael Chan944d9802005-05-29 14:57:48 -070011111 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011112 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011113
David S. Millerf47c11e2005-06-24 20:18:35 -070011114 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011115
11116 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011117 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011118
11119 return err;
11120}
11121
Matt Carlson9e9fd122009-01-19 16:57:45 -080011122static int tg3_request_firmware(struct tg3 *tp)
11123{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011124 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011125
11126 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011127 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11128 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011129 return -ENOENT;
11130 }
11131
Nithin Sujir77997ea2013-03-06 17:02:32 +000011132 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011133
11134 /* Firmware blob starts with version numbers, followed by
11135 * start address and _full_ length including BSS sections
11136 * (which must be longer than the actual data, of course
11137 */
11138
Nithin Sujir77997ea2013-03-06 17:02:32 +000011139 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11140 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011141 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11142 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011143 release_firmware(tp->fw);
11144 tp->fw = NULL;
11145 return -EINVAL;
11146 }
11147
11148 /* We no longer need firmware; we have it. */
11149 tp->fw_needed = NULL;
11150 return 0;
11151}
11152
Michael Chan91024262012-09-28 07:12:38 +000011153static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011154{
Michael Chan91024262012-09-28 07:12:38 +000011155 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011156
Michael Chan91024262012-09-28 07:12:38 +000011157 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011158 /* We want as many rx rings enabled as there are cpus.
11159 * In multiqueue MSI-X mode, the first MSI-X vector
11160 * only deals with link interrupts, etc, so we add
11161 * one to the number of vectors we are requesting.
11162 */
Michael Chan91024262012-09-28 07:12:38 +000011163 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011164 }
Matt Carlson679563f2009-09-01 12:55:46 +000011165
Michael Chan91024262012-09-28 07:12:38 +000011166 return irq_cnt;
11167}
11168
11169static bool tg3_enable_msix(struct tg3 *tp)
11170{
11171 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011172 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011173
Michael Chan09681692012-09-28 07:12:42 +000011174 tp->txq_cnt = tp->txq_req;
11175 tp->rxq_cnt = tp->rxq_req;
11176 if (!tp->rxq_cnt)
11177 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011178 if (tp->rxq_cnt > tp->rxq_max)
11179 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011180
11181 /* Disable multiple TX rings by default. Simple round-robin hardware
11182 * scheduling of the TX rings can cause starvation of rings with
11183 * small packets when other rings have TSO or jumbo packets.
11184 */
11185 if (!tp->txq_req)
11186 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011187
11188 tp->irq_cnt = tg3_irq_count(tp);
11189
Matt Carlson679563f2009-09-01 12:55:46 +000011190 for (i = 0; i < tp->irq_max; i++) {
11191 msix_ent[i].entry = i;
11192 msix_ent[i].vector = 0;
11193 }
11194
11195 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011196 if (rc < 0) {
11197 return false;
11198 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011199 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11200 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011201 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11202 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011203 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011204 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011205 if (tp->txq_cnt)
11206 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011207 }
11208
11209 for (i = 0; i < tp->irq_max; i++)
11210 tp->napi[i].irq_vec = msix_ent[i].vector;
11211
Michael Chan49a359e2012-09-28 07:12:37 +000011212 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011213 pci_disable_msix(tp->pdev);
11214 return false;
11215 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011216
Michael Chan91024262012-09-28 07:12:38 +000011217 if (tp->irq_cnt == 1)
11218 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011219
Michael Chan91024262012-09-28 07:12:38 +000011220 tg3_flag_set(tp, ENABLE_RSS);
11221
11222 if (tp->txq_cnt > 1)
11223 tg3_flag_set(tp, ENABLE_TSS);
11224
11225 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011226
Matt Carlson679563f2009-09-01 12:55:46 +000011227 return true;
11228}
11229
Matt Carlson07b01732009-08-28 14:01:15 +000011230static void tg3_ints_init(struct tg3 *tp)
11231{
Joe Perches63c3a662011-04-26 08:12:10 +000011232 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11233 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011234 /* All MSI supporting chips should support tagged
11235 * status. Assert that this is the case.
11236 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011237 netdev_warn(tp->dev,
11238 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011239 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011240 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011241
Joe Perches63c3a662011-04-26 08:12:10 +000011242 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11243 tg3_flag_set(tp, USING_MSIX);
11244 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11245 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011246
Joe Perches63c3a662011-04-26 08:12:10 +000011247 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011248 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011249 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011250 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011251 if (!tg3_flag(tp, 1SHOT_MSI))
11252 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011253 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11254 }
11255defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011256 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011257 tp->irq_cnt = 1;
11258 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011259 }
11260
11261 if (tp->irq_cnt == 1) {
11262 tp->txq_cnt = 1;
11263 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011264 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011265 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011266 }
Matt Carlson07b01732009-08-28 14:01:15 +000011267}
11268
11269static void tg3_ints_fini(struct tg3 *tp)
11270{
Joe Perches63c3a662011-04-26 08:12:10 +000011271 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011272 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011273 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011274 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011275 tg3_flag_clear(tp, USING_MSI);
11276 tg3_flag_clear(tp, USING_MSIX);
11277 tg3_flag_clear(tp, ENABLE_RSS);
11278 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011279}
11280
Matt Carlsonbe947302012-12-03 19:36:57 +000011281static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11282 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011283{
Michael Chand8f4cd32012-09-28 07:12:40 +000011284 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011285 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011286
Matt Carlson679563f2009-09-01 12:55:46 +000011287 /*
11288 * Setup interrupts first so we know how
11289 * many NAPI resources to allocate
11290 */
11291 tg3_ints_init(tp);
11292
Matt Carlson90415472011-12-16 13:33:23 +000011293 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011294
Linus Torvalds1da177e2005-04-16 15:20:36 -070011295 /* The placement of this call is tied
11296 * to the setup and use of Host TX descriptors.
11297 */
11298 err = tg3_alloc_consistent(tp);
11299 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011300 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011301
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011302 tg3_napi_init(tp);
11303
Matt Carlsonfed97812009-09-01 13:10:19 +000011304 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011305
Matt Carlson4f125f42009-09-01 12:55:02 +000011306 for (i = 0; i < tp->irq_cnt; i++) {
11307 struct tg3_napi *tnapi = &tp->napi[i];
11308 err = tg3_request_irq(tp, i);
11309 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011310 for (i--; i >= 0; i--) {
11311 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011312 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011313 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011314 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011315 }
11316 }
Matt Carlson07b01732009-08-28 14:01:15 +000011317
David S. Millerf47c11e2005-06-24 20:18:35 -070011318 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011319
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011320 if (init)
11321 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11322
Michael Chand8f4cd32012-09-28 07:12:40 +000011323 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011324 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011325 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011326 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011327 }
11328
David S. Millerf47c11e2005-06-24 20:18:35 -070011329 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011330
Matt Carlson07b01732009-08-28 14:01:15 +000011331 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011332 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011333
Michael Chand8f4cd32012-09-28 07:12:40 +000011334 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011335 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011336
Michael Chan79381092005-04-21 17:13:59 -070011337 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011338 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011339 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011340 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011341 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011342
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011343 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011344 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011345
Joe Perches63c3a662011-04-26 08:12:10 +000011346 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011347 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011348
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011349 tw32(PCIE_TRANSACTION_CFG,
11350 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011351 }
Michael Chan79381092005-04-21 17:13:59 -070011352 }
11353
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011354 tg3_phy_start(tp);
11355
Michael Chanaed93e02012-07-16 16:24:02 +000011356 tg3_hwmon_open(tp);
11357
David S. Millerf47c11e2005-06-24 20:18:35 -070011358 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011359
Matt Carlson21f76382012-02-22 12:35:21 +000011360 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011361 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011362 tg3_enable_ints(tp);
11363
Matt Carlsonbe947302012-12-03 19:36:57 +000011364 if (init)
11365 tg3_ptp_init(tp);
11366 else
11367 tg3_ptp_resume(tp);
11368
11369
David S. Millerf47c11e2005-06-24 20:18:35 -070011370 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011371
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011372 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011373
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011374 /*
11375 * Reset loopback feature if it was turned on while the device was down
11376 * make sure that it's installed properly now.
11377 */
11378 if (dev->features & NETIF_F_LOOPBACK)
11379 tg3_set_loopback(dev, dev->features);
11380
Linus Torvalds1da177e2005-04-16 15:20:36 -070011381 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011382
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011383out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011384 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11385 struct tg3_napi *tnapi = &tp->napi[i];
11386 free_irq(tnapi->irq_vec, tnapi);
11387 }
Matt Carlson07b01732009-08-28 14:01:15 +000011388
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011389out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011390 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011391 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011392 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011393
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011394out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011395 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011396
Matt Carlson07b01732009-08-28 14:01:15 +000011397 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011398}
11399
Michael Chan65138592012-09-28 07:12:41 +000011400static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011401{
Matt Carlson4f125f42009-09-01 12:55:02 +000011402 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011403
Matt Carlsondb219972011-11-04 09:15:03 +000011404 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011405 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011406
Matt Carlson21f76382012-02-22 12:35:21 +000011407 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011408
Michael Chanaed93e02012-07-16 16:24:02 +000011409 tg3_hwmon_close(tp);
11410
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011411 tg3_phy_stop(tp);
11412
David S. Millerf47c11e2005-06-24 20:18:35 -070011413 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011414
11415 tg3_disable_ints(tp);
11416
Michael Chan944d9802005-05-29 14:57:48 -070011417 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011418 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011419 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011420
David S. Millerf47c11e2005-06-24 20:18:35 -070011421 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011422
Matt Carlson4f125f42009-09-01 12:55:02 +000011423 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11424 struct tg3_napi *tnapi = &tp->napi[i];
11425 free_irq(tnapi->irq_vec, tnapi);
11426 }
Matt Carlson07b01732009-08-28 14:01:15 +000011427
11428 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011429
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011430 tg3_napi_fini(tp);
11431
Linus Torvalds1da177e2005-04-16 15:20:36 -070011432 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011433}
11434
Michael Chand8f4cd32012-09-28 07:12:40 +000011435static int tg3_open(struct net_device *dev)
11436{
11437 struct tg3 *tp = netdev_priv(dev);
11438 int err;
11439
11440 if (tp->fw_needed) {
11441 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011442 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11443 if (err) {
11444 netdev_warn(tp->dev, "EEE capability disabled\n");
11445 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11446 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11447 netdev_warn(tp->dev, "EEE capability restored\n");
11448 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11449 }
11450 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011451 if (err)
11452 return err;
11453 } else if (err) {
11454 netdev_warn(tp->dev, "TSO capability disabled\n");
11455 tg3_flag_clear(tp, TSO_CAPABLE);
11456 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11457 netdev_notice(tp->dev, "TSO capability restored\n");
11458 tg3_flag_set(tp, TSO_CAPABLE);
11459 }
11460 }
11461
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011462 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011463
11464 err = tg3_power_up(tp);
11465 if (err)
11466 return err;
11467
11468 tg3_full_lock(tp, 0);
11469
11470 tg3_disable_ints(tp);
11471 tg3_flag_clear(tp, INIT_COMPLETE);
11472
11473 tg3_full_unlock(tp);
11474
Nithin Sujir942d1af2013-04-09 08:48:07 +000011475 err = tg3_start(tp,
11476 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11477 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011478 if (err) {
11479 tg3_frob_aux_power(tp, false);
11480 pci_set_power_state(tp->pdev, PCI_D3hot);
11481 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011482
Matt Carlson7d41e492012-12-03 19:36:58 +000011483 if (tg3_flag(tp, PTP_CAPABLE)) {
11484 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11485 &tp->pdev->dev);
11486 if (IS_ERR(tp->ptp_clock))
11487 tp->ptp_clock = NULL;
11488 }
11489
Linus Torvalds1da177e2005-04-16 15:20:36 -070011490 return err;
11491}
11492
11493static int tg3_close(struct net_device *dev)
11494{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011495 struct tg3 *tp = netdev_priv(dev);
11496
Matt Carlsonbe947302012-12-03 19:36:57 +000011497 tg3_ptp_fini(tp);
11498
Michael Chan65138592012-09-28 07:12:41 +000011499 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011500
11501 /* Clear stats across close / open calls */
11502 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11503 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011504
Nithin Sujir5137a2e2013-07-29 13:58:36 -070011505 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011506
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011507 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011508
11509 return 0;
11510}
11511
11512static inline u64 get_stat64(tg3_stat64_t *val)
11513{
11514 return ((u64)val->high << 32) | ((u64)val->low);
11515}
11516
11517static u64 tg3_calc_crc_errors(struct tg3 *tp)
11518{
11519 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11520
11521 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011522 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11523 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011524 u32 val;
11525
11526 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11527 tg3_writephy(tp, MII_TG3_TEST1,
11528 val | MII_TG3_TEST1_CRC_EN);
11529 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11530 } else
11531 val = 0;
11532
11533 tp->phy_crc_errors += val;
11534
11535 return tp->phy_crc_errors;
11536 }
11537
11538 return get_stat64(&hw_stats->rx_fcs_errors);
11539}
11540
11541#define ESTAT_ADD(member) \
11542 estats->member = old_estats->member + \
11543 get_stat64(&hw_stats->member)
11544
11545static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11546{
11547 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11548 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11549
11550 ESTAT_ADD(rx_octets);
11551 ESTAT_ADD(rx_fragments);
11552 ESTAT_ADD(rx_ucast_packets);
11553 ESTAT_ADD(rx_mcast_packets);
11554 ESTAT_ADD(rx_bcast_packets);
11555 ESTAT_ADD(rx_fcs_errors);
11556 ESTAT_ADD(rx_align_errors);
11557 ESTAT_ADD(rx_xon_pause_rcvd);
11558 ESTAT_ADD(rx_xoff_pause_rcvd);
11559 ESTAT_ADD(rx_mac_ctrl_rcvd);
11560 ESTAT_ADD(rx_xoff_entered);
11561 ESTAT_ADD(rx_frame_too_long_errors);
11562 ESTAT_ADD(rx_jabbers);
11563 ESTAT_ADD(rx_undersize_packets);
11564 ESTAT_ADD(rx_in_length_errors);
11565 ESTAT_ADD(rx_out_length_errors);
11566 ESTAT_ADD(rx_64_or_less_octet_packets);
11567 ESTAT_ADD(rx_65_to_127_octet_packets);
11568 ESTAT_ADD(rx_128_to_255_octet_packets);
11569 ESTAT_ADD(rx_256_to_511_octet_packets);
11570 ESTAT_ADD(rx_512_to_1023_octet_packets);
11571 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11572 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11573 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11574 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11575 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11576
11577 ESTAT_ADD(tx_octets);
11578 ESTAT_ADD(tx_collisions);
11579 ESTAT_ADD(tx_xon_sent);
11580 ESTAT_ADD(tx_xoff_sent);
11581 ESTAT_ADD(tx_flow_control);
11582 ESTAT_ADD(tx_mac_errors);
11583 ESTAT_ADD(tx_single_collisions);
11584 ESTAT_ADD(tx_mult_collisions);
11585 ESTAT_ADD(tx_deferred);
11586 ESTAT_ADD(tx_excessive_collisions);
11587 ESTAT_ADD(tx_late_collisions);
11588 ESTAT_ADD(tx_collide_2times);
11589 ESTAT_ADD(tx_collide_3times);
11590 ESTAT_ADD(tx_collide_4times);
11591 ESTAT_ADD(tx_collide_5times);
11592 ESTAT_ADD(tx_collide_6times);
11593 ESTAT_ADD(tx_collide_7times);
11594 ESTAT_ADD(tx_collide_8times);
11595 ESTAT_ADD(tx_collide_9times);
11596 ESTAT_ADD(tx_collide_10times);
11597 ESTAT_ADD(tx_collide_11times);
11598 ESTAT_ADD(tx_collide_12times);
11599 ESTAT_ADD(tx_collide_13times);
11600 ESTAT_ADD(tx_collide_14times);
11601 ESTAT_ADD(tx_collide_15times);
11602 ESTAT_ADD(tx_ucast_packets);
11603 ESTAT_ADD(tx_mcast_packets);
11604 ESTAT_ADD(tx_bcast_packets);
11605 ESTAT_ADD(tx_carrier_sense_errors);
11606 ESTAT_ADD(tx_discards);
11607 ESTAT_ADD(tx_errors);
11608
11609 ESTAT_ADD(dma_writeq_full);
11610 ESTAT_ADD(dma_write_prioq_full);
11611 ESTAT_ADD(rxbds_empty);
11612 ESTAT_ADD(rx_discards);
11613 ESTAT_ADD(rx_errors);
11614 ESTAT_ADD(rx_threshold_hit);
11615
11616 ESTAT_ADD(dma_readq_full);
11617 ESTAT_ADD(dma_read_prioq_full);
11618 ESTAT_ADD(tx_comp_queue_full);
11619
11620 ESTAT_ADD(ring_set_send_prod_index);
11621 ESTAT_ADD(ring_status_update);
11622 ESTAT_ADD(nic_irqs);
11623 ESTAT_ADD(nic_avoided_irqs);
11624 ESTAT_ADD(nic_tx_threshold_hit);
11625
Matt Carlson4452d092011-05-19 12:12:51 +000011626 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011627}
11628
Matt Carlson65ec6982012-02-28 23:33:37 +000011629static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011630{
Eric Dumazet511d2222010-07-07 20:44:24 +000011631 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011632 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11633
Linus Torvalds1da177e2005-04-16 15:20:36 -070011634 stats->rx_packets = old_stats->rx_packets +
11635 get_stat64(&hw_stats->rx_ucast_packets) +
11636 get_stat64(&hw_stats->rx_mcast_packets) +
11637 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011638
Linus Torvalds1da177e2005-04-16 15:20:36 -070011639 stats->tx_packets = old_stats->tx_packets +
11640 get_stat64(&hw_stats->tx_ucast_packets) +
11641 get_stat64(&hw_stats->tx_mcast_packets) +
11642 get_stat64(&hw_stats->tx_bcast_packets);
11643
11644 stats->rx_bytes = old_stats->rx_bytes +
11645 get_stat64(&hw_stats->rx_octets);
11646 stats->tx_bytes = old_stats->tx_bytes +
11647 get_stat64(&hw_stats->tx_octets);
11648
11649 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011650 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011651 stats->tx_errors = old_stats->tx_errors +
11652 get_stat64(&hw_stats->tx_errors) +
11653 get_stat64(&hw_stats->tx_mac_errors) +
11654 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11655 get_stat64(&hw_stats->tx_discards);
11656
11657 stats->multicast = old_stats->multicast +
11658 get_stat64(&hw_stats->rx_mcast_packets);
11659 stats->collisions = old_stats->collisions +
11660 get_stat64(&hw_stats->tx_collisions);
11661
11662 stats->rx_length_errors = old_stats->rx_length_errors +
11663 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11664 get_stat64(&hw_stats->rx_undersize_packets);
11665
11666 stats->rx_over_errors = old_stats->rx_over_errors +
11667 get_stat64(&hw_stats->rxbds_empty);
11668 stats->rx_frame_errors = old_stats->rx_frame_errors +
11669 get_stat64(&hw_stats->rx_align_errors);
11670 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11671 get_stat64(&hw_stats->tx_discards);
11672 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11673 get_stat64(&hw_stats->tx_carrier_sense_errors);
11674
11675 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011676 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011677
John W. Linville4f63b872005-09-12 14:43:18 -070011678 stats->rx_missed_errors = old_stats->rx_missed_errors +
11679 get_stat64(&hw_stats->rx_discards);
11680
Eric Dumazetb0057c52010-10-10 19:55:52 +000011681 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011682 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011683}
11684
Linus Torvalds1da177e2005-04-16 15:20:36 -070011685static int tg3_get_regs_len(struct net_device *dev)
11686{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011687 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011688}
11689
11690static void tg3_get_regs(struct net_device *dev,
11691 struct ethtool_regs *regs, void *_p)
11692{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011693 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011694
11695 regs->version = 0;
11696
Matt Carlson97bd8e42011-04-13 11:05:04 +000011697 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011698
Matt Carlson80096062010-08-02 11:26:06 +000011699 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011700 return;
11701
David S. Millerf47c11e2005-06-24 20:18:35 -070011702 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011703
Matt Carlson97bd8e42011-04-13 11:05:04 +000011704 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011705
David S. Millerf47c11e2005-06-24 20:18:35 -070011706 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011707}
11708
11709static int tg3_get_eeprom_len(struct net_device *dev)
11710{
11711 struct tg3 *tp = netdev_priv(dev);
11712
11713 return tp->nvram_size;
11714}
11715
Linus Torvalds1da177e2005-04-16 15:20:36 -070011716static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11717{
11718 struct tg3 *tp = netdev_priv(dev);
11719 int ret;
11720 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011721 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011722 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011723
Joe Perches63c3a662011-04-26 08:12:10 +000011724 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011725 return -EINVAL;
11726
Linus Torvalds1da177e2005-04-16 15:20:36 -070011727 offset = eeprom->offset;
11728 len = eeprom->len;
11729 eeprom->len = 0;
11730
11731 eeprom->magic = TG3_EEPROM_MAGIC;
11732
11733 if (offset & 3) {
11734 /* adjustments to start on required 4 byte boundary */
11735 b_offset = offset & 3;
11736 b_count = 4 - b_offset;
11737 if (b_count > len) {
11738 /* i.e. offset=1 len=2 */
11739 b_count = len;
11740 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011741 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011742 if (ret)
11743 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011744 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011745 len -= b_count;
11746 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011747 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011748 }
11749
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011750 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011751 pd = &data[eeprom->len];
11752 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011753 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011754 if (ret) {
11755 eeprom->len += i;
11756 return ret;
11757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011758 memcpy(pd + i, &val, 4);
11759 }
11760 eeprom->len += i;
11761
11762 if (len & 3) {
11763 /* read last bytes not ending on 4 byte boundary */
11764 pd = &data[eeprom->len];
11765 b_count = len & 3;
11766 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011767 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011768 if (ret)
11769 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011770 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011771 eeprom->len += b_count;
11772 }
11773 return 0;
11774}
11775
Linus Torvalds1da177e2005-04-16 15:20:36 -070011776static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11777{
11778 struct tg3 *tp = netdev_priv(dev);
11779 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011780 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011781 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011782 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011783
Joe Perches63c3a662011-04-26 08:12:10 +000011784 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011785 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011786 return -EINVAL;
11787
11788 offset = eeprom->offset;
11789 len = eeprom->len;
11790
11791 if ((b_offset = (offset & 3))) {
11792 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011793 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011794 if (ret)
11795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011796 len += b_offset;
11797 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011798 if (len < 4)
11799 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011800 }
11801
11802 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011803 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011804 /* adjustments to end on required 4 byte boundary */
11805 odd_len = 1;
11806 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011807 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011808 if (ret)
11809 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011810 }
11811
11812 buf = data;
11813 if (b_offset || odd_len) {
11814 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011815 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011816 return -ENOMEM;
11817 if (b_offset)
11818 memcpy(buf, &start, 4);
11819 if (odd_len)
11820 memcpy(buf+len-4, &end, 4);
11821 memcpy(buf + b_offset, data, eeprom->len);
11822 }
11823
11824 ret = tg3_nvram_write_block(tp, offset, len, buf);
11825
11826 if (buf != data)
11827 kfree(buf);
11828
11829 return ret;
11830}
11831
11832static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11833{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011834 struct tg3 *tp = netdev_priv(dev);
11835
Joe Perches63c3a662011-04-26 08:12:10 +000011836 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011837 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011838 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011839 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011840 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11841 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011842 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011843
Linus Torvalds1da177e2005-04-16 15:20:36 -070011844 cmd->supported = (SUPPORTED_Autoneg);
11845
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011846 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011847 cmd->supported |= (SUPPORTED_1000baseT_Half |
11848 SUPPORTED_1000baseT_Full);
11849
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011850 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011851 cmd->supported |= (SUPPORTED_100baseT_Half |
11852 SUPPORTED_100baseT_Full |
11853 SUPPORTED_10baseT_Half |
11854 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011855 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011856 cmd->port = PORT_TP;
11857 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011858 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011859 cmd->port = PORT_FIBRE;
11860 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011861
Linus Torvalds1da177e2005-04-16 15:20:36 -070011862 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011863 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11864 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11865 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11866 cmd->advertising |= ADVERTISED_Pause;
11867 } else {
11868 cmd->advertising |= ADVERTISED_Pause |
11869 ADVERTISED_Asym_Pause;
11870 }
11871 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11872 cmd->advertising |= ADVERTISED_Asym_Pause;
11873 }
11874 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011875 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011876 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011877 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011878 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011879 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11880 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11881 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11882 else
11883 cmd->eth_tp_mdix = ETH_TP_MDI;
11884 }
Matt Carlson64c22182010-10-14 10:37:44 +000011885 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011886 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11887 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011888 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889 }
Matt Carlson882e9792009-09-01 13:21:36 +000011890 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011891 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011892 cmd->autoneg = tp->link_config.autoneg;
11893 cmd->maxtxpkt = 0;
11894 cmd->maxrxpkt = 0;
11895 return 0;
11896}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011897
Linus Torvalds1da177e2005-04-16 15:20:36 -070011898static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11899{
11900 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011901 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011902
Joe Perches63c3a662011-04-26 08:12:10 +000011903 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011904 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011905 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011906 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011907 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11908 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011909 }
11910
Matt Carlson7e5856b2009-02-25 14:23:01 +000011911 if (cmd->autoneg != AUTONEG_ENABLE &&
11912 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011913 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011914
11915 if (cmd->autoneg == AUTONEG_DISABLE &&
11916 cmd->duplex != DUPLEX_FULL &&
11917 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011918 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011919
Matt Carlson7e5856b2009-02-25 14:23:01 +000011920 if (cmd->autoneg == AUTONEG_ENABLE) {
11921 u32 mask = ADVERTISED_Autoneg |
11922 ADVERTISED_Pause |
11923 ADVERTISED_Asym_Pause;
11924
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011925 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011926 mask |= ADVERTISED_1000baseT_Half |
11927 ADVERTISED_1000baseT_Full;
11928
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011929 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011930 mask |= ADVERTISED_100baseT_Half |
11931 ADVERTISED_100baseT_Full |
11932 ADVERTISED_10baseT_Half |
11933 ADVERTISED_10baseT_Full |
11934 ADVERTISED_TP;
11935 else
11936 mask |= ADVERTISED_FIBRE;
11937
11938 if (cmd->advertising & ~mask)
11939 return -EINVAL;
11940
11941 mask &= (ADVERTISED_1000baseT_Half |
11942 ADVERTISED_1000baseT_Full |
11943 ADVERTISED_100baseT_Half |
11944 ADVERTISED_100baseT_Full |
11945 ADVERTISED_10baseT_Half |
11946 ADVERTISED_10baseT_Full);
11947
11948 cmd->advertising &= mask;
11949 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011950 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011951 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011952 return -EINVAL;
11953
11954 if (cmd->duplex != DUPLEX_FULL)
11955 return -EINVAL;
11956 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011957 if (speed != SPEED_100 &&
11958 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011959 return -EINVAL;
11960 }
11961 }
11962
David S. Millerf47c11e2005-06-24 20:18:35 -070011963 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011964
11965 tp->link_config.autoneg = cmd->autoneg;
11966 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011967 tp->link_config.advertising = (cmd->advertising |
11968 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011969 tp->link_config.speed = SPEED_UNKNOWN;
11970 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011971 } else {
11972 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011973 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011974 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011975 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011976
Nithin Sujirfdad8de2013-04-09 08:48:08 +000011977 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
11978
Nithin Sujirce20f162013-04-09 08:48:04 +000011979 tg3_warn_mgmt_link_flap(tp);
11980
Linus Torvalds1da177e2005-04-16 15:20:36 -070011981 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000011982 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011983
David S. Millerf47c11e2005-06-24 20:18:35 -070011984 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011985
Linus Torvalds1da177e2005-04-16 15:20:36 -070011986 return 0;
11987}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011988
Linus Torvalds1da177e2005-04-16 15:20:36 -070011989static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11990{
11991 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011992
Rick Jones68aad782011-11-07 13:29:27 +000011993 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11994 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11995 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11996 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011997}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011998
Linus Torvalds1da177e2005-04-16 15:20:36 -070011999static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12000{
12001 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012002
Joe Perches63c3a662011-04-26 08:12:10 +000012003 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012004 wol->supported = WAKE_MAGIC;
12005 else
12006 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012007 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012008 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009 wol->wolopts = WAKE_MAGIC;
12010 memset(&wol->sopass, 0, sizeof(wol->sopass));
12011}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012012
Linus Torvalds1da177e2005-04-16 15:20:36 -070012013static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12014{
12015 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012016 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012017
Linus Torvalds1da177e2005-04-16 15:20:36 -070012018 if (wol->wolopts & ~WAKE_MAGIC)
12019 return -EINVAL;
12020 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012021 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012023
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012024 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12025
David S. Millerf47c11e2005-06-24 20:18:35 -070012026 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012027 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012028 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012029 else
Joe Perches63c3a662011-04-26 08:12:10 +000012030 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070012031 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012032
Linus Torvalds1da177e2005-04-16 15:20:36 -070012033 return 0;
12034}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012035
Linus Torvalds1da177e2005-04-16 15:20:36 -070012036static u32 tg3_get_msglevel(struct net_device *dev)
12037{
12038 struct tg3 *tp = netdev_priv(dev);
12039 return tp->msg_enable;
12040}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012041
Linus Torvalds1da177e2005-04-16 15:20:36 -070012042static void tg3_set_msglevel(struct net_device *dev, u32 value)
12043{
12044 struct tg3 *tp = netdev_priv(dev);
12045 tp->msg_enable = value;
12046}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012047
Linus Torvalds1da177e2005-04-16 15:20:36 -070012048static int tg3_nway_reset(struct net_device *dev)
12049{
12050 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012051 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012052
Linus Torvalds1da177e2005-04-16 15:20:36 -070012053 if (!netif_running(dev))
12054 return -EAGAIN;
12055
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012056 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012057 return -EINVAL;
12058
Nithin Sujirce20f162013-04-09 08:48:04 +000012059 tg3_warn_mgmt_link_flap(tp);
12060
Joe Perches63c3a662011-04-26 08:12:10 +000012061 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012062 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012063 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012064 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012065 } else {
12066 u32 bmcr;
12067
12068 spin_lock_bh(&tp->lock);
12069 r = -EINVAL;
12070 tg3_readphy(tp, MII_BMCR, &bmcr);
12071 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12072 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012073 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012074 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12075 BMCR_ANENABLE);
12076 r = 0;
12077 }
12078 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012079 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012080
Linus Torvalds1da177e2005-04-16 15:20:36 -070012081 return r;
12082}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012083
Linus Torvalds1da177e2005-04-16 15:20:36 -070012084static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12085{
12086 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012087
Matt Carlson2c49a442010-09-30 10:34:35 +000012088 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012089 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012090 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012091 else
12092 ering->rx_jumbo_max_pending = 0;
12093
12094 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012095
12096 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012097 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012098 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12099 else
12100 ering->rx_jumbo_pending = 0;
12101
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012102 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012104
Linus Torvalds1da177e2005-04-16 15:20:36 -070012105static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12106{
12107 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012108 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012109
Matt Carlson2c49a442010-09-30 10:34:35 +000012110 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12111 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012112 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12113 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012114 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012115 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012116 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012117
Michael Chanbbe832c2005-06-24 20:20:04 -070012118 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012119 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012120 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012121 irq_sync = 1;
12122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012123
Michael Chanbbe832c2005-06-24 20:20:04 -070012124 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012125
Linus Torvalds1da177e2005-04-16 15:20:36 -070012126 tp->rx_pending = ering->rx_pending;
12127
Joe Perches63c3a662011-04-26 08:12:10 +000012128 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012129 tp->rx_pending > 63)
12130 tp->rx_pending = 63;
12131 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012132
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012133 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012134 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012135
12136 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012137 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012138 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012139 if (!err)
12140 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012141 }
12142
David S. Millerf47c11e2005-06-24 20:18:35 -070012143 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012144
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012145 if (irq_sync && !err)
12146 tg3_phy_start(tp);
12147
Michael Chanb9ec6c12006-07-25 16:37:27 -070012148 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012149}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012150
Linus Torvalds1da177e2005-04-16 15:20:36 -070012151static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12152{
12153 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012154
Joe Perches63c3a662011-04-26 08:12:10 +000012155 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012156
Matt Carlson4a2db502011-12-08 14:40:17 +000012157 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012158 epause->rx_pause = 1;
12159 else
12160 epause->rx_pause = 0;
12161
Matt Carlson4a2db502011-12-08 14:40:17 +000012162 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012163 epause->tx_pause = 1;
12164 else
12165 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012166}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012167
Linus Torvalds1da177e2005-04-16 15:20:36 -070012168static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12169{
12170 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012171 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012172
Nithin Sujirce20f162013-04-09 08:48:04 +000012173 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12174 tg3_warn_mgmt_link_flap(tp);
12175
Joe Perches63c3a662011-04-26 08:12:10 +000012176 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012177 u32 newadv;
12178 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012179
Matt Carlson27121682010-02-17 15:16:57 +000012180 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012181
Matt Carlson27121682010-02-17 15:16:57 +000012182 if (!(phydev->supported & SUPPORTED_Pause) ||
12183 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012184 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012185 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012186
Matt Carlson27121682010-02-17 15:16:57 +000012187 tp->link_config.flowctrl = 0;
12188 if (epause->rx_pause) {
12189 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012190
Matt Carlson27121682010-02-17 15:16:57 +000012191 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012192 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012193 newadv = ADVERTISED_Pause;
12194 } else
12195 newadv = ADVERTISED_Pause |
12196 ADVERTISED_Asym_Pause;
12197 } else if (epause->tx_pause) {
12198 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12199 newadv = ADVERTISED_Asym_Pause;
12200 } else
12201 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012202
Matt Carlson27121682010-02-17 15:16:57 +000012203 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012204 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012205 else
Joe Perches63c3a662011-04-26 08:12:10 +000012206 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012207
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012208 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012209 u32 oldadv = phydev->advertising &
12210 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12211 if (oldadv != newadv) {
12212 phydev->advertising &=
12213 ~(ADVERTISED_Pause |
12214 ADVERTISED_Asym_Pause);
12215 phydev->advertising |= newadv;
12216 if (phydev->autoneg) {
12217 /*
12218 * Always renegotiate the link to
12219 * inform our link partner of our
12220 * flow control settings, even if the
12221 * flow control is forced. Let
12222 * tg3_adjust_link() do the final
12223 * flow control setup.
12224 */
12225 return phy_start_aneg(phydev);
12226 }
12227 }
12228
12229 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012230 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012231 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012232 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012233 ~(ADVERTISED_Pause |
12234 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012235 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012236 }
12237 } else {
12238 int irq_sync = 0;
12239
12240 if (netif_running(dev)) {
12241 tg3_netif_stop(tp);
12242 irq_sync = 1;
12243 }
12244
12245 tg3_full_lock(tp, irq_sync);
12246
12247 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012248 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012249 else
Joe Perches63c3a662011-04-26 08:12:10 +000012250 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012251 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012252 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012253 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012254 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012255 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012256 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012257 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012258 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012259
12260 if (netif_running(dev)) {
12261 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012262 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012263 if (!err)
12264 tg3_netif_start(tp);
12265 }
12266
12267 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012269
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012270 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12271
Michael Chanb9ec6c12006-07-25 16:37:27 -070012272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012273}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012274
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012275static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012276{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012277 switch (sset) {
12278 case ETH_SS_TEST:
12279 return TG3_NUM_TEST;
12280 case ETH_SS_STATS:
12281 return TG3_NUM_STATS;
12282 default:
12283 return -EOPNOTSUPP;
12284 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012285}
12286
Matt Carlson90415472011-12-16 13:33:23 +000012287static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12288 u32 *rules __always_unused)
12289{
12290 struct tg3 *tp = netdev_priv(dev);
12291
12292 if (!tg3_flag(tp, SUPPORT_MSIX))
12293 return -EOPNOTSUPP;
12294
12295 switch (info->cmd) {
12296 case ETHTOOL_GRXRINGS:
12297 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012298 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012299 else {
12300 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012301 if (info->data > TG3_RSS_MAX_NUM_QS)
12302 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012303 }
12304
12305 /* The first interrupt vector only
12306 * handles link interrupts.
12307 */
12308 info->data -= 1;
12309 return 0;
12310
12311 default:
12312 return -EOPNOTSUPP;
12313 }
12314}
12315
12316static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12317{
12318 u32 size = 0;
12319 struct tg3 *tp = netdev_priv(dev);
12320
12321 if (tg3_flag(tp, SUPPORT_MSIX))
12322 size = TG3_RSS_INDIR_TBL_SIZE;
12323
12324 return size;
12325}
12326
12327static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12328{
12329 struct tg3 *tp = netdev_priv(dev);
12330 int i;
12331
12332 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12333 indir[i] = tp->rss_ind_tbl[i];
12334
12335 return 0;
12336}
12337
12338static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12339{
12340 struct tg3 *tp = netdev_priv(dev);
12341 size_t i;
12342
12343 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12344 tp->rss_ind_tbl[i] = indir[i];
12345
12346 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12347 return 0;
12348
12349 /* It is legal to write the indirection
12350 * table while the device is running.
12351 */
12352 tg3_full_lock(tp, 0);
12353 tg3_rss_write_indir_tbl(tp);
12354 tg3_full_unlock(tp);
12355
12356 return 0;
12357}
12358
Michael Chan09681692012-09-28 07:12:42 +000012359static void tg3_get_channels(struct net_device *dev,
12360 struct ethtool_channels *channel)
12361{
12362 struct tg3 *tp = netdev_priv(dev);
12363 u32 deflt_qs = netif_get_num_default_rss_queues();
12364
12365 channel->max_rx = tp->rxq_max;
12366 channel->max_tx = tp->txq_max;
12367
12368 if (netif_running(dev)) {
12369 channel->rx_count = tp->rxq_cnt;
12370 channel->tx_count = tp->txq_cnt;
12371 } else {
12372 if (tp->rxq_req)
12373 channel->rx_count = tp->rxq_req;
12374 else
12375 channel->rx_count = min(deflt_qs, tp->rxq_max);
12376
12377 if (tp->txq_req)
12378 channel->tx_count = tp->txq_req;
12379 else
12380 channel->tx_count = min(deflt_qs, tp->txq_max);
12381 }
12382}
12383
12384static int tg3_set_channels(struct net_device *dev,
12385 struct ethtool_channels *channel)
12386{
12387 struct tg3 *tp = netdev_priv(dev);
12388
12389 if (!tg3_flag(tp, SUPPORT_MSIX))
12390 return -EOPNOTSUPP;
12391
12392 if (channel->rx_count > tp->rxq_max ||
12393 channel->tx_count > tp->txq_max)
12394 return -EINVAL;
12395
12396 tp->rxq_req = channel->rx_count;
12397 tp->txq_req = channel->tx_count;
12398
12399 if (!netif_running(dev))
12400 return 0;
12401
12402 tg3_stop(tp);
12403
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012404 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012405
Matt Carlsonbe947302012-12-03 19:36:57 +000012406 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012407
12408 return 0;
12409}
12410
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012411static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012412{
12413 switch (stringset) {
12414 case ETH_SS_STATS:
12415 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12416 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012417 case ETH_SS_TEST:
12418 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012420 default:
12421 WARN_ON(1); /* we need a WARN() */
12422 break;
12423 }
12424}
12425
stephen hemminger81b87092011-04-04 08:43:50 +000012426static int tg3_set_phys_id(struct net_device *dev,
12427 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012428{
12429 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012430
12431 if (!netif_running(tp->dev))
12432 return -EAGAIN;
12433
stephen hemminger81b87092011-04-04 08:43:50 +000012434 switch (state) {
12435 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012436 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012437
stephen hemminger81b87092011-04-04 08:43:50 +000012438 case ETHTOOL_ID_ON:
12439 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12440 LED_CTRL_1000MBPS_ON |
12441 LED_CTRL_100MBPS_ON |
12442 LED_CTRL_10MBPS_ON |
12443 LED_CTRL_TRAFFIC_OVERRIDE |
12444 LED_CTRL_TRAFFIC_BLINK |
12445 LED_CTRL_TRAFFIC_LED);
12446 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012447
stephen hemminger81b87092011-04-04 08:43:50 +000012448 case ETHTOOL_ID_OFF:
12449 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12450 LED_CTRL_TRAFFIC_OVERRIDE);
12451 break;
Michael Chan4009a932005-09-05 17:52:54 -070012452
stephen hemminger81b87092011-04-04 08:43:50 +000012453 case ETHTOOL_ID_INACTIVE:
12454 tw32(MAC_LED_CTRL, tp->led_ctrl);
12455 break;
Michael Chan4009a932005-09-05 17:52:54 -070012456 }
stephen hemminger81b87092011-04-04 08:43:50 +000012457
Michael Chan4009a932005-09-05 17:52:54 -070012458 return 0;
12459}
12460
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012461static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012462 struct ethtool_stats *estats, u64 *tmp_stats)
12463{
12464 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012465
Matt Carlsonb546e462012-02-13 15:20:09 +000012466 if (tp->hw_stats)
12467 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12468 else
12469 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012470}
12471
Matt Carlson535a4902011-07-20 10:20:56 +000012472static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012473{
12474 int i;
12475 __be32 *buf;
12476 u32 offset = 0, len = 0;
12477 u32 magic, val;
12478
Joe Perches63c3a662011-04-26 08:12:10 +000012479 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012480 return NULL;
12481
12482 if (magic == TG3_EEPROM_MAGIC) {
12483 for (offset = TG3_NVM_DIR_START;
12484 offset < TG3_NVM_DIR_END;
12485 offset += TG3_NVM_DIRENT_SIZE) {
12486 if (tg3_nvram_read(tp, offset, &val))
12487 return NULL;
12488
12489 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12490 TG3_NVM_DIRTYPE_EXTVPD)
12491 break;
12492 }
12493
12494 if (offset != TG3_NVM_DIR_END) {
12495 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12496 if (tg3_nvram_read(tp, offset + 4, &offset))
12497 return NULL;
12498
12499 offset = tg3_nvram_logical_addr(tp, offset);
12500 }
12501 }
12502
12503 if (!offset || !len) {
12504 offset = TG3_NVM_VPD_OFF;
12505 len = TG3_NVM_VPD_LEN;
12506 }
12507
12508 buf = kmalloc(len, GFP_KERNEL);
12509 if (buf == NULL)
12510 return NULL;
12511
12512 if (magic == TG3_EEPROM_MAGIC) {
12513 for (i = 0; i < len; i += 4) {
12514 /* The data is in little-endian format in NVRAM.
12515 * Use the big-endian read routines to preserve
12516 * the byte order as it exists in NVRAM.
12517 */
12518 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12519 goto error;
12520 }
12521 } else {
12522 u8 *ptr;
12523 ssize_t cnt;
12524 unsigned int pos = 0;
12525
12526 ptr = (u8 *)&buf[0];
12527 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12528 cnt = pci_read_vpd(tp->pdev, pos,
12529 len - pos, ptr);
12530 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12531 cnt = 0;
12532 else if (cnt < 0)
12533 goto error;
12534 }
12535 if (pos != len)
12536 goto error;
12537 }
12538
Matt Carlson535a4902011-07-20 10:20:56 +000012539 *vpdlen = len;
12540
Matt Carlsonc3e94502011-04-13 11:05:08 +000012541 return buf;
12542
12543error:
12544 kfree(buf);
12545 return NULL;
12546}
12547
Michael Chan566f86a2005-05-29 14:56:58 -070012548#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012549#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12550#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12551#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012552#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12553#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012554#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012555#define NVRAM_SELFBOOT_HW_SIZE 0x20
12556#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012557
12558static int tg3_test_nvram(struct tg3 *tp)
12559{
Matt Carlson535a4902011-07-20 10:20:56 +000012560 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012561 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012562 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012563
Joe Perches63c3a662011-04-26 08:12:10 +000012564 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012565 return 0;
12566
Matt Carlsone4f34112009-02-25 14:25:00 +000012567 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012568 return -EIO;
12569
Michael Chan1b277772006-03-20 22:27:48 -080012570 if (magic == TG3_EEPROM_MAGIC)
12571 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012572 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012573 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12574 TG3_EEPROM_SB_FORMAT_1) {
12575 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12576 case TG3_EEPROM_SB_REVISION_0:
12577 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12578 break;
12579 case TG3_EEPROM_SB_REVISION_2:
12580 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12581 break;
12582 case TG3_EEPROM_SB_REVISION_3:
12583 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12584 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012585 case TG3_EEPROM_SB_REVISION_4:
12586 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12587 break;
12588 case TG3_EEPROM_SB_REVISION_5:
12589 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12590 break;
12591 case TG3_EEPROM_SB_REVISION_6:
12592 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12593 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012594 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012595 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012596 }
12597 } else
Michael Chan1b277772006-03-20 22:27:48 -080012598 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012599 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12600 size = NVRAM_SELFBOOT_HW_SIZE;
12601 else
Michael Chan1b277772006-03-20 22:27:48 -080012602 return -EIO;
12603
12604 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012605 if (buf == NULL)
12606 return -ENOMEM;
12607
Michael Chan1b277772006-03-20 22:27:48 -080012608 err = -EIO;
12609 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012610 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12611 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012612 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012613 }
Michael Chan1b277772006-03-20 22:27:48 -080012614 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012615 goto out;
12616
Michael Chan1b277772006-03-20 22:27:48 -080012617 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012618 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012619 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012620 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012621 u8 *buf8 = (u8 *) buf, csum8 = 0;
12622
Al Virob9fc7dc2007-12-17 22:59:57 -080012623 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012624 TG3_EEPROM_SB_REVISION_2) {
12625 /* For rev 2, the csum doesn't include the MBA. */
12626 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12627 csum8 += buf8[i];
12628 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12629 csum8 += buf8[i];
12630 } else {
12631 for (i = 0; i < size; i++)
12632 csum8 += buf8[i];
12633 }
Michael Chan1b277772006-03-20 22:27:48 -080012634
Adrian Bunkad96b482006-04-05 22:21:04 -070012635 if (csum8 == 0) {
12636 err = 0;
12637 goto out;
12638 }
12639
12640 err = -EIO;
12641 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012642 }
Michael Chan566f86a2005-05-29 14:56:58 -070012643
Al Virob9fc7dc2007-12-17 22:59:57 -080012644 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012645 TG3_EEPROM_MAGIC_HW) {
12646 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012647 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012648 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012649
12650 /* Separate the parity bits and the data bytes. */
12651 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12652 if ((i == 0) || (i == 8)) {
12653 int l;
12654 u8 msk;
12655
12656 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12657 parity[k++] = buf8[i] & msk;
12658 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012659 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012660 int l;
12661 u8 msk;
12662
12663 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12664 parity[k++] = buf8[i] & msk;
12665 i++;
12666
12667 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12668 parity[k++] = buf8[i] & msk;
12669 i++;
12670 }
12671 data[j++] = buf8[i];
12672 }
12673
12674 err = -EIO;
12675 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12676 u8 hw8 = hweight8(data[i]);
12677
12678 if ((hw8 & 0x1) && parity[i])
12679 goto out;
12680 else if (!(hw8 & 0x1) && !parity[i])
12681 goto out;
12682 }
12683 err = 0;
12684 goto out;
12685 }
12686
Matt Carlson01c3a392011-03-09 16:58:20 +000012687 err = -EIO;
12688
Michael Chan566f86a2005-05-29 14:56:58 -070012689 /* Bootstrap checksum at offset 0x10 */
12690 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012691 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012692 goto out;
12693
12694 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12695 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012696 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012697 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012698
Matt Carlsonc3e94502011-04-13 11:05:08 +000012699 kfree(buf);
12700
Matt Carlson535a4902011-07-20 10:20:56 +000012701 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012702 if (!buf)
12703 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012704
Matt Carlson535a4902011-07-20 10:20:56 +000012705 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012706 if (i > 0) {
12707 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12708 if (j < 0)
12709 goto out;
12710
Matt Carlson535a4902011-07-20 10:20:56 +000012711 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012712 goto out;
12713
12714 i += PCI_VPD_LRDT_TAG_SIZE;
12715 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12716 PCI_VPD_RO_KEYWORD_CHKSUM);
12717 if (j > 0) {
12718 u8 csum8 = 0;
12719
12720 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12721
12722 for (i = 0; i <= j; i++)
12723 csum8 += ((u8 *)buf)[i];
12724
12725 if (csum8)
12726 goto out;
12727 }
12728 }
12729
Michael Chan566f86a2005-05-29 14:56:58 -070012730 err = 0;
12731
12732out:
12733 kfree(buf);
12734 return err;
12735}
12736
Michael Chanca430072005-05-29 14:57:23 -070012737#define TG3_SERDES_TIMEOUT_SEC 2
12738#define TG3_COPPER_TIMEOUT_SEC 6
12739
12740static int tg3_test_link(struct tg3 *tp)
12741{
12742 int i, max;
12743
12744 if (!netif_running(tp->dev))
12745 return -ENODEV;
12746
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012747 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012748 max = TG3_SERDES_TIMEOUT_SEC;
12749 else
12750 max = TG3_COPPER_TIMEOUT_SEC;
12751
12752 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012753 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012754 return 0;
12755
12756 if (msleep_interruptible(1000))
12757 break;
12758 }
12759
12760 return -EIO;
12761}
12762
Michael Chana71116d2005-05-29 14:58:11 -070012763/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012764static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012765{
Michael Chanb16250e2006-09-27 16:10:14 -070012766 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012767 u32 offset, read_mask, write_mask, val, save_val, read_val;
12768 static struct {
12769 u16 offset;
12770 u16 flags;
12771#define TG3_FL_5705 0x1
12772#define TG3_FL_NOT_5705 0x2
12773#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012774#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012775 u32 read_mask;
12776 u32 write_mask;
12777 } reg_tbl[] = {
12778 /* MAC Control Registers */
12779 { MAC_MODE, TG3_FL_NOT_5705,
12780 0x00000000, 0x00ef6f8c },
12781 { MAC_MODE, TG3_FL_5705,
12782 0x00000000, 0x01ef6b8c },
12783 { MAC_STATUS, TG3_FL_NOT_5705,
12784 0x03800107, 0x00000000 },
12785 { MAC_STATUS, TG3_FL_5705,
12786 0x03800100, 0x00000000 },
12787 { MAC_ADDR_0_HIGH, 0x0000,
12788 0x00000000, 0x0000ffff },
12789 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012790 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012791 { MAC_RX_MTU_SIZE, 0x0000,
12792 0x00000000, 0x0000ffff },
12793 { MAC_TX_MODE, 0x0000,
12794 0x00000000, 0x00000070 },
12795 { MAC_TX_LENGTHS, 0x0000,
12796 0x00000000, 0x00003fff },
12797 { MAC_RX_MODE, TG3_FL_NOT_5705,
12798 0x00000000, 0x000007fc },
12799 { MAC_RX_MODE, TG3_FL_5705,
12800 0x00000000, 0x000007dc },
12801 { MAC_HASH_REG_0, 0x0000,
12802 0x00000000, 0xffffffff },
12803 { MAC_HASH_REG_1, 0x0000,
12804 0x00000000, 0xffffffff },
12805 { MAC_HASH_REG_2, 0x0000,
12806 0x00000000, 0xffffffff },
12807 { MAC_HASH_REG_3, 0x0000,
12808 0x00000000, 0xffffffff },
12809
12810 /* Receive Data and Receive BD Initiator Control Registers. */
12811 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12812 0x00000000, 0xffffffff },
12813 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12814 0x00000000, 0xffffffff },
12815 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12816 0x00000000, 0x00000003 },
12817 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12818 0x00000000, 0xffffffff },
12819 { RCVDBDI_STD_BD+0, 0x0000,
12820 0x00000000, 0xffffffff },
12821 { RCVDBDI_STD_BD+4, 0x0000,
12822 0x00000000, 0xffffffff },
12823 { RCVDBDI_STD_BD+8, 0x0000,
12824 0x00000000, 0xffff0002 },
12825 { RCVDBDI_STD_BD+0xc, 0x0000,
12826 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012827
Michael Chana71116d2005-05-29 14:58:11 -070012828 /* Receive BD Initiator Control Registers. */
12829 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12830 0x00000000, 0xffffffff },
12831 { RCVBDI_STD_THRESH, TG3_FL_5705,
12832 0x00000000, 0x000003ff },
12833 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12834 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012835
Michael Chana71116d2005-05-29 14:58:11 -070012836 /* Host Coalescing Control Registers. */
12837 { HOSTCC_MODE, TG3_FL_NOT_5705,
12838 0x00000000, 0x00000004 },
12839 { HOSTCC_MODE, TG3_FL_5705,
12840 0x00000000, 0x000000f6 },
12841 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12842 0x00000000, 0xffffffff },
12843 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12844 0x00000000, 0x000003ff },
12845 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12846 0x00000000, 0xffffffff },
12847 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12848 0x00000000, 0x000003ff },
12849 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12850 0x00000000, 0xffffffff },
12851 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12852 0x00000000, 0x000000ff },
12853 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12854 0x00000000, 0xffffffff },
12855 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12856 0x00000000, 0x000000ff },
12857 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12858 0x00000000, 0xffffffff },
12859 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12860 0x00000000, 0xffffffff },
12861 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12862 0x00000000, 0xffffffff },
12863 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12864 0x00000000, 0x000000ff },
12865 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12866 0x00000000, 0xffffffff },
12867 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12868 0x00000000, 0x000000ff },
12869 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12870 0x00000000, 0xffffffff },
12871 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12872 0x00000000, 0xffffffff },
12873 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12874 0x00000000, 0xffffffff },
12875 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12876 0x00000000, 0xffffffff },
12877 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12878 0x00000000, 0xffffffff },
12879 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12880 0xffffffff, 0x00000000 },
12881 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12882 0xffffffff, 0x00000000 },
12883
12884 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012885 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012886 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012887 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012888 0x00000000, 0x007fffff },
12889 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12890 0x00000000, 0x0000003f },
12891 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12892 0x00000000, 0x000001ff },
12893 { BUFMGR_MB_HIGH_WATER, 0x0000,
12894 0x00000000, 0x000001ff },
12895 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12896 0xffffffff, 0x00000000 },
12897 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12898 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012899
Michael Chana71116d2005-05-29 14:58:11 -070012900 /* Mailbox Registers */
12901 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12902 0x00000000, 0x000001ff },
12903 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12904 0x00000000, 0x000001ff },
12905 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12906 0x00000000, 0x000007ff },
12907 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12908 0x00000000, 0x000001ff },
12909
12910 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12911 };
12912
Michael Chanb16250e2006-09-27 16:10:14 -070012913 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012914 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012915 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012916 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012917 is_5750 = 1;
12918 }
Michael Chana71116d2005-05-29 14:58:11 -070012919
12920 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12921 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12922 continue;
12923
12924 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12925 continue;
12926
Joe Perches63c3a662011-04-26 08:12:10 +000012927 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012928 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12929 continue;
12930
Michael Chanb16250e2006-09-27 16:10:14 -070012931 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12932 continue;
12933
Michael Chana71116d2005-05-29 14:58:11 -070012934 offset = (u32) reg_tbl[i].offset;
12935 read_mask = reg_tbl[i].read_mask;
12936 write_mask = reg_tbl[i].write_mask;
12937
12938 /* Save the original register content */
12939 save_val = tr32(offset);
12940
12941 /* Determine the read-only value. */
12942 read_val = save_val & read_mask;
12943
12944 /* Write zero to the register, then make sure the read-only bits
12945 * are not changed and the read/write bits are all zeros.
12946 */
12947 tw32(offset, 0);
12948
12949 val = tr32(offset);
12950
12951 /* Test the read-only and read/write bits. */
12952 if (((val & read_mask) != read_val) || (val & write_mask))
12953 goto out;
12954
12955 /* Write ones to all the bits defined by RdMask and WrMask, then
12956 * make sure the read-only bits are not changed and the
12957 * read/write bits are all ones.
12958 */
12959 tw32(offset, read_mask | write_mask);
12960
12961 val = tr32(offset);
12962
12963 /* Test the read-only bits. */
12964 if ((val & read_mask) != read_val)
12965 goto out;
12966
12967 /* Test the read/write bits. */
12968 if ((val & write_mask) != write_mask)
12969 goto out;
12970
12971 tw32(offset, save_val);
12972 }
12973
12974 return 0;
12975
12976out:
Michael Chan9f88f292006-12-07 00:22:54 -080012977 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012978 netdev_err(tp->dev,
12979 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012980 tw32(offset, save_val);
12981 return -EIO;
12982}
12983
Michael Chan7942e1d2005-05-29 14:58:36 -070012984static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12985{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012986 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012987 int i;
12988 u32 j;
12989
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012990 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012991 for (j = 0; j < len; j += 4) {
12992 u32 val;
12993
12994 tg3_write_mem(tp, offset + j, test_pattern[i]);
12995 tg3_read_mem(tp, offset + j, &val);
12996 if (val != test_pattern[i])
12997 return -EIO;
12998 }
12999 }
13000 return 0;
13001}
13002
13003static int tg3_test_memory(struct tg3 *tp)
13004{
13005 static struct mem_entry {
13006 u32 offset;
13007 u32 len;
13008 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013009 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013010 { 0x00002000, 0x1c000},
13011 { 0xffffffff, 0x00000}
13012 }, mem_tbl_5705[] = {
13013 { 0x00000100, 0x0000c},
13014 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013015 { 0x00004000, 0x00800},
13016 { 0x00006000, 0x01000},
13017 { 0x00008000, 0x02000},
13018 { 0x00010000, 0x0e000},
13019 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013020 }, mem_tbl_5755[] = {
13021 { 0x00000200, 0x00008},
13022 { 0x00004000, 0x00800},
13023 { 0x00006000, 0x00800},
13024 { 0x00008000, 0x02000},
13025 { 0x00010000, 0x0c000},
13026 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013027 }, mem_tbl_5906[] = {
13028 { 0x00000200, 0x00008},
13029 { 0x00004000, 0x00400},
13030 { 0x00006000, 0x00400},
13031 { 0x00008000, 0x01000},
13032 { 0x00010000, 0x01000},
13033 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013034 }, mem_tbl_5717[] = {
13035 { 0x00000200, 0x00008},
13036 { 0x00010000, 0x0a000},
13037 { 0x00020000, 0x13c00},
13038 { 0xffffffff, 0x00000}
13039 }, mem_tbl_57765[] = {
13040 { 0x00000200, 0x00008},
13041 { 0x00004000, 0x00800},
13042 { 0x00006000, 0x09800},
13043 { 0x00010000, 0x0a000},
13044 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013045 };
13046 struct mem_entry *mem_tbl;
13047 int err = 0;
13048 int i;
13049
Joe Perches63c3a662011-04-26 08:12:10 +000013050 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013051 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013052 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013053 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013054 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013055 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013056 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013057 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013058 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013059 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013060 mem_tbl = mem_tbl_5705;
13061 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013062 mem_tbl = mem_tbl_570x;
13063
13064 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013065 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13066 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013067 break;
13068 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013069
Michael Chan7942e1d2005-05-29 14:58:36 -070013070 return err;
13071}
13072
Matt Carlsonbb158d62011-04-25 12:42:47 +000013073#define TG3_TSO_MSS 500
13074
13075#define TG3_TSO_IP_HDR_LEN 20
13076#define TG3_TSO_TCP_HDR_LEN 20
13077#define TG3_TSO_TCP_OPT_LEN 12
13078
13079static const u8 tg3_tso_header[] = {
130800x08, 0x00,
130810x45, 0x00, 0x00, 0x00,
130820x00, 0x00, 0x40, 0x00,
130830x40, 0x06, 0x00, 0x00,
130840x0a, 0x00, 0x00, 0x01,
130850x0a, 0x00, 0x00, 0x02,
130860x0d, 0x00, 0xe0, 0x00,
130870x00, 0x00, 0x01, 0x00,
130880x00, 0x00, 0x02, 0x00,
130890x80, 0x10, 0x10, 0x00,
130900x14, 0x09, 0x00, 0x00,
130910x01, 0x01, 0x08, 0x0a,
130920x11, 0x11, 0x11, 0x11,
130930x11, 0x11, 0x11, 0x11,
13094};
Michael Chan9f40dea2005-09-05 17:53:06 -070013095
Matt Carlson28a45952011-08-19 13:58:22 +000013096static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013097{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013098 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013099 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013100 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013101 struct sk_buff *skb;
13102 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013103 dma_addr_t map;
13104 int num_pkts, tx_len, rx_len, i, err;
13105 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013106 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013107 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013108
Matt Carlsonc8873402010-02-12 14:47:11 +000013109 tnapi = &tp->napi[0];
13110 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013111 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013112 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013113 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013114 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013115 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013116 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013117 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013118
Michael Chanc76949a2005-05-29 14:58:59 -070013119 err = -EIO;
13120
Matt Carlson4852a862011-04-13 11:05:07 +000013121 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013122 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013123 if (!skb)
13124 return -ENOMEM;
13125
Michael Chanc76949a2005-05-29 14:58:59 -070013126 tx_data = skb_put(skb, tx_len);
13127 memcpy(tx_data, tp->dev->dev_addr, 6);
13128 memset(tx_data + 6, 0x0, 8);
13129
Matt Carlson4852a862011-04-13 11:05:07 +000013130 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013131
Matt Carlson28a45952011-08-19 13:58:22 +000013132 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013133 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13134
13135 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13136 TG3_TSO_TCP_OPT_LEN;
13137
13138 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13139 sizeof(tg3_tso_header));
13140 mss = TG3_TSO_MSS;
13141
13142 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13143 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13144
13145 /* Set the total length field in the IP header */
13146 iph->tot_len = htons((u16)(mss + hdr_len));
13147
13148 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13149 TXD_FLAG_CPU_POST_DMA);
13150
Joe Perches63c3a662011-04-26 08:12:10 +000013151 if (tg3_flag(tp, HW_TSO_1) ||
13152 tg3_flag(tp, HW_TSO_2) ||
13153 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013154 struct tcphdr *th;
13155 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13156 th = (struct tcphdr *)&tx_data[val];
13157 th->check = 0;
13158 } else
13159 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13160
Joe Perches63c3a662011-04-26 08:12:10 +000013161 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013162 mss |= (hdr_len & 0xc) << 12;
13163 if (hdr_len & 0x10)
13164 base_flags |= 0x00000010;
13165 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013166 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013167 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013168 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013169 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013170 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13171 } else {
13172 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13173 }
13174
13175 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13176 } else {
13177 num_pkts = 1;
13178 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013179
13180 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13181 tx_len > VLAN_ETH_FRAME_LEN)
13182 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013183 }
13184
13185 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013186 tx_data[i] = (u8) (i & 0xff);
13187
Alexander Duyckf4188d82009-12-02 16:48:38 +000013188 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13189 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013190 dev_kfree_skb(skb);
13191 return -EIO;
13192 }
Michael Chanc76949a2005-05-29 14:58:59 -070013193
Matt Carlson0d681b22011-07-27 14:20:49 +000013194 val = tnapi->tx_prod;
13195 tnapi->tx_buffers[val].skb = skb;
13196 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13197
Michael Chanc76949a2005-05-29 14:58:59 -070013198 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013199 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013200
13201 udelay(10);
13202
Matt Carlson898a56f2009-08-28 14:02:40 +000013203 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013204
Matt Carlson84b67b22011-07-27 14:20:52 +000013205 budget = tg3_tx_avail(tnapi);
13206 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013207 base_flags | TXD_FLAG_END, mss, 0)) {
13208 tnapi->tx_buffers[val].skb = NULL;
13209 dev_kfree_skb(skb);
13210 return -EIO;
13211 }
Michael Chanc76949a2005-05-29 14:58:59 -070013212
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013213 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013214
Michael Chan6541b802012-03-04 14:48:14 +000013215 /* Sync BD data before updating mailbox */
13216 wmb();
13217
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013218 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13219 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013220
13221 udelay(10);
13222
Matt Carlson303fc922009-11-02 14:27:34 +000013223 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13224 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013225 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013226 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013227
13228 udelay(10);
13229
Matt Carlson898a56f2009-08-28 14:02:40 +000013230 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13231 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013232 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013233 (rx_idx == (rx_start_idx + num_pkts)))
13234 break;
13235 }
13236
Matt Carlsonba1142e2011-11-04 09:15:00 +000013237 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013238 dev_kfree_skb(skb);
13239
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013240 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013241 goto out;
13242
13243 if (rx_idx != rx_start_idx + num_pkts)
13244 goto out;
13245
Matt Carlsonbb158d62011-04-25 12:42:47 +000013246 val = data_off;
13247 while (rx_idx != rx_start_idx) {
13248 desc = &rnapi->rx_rcb[rx_start_idx++];
13249 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13250 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013251
Matt Carlsonbb158d62011-04-25 12:42:47 +000013252 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13253 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013254 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013255
Matt Carlsonbb158d62011-04-25 12:42:47 +000013256 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13257 - ETH_FCS_LEN;
13258
Matt Carlson28a45952011-08-19 13:58:22 +000013259 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013260 if (rx_len != tx_len)
13261 goto out;
13262
13263 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13264 if (opaque_key != RXD_OPAQUE_RING_STD)
13265 goto out;
13266 } else {
13267 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13268 goto out;
13269 }
13270 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13271 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013272 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013273 goto out;
13274 }
13275
13276 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013277 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013278 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13279 mapping);
13280 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013281 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013282 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13283 mapping);
13284 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013285 goto out;
13286
Matt Carlsonbb158d62011-04-25 12:42:47 +000013287 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13288 PCI_DMA_FROMDEVICE);
13289
Eric Dumazet9205fd92011-11-18 06:47:01 +000013290 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013291 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013292 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013293 goto out;
13294 }
Matt Carlson4852a862011-04-13 11:05:07 +000013295 }
13296
Michael Chanc76949a2005-05-29 14:58:59 -070013297 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013298
Eric Dumazet9205fd92011-11-18 06:47:01 +000013299 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013300out:
13301 return err;
13302}
13303
Matt Carlson00c266b2011-04-25 12:42:46 +000013304#define TG3_STD_LOOPBACK_FAILED 1
13305#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013306#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013307#define TG3_LOOPBACK_FAILED \
13308 (TG3_STD_LOOPBACK_FAILED | \
13309 TG3_JMB_LOOPBACK_FAILED | \
13310 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013311
Matt Carlson941ec902011-08-19 13:58:23 +000013312static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013313{
Matt Carlson28a45952011-08-19 13:58:22 +000013314 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013315 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013316 u32 jmb_pkt_sz = 9000;
13317
13318 if (tp->dma_limit)
13319 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013320
Matt Carlsonab789042011-01-25 15:58:54 +000013321 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13322 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13323
Matt Carlson28a45952011-08-19 13:58:22 +000013324 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013325 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13326 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013327 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013328 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013329 goto done;
13330 }
13331
Joe Perches953c96e2013-04-09 10:18:14 +000013332 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013333 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013334 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13335 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013336 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013337 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013338 goto done;
13339 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013340
Joe Perches63c3a662011-04-26 08:12:10 +000013341 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013342 int i;
13343
13344 /* Reroute all rx packets to the 1st queue */
13345 for (i = MAC_RSS_INDIR_TBL_0;
13346 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13347 tw32(i, 0x0);
13348 }
13349
Matt Carlson6e01b202011-08-19 13:58:20 +000013350 /* HW errata - mac loopback fails in some cases on 5780.
13351 * Normal traffic and PHY loopback are not affected by
13352 * errata. Also, the MAC loopback test is deprecated for
13353 * all newer ASIC revisions.
13354 */
Joe Perches41535772013-02-16 11:20:04 +000013355 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013356 !tg3_flag(tp, CPMU_PRESENT)) {
13357 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013358
Matt Carlson28a45952011-08-19 13:58:22 +000013359 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013360 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013361
13362 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013363 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013364 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013365
13366 tg3_mac_loopback(tp, false);
13367 }
Matt Carlson4852a862011-04-13 11:05:07 +000013368
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013369 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013370 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013371 int i;
13372
Matt Carlson941ec902011-08-19 13:58:23 +000013373 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013374
13375 /* Wait for link */
13376 for (i = 0; i < 100; i++) {
13377 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13378 break;
13379 mdelay(1);
13380 }
13381
Matt Carlson28a45952011-08-19 13:58:22 +000013382 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013383 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013384 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013385 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013386 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013387 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013388 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013389 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013390
Matt Carlson941ec902011-08-19 13:58:23 +000013391 if (do_extlpbk) {
13392 tg3_phy_lpbk_set(tp, 0, true);
13393
13394 /* All link indications report up, but the hardware
13395 * isn't really ready for about 20 msec. Double it
13396 * to be sure.
13397 */
13398 mdelay(40);
13399
13400 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013401 data[TG3_EXT_LOOPB_TEST] |=
13402 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013403 if (tg3_flag(tp, TSO_CAPABLE) &&
13404 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013405 data[TG3_EXT_LOOPB_TEST] |=
13406 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013407 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013408 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013409 data[TG3_EXT_LOOPB_TEST] |=
13410 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013411 }
13412
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013413 /* Re-enable gphy autopowerdown. */
13414 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13415 tg3_phy_toggle_apd(tp, true);
13416 }
Matt Carlson6833c042008-11-21 17:18:59 -080013417
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013418 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13419 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013420
Matt Carlsonab789042011-01-25 15:58:54 +000013421done:
13422 tp->phy_flags |= eee_cap;
13423
Michael Chan9f40dea2005-09-05 17:53:06 -070013424 return err;
13425}
13426
Michael Chan4cafd3f2005-05-29 14:56:34 -070013427static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13428 u64 *data)
13429{
Michael Chan566f86a2005-05-29 14:56:58 -070013430 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013431 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013432
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013433 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13434 if (tg3_power_up(tp)) {
13435 etest->flags |= ETH_TEST_FL_FAILED;
13436 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13437 return;
13438 }
13439 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013440 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013441
Michael Chan566f86a2005-05-29 14:56:58 -070013442 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13443
13444 if (tg3_test_nvram(tp) != 0) {
13445 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013446 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013447 }
Matt Carlson941ec902011-08-19 13:58:23 +000013448 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013449 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013450 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013451 }
Michael Chana71116d2005-05-29 14:58:11 -070013452 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013453 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013454
Michael Chanbbe832c2005-06-24 20:20:04 -070013455 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013456 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013457 tg3_netif_stop(tp);
13458 irq_sync = 1;
13459 }
13460
13461 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013462 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013463 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013464 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013465 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013466 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013467 if (!err)
13468 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013469
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013470 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013471 tg3_phy_reset(tp);
13472
Michael Chana71116d2005-05-29 14:58:11 -070013473 if (tg3_test_registers(tp) != 0) {
13474 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013475 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013476 }
Matt Carlson28a45952011-08-19 13:58:22 +000013477
Michael Chan7942e1d2005-05-29 14:58:36 -070013478 if (tg3_test_memory(tp) != 0) {
13479 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013480 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013481 }
Matt Carlson28a45952011-08-19 13:58:22 +000013482
Matt Carlson941ec902011-08-19 13:58:23 +000013483 if (doextlpbk)
13484 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13485
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013486 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013487 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013488
David S. Millerf47c11e2005-06-24 20:18:35 -070013489 tg3_full_unlock(tp);
13490
Michael Chand4bc3922005-05-29 14:59:20 -070013491 if (tg3_test_interrupt(tp) != 0) {
13492 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013493 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013494 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013495
13496 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013497
Michael Chana71116d2005-05-29 14:58:11 -070013498 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13499 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013500 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013501 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013502 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013503 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013504 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013505
13506 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013507
13508 if (irq_sync && !err2)
13509 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013510 }
Matt Carlson80096062010-08-02 11:26:06 +000013511 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013512 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013513
Michael Chan4cafd3f2005-05-29 14:56:34 -070013514}
13515
Matt Carlson0a633ac2012-12-03 19:36:59 +000013516static int tg3_hwtstamp_ioctl(struct net_device *dev,
13517 struct ifreq *ifr, int cmd)
13518{
13519 struct tg3 *tp = netdev_priv(dev);
13520 struct hwtstamp_config stmpconf;
13521
13522 if (!tg3_flag(tp, PTP_CAPABLE))
13523 return -EINVAL;
13524
13525 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13526 return -EFAULT;
13527
13528 if (stmpconf.flags)
13529 return -EINVAL;
13530
13531 switch (stmpconf.tx_type) {
13532 case HWTSTAMP_TX_ON:
13533 tg3_flag_set(tp, TX_TSTAMP_EN);
13534 break;
13535 case HWTSTAMP_TX_OFF:
13536 tg3_flag_clear(tp, TX_TSTAMP_EN);
13537 break;
13538 default:
13539 return -ERANGE;
13540 }
13541
13542 switch (stmpconf.rx_filter) {
13543 case HWTSTAMP_FILTER_NONE:
13544 tp->rxptpctl = 0;
13545 break;
13546 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13547 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13548 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13549 break;
13550 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13551 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13552 TG3_RX_PTP_CTL_SYNC_EVNT;
13553 break;
13554 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13555 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13556 TG3_RX_PTP_CTL_DELAY_REQ;
13557 break;
13558 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13559 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13560 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13561 break;
13562 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13563 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13564 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13565 break;
13566 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13567 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13568 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13569 break;
13570 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13571 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13572 TG3_RX_PTP_CTL_SYNC_EVNT;
13573 break;
13574 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13575 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13576 TG3_RX_PTP_CTL_SYNC_EVNT;
13577 break;
13578 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13579 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13580 TG3_RX_PTP_CTL_SYNC_EVNT;
13581 break;
13582 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13583 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13584 TG3_RX_PTP_CTL_DELAY_REQ;
13585 break;
13586 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13587 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13588 TG3_RX_PTP_CTL_DELAY_REQ;
13589 break;
13590 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13591 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13592 TG3_RX_PTP_CTL_DELAY_REQ;
13593 break;
13594 default:
13595 return -ERANGE;
13596 }
13597
13598 if (netif_running(dev) && tp->rxptpctl)
13599 tw32(TG3_RX_PTP_CTL,
13600 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13601
13602 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13603 -EFAULT : 0;
13604}
13605
Linus Torvalds1da177e2005-04-16 15:20:36 -070013606static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13607{
13608 struct mii_ioctl_data *data = if_mii(ifr);
13609 struct tg3 *tp = netdev_priv(dev);
13610 int err;
13611
Joe Perches63c3a662011-04-26 08:12:10 +000013612 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013613 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013614 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013615 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013616 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013617 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013618 }
13619
Matt Carlson33f401a2010-04-05 10:19:27 +000013620 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013621 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013622 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013623
13624 /* fallthru */
13625 case SIOCGMIIREG: {
13626 u32 mii_regval;
13627
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013628 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013629 break; /* We have no PHY */
13630
Matt Carlson34eea5a2011-04-20 07:57:38 +000013631 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013632 return -EAGAIN;
13633
David S. Millerf47c11e2005-06-24 20:18:35 -070013634 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013635 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13636 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013637 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013638
13639 data->val_out = mii_regval;
13640
13641 return err;
13642 }
13643
13644 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013645 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013646 break; /* We have no PHY */
13647
Matt Carlson34eea5a2011-04-20 07:57:38 +000013648 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013649 return -EAGAIN;
13650
David S. Millerf47c11e2005-06-24 20:18:35 -070013651 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013652 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13653 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013654 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013655
13656 return err;
13657
Matt Carlson0a633ac2012-12-03 19:36:59 +000013658 case SIOCSHWTSTAMP:
13659 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13660
Linus Torvalds1da177e2005-04-16 15:20:36 -070013661 default:
13662 /* do nothing */
13663 break;
13664 }
13665 return -EOPNOTSUPP;
13666}
13667
David S. Miller15f98502005-05-18 22:49:26 -070013668static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13669{
13670 struct tg3 *tp = netdev_priv(dev);
13671
13672 memcpy(ec, &tp->coal, sizeof(*ec));
13673 return 0;
13674}
13675
Michael Chand244c892005-07-05 14:42:33 -070013676static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13677{
13678 struct tg3 *tp = netdev_priv(dev);
13679 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13680 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13681
Joe Perches63c3a662011-04-26 08:12:10 +000013682 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013683 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13684 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13685 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13686 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13687 }
13688
13689 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13690 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13691 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13692 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13693 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13694 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13695 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13696 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13697 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13698 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13699 return -EINVAL;
13700
13701 /* No rx interrupts will be generated if both are zero */
13702 if ((ec->rx_coalesce_usecs == 0) &&
13703 (ec->rx_max_coalesced_frames == 0))
13704 return -EINVAL;
13705
13706 /* No tx interrupts will be generated if both are zero */
13707 if ((ec->tx_coalesce_usecs == 0) &&
13708 (ec->tx_max_coalesced_frames == 0))
13709 return -EINVAL;
13710
13711 /* Only copy relevant parameters, ignore all others. */
13712 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13713 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13714 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13715 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13716 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13717 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13718 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13719 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13720 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13721
13722 if (netif_running(dev)) {
13723 tg3_full_lock(tp, 0);
13724 __tg3_set_coalesce(tp, &tp->coal);
13725 tg3_full_unlock(tp);
13726 }
13727 return 0;
13728}
13729
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013730static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
13731{
13732 struct tg3 *tp = netdev_priv(dev);
13733
13734 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13735 netdev_warn(tp->dev, "Board does not support EEE!\n");
13736 return -EOPNOTSUPP;
13737 }
13738
13739 if (edata->advertised != tp->eee.advertised) {
13740 netdev_warn(tp->dev,
13741 "Direct manipulation of EEE advertisement is not supported\n");
13742 return -EINVAL;
13743 }
13744
13745 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
13746 netdev_warn(tp->dev,
13747 "Maximal Tx Lpi timer supported is %#x(u)\n",
13748 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
13749 return -EINVAL;
13750 }
13751
13752 tp->eee = *edata;
13753
13754 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
13755 tg3_warn_mgmt_link_flap(tp);
13756
13757 if (netif_running(tp->dev)) {
13758 tg3_full_lock(tp, 0);
13759 tg3_setup_eee(tp);
13760 tg3_phy_reset(tp);
13761 tg3_full_unlock(tp);
13762 }
13763
13764 return 0;
13765}
13766
13767static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
13768{
13769 struct tg3 *tp = netdev_priv(dev);
13770
13771 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13772 netdev_warn(tp->dev,
13773 "Board does not support EEE!\n");
13774 return -EOPNOTSUPP;
13775 }
13776
13777 *edata = tp->eee;
13778 return 0;
13779}
13780
Jeff Garzik7282d492006-09-13 14:30:00 -040013781static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013782 .get_settings = tg3_get_settings,
13783 .set_settings = tg3_set_settings,
13784 .get_drvinfo = tg3_get_drvinfo,
13785 .get_regs_len = tg3_get_regs_len,
13786 .get_regs = tg3_get_regs,
13787 .get_wol = tg3_get_wol,
13788 .set_wol = tg3_set_wol,
13789 .get_msglevel = tg3_get_msglevel,
13790 .set_msglevel = tg3_set_msglevel,
13791 .nway_reset = tg3_nway_reset,
13792 .get_link = ethtool_op_get_link,
13793 .get_eeprom_len = tg3_get_eeprom_len,
13794 .get_eeprom = tg3_get_eeprom,
13795 .set_eeprom = tg3_set_eeprom,
13796 .get_ringparam = tg3_get_ringparam,
13797 .set_ringparam = tg3_set_ringparam,
13798 .get_pauseparam = tg3_get_pauseparam,
13799 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013800 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013801 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013802 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013803 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013804 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013805 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013806 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013807 .get_rxnfc = tg3_get_rxnfc,
13808 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13809 .get_rxfh_indir = tg3_get_rxfh_indir,
13810 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013811 .get_channels = tg3_get_channels,
13812 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013813 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013814 .get_eee = tg3_get_eee,
13815 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013816};
13817
David S. Millerb4017c52012-03-01 17:57:40 -050013818static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13819 struct rtnl_link_stats64 *stats)
13820{
13821 struct tg3 *tp = netdev_priv(dev);
13822
David S. Millerb4017c52012-03-01 17:57:40 -050013823 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013824 if (!tp->hw_stats) {
13825 spin_unlock_bh(&tp->lock);
13826 return &tp->net_stats_prev;
13827 }
13828
David S. Millerb4017c52012-03-01 17:57:40 -050013829 tg3_get_nstats(tp, stats);
13830 spin_unlock_bh(&tp->lock);
13831
13832 return stats;
13833}
13834
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013835static void tg3_set_rx_mode(struct net_device *dev)
13836{
13837 struct tg3 *tp = netdev_priv(dev);
13838
13839 if (!netif_running(dev))
13840 return;
13841
13842 tg3_full_lock(tp, 0);
13843 __tg3_set_rx_mode(dev);
13844 tg3_full_unlock(tp);
13845}
13846
Matt Carlsonfaf16272012-02-13 10:20:07 +000013847static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13848 int new_mtu)
13849{
13850 dev->mtu = new_mtu;
13851
13852 if (new_mtu > ETH_DATA_LEN) {
13853 if (tg3_flag(tp, 5780_CLASS)) {
13854 netdev_update_features(dev);
13855 tg3_flag_clear(tp, TSO_CAPABLE);
13856 } else {
13857 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13858 }
13859 } else {
13860 if (tg3_flag(tp, 5780_CLASS)) {
13861 tg3_flag_set(tp, TSO_CAPABLE);
13862 netdev_update_features(dev);
13863 }
13864 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13865 }
13866}
13867
13868static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13869{
13870 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000013871 int err;
13872 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013873
13874 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13875 return -EINVAL;
13876
13877 if (!netif_running(dev)) {
13878 /* We'll just catch it later when the
13879 * device is up'd.
13880 */
13881 tg3_set_mtu(dev, tp, new_mtu);
13882 return 0;
13883 }
13884
13885 tg3_phy_stop(tp);
13886
13887 tg3_netif_stop(tp);
13888
13889 tg3_full_lock(tp, 1);
13890
13891 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13892
13893 tg3_set_mtu(dev, tp, new_mtu);
13894
Michael Chan2fae5e32012-03-04 14:48:15 +000013895 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13896 * breaks all requests to 256 bytes.
13897 */
Joe Perches41535772013-02-16 11:20:04 +000013898 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000013899 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000013900
13901 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013902
13903 if (!err)
13904 tg3_netif_start(tp);
13905
13906 tg3_full_unlock(tp);
13907
13908 if (!err)
13909 tg3_phy_start(tp);
13910
13911 return err;
13912}
13913
13914static const struct net_device_ops tg3_netdev_ops = {
13915 .ndo_open = tg3_open,
13916 .ndo_stop = tg3_close,
13917 .ndo_start_xmit = tg3_start_xmit,
13918 .ndo_get_stats64 = tg3_get_stats64,
13919 .ndo_validate_addr = eth_validate_addr,
13920 .ndo_set_rx_mode = tg3_set_rx_mode,
13921 .ndo_set_mac_address = tg3_set_mac_addr,
13922 .ndo_do_ioctl = tg3_ioctl,
13923 .ndo_tx_timeout = tg3_tx_timeout,
13924 .ndo_change_mtu = tg3_change_mtu,
13925 .ndo_fix_features = tg3_fix_features,
13926 .ndo_set_features = tg3_set_features,
13927#ifdef CONFIG_NET_POLL_CONTROLLER
13928 .ndo_poll_controller = tg3_poll_controller,
13929#endif
13930};
13931
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013932static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013933{
Michael Chan1b277772006-03-20 22:27:48 -080013934 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013935
13936 tp->nvram_size = EEPROM_CHIP_SIZE;
13937
Matt Carlsone4f34112009-02-25 14:25:00 +000013938 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013939 return;
13940
Michael Chanb16250e2006-09-27 16:10:14 -070013941 if ((magic != TG3_EEPROM_MAGIC) &&
13942 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13943 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013944 return;
13945
13946 /*
13947 * Size the chip by reading offsets at increasing powers of two.
13948 * When we encounter our validation signature, we know the addressing
13949 * has wrapped around, and thus have our chip size.
13950 */
Michael Chan1b277772006-03-20 22:27:48 -080013951 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013952
13953 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013954 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013955 return;
13956
Michael Chan18201802006-03-20 22:29:15 -080013957 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013958 break;
13959
13960 cursize <<= 1;
13961 }
13962
13963 tp->nvram_size = cursize;
13964}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013965
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013966static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013967{
13968 u32 val;
13969
Joe Perches63c3a662011-04-26 08:12:10 +000013970 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013971 return;
13972
13973 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013974 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013975 tg3_get_eeprom_size(tp);
13976 return;
13977 }
13978
Matt Carlson6d348f22009-02-25 14:25:52 +000013979 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013981 /* This is confusing. We want to operate on the
13982 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13983 * call will read from NVRAM and byteswap the data
13984 * according to the byteswapping settings for all
13985 * other register accesses. This ensures the data we
13986 * want will always reside in the lower 16-bits.
13987 * However, the data in NVRAM is in LE format, which
13988 * means the data from the NVRAM read will always be
13989 * opposite the endianness of the CPU. The 16-bit
13990 * byteswap then brings the data to CPU endianness.
13991 */
13992 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013993 return;
13994 }
13995 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013996 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013997}
13998
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013999static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014000{
14001 u32 nvcfg1;
14002
14003 nvcfg1 = tr32(NVRAM_CFG1);
14004 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014005 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014006 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014007 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14008 tw32(NVRAM_CFG1, nvcfg1);
14009 }
14010
Joe Perches41535772013-02-16 11:20:04 +000014011 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014012 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014014 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14015 tp->nvram_jedecnum = JEDEC_ATMEL;
14016 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014017 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014018 break;
14019 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14020 tp->nvram_jedecnum = JEDEC_ATMEL;
14021 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14022 break;
14023 case FLASH_VENDOR_ATMEL_EEPROM:
14024 tp->nvram_jedecnum = JEDEC_ATMEL;
14025 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014026 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014027 break;
14028 case FLASH_VENDOR_ST:
14029 tp->nvram_jedecnum = JEDEC_ST;
14030 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014031 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014032 break;
14033 case FLASH_VENDOR_SAIFUN:
14034 tp->nvram_jedecnum = JEDEC_SAIFUN;
14035 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14036 break;
14037 case FLASH_VENDOR_SST_SMALL:
14038 case FLASH_VENDOR_SST_LARGE:
14039 tp->nvram_jedecnum = JEDEC_SST;
14040 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014042 }
Matt Carlson8590a602009-08-28 12:29:16 +000014043 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014044 tp->nvram_jedecnum = JEDEC_ATMEL;
14045 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014046 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014047 }
14048}
14049
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014050static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014051{
14052 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14053 case FLASH_5752PAGE_SIZE_256:
14054 tp->nvram_pagesize = 256;
14055 break;
14056 case FLASH_5752PAGE_SIZE_512:
14057 tp->nvram_pagesize = 512;
14058 break;
14059 case FLASH_5752PAGE_SIZE_1K:
14060 tp->nvram_pagesize = 1024;
14061 break;
14062 case FLASH_5752PAGE_SIZE_2K:
14063 tp->nvram_pagesize = 2048;
14064 break;
14065 case FLASH_5752PAGE_SIZE_4K:
14066 tp->nvram_pagesize = 4096;
14067 break;
14068 case FLASH_5752PAGE_SIZE_264:
14069 tp->nvram_pagesize = 264;
14070 break;
14071 case FLASH_5752PAGE_SIZE_528:
14072 tp->nvram_pagesize = 528;
14073 break;
14074 }
14075}
14076
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014077static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014078{
14079 u32 nvcfg1;
14080
14081 nvcfg1 = tr32(NVRAM_CFG1);
14082
Michael Chane6af3012005-04-21 17:12:05 -070014083 /* NVRAM protection for TPM */
14084 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014085 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014086
Michael Chan361b4ac2005-04-21 17:11:21 -070014087 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014088 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14089 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14090 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014091 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014092 break;
14093 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14094 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014095 tg3_flag_set(tp, NVRAM_BUFFERED);
14096 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014097 break;
14098 case FLASH_5752VENDOR_ST_M45PE10:
14099 case FLASH_5752VENDOR_ST_M45PE20:
14100 case FLASH_5752VENDOR_ST_M45PE40:
14101 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014102 tg3_flag_set(tp, NVRAM_BUFFERED);
14103 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014104 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014105 }
14106
Joe Perches63c3a662011-04-26 08:12:10 +000014107 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014108 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014109 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014110 /* For eeprom, set pagesize to maximum eeprom size */
14111 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14112
14113 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14114 tw32(NVRAM_CFG1, nvcfg1);
14115 }
14116}
14117
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014118static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014119{
Matt Carlson989a9d22007-05-05 11:51:05 -070014120 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014121
14122 nvcfg1 = tr32(NVRAM_CFG1);
14123
14124 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014125 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014126 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014127 protect = 1;
14128 }
Michael Chand3c7b882006-03-23 01:28:25 -080014129
Matt Carlson989a9d22007-05-05 11:51:05 -070014130 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14131 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014132 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14133 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14134 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14135 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14136 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014137 tg3_flag_set(tp, NVRAM_BUFFERED);
14138 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014139 tp->nvram_pagesize = 264;
14140 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14141 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14142 tp->nvram_size = (protect ? 0x3e200 :
14143 TG3_NVRAM_SIZE_512KB);
14144 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14145 tp->nvram_size = (protect ? 0x1f200 :
14146 TG3_NVRAM_SIZE_256KB);
14147 else
14148 tp->nvram_size = (protect ? 0x1f200 :
14149 TG3_NVRAM_SIZE_128KB);
14150 break;
14151 case FLASH_5752VENDOR_ST_M45PE10:
14152 case FLASH_5752VENDOR_ST_M45PE20:
14153 case FLASH_5752VENDOR_ST_M45PE40:
14154 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014155 tg3_flag_set(tp, NVRAM_BUFFERED);
14156 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014157 tp->nvram_pagesize = 256;
14158 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14159 tp->nvram_size = (protect ?
14160 TG3_NVRAM_SIZE_64KB :
14161 TG3_NVRAM_SIZE_128KB);
14162 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14163 tp->nvram_size = (protect ?
14164 TG3_NVRAM_SIZE_64KB :
14165 TG3_NVRAM_SIZE_256KB);
14166 else
14167 tp->nvram_size = (protect ?
14168 TG3_NVRAM_SIZE_128KB :
14169 TG3_NVRAM_SIZE_512KB);
14170 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014171 }
14172}
14173
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014174static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014175{
14176 u32 nvcfg1;
14177
14178 nvcfg1 = tr32(NVRAM_CFG1);
14179
14180 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014181 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14182 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14183 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14184 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14185 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014186 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014187 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014188
Matt Carlson8590a602009-08-28 12:29:16 +000014189 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14190 tw32(NVRAM_CFG1, nvcfg1);
14191 break;
14192 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14193 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14194 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14195 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14196 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014197 tg3_flag_set(tp, NVRAM_BUFFERED);
14198 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014199 tp->nvram_pagesize = 264;
14200 break;
14201 case FLASH_5752VENDOR_ST_M45PE10:
14202 case FLASH_5752VENDOR_ST_M45PE20:
14203 case FLASH_5752VENDOR_ST_M45PE40:
14204 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014205 tg3_flag_set(tp, NVRAM_BUFFERED);
14206 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014207 tp->nvram_pagesize = 256;
14208 break;
Michael Chan1b277772006-03-20 22:27:48 -080014209 }
14210}
14211
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014212static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014213{
14214 u32 nvcfg1, protect = 0;
14215
14216 nvcfg1 = tr32(NVRAM_CFG1);
14217
14218 /* NVRAM protection for TPM */
14219 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014220 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014221 protect = 1;
14222 }
14223
14224 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14225 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014226 case FLASH_5761VENDOR_ATMEL_ADB021D:
14227 case FLASH_5761VENDOR_ATMEL_ADB041D:
14228 case FLASH_5761VENDOR_ATMEL_ADB081D:
14229 case FLASH_5761VENDOR_ATMEL_ADB161D:
14230 case FLASH_5761VENDOR_ATMEL_MDB021D:
14231 case FLASH_5761VENDOR_ATMEL_MDB041D:
14232 case FLASH_5761VENDOR_ATMEL_MDB081D:
14233 case FLASH_5761VENDOR_ATMEL_MDB161D:
14234 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014235 tg3_flag_set(tp, NVRAM_BUFFERED);
14236 tg3_flag_set(tp, FLASH);
14237 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014238 tp->nvram_pagesize = 256;
14239 break;
14240 case FLASH_5761VENDOR_ST_A_M45PE20:
14241 case FLASH_5761VENDOR_ST_A_M45PE40:
14242 case FLASH_5761VENDOR_ST_A_M45PE80:
14243 case FLASH_5761VENDOR_ST_A_M45PE16:
14244 case FLASH_5761VENDOR_ST_M_M45PE20:
14245 case FLASH_5761VENDOR_ST_M_M45PE40:
14246 case FLASH_5761VENDOR_ST_M_M45PE80:
14247 case FLASH_5761VENDOR_ST_M_M45PE16:
14248 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014249 tg3_flag_set(tp, NVRAM_BUFFERED);
14250 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014251 tp->nvram_pagesize = 256;
14252 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014253 }
14254
14255 if (protect) {
14256 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14257 } else {
14258 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014259 case FLASH_5761VENDOR_ATMEL_ADB161D:
14260 case FLASH_5761VENDOR_ATMEL_MDB161D:
14261 case FLASH_5761VENDOR_ST_A_M45PE16:
14262 case FLASH_5761VENDOR_ST_M_M45PE16:
14263 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14264 break;
14265 case FLASH_5761VENDOR_ATMEL_ADB081D:
14266 case FLASH_5761VENDOR_ATMEL_MDB081D:
14267 case FLASH_5761VENDOR_ST_A_M45PE80:
14268 case FLASH_5761VENDOR_ST_M_M45PE80:
14269 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14270 break;
14271 case FLASH_5761VENDOR_ATMEL_ADB041D:
14272 case FLASH_5761VENDOR_ATMEL_MDB041D:
14273 case FLASH_5761VENDOR_ST_A_M45PE40:
14274 case FLASH_5761VENDOR_ST_M_M45PE40:
14275 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14276 break;
14277 case FLASH_5761VENDOR_ATMEL_ADB021D:
14278 case FLASH_5761VENDOR_ATMEL_MDB021D:
14279 case FLASH_5761VENDOR_ST_A_M45PE20:
14280 case FLASH_5761VENDOR_ST_M_M45PE20:
14281 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14282 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014283 }
14284 }
14285}
14286
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014287static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014288{
14289 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014290 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014291 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14292}
14293
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014294static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014295{
14296 u32 nvcfg1;
14297
14298 nvcfg1 = tr32(NVRAM_CFG1);
14299
14300 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14301 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14302 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14303 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014304 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014305 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14306
14307 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14308 tw32(NVRAM_CFG1, nvcfg1);
14309 return;
14310 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14311 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14312 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14313 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14314 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14315 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14316 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14317 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014318 tg3_flag_set(tp, NVRAM_BUFFERED);
14319 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014320
14321 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14322 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14323 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14324 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14325 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14326 break;
14327 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14328 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14329 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14330 break;
14331 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14332 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14333 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14334 break;
14335 }
14336 break;
14337 case FLASH_5752VENDOR_ST_M45PE10:
14338 case FLASH_5752VENDOR_ST_M45PE20:
14339 case FLASH_5752VENDOR_ST_M45PE40:
14340 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014341 tg3_flag_set(tp, NVRAM_BUFFERED);
14342 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014343
14344 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14345 case FLASH_5752VENDOR_ST_M45PE10:
14346 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14347 break;
14348 case FLASH_5752VENDOR_ST_M45PE20:
14349 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14350 break;
14351 case FLASH_5752VENDOR_ST_M45PE40:
14352 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14353 break;
14354 }
14355 break;
14356 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014357 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014358 return;
14359 }
14360
Matt Carlsona1b950d2009-09-01 13:20:17 +000014361 tg3_nvram_get_pagesize(tp, nvcfg1);
14362 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014363 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014364}
14365
14366
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014367static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014368{
14369 u32 nvcfg1;
14370
14371 nvcfg1 = tr32(NVRAM_CFG1);
14372
14373 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14374 case FLASH_5717VENDOR_ATMEL_EEPROM:
14375 case FLASH_5717VENDOR_MICRO_EEPROM:
14376 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014377 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014378 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14379
14380 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14381 tw32(NVRAM_CFG1, nvcfg1);
14382 return;
14383 case FLASH_5717VENDOR_ATMEL_MDB011D:
14384 case FLASH_5717VENDOR_ATMEL_ADB011B:
14385 case FLASH_5717VENDOR_ATMEL_ADB011D:
14386 case FLASH_5717VENDOR_ATMEL_MDB021D:
14387 case FLASH_5717VENDOR_ATMEL_ADB021B:
14388 case FLASH_5717VENDOR_ATMEL_ADB021D:
14389 case FLASH_5717VENDOR_ATMEL_45USPT:
14390 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014391 tg3_flag_set(tp, NVRAM_BUFFERED);
14392 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014393
14394 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14395 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014396 /* Detect size with tg3_nvram_get_size() */
14397 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014398 case FLASH_5717VENDOR_ATMEL_ADB021B:
14399 case FLASH_5717VENDOR_ATMEL_ADB021D:
14400 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14401 break;
14402 default:
14403 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14404 break;
14405 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014406 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014407 case FLASH_5717VENDOR_ST_M_M25PE10:
14408 case FLASH_5717VENDOR_ST_A_M25PE10:
14409 case FLASH_5717VENDOR_ST_M_M45PE10:
14410 case FLASH_5717VENDOR_ST_A_M45PE10:
14411 case FLASH_5717VENDOR_ST_M_M25PE20:
14412 case FLASH_5717VENDOR_ST_A_M25PE20:
14413 case FLASH_5717VENDOR_ST_M_M45PE20:
14414 case FLASH_5717VENDOR_ST_A_M45PE20:
14415 case FLASH_5717VENDOR_ST_25USPT:
14416 case FLASH_5717VENDOR_ST_45USPT:
14417 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014418 tg3_flag_set(tp, NVRAM_BUFFERED);
14419 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014420
14421 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14422 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014423 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014424 /* Detect size with tg3_nvram_get_size() */
14425 break;
14426 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014427 case FLASH_5717VENDOR_ST_A_M45PE20:
14428 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14429 break;
14430 default:
14431 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14432 break;
14433 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014434 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014435 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014436 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014437 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014438 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014439
14440 tg3_nvram_get_pagesize(tp, nvcfg1);
14441 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014442 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014443}
14444
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014445static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014446{
14447 u32 nvcfg1, nvmpinstrp;
14448
14449 nvcfg1 = tr32(NVRAM_CFG1);
14450 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14451
Joe Perches41535772013-02-16 11:20:04 +000014452 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014453 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14454 tg3_flag_set(tp, NO_NVRAM);
14455 return;
14456 }
14457
14458 switch (nvmpinstrp) {
14459 case FLASH_5762_EEPROM_HD:
14460 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014461 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014462 case FLASH_5762_EEPROM_LD:
14463 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014464 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014465 case FLASH_5720VENDOR_M_ST_M45PE20:
14466 /* This pinstrap supports multiple sizes, so force it
14467 * to read the actual size from location 0xf0.
14468 */
14469 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14470 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014471 }
14472 }
14473
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014474 switch (nvmpinstrp) {
14475 case FLASH_5720_EEPROM_HD:
14476 case FLASH_5720_EEPROM_LD:
14477 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014478 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014479
14480 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14481 tw32(NVRAM_CFG1, nvcfg1);
14482 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14483 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14484 else
14485 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14486 return;
14487 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14488 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14489 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14490 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14491 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14492 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14493 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14494 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14495 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14496 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14497 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14498 case FLASH_5720VENDOR_ATMEL_45USPT:
14499 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014500 tg3_flag_set(tp, NVRAM_BUFFERED);
14501 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014502
14503 switch (nvmpinstrp) {
14504 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14505 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14506 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14507 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14508 break;
14509 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14510 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14511 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14512 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14513 break;
14514 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14515 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14516 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14517 break;
14518 default:
Joe Perches41535772013-02-16 11:20:04 +000014519 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014520 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014521 break;
14522 }
14523 break;
14524 case FLASH_5720VENDOR_M_ST_M25PE10:
14525 case FLASH_5720VENDOR_M_ST_M45PE10:
14526 case FLASH_5720VENDOR_A_ST_M25PE10:
14527 case FLASH_5720VENDOR_A_ST_M45PE10:
14528 case FLASH_5720VENDOR_M_ST_M25PE20:
14529 case FLASH_5720VENDOR_M_ST_M45PE20:
14530 case FLASH_5720VENDOR_A_ST_M25PE20:
14531 case FLASH_5720VENDOR_A_ST_M45PE20:
14532 case FLASH_5720VENDOR_M_ST_M25PE40:
14533 case FLASH_5720VENDOR_M_ST_M45PE40:
14534 case FLASH_5720VENDOR_A_ST_M25PE40:
14535 case FLASH_5720VENDOR_A_ST_M45PE40:
14536 case FLASH_5720VENDOR_M_ST_M25PE80:
14537 case FLASH_5720VENDOR_M_ST_M45PE80:
14538 case FLASH_5720VENDOR_A_ST_M25PE80:
14539 case FLASH_5720VENDOR_A_ST_M45PE80:
14540 case FLASH_5720VENDOR_ST_25USPT:
14541 case FLASH_5720VENDOR_ST_45USPT:
14542 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014543 tg3_flag_set(tp, NVRAM_BUFFERED);
14544 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014545
14546 switch (nvmpinstrp) {
14547 case FLASH_5720VENDOR_M_ST_M25PE20:
14548 case FLASH_5720VENDOR_M_ST_M45PE20:
14549 case FLASH_5720VENDOR_A_ST_M25PE20:
14550 case FLASH_5720VENDOR_A_ST_M45PE20:
14551 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14552 break;
14553 case FLASH_5720VENDOR_M_ST_M25PE40:
14554 case FLASH_5720VENDOR_M_ST_M45PE40:
14555 case FLASH_5720VENDOR_A_ST_M25PE40:
14556 case FLASH_5720VENDOR_A_ST_M45PE40:
14557 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14558 break;
14559 case FLASH_5720VENDOR_M_ST_M25PE80:
14560 case FLASH_5720VENDOR_M_ST_M45PE80:
14561 case FLASH_5720VENDOR_A_ST_M25PE80:
14562 case FLASH_5720VENDOR_A_ST_M45PE80:
14563 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14564 break;
14565 default:
Joe Perches41535772013-02-16 11:20:04 +000014566 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014567 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014568 break;
14569 }
14570 break;
14571 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014572 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014573 return;
14574 }
14575
14576 tg3_nvram_get_pagesize(tp, nvcfg1);
14577 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014578 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014579
Joe Perches41535772013-02-16 11:20:04 +000014580 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014581 u32 val;
14582
14583 if (tg3_nvram_read(tp, 0, &val))
14584 return;
14585
14586 if (val != TG3_EEPROM_MAGIC &&
14587 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14588 tg3_flag_set(tp, NO_NVRAM);
14589 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014590}
14591
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014593static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014594{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014595 if (tg3_flag(tp, IS_SSB_CORE)) {
14596 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14597 tg3_flag_clear(tp, NVRAM);
14598 tg3_flag_clear(tp, NVRAM_BUFFERED);
14599 tg3_flag_set(tp, NO_NVRAM);
14600 return;
14601 }
14602
Linus Torvalds1da177e2005-04-16 15:20:36 -070014603 tw32_f(GRC_EEPROM_ADDR,
14604 (EEPROM_ADDR_FSM_RESET |
14605 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14606 EEPROM_ADDR_CLKPERD_SHIFT)));
14607
Michael Chan9d57f012006-12-07 00:23:25 -080014608 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014609
14610 /* Enable seeprom accesses. */
14611 tw32_f(GRC_LOCAL_CTRL,
14612 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14613 udelay(100);
14614
Joe Perches41535772013-02-16 11:20:04 +000014615 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14616 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014617 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618
Michael Chanec41c7d2006-01-17 02:40:55 -080014619 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014620 netdev_warn(tp->dev,
14621 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014622 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014623 return;
14624 }
Michael Chane6af3012005-04-21 17:12:05 -070014625 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014626
Matt Carlson989a9d22007-05-05 11:51:05 -070014627 tp->nvram_size = 0;
14628
Joe Perches41535772013-02-16 11:20:04 +000014629 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014630 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014631 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014632 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014633 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14634 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14635 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014636 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014637 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014638 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014639 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014640 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014641 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014642 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014643 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014644 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14645 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014646 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014647 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14648 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014649 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014650 else
14651 tg3_get_nvram_info(tp);
14652
Matt Carlson989a9d22007-05-05 11:51:05 -070014653 if (tp->nvram_size == 0)
14654 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014655
Michael Chane6af3012005-04-21 17:12:05 -070014656 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014657 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014658
14659 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014660 tg3_flag_clear(tp, NVRAM);
14661 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014662
14663 tg3_get_eeprom_size(tp);
14664 }
14665}
14666
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667struct subsys_tbl_ent {
14668 u16 subsys_vendor, subsys_devid;
14669 u32 phy_id;
14670};
14671
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014672static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014673 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014674 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014675 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014676 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014677 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014678 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014679 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014680 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14681 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14682 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014683 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014684 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014685 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014686 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14687 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14688 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014689 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014690 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014691 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014692 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014693 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014694 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014695 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014696
14697 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014698 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014699 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014700 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014701 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014702 { TG3PCI_SUBVENDOR_ID_3COM,
14703 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14704 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014705 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014706 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014707 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708
14709 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014710 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014711 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014712 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014713 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014714 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014715 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014716 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014717 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014718
14719 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014720 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014721 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014722 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014723 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014724 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14725 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14726 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014727 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014728 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014729 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014730
14731 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014732 { TG3PCI_SUBVENDOR_ID_IBM,
14733 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014734};
14735
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014736static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014737{
14738 int i;
14739
14740 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14741 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14742 tp->pdev->subsystem_vendor) &&
14743 (subsys_id_to_phy_id[i].subsys_devid ==
14744 tp->pdev->subsystem_device))
14745 return &subsys_id_to_phy_id[i];
14746 }
14747 return NULL;
14748}
14749
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014750static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014751{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014752 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014753
Matt Carlson79eb6902010-02-17 15:17:03 +000014754 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014755 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14756
Gary Zambranoa85feb82007-05-05 11:52:19 -070014757 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014758 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14759 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014760
Joe Perches41535772013-02-16 11:20:04 +000014761 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014762 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014763 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14764 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014765 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014766 val = tr32(VCPU_CFGSHDW);
14767 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014768 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014769 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014770 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014771 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014772 device_set_wakeup_enable(&tp->pdev->dev, true);
14773 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014774 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014775 }
14776
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14778 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14779 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014780 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014781 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014782
14783 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14784 tp->nic_sram_data_cfg = nic_cfg;
14785
14786 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14787 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014788 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14789 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14790 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014791 (ver > 0) && (ver < 0x100))
14792 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14793
Joe Perches41535772013-02-16 11:20:04 +000014794 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014795 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14796
Linus Torvalds1da177e2005-04-16 15:20:36 -070014797 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14798 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14799 eeprom_phy_serdes = 1;
14800
14801 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14802 if (nic_phy_id != 0) {
14803 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14804 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14805
14806 eeprom_phy_id = (id1 >> 16) << 10;
14807 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14808 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14809 } else
14810 eeprom_phy_id = 0;
14811
Michael Chan7d0c41e2005-04-21 17:06:20 -070014812 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014813 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014814 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014815 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014816 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014817 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014818 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014819
Joe Perches63c3a662011-04-26 08:12:10 +000014820 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014821 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14822 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014823 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014824 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14825
14826 switch (led_cfg) {
14827 default:
14828 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14829 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14830 break;
14831
14832 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14833 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14834 break;
14835
14836 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14837 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014838
14839 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14840 * read on some older 5700/5701 bootcode.
14841 */
Joe Perches41535772013-02-16 11:20:04 +000014842 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14843 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014844 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14845
Linus Torvalds1da177e2005-04-16 15:20:36 -070014846 break;
14847
14848 case SHASTA_EXT_LED_SHARED:
14849 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014850 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14851 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014852 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14853 LED_CTRL_MODE_PHY_2);
14854 break;
14855
14856 case SHASTA_EXT_LED_MAC:
14857 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14858 break;
14859
14860 case SHASTA_EXT_LED_COMBO:
14861 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014862 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014863 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14864 LED_CTRL_MODE_PHY_2);
14865 break;
14866
Stephen Hemminger855e1112008-04-16 16:37:28 -070014867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014868
Joe Perches41535772013-02-16 11:20:04 +000014869 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14870 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014871 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14872 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14873
Joe Perches41535772013-02-16 11:20:04 +000014874 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014875 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014876
Michael Chan9d26e212006-12-07 00:21:14 -080014877 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014878 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014879 if ((tp->pdev->subsystem_vendor ==
14880 PCI_VENDOR_ID_ARIMA) &&
14881 (tp->pdev->subsystem_device == 0x205a ||
14882 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014883 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014884 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014885 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14886 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014888
14889 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014890 tg3_flag_set(tp, ENABLE_ASF);
14891 if (tg3_flag(tp, 5750_PLUS))
14892 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014893 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014894
14895 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014896 tg3_flag(tp, 5750_PLUS))
14897 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014898
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014899 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014900 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014901 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014902
Joe Perches63c3a662011-04-26 08:12:10 +000014903 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014904 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014905 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014906 device_set_wakeup_enable(&tp->pdev->dev, true);
14907 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014908
Linus Torvalds1da177e2005-04-16 15:20:36 -070014909 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014910 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014911
14912 /* serdes signal pre-emphasis in register 0x590 set by */
14913 /* bootcode if bit 18 is set */
14914 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014915 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014916
Joe Perches63c3a662011-04-26 08:12:10 +000014917 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014918 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14919 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014920 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014921 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014922
Nithin Sujir942d1af2013-04-09 08:48:07 +000014923 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014924 u32 cfg3;
14925
14926 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014927 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
14928 !tg3_flag(tp, 57765_PLUS) &&
14929 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000014930 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014931 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
14932 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
14933 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
14934 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014935 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014936
Matt Carlson14417062010-02-17 15:16:59 +000014937 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014938 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014939 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014940 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014941 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014942 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014943 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014944done:
Joe Perches63c3a662011-04-26 08:12:10 +000014945 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014946 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014947 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014948 else
14949 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014950}
14951
Michael Chanc86a8562013-01-06 12:51:08 +000014952static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14953{
14954 int i, err;
14955 u32 val2, off = offset * 8;
14956
14957 err = tg3_nvram_lock(tp);
14958 if (err)
14959 return err;
14960
14961 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14962 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14963 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14964 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14965 udelay(10);
14966
14967 for (i = 0; i < 100; i++) {
14968 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14969 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14970 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14971 break;
14972 }
14973 udelay(10);
14974 }
14975
14976 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14977
14978 tg3_nvram_unlock(tp);
14979 if (val2 & APE_OTP_STATUS_CMD_DONE)
14980 return 0;
14981
14982 return -EBUSY;
14983}
14984
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014985static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014986{
14987 int i;
14988 u32 val;
14989
14990 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14991 tw32(OTP_CTRL, cmd);
14992
14993 /* Wait for up to 1 ms for command to execute. */
14994 for (i = 0; i < 100; i++) {
14995 val = tr32(OTP_STATUS);
14996 if (val & OTP_STATUS_CMD_DONE)
14997 break;
14998 udelay(10);
14999 }
15000
15001 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15002}
15003
15004/* Read the gphy configuration from the OTP region of the chip. The gphy
15005 * configuration is a 32-bit value that straddles the alignment boundary.
15006 * We do two 32-bit reads and then shift and merge the results.
15007 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015008static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015009{
15010 u32 bhalf_otp, thalf_otp;
15011
15012 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15013
15014 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15015 return 0;
15016
15017 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15018
15019 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15020 return 0;
15021
15022 thalf_otp = tr32(OTP_READ_DATA);
15023
15024 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15025
15026 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15027 return 0;
15028
15029 bhalf_otp = tr32(OTP_READ_DATA);
15030
15031 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15032}
15033
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015034static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015035{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015036 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015037
15038 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15039 adv |= ADVERTISED_1000baseT_Half |
15040 ADVERTISED_1000baseT_Full;
15041
15042 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15043 adv |= ADVERTISED_100baseT_Half |
15044 ADVERTISED_100baseT_Full |
15045 ADVERTISED_10baseT_Half |
15046 ADVERTISED_10baseT_Full |
15047 ADVERTISED_TP;
15048 else
15049 adv |= ADVERTISED_FIBRE;
15050
15051 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015052 tp->link_config.speed = SPEED_UNKNOWN;
15053 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015054 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015055 tp->link_config.active_speed = SPEED_UNKNOWN;
15056 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015057
15058 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015059}
15060
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015061static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015062{
15063 u32 hw_phy_id_1, hw_phy_id_2;
15064 u32 hw_phy_id, hw_phy_id_masked;
15065 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015066
Matt Carlsone256f8a2011-03-09 16:58:24 +000015067 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015068 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015069 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15070
Michael Chan8151ad52012-07-29 19:15:41 +000015071 if (tg3_flag(tp, ENABLE_APE)) {
15072 switch (tp->pci_fn) {
15073 case 0:
15074 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15075 break;
15076 case 1:
15077 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15078 break;
15079 case 2:
15080 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15081 break;
15082 case 3:
15083 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15084 break;
15085 }
15086 }
15087
Nithin Sujir942d1af2013-04-09 08:48:07 +000015088 if (!tg3_flag(tp, ENABLE_ASF) &&
15089 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15090 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15091 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15092 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15093
Joe Perches63c3a662011-04-26 08:12:10 +000015094 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015095 return tg3_phy_init(tp);
15096
Linus Torvalds1da177e2005-04-16 15:20:36 -070015097 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015098 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015099 */
15100 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015101 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015102 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015103 } else {
15104 /* Now read the physical PHY_ID from the chip and verify
15105 * that it is sane. If it doesn't look good, we fall back
15106 * to either the hard-coded table based PHY_ID and failing
15107 * that the value found in the eeprom area.
15108 */
15109 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15110 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15111
15112 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15113 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15114 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15115
Matt Carlson79eb6902010-02-17 15:17:03 +000015116 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015117 }
15118
Matt Carlson79eb6902010-02-17 15:17:03 +000015119 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015120 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015121 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015122 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015123 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015124 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015125 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015126 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015127 /* Do nothing, phy ID already set up in
15128 * tg3_get_eeprom_hw_cfg().
15129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015130 } else {
15131 struct subsys_tbl_ent *p;
15132
15133 /* No eeprom signature? Try the hardcoded
15134 * subsys device table.
15135 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015136 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015137 if (p) {
15138 tp->phy_id = p->phy_id;
15139 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15140 /* For now we saw the IDs 0xbc050cd0,
15141 * 0xbc050f80 and 0xbc050c30 on devices
15142 * connected to an BCM4785 and there are
15143 * probably more. Just assume that the phy is
15144 * supported when it is connected to a SSB core
15145 * for now.
15146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015147 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015149
Linus Torvalds1da177e2005-04-16 15:20:36 -070015150 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015151 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015152 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015153 }
15154 }
15155
Matt Carlsona6b68da2010-12-06 08:28:52 +000015156 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015157 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15158 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015159 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015160 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15161 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15162 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15163 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015164 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015165 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15166
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015167 tp->eee.supported = SUPPORTED_100baseT_Full |
15168 SUPPORTED_1000baseT_Full;
15169 tp->eee.advertised = ADVERTISED_100baseT_Full |
15170 ADVERTISED_1000baseT_Full;
15171 tp->eee.eee_enabled = 1;
15172 tp->eee.tx_lpi_enabled = 1;
15173 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15174 }
15175
Matt Carlsone256f8a2011-03-09 16:58:24 +000015176 tg3_phy_init_link_config(tp);
15177
Nithin Sujir942d1af2013-04-09 08:48:07 +000015178 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15179 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015180 !tg3_flag(tp, ENABLE_APE) &&
15181 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015182 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015183
15184 tg3_readphy(tp, MII_BMSR, &bmsr);
15185 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15186 (bmsr & BMSR_LSTATUS))
15187 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015188
Linus Torvalds1da177e2005-04-16 15:20:36 -070015189 err = tg3_phy_reset(tp);
15190 if (err)
15191 return err;
15192
Matt Carlson42b64a42011-05-19 12:12:49 +000015193 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015194
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015195 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015196 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15197 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015198
15199 tg3_writephy(tp, MII_BMCR,
15200 BMCR_ANENABLE | BMCR_ANRESTART);
15201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015202 }
15203
15204skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015205 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015206 err = tg3_init_5401phy_dsp(tp);
15207 if (err)
15208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015209
Linus Torvalds1da177e2005-04-16 15:20:36 -070015210 err = tg3_init_5401phy_dsp(tp);
15211 }
15212
Linus Torvalds1da177e2005-04-16 15:20:36 -070015213 return err;
15214}
15215
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015216static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015217{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015218 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015219 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015220 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015221 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015222
Matt Carlson535a4902011-07-20 10:20:56 +000015223 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015224 if (!vpd_data)
15225 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015226
Matt Carlson535a4902011-07-20 10:20:56 +000015227 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015228 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015229 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015230
15231 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15232 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15233 i += PCI_VPD_LRDT_TAG_SIZE;
15234
Matt Carlson535a4902011-07-20 10:20:56 +000015235 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015236 goto out_not_found;
15237
Matt Carlson184b8902010-04-05 10:19:25 +000015238 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15239 PCI_VPD_RO_KEYWORD_MFR_ID);
15240 if (j > 0) {
15241 len = pci_vpd_info_field_size(&vpd_data[j]);
15242
15243 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15244 if (j + len > block_end || len != 4 ||
15245 memcmp(&vpd_data[j], "1028", 4))
15246 goto partno;
15247
15248 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15249 PCI_VPD_RO_KEYWORD_VENDOR0);
15250 if (j < 0)
15251 goto partno;
15252
15253 len = pci_vpd_info_field_size(&vpd_data[j]);
15254
15255 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15256 if (j + len > block_end)
15257 goto partno;
15258
Kees Cook715230a2013-03-27 06:40:50 +000015259 if (len >= sizeof(tp->fw_ver))
15260 len = sizeof(tp->fw_ver) - 1;
15261 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15262 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15263 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015264 }
15265
15266partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015267 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15268 PCI_VPD_RO_KEYWORD_PARTNO);
15269 if (i < 0)
15270 goto out_not_found;
15271
15272 len = pci_vpd_info_field_size(&vpd_data[i]);
15273
15274 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15275 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015276 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015277 goto out_not_found;
15278
15279 memcpy(tp->board_part_number, &vpd_data[i], len);
15280
Linus Torvalds1da177e2005-04-16 15:20:36 -070015281out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015282 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015283 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015284 return;
15285
15286out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015287 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015288 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15289 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015290 strcpy(tp->board_part_number, "BCM5717");
15291 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15292 strcpy(tp->board_part_number, "BCM5718");
15293 else
15294 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015295 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015296 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15297 strcpy(tp->board_part_number, "BCM57780");
15298 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15299 strcpy(tp->board_part_number, "BCM57760");
15300 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15301 strcpy(tp->board_part_number, "BCM57790");
15302 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15303 strcpy(tp->board_part_number, "BCM57788");
15304 else
15305 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015306 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015307 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15308 strcpy(tp->board_part_number, "BCM57761");
15309 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15310 strcpy(tp->board_part_number, "BCM57765");
15311 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15312 strcpy(tp->board_part_number, "BCM57781");
15313 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15314 strcpy(tp->board_part_number, "BCM57785");
15315 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15316 strcpy(tp->board_part_number, "BCM57791");
15317 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15318 strcpy(tp->board_part_number, "BCM57795");
15319 else
15320 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015321 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015322 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15323 strcpy(tp->board_part_number, "BCM57762");
15324 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15325 strcpy(tp->board_part_number, "BCM57766");
15326 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15327 strcpy(tp->board_part_number, "BCM57782");
15328 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15329 strcpy(tp->board_part_number, "BCM57786");
15330 else
15331 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015332 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015333 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015334 } else {
15335nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015336 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015338}
15339
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015340static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015341{
15342 u32 val;
15343
Matt Carlsone4f34112009-02-25 14:25:00 +000015344 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015345 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015346 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015347 val != 0)
15348 return 0;
15349
15350 return 1;
15351}
15352
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015353static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015354{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015355 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015356 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015357 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015358
15359 if (tg3_nvram_read(tp, 0xc, &offset) ||
15360 tg3_nvram_read(tp, 0x4, &start))
15361 return;
15362
15363 offset = tg3_nvram_logical_addr(tp, offset);
15364
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015365 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015366 return;
15367
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015368 if ((val & 0xfc000000) == 0x0c000000) {
15369 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015370 return;
15371
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015372 if (val == 0)
15373 newver = true;
15374 }
15375
Matt Carlson75f99362010-04-05 10:19:24 +000015376 dst_off = strlen(tp->fw_ver);
15377
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015378 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015379 if (TG3_VER_SIZE - dst_off < 16 ||
15380 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015381 return;
15382
15383 offset = offset + ver_offset - start;
15384 for (i = 0; i < 16; i += 4) {
15385 __be32 v;
15386 if (tg3_nvram_read_be32(tp, offset + i, &v))
15387 return;
15388
Matt Carlson75f99362010-04-05 10:19:24 +000015389 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015390 }
15391 } else {
15392 u32 major, minor;
15393
15394 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15395 return;
15396
15397 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15398 TG3_NVM_BCVER_MAJSFT;
15399 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015400 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15401 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015402 }
15403}
15404
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015405static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015406{
15407 u32 val, major, minor;
15408
15409 /* Use native endian representation */
15410 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15411 return;
15412
15413 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15414 TG3_NVM_HWSB_CFG1_MAJSFT;
15415 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15416 TG3_NVM_HWSB_CFG1_MINSFT;
15417
15418 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15419}
15420
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015421static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015422{
15423 u32 offset, major, minor, build;
15424
Matt Carlson75f99362010-04-05 10:19:24 +000015425 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015426
15427 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15428 return;
15429
15430 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15431 case TG3_EEPROM_SB_REVISION_0:
15432 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15433 break;
15434 case TG3_EEPROM_SB_REVISION_2:
15435 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15436 break;
15437 case TG3_EEPROM_SB_REVISION_3:
15438 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15439 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015440 case TG3_EEPROM_SB_REVISION_4:
15441 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15442 break;
15443 case TG3_EEPROM_SB_REVISION_5:
15444 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15445 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015446 case TG3_EEPROM_SB_REVISION_6:
15447 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15448 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015449 default:
15450 return;
15451 }
15452
Matt Carlsone4f34112009-02-25 14:25:00 +000015453 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015454 return;
15455
15456 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15457 TG3_EEPROM_SB_EDH_BLD_SHFT;
15458 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15459 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15460 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15461
15462 if (minor > 99 || build > 26)
15463 return;
15464
Matt Carlson75f99362010-04-05 10:19:24 +000015465 offset = strlen(tp->fw_ver);
15466 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15467 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015468
15469 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015470 offset = strlen(tp->fw_ver);
15471 if (offset < TG3_VER_SIZE - 1)
15472 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015473 }
15474}
15475
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015476static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015477{
15478 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015479 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015480
15481 for (offset = TG3_NVM_DIR_START;
15482 offset < TG3_NVM_DIR_END;
15483 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015484 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015485 return;
15486
15487 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15488 break;
15489 }
15490
15491 if (offset == TG3_NVM_DIR_END)
15492 return;
15493
Joe Perches63c3a662011-04-26 08:12:10 +000015494 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015495 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015496 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015497 return;
15498
Matt Carlsone4f34112009-02-25 14:25:00 +000015499 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015500 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015501 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015502 return;
15503
15504 offset += val - start;
15505
Matt Carlsonacd9c112009-02-25 14:26:33 +000015506 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015507
Matt Carlsonacd9c112009-02-25 14:26:33 +000015508 tp->fw_ver[vlen++] = ',';
15509 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015510
15511 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015512 __be32 v;
15513 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015514 return;
15515
Al Virob9fc7dc2007-12-17 22:59:57 -080015516 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015517
Matt Carlsonacd9c112009-02-25 14:26:33 +000015518 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15519 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015520 break;
15521 }
15522
Matt Carlsonacd9c112009-02-25 14:26:33 +000015523 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15524 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015525 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015526}
15527
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015528static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015529{
Matt Carlson7fd76442009-02-25 14:27:20 +000015530 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015531
15532 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15533 if (apedata != APE_SEG_SIG_MAGIC)
15534 return;
15535
15536 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15537 if (!(apedata & APE_FW_STATUS_READY))
15538 return;
15539
Michael Chan165f4d12012-07-16 16:23:59 +000015540 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15541 tg3_flag_set(tp, APE_HAS_NCSI);
15542}
15543
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015544static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015545{
15546 int vlen;
15547 u32 apedata;
15548 char *fwtype;
15549
Matt Carlson7fd76442009-02-25 14:27:20 +000015550 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15551
Michael Chan165f4d12012-07-16 16:23:59 +000015552 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015553 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015554 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15555 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015556 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015557 fwtype = "DASH";
15558
Matt Carlson7fd76442009-02-25 14:27:20 +000015559 vlen = strlen(tp->fw_ver);
15560
Matt Carlsonecc79642010-08-02 11:26:01 +000015561 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15562 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015563 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15564 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15565 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15566 (apedata & APE_FW_VERSION_BLDMSK));
15567}
15568
Michael Chanc86a8562013-01-06 12:51:08 +000015569static void tg3_read_otp_ver(struct tg3 *tp)
15570{
15571 u32 val, val2;
15572
Joe Perches41535772013-02-16 11:20:04 +000015573 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015574 return;
15575
15576 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15577 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15578 TG3_OTP_MAGIC0_VALID(val)) {
15579 u64 val64 = (u64) val << 32 | val2;
15580 u32 ver = 0;
15581 int i, vlen;
15582
15583 for (i = 0; i < 7; i++) {
15584 if ((val64 & 0xff) == 0)
15585 break;
15586 ver = val64 & 0xff;
15587 val64 >>= 8;
15588 }
15589 vlen = strlen(tp->fw_ver);
15590 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15591 }
15592}
15593
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015594static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015595{
15596 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015597 bool vpd_vers = false;
15598
15599 if (tp->fw_ver[0] != 0)
15600 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015601
Joe Perches63c3a662011-04-26 08:12:10 +000015602 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015603 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015604 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015605 return;
15606 }
15607
Matt Carlsonacd9c112009-02-25 14:26:33 +000015608 if (tg3_nvram_read(tp, 0, &val))
15609 return;
15610
15611 if (val == TG3_EEPROM_MAGIC)
15612 tg3_read_bc_ver(tp);
15613 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15614 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015615 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15616 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015617
Michael Chan165f4d12012-07-16 16:23:59 +000015618 if (tg3_flag(tp, ENABLE_ASF)) {
15619 if (tg3_flag(tp, ENABLE_APE)) {
15620 tg3_probe_ncsi(tp);
15621 if (!vpd_vers)
15622 tg3_read_dash_ver(tp);
15623 } else if (!vpd_vers) {
15624 tg3_read_mgmtfw_ver(tp);
15625 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015626 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015627
15628 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015629}
15630
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015631static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15632{
Joe Perches63c3a662011-04-26 08:12:10 +000015633 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015634 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015635 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015636 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015637 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015638 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015639}
15640
Matt Carlson41434702011-03-09 16:58:22 +000015641static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015642 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15643 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15644 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15645 { },
15646};
15647
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015648static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015649{
15650 struct pci_dev *peer;
15651 unsigned int func, devnr = tp->pdev->devfn & ~7;
15652
15653 for (func = 0; func < 8; func++) {
15654 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15655 if (peer && peer != tp->pdev)
15656 break;
15657 pci_dev_put(peer);
15658 }
15659 /* 5704 can be configured in single-port mode, set peer to
15660 * tp->pdev in that case.
15661 */
15662 if (!peer) {
15663 peer = tp->pdev;
15664 return peer;
15665 }
15666
15667 /*
15668 * We don't need to keep the refcount elevated; there's no way
15669 * to remove one half of this device without removing the other
15670 */
15671 pci_dev_put(peer);
15672
15673 return peer;
15674}
15675
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015676static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015677{
15678 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015679 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015680 u32 reg;
15681
15682 /* All devices that use the alternate
15683 * ASIC REV location have a CPMU.
15684 */
15685 tg3_flag_set(tp, CPMU_PRESENT);
15686
15687 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015688 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015689 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15690 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015691 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15692 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15693 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15694 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015695 reg = TG3PCI_GEN2_PRODID_ASICREV;
15696 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15697 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15698 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15699 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15700 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15701 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15702 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15703 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15704 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15705 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15706 reg = TG3PCI_GEN15_PRODID_ASICREV;
15707 else
15708 reg = TG3PCI_PRODID_ASICREV;
15709
15710 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15711 }
15712
15713 /* Wrong chip ID in 5752 A0. This code can be removed later
15714 * as A0 is not in production.
15715 */
Joe Perches41535772013-02-16 11:20:04 +000015716 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015717 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15718
Joe Perches41535772013-02-16 11:20:04 +000015719 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015720 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15721
Joe Perches41535772013-02-16 11:20:04 +000015722 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15723 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15724 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015725 tg3_flag_set(tp, 5717_PLUS);
15726
Joe Perches41535772013-02-16 11:20:04 +000015727 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15728 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015729 tg3_flag_set(tp, 57765_CLASS);
15730
Michael Chanc65a17f2013-01-06 12:51:07 +000015731 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015732 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015733 tg3_flag_set(tp, 57765_PLUS);
15734
15735 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015736 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15737 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15738 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15739 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15740 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15741 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015742 tg3_flag(tp, 57765_PLUS))
15743 tg3_flag_set(tp, 5755_PLUS);
15744
Joe Perches41535772013-02-16 11:20:04 +000015745 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15746 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015747 tg3_flag_set(tp, 5780_CLASS);
15748
Joe Perches41535772013-02-16 11:20:04 +000015749 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15750 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15751 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015752 tg3_flag(tp, 5755_PLUS) ||
15753 tg3_flag(tp, 5780_CLASS))
15754 tg3_flag_set(tp, 5750_PLUS);
15755
Joe Perches41535772013-02-16 11:20:04 +000015756 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015757 tg3_flag(tp, 5750_PLUS))
15758 tg3_flag_set(tp, 5705_PLUS);
15759}
15760
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015761static bool tg3_10_100_only_device(struct tg3 *tp,
15762 const struct pci_device_id *ent)
15763{
15764 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15765
Joe Perches41535772013-02-16 11:20:04 +000015766 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15767 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015768 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15769 return true;
15770
15771 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015772 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015773 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15774 return true;
15775 } else {
15776 return true;
15777 }
15778 }
15779
15780 return false;
15781}
15782
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015783static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015784{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015785 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015786 u32 pci_state_reg, grc_misc_cfg;
15787 u32 val;
15788 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015789 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015790
Linus Torvalds1da177e2005-04-16 15:20:36 -070015791 /* Force memory write invalidate off. If we leave it on,
15792 * then on 5700_BX chips we have to enable a workaround.
15793 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15794 * to match the cacheline size. The Broadcom driver have this
15795 * workaround but turns MWI off all the times so never uses
15796 * it. This seems to suggest that the workaround is insufficient.
15797 */
15798 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15799 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15800 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15801
Matt Carlson16821282011-07-13 09:27:28 +000015802 /* Important! -- Make sure register accesses are byteswapped
15803 * correctly. Also, for those chips that require it, make
15804 * sure that indirect register accesses are enabled before
15805 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015806 */
15807 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15808 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015809 tp->misc_host_ctrl |= (misc_ctrl_reg &
15810 MISC_HOST_CTRL_CHIPREV);
15811 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15812 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015813
Matt Carlson42b123b2012-02-13 15:20:13 +000015814 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015815
Michael Chan68929142005-08-09 20:17:14 -070015816 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15817 * we need to disable memory and use config. cycles
15818 * only to access all registers. The 5702/03 chips
15819 * can mistakenly decode the special cycles from the
15820 * ICH chipsets as memory write cycles, causing corruption
15821 * of register and memory space. Only certain ICH bridges
15822 * will drive special cycles with non-zero data during the
15823 * address phase which can fall within the 5703's address
15824 * range. This is not an ICH bug as the PCI spec allows
15825 * non-zero address during special cycles. However, only
15826 * these ICH bridges are known to drive non-zero addresses
15827 * during special cycles.
15828 *
15829 * Since special cycles do not cross PCI bridges, we only
15830 * enable this workaround if the 5703 is on the secondary
15831 * bus of these ICH bridges.
15832 */
Joe Perches41535772013-02-16 11:20:04 +000015833 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15834 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015835 static struct tg3_dev_id {
15836 u32 vendor;
15837 u32 device;
15838 u32 rev;
15839 } ich_chipsets[] = {
15840 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15841 PCI_ANY_ID },
15842 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15843 PCI_ANY_ID },
15844 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15845 0xa },
15846 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15847 PCI_ANY_ID },
15848 { },
15849 };
15850 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15851 struct pci_dev *bridge = NULL;
15852
15853 while (pci_id->vendor != 0) {
15854 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15855 bridge);
15856 if (!bridge) {
15857 pci_id++;
15858 continue;
15859 }
15860 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015861 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015862 continue;
15863 }
15864 if (bridge->subordinate &&
15865 (bridge->subordinate->number ==
15866 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015867 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015868 pci_dev_put(bridge);
15869 break;
15870 }
15871 }
15872 }
15873
Joe Perches41535772013-02-16 11:20:04 +000015874 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015875 static struct tg3_dev_id {
15876 u32 vendor;
15877 u32 device;
15878 } bridge_chipsets[] = {
15879 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15880 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15881 { },
15882 };
15883 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15884 struct pci_dev *bridge = NULL;
15885
15886 while (pci_id->vendor != 0) {
15887 bridge = pci_get_device(pci_id->vendor,
15888 pci_id->device,
15889 bridge);
15890 if (!bridge) {
15891 pci_id++;
15892 continue;
15893 }
15894 if (bridge->subordinate &&
15895 (bridge->subordinate->number <=
15896 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015897 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015898 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015899 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015900 pci_dev_put(bridge);
15901 break;
15902 }
15903 }
15904 }
15905
Michael Chan4a29cc22006-03-19 13:21:12 -080015906 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15907 * DMA addresses > 40-bit. This bridge may have other additional
15908 * 57xx devices behind it in some 4-port NIC designs for example.
15909 * Any tg3 device found behind the bridge will also need the 40-bit
15910 * DMA workaround.
15911 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015912 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015913 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015914 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015915 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015916 struct pci_dev *bridge = NULL;
15917
15918 do {
15919 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15920 PCI_DEVICE_ID_SERVERWORKS_EPB,
15921 bridge);
15922 if (bridge && bridge->subordinate &&
15923 (bridge->subordinate->number <=
15924 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015925 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015926 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015927 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015928 pci_dev_put(bridge);
15929 break;
15930 }
15931 } while (bridge);
15932 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015933
Joe Perches41535772013-02-16 11:20:04 +000015934 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15935 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015936 tp->pdev_peer = tg3_find_peer(tp);
15937
Matt Carlson507399f2009-11-13 13:03:37 +000015938 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015939 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015940 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015941 else if (tg3_flag(tp, 57765_PLUS))
15942 tg3_flag_set(tp, HW_TSO_3);
15943 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015944 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015945 tg3_flag_set(tp, HW_TSO_2);
15946 else if (tg3_flag(tp, 5750_PLUS)) {
15947 tg3_flag_set(tp, HW_TSO_1);
15948 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015949 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15950 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015951 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015952 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15953 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15954 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000015955 tg3_flag_set(tp, FW_TSO);
15956 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015957 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015958 tp->fw_needed = FIRMWARE_TG3TSO5;
15959 else
15960 tp->fw_needed = FIRMWARE_TG3TSO;
15961 }
15962
Matt Carlsondabc5c62011-05-19 12:12:52 +000015963 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015964 if (tg3_flag(tp, HW_TSO_1) ||
15965 tg3_flag(tp, HW_TSO_2) ||
15966 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000015967 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015968 /* For firmware TSO, assume ASF is disabled.
15969 * We'll disable TSO later if we discover ASF
15970 * is enabled in tg3_get_eeprom_hw_cfg().
15971 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015972 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015973 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015974 tg3_flag_clear(tp, TSO_CAPABLE);
15975 tg3_flag_clear(tp, TSO_BUG);
15976 tp->fw_needed = NULL;
15977 }
15978
Joe Perches41535772013-02-16 11:20:04 +000015979 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015980 tp->fw_needed = FIRMWARE_TG3;
15981
Nithin Sujirc4dab502013-03-06 17:02:34 +000015982 if (tg3_asic_rev(tp) == ASIC_REV_57766)
15983 tp->fw_needed = FIRMWARE_TG357766;
15984
Matt Carlson507399f2009-11-13 13:03:37 +000015985 tp->irq_max = 1;
15986
Joe Perches63c3a662011-04-26 08:12:10 +000015987 if (tg3_flag(tp, 5750_PLUS)) {
15988 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015989 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15990 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15991 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15992 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015993 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015994 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015995
Joe Perches63c3a662011-04-26 08:12:10 +000015996 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015997 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015998 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016000
Joe Perches63c3a662011-04-26 08:12:10 +000016001 if (tg3_flag(tp, 57765_PLUS)) {
16002 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016003 tp->irq_max = TG3_IRQ_MAX_VECS;
16004 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016005 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016006
Michael Chan91024262012-09-28 07:12:38 +000016007 tp->txq_max = 1;
16008 tp->rxq_max = 1;
16009 if (tp->irq_max > 1) {
16010 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16011 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16012
Joe Perches41535772013-02-16 11:20:04 +000016013 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16014 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016015 tp->txq_max = tp->irq_max - 1;
16016 }
16017
Matt Carlsonb7abee62012-06-07 12:56:54 +000016018 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016019 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016020 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016021
Joe Perches41535772013-02-16 11:20:04 +000016022 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016023 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016024
Joe Perches41535772013-02-16 11:20:04 +000016025 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16026 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16027 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16028 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016029 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016030
Joe Perches63c3a662011-04-26 08:12:10 +000016031 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016032 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016033 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016034
Joe Perches63c3a662011-04-26 08:12:10 +000016035 if (!tg3_flag(tp, 5705_PLUS) ||
16036 tg3_flag(tp, 5780_CLASS) ||
16037 tg3_flag(tp, USE_JUMBO_BDFLAG))
16038 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016039
Matt Carlson52f44902008-11-21 17:17:04 -080016040 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16041 &pci_state_reg);
16042
Jon Mason708ebb3a2011-06-27 12:56:50 +000016043 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016044 u16 lnkctl;
16045
Joe Perches63c3a662011-04-26 08:12:10 +000016046 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016047
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016048 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016049 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016050 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016051 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016052 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016053 }
Joe Perches41535772013-02-16 11:20:04 +000016054 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16055 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16056 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16057 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016058 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016059 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016060 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016061 }
Joe Perches41535772013-02-16 11:20:04 +000016062 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016063 /* BCM5785 devices are effectively PCIe devices, and should
16064 * follow PCIe codepaths, but do not have a PCIe capabilities
16065 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016066 */
Joe Perches63c3a662011-04-26 08:12:10 +000016067 tg3_flag_set(tp, PCI_EXPRESS);
16068 } else if (!tg3_flag(tp, 5705_PLUS) ||
16069 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016070 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16071 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016072 dev_err(&tp->pdev->dev,
16073 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016074 return -EIO;
16075 }
16076
16077 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016078 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016080
Michael Chan399de502005-10-03 14:02:39 -070016081 /* If we have an AMD 762 or VIA K8T800 chipset, write
16082 * reordering to the mailbox registers done by the host
16083 * controller can cause major troubles. We read back from
16084 * every mailbox register write to force the writes to be
16085 * posted to the chip in order.
16086 */
Matt Carlson41434702011-03-09 16:58:22 +000016087 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016088 !tg3_flag(tp, PCI_EXPRESS))
16089 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016090
Matt Carlson69fc4052008-12-21 20:19:57 -080016091 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16092 &tp->pci_cacheline_sz);
16093 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16094 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016095 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016096 tp->pci_lat_timer < 64) {
16097 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016098 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16099 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016100 }
16101
Matt Carlson16821282011-07-13 09:27:28 +000016102 /* Important! -- It is critical that the PCI-X hw workaround
16103 * situation is decided before the first MMIO register access.
16104 */
Joe Perches41535772013-02-16 11:20:04 +000016105 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016106 /* 5700 BX chips need to have their TX producer index
16107 * mailboxes written twice to workaround a bug.
16108 */
Joe Perches63c3a662011-04-26 08:12:10 +000016109 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016110
Matt Carlson52f44902008-11-21 17:17:04 -080016111 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016112 *
16113 * The workaround is to use indirect register accesses
16114 * for all chip writes not to mailbox registers.
16115 */
Joe Perches63c3a662011-04-26 08:12:10 +000016116 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016117 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016118
Joe Perches63c3a662011-04-26 08:12:10 +000016119 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016120
16121 /* The chip can have it's power management PCI config
16122 * space registers clobbered due to this bug.
16123 * So explicitly force the chip into D0 here.
16124 */
Matt Carlson9974a352007-10-07 23:27:28 -070016125 pci_read_config_dword(tp->pdev,
16126 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016127 &pm_reg);
16128 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16129 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016130 pci_write_config_dword(tp->pdev,
16131 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016132 pm_reg);
16133
16134 /* Also, force SERR#/PERR# in PCI command. */
16135 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16136 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16137 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16138 }
16139 }
16140
Linus Torvalds1da177e2005-04-16 15:20:36 -070016141 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016142 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016143 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016144 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016145
16146 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016147 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016148 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16149 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16150 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16151 }
16152
Michael Chan1ee582d2005-08-09 20:16:46 -070016153 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016154 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016155 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016156 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016157 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016158 tp->write32_tx_mbox = tg3_write32;
16159 tp->write32_rx_mbox = tg3_write32;
16160
16161 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016162 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016163 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016164 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016165 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016166 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016167 /*
16168 * Back to back register writes can cause problems on these
16169 * chips, the workaround is to read back all reg writes
16170 * except those to mailbox regs.
16171 *
16172 * See tg3_write_indirect_reg32().
16173 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016174 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016175 }
16176
Joe Perches63c3a662011-04-26 08:12:10 +000016177 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016178 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016179 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016180 tp->write32_rx_mbox = tg3_write_flush_reg32;
16181 }
Michael Chan20094932005-08-09 20:16:32 -070016182
Joe Perches63c3a662011-04-26 08:12:10 +000016183 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016184 tp->read32 = tg3_read_indirect_reg32;
16185 tp->write32 = tg3_write_indirect_reg32;
16186 tp->read32_mbox = tg3_read_indirect_mbox;
16187 tp->write32_mbox = tg3_write_indirect_mbox;
16188 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16189 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16190
16191 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016192 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016193
16194 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16195 pci_cmd &= ~PCI_COMMAND_MEMORY;
16196 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16197 }
Joe Perches41535772013-02-16 11:20:04 +000016198 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016199 tp->read32_mbox = tg3_read32_mbox_5906;
16200 tp->write32_mbox = tg3_write32_mbox_5906;
16201 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16202 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16203 }
Michael Chan68929142005-08-09 20:17:14 -070016204
Michael Chanbbadf502006-04-06 21:46:34 -070016205 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016206 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016207 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16208 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016209 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016210
Matt Carlson16821282011-07-13 09:27:28 +000016211 /* The memory arbiter has to be enabled in order for SRAM accesses
16212 * to succeed. Normally on powerup the tg3 chip firmware will make
16213 * sure it is enabled, but other entities such as system netboot
16214 * code might disable it.
16215 */
16216 val = tr32(MEMARB_MODE);
16217 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16218
Matt Carlson9dc5e342011-11-04 09:15:02 +000016219 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016220 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016221 tg3_flag(tp, 5780_CLASS)) {
16222 if (tg3_flag(tp, PCIX_MODE)) {
16223 pci_read_config_dword(tp->pdev,
16224 tp->pcix_cap + PCI_X_STATUS,
16225 &val);
16226 tp->pci_fn = val & 0x7;
16227 }
Joe Perches41535772013-02-16 11:20:04 +000016228 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16229 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16230 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016231 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016232 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16233 val = tr32(TG3_CPMU_STATUS);
16234
Joe Perches41535772013-02-16 11:20:04 +000016235 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016236 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16237 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016238 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16239 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016240 }
16241
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016242 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16243 tp->write32_tx_mbox = tg3_write_flush_reg32;
16244 tp->write32_rx_mbox = tg3_write_flush_reg32;
16245 }
16246
Michael Chan7d0c41e2005-04-21 17:06:20 -070016247 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016248 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016249 * determined before calling tg3_set_power_state() so that
16250 * we know whether or not to switch out of Vaux power.
16251 * When the flag is set, it means that GPIO1 is used for eeprom
16252 * write protect and also implies that it is a LOM where GPIOs
16253 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016254 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016255 tg3_get_eeprom_hw_cfg(tp);
16256
Matt Carlson1caf13e2013-03-06 17:02:29 +000016257 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016258 tg3_flag_clear(tp, TSO_CAPABLE);
16259 tg3_flag_clear(tp, TSO_BUG);
16260 tp->fw_needed = NULL;
16261 }
16262
Joe Perches63c3a662011-04-26 08:12:10 +000016263 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016264 /* Allow reads and writes to the
16265 * APE register and memory space.
16266 */
16267 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016268 PCISTATE_ALLOW_APE_SHMEM_WR |
16269 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016270 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16271 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016272
16273 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016274 }
16275
Matt Carlson16821282011-07-13 09:27:28 +000016276 /* Set up tp->grc_local_ctrl before calling
16277 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16278 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016279 * It is also used as eeprom write protect on LOMs.
16280 */
16281 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016282 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016283 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016284 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16285 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016286 /* Unused GPIO3 must be driven as output on 5752 because there
16287 * are no pull-up resistors on unused GPIO pins.
16288 */
Joe Perches41535772013-02-16 11:20:04 +000016289 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016290 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016291
Joe Perches41535772013-02-16 11:20:04 +000016292 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16293 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016294 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016295 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16296
Matt Carlson8d519ab2009-04-20 06:58:01 +000016297 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16298 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016299 /* Turn off the debug UART. */
16300 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016301 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016302 /* Keep VMain power. */
16303 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16304 GRC_LCLCTRL_GPIO_OUTPUT0;
16305 }
16306
Joe Perches41535772013-02-16 11:20:04 +000016307 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016308 tp->grc_local_ctrl |=
16309 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16310
Matt Carlson16821282011-07-13 09:27:28 +000016311 /* Switch out of Vaux if it is a NIC */
16312 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016313
Linus Torvalds1da177e2005-04-16 15:20:36 -070016314 /* Derive initial jumbo mode from MTU assigned in
16315 * ether_setup() via the alloc_etherdev() call
16316 */
Joe Perches63c3a662011-04-26 08:12:10 +000016317 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16318 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016319
16320 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016321 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16322 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16323 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16324 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016325 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016326 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016327 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016328 }
16329
Joe Perches41535772013-02-16 11:20:04 +000016330 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016331 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016332
Linus Torvalds1da177e2005-04-16 15:20:36 -070016333 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016334 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16335 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16336 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16337 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016338 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16339 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16340 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016341
Joe Perches41535772013-02-16 11:20:04 +000016342 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16343 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016344 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016345 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016346 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016347
Joe Perches63c3a662011-04-26 08:12:10 +000016348 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016349 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016350 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16351 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016352 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016353 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16354 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16355 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16356 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016357 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16358 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016359 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016360 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016361 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016362 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016363 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016365
Joe Perches41535772013-02-16 11:20:04 +000016366 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16367 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016368 tp->phy_otp = tg3_read_otp_phycfg(tp);
16369 if (tp->phy_otp == 0)
16370 tp->phy_otp = TG3_OTP_DEFAULT;
16371 }
16372
Joe Perches63c3a662011-04-26 08:12:10 +000016373 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016374 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16375 else
16376 tp->mi_mode = MAC_MI_MODE_BASE;
16377
Linus Torvalds1da177e2005-04-16 15:20:36 -070016378 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016379 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16380 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016381 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16382
Matt Carlson4d958472011-04-20 07:57:35 +000016383 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016384 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16385 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16386 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016387 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16388 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16389 }
16390
Joe Perches41535772013-02-16 11:20:04 +000016391 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16392 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016393 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016394
Matt Carlson158d7ab2008-05-29 01:37:54 -070016395 err = tg3_mdio_init(tp);
16396 if (err)
16397 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016398
16399 /* Initialize data/descriptor byte/word swapping. */
16400 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016401 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16402 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016403 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16404 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16405 GRC_MODE_B2HRX_ENABLE |
16406 GRC_MODE_HTX2B_ENABLE |
16407 GRC_MODE_HOST_STACKUP);
16408 else
16409 val &= GRC_MODE_HOST_STACKUP;
16410
Linus Torvalds1da177e2005-04-16 15:20:36 -070016411 tw32(GRC_MODE, val | tp->grc_mode);
16412
16413 tg3_switch_clocks(tp);
16414
16415 /* Clear this out for sanity. */
16416 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16417
16418 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16419 &pci_state_reg);
16420 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016421 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016422 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16423 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16424 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16425 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016426 void __iomem *sram_base;
16427
16428 /* Write some dummy words into the SRAM status block
16429 * area, see if it reads back correctly. If the return
16430 * value is bad, force enable the PCIX workaround.
16431 */
16432 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16433
16434 writel(0x00000000, sram_base);
16435 writel(0x00000000, sram_base + 4);
16436 writel(0xffffffff, sram_base + 4);
16437 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016438 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016439 }
16440 }
16441
16442 udelay(50);
16443 tg3_nvram_init(tp);
16444
Nithin Sujirc4dab502013-03-06 17:02:34 +000016445 /* If the device has an NVRAM, no need to load patch firmware */
16446 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16447 !tg3_flag(tp, NO_NVRAM))
16448 tp->fw_needed = NULL;
16449
Linus Torvalds1da177e2005-04-16 15:20:36 -070016450 grc_misc_cfg = tr32(GRC_MISC_CFG);
16451 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16452
Joe Perches41535772013-02-16 11:20:04 +000016453 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016454 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16455 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016456 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016457
Joe Perches63c3a662011-04-26 08:12:10 +000016458 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016459 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016460 tg3_flag_set(tp, TAGGED_STATUS);
16461 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016462 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16463 HOSTCC_MODE_CLRTICK_TXBD);
16464
16465 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16466 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16467 tp->misc_host_ctrl);
16468 }
16469
Matt Carlson3bda1252008-08-15 14:08:22 -070016470 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016471 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016472 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016473 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016474 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016475
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016476 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016477 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016478
16479 err = tg3_phy_probe(tp);
16480 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016481 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016482 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016483 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016484 }
16485
Matt Carlson184b8902010-04-05 10:19:25 +000016486 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016487 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016488
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016489 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16490 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016491 } else {
Joe Perches41535772013-02-16 11:20:04 +000016492 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016493 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016494 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016495 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016496 }
16497
16498 /* 5700 {AX,BX} chips have a broken status block link
16499 * change bit implementation, so we must use the
16500 * status register in those cases.
16501 */
Joe Perches41535772013-02-16 11:20:04 +000016502 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016503 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016504 else
Joe Perches63c3a662011-04-26 08:12:10 +000016505 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016506
16507 /* The led_ctrl is set during tg3_phy_probe, here we might
16508 * have to force the link status polling mechanism based
16509 * upon subsystem IDs.
16510 */
16511 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016512 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016513 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16514 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016515 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016516 }
16517
16518 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016519 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016520 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016521 else
Joe Perches63c3a662011-04-26 08:12:10 +000016522 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016523
Eric Dumazet9205fd92011-11-18 06:47:01 +000016524 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016525 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016526 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016527 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016528 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016529#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016530 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016531#endif
16532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016533
Matt Carlson2c49a442010-09-30 10:34:35 +000016534 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16535 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016536 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16537
Matt Carlson2c49a442010-09-30 10:34:35 +000016538 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016539
16540 /* Increment the rx prod index on the rx std ring by at most
16541 * 8 for these chips to workaround hw errata.
16542 */
Joe Perches41535772013-02-16 11:20:04 +000016543 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16544 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16545 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016546 tp->rx_std_max_post = 8;
16547
Joe Perches63c3a662011-04-26 08:12:10 +000016548 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016549 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16550 PCIE_PWR_MGMT_L1_THRESH_MSK;
16551
Linus Torvalds1da177e2005-04-16 15:20:36 -070016552 return err;
16553}
16554
David S. Miller49b6e95f2007-03-29 01:38:42 -070016555#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016556static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016557{
16558 struct net_device *dev = tp->dev;
16559 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016560 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016561 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016562 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016563
David S. Miller49b6e95f2007-03-29 01:38:42 -070016564 addr = of_get_property(dp, "local-mac-address", &len);
16565 if (addr && len == 6) {
16566 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016568 }
16569 return -ENODEV;
16570}
16571
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016572static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016573{
16574 struct net_device *dev = tp->dev;
16575
16576 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
16577 return 0;
16578}
16579#endif
16580
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016581static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016582{
16583 struct net_device *dev = tp->dev;
16584 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016585 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016586 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016587
David S. Miller49b6e95f2007-03-29 01:38:42 -070016588#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016589 if (!tg3_get_macaddr_sparc(tp))
16590 return 0;
16591#endif
16592
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016593 if (tg3_flag(tp, IS_SSB_CORE)) {
16594 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16595 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16596 return 0;
16597 }
16598
Linus Torvalds1da177e2005-04-16 15:20:36 -070016599 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016600 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016601 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016602 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16603 mac_offset = 0xcc;
16604 if (tg3_nvram_lock(tp))
16605 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16606 else
16607 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016608 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016609 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016610 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016611 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016612 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016613 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016614 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016615
16616 /* First try to get it from MAC address mailbox. */
16617 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16618 if ((hi >> 16) == 0x484b) {
16619 dev->dev_addr[0] = (hi >> 8) & 0xff;
16620 dev->dev_addr[1] = (hi >> 0) & 0xff;
16621
16622 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16623 dev->dev_addr[2] = (lo >> 24) & 0xff;
16624 dev->dev_addr[3] = (lo >> 16) & 0xff;
16625 dev->dev_addr[4] = (lo >> 8) & 0xff;
16626 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016627
Michael Chan008652b2006-03-27 23:14:53 -080016628 /* Some old bootcode may report a 0 MAC address in SRAM */
16629 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16630 }
16631 if (!addr_ok) {
16632 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016633 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016634 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016635 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016636 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16637 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016638 }
16639 /* Finally just fetch it out of the MAC control regs. */
16640 else {
16641 hi = tr32(MAC_ADDR_0_HIGH);
16642 lo = tr32(MAC_ADDR_0_LOW);
16643
16644 dev->dev_addr[5] = lo & 0xff;
16645 dev->dev_addr[4] = (lo >> 8) & 0xff;
16646 dev->dev_addr[3] = (lo >> 16) & 0xff;
16647 dev->dev_addr[2] = (lo >> 24) & 0xff;
16648 dev->dev_addr[1] = hi & 0xff;
16649 dev->dev_addr[0] = (hi >> 8) & 0xff;
16650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016651 }
16652
16653 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016654#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016655 if (!tg3_get_default_macaddr_sparc(tp))
16656 return 0;
16657#endif
16658 return -EINVAL;
16659 }
16660 return 0;
16661}
16662
David S. Miller59e6b432005-05-18 22:50:10 -070016663#define BOUNDARY_SINGLE_CACHELINE 1
16664#define BOUNDARY_MULTI_CACHELINE 2
16665
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016666static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016667{
16668 int cacheline_size;
16669 u8 byte;
16670 int goal;
16671
16672 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16673 if (byte == 0)
16674 cacheline_size = 1024;
16675 else
16676 cacheline_size = (int) byte * 4;
16677
16678 /* On 5703 and later chips, the boundary bits have no
16679 * effect.
16680 */
Joe Perches41535772013-02-16 11:20:04 +000016681 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16682 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016683 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016684 goto out;
16685
16686#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16687 goal = BOUNDARY_MULTI_CACHELINE;
16688#else
16689#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16690 goal = BOUNDARY_SINGLE_CACHELINE;
16691#else
16692 goal = 0;
16693#endif
16694#endif
16695
Joe Perches63c3a662011-04-26 08:12:10 +000016696 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016697 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16698 goto out;
16699 }
16700
David S. Miller59e6b432005-05-18 22:50:10 -070016701 if (!goal)
16702 goto out;
16703
16704 /* PCI controllers on most RISC systems tend to disconnect
16705 * when a device tries to burst across a cache-line boundary.
16706 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16707 *
16708 * Unfortunately, for PCI-E there are only limited
16709 * write-side controls for this, and thus for reads
16710 * we will still get the disconnects. We'll also waste
16711 * these PCI cycles for both read and write for chips
16712 * other than 5700 and 5701 which do not implement the
16713 * boundary bits.
16714 */
Joe Perches63c3a662011-04-26 08:12:10 +000016715 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016716 switch (cacheline_size) {
16717 case 16:
16718 case 32:
16719 case 64:
16720 case 128:
16721 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16722 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16723 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16724 } else {
16725 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16726 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16727 }
16728 break;
16729
16730 case 256:
16731 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16732 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16733 break;
16734
16735 default:
16736 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16737 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16738 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016739 }
Joe Perches63c3a662011-04-26 08:12:10 +000016740 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016741 switch (cacheline_size) {
16742 case 16:
16743 case 32:
16744 case 64:
16745 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16746 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16747 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16748 break;
16749 }
16750 /* fallthrough */
16751 case 128:
16752 default:
16753 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16754 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16755 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016756 }
David S. Miller59e6b432005-05-18 22:50:10 -070016757 } else {
16758 switch (cacheline_size) {
16759 case 16:
16760 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16761 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16762 DMA_RWCTRL_WRITE_BNDRY_16);
16763 break;
16764 }
16765 /* fallthrough */
16766 case 32:
16767 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16768 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16769 DMA_RWCTRL_WRITE_BNDRY_32);
16770 break;
16771 }
16772 /* fallthrough */
16773 case 64:
16774 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16775 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16776 DMA_RWCTRL_WRITE_BNDRY_64);
16777 break;
16778 }
16779 /* fallthrough */
16780 case 128:
16781 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16782 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16783 DMA_RWCTRL_WRITE_BNDRY_128);
16784 break;
16785 }
16786 /* fallthrough */
16787 case 256:
16788 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16789 DMA_RWCTRL_WRITE_BNDRY_256);
16790 break;
16791 case 512:
16792 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16793 DMA_RWCTRL_WRITE_BNDRY_512);
16794 break;
16795 case 1024:
16796 default:
16797 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16798 DMA_RWCTRL_WRITE_BNDRY_1024);
16799 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016800 }
David S. Miller59e6b432005-05-18 22:50:10 -070016801 }
16802
16803out:
16804 return val;
16805}
16806
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016807static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000016808 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016809{
16810 struct tg3_internal_buffer_desc test_desc;
16811 u32 sram_dma_descs;
16812 int i, ret;
16813
16814 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16815
16816 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16817 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16818 tw32(RDMAC_STATUS, 0);
16819 tw32(WDMAC_STATUS, 0);
16820
16821 tw32(BUFMGR_MODE, 0);
16822 tw32(FTQ_RESET, 0);
16823
16824 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16825 test_desc.addr_lo = buf_dma & 0xffffffff;
16826 test_desc.nic_mbuf = 0x00002100;
16827 test_desc.len = size;
16828
16829 /*
16830 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16831 * the *second* time the tg3 driver was getting loaded after an
16832 * initial scan.
16833 *
16834 * Broadcom tells me:
16835 * ...the DMA engine is connected to the GRC block and a DMA
16836 * reset may affect the GRC block in some unpredictable way...
16837 * The behavior of resets to individual blocks has not been tested.
16838 *
16839 * Broadcom noted the GRC reset will also reset all sub-components.
16840 */
16841 if (to_device) {
16842 test_desc.cqid_sqid = (13 << 8) | 2;
16843
16844 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16845 udelay(40);
16846 } else {
16847 test_desc.cqid_sqid = (16 << 8) | 7;
16848
16849 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16850 udelay(40);
16851 }
16852 test_desc.flags = 0x00000005;
16853
16854 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16855 u32 val;
16856
16857 val = *(((u32 *)&test_desc) + i);
16858 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16859 sram_dma_descs + (i * sizeof(u32)));
16860 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16861 }
16862 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16863
Matt Carlson859a588792010-04-05 10:19:28 +000016864 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016865 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016866 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016867 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016868
16869 ret = -ENODEV;
16870 for (i = 0; i < 40; i++) {
16871 u32 val;
16872
16873 if (to_device)
16874 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16875 else
16876 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16877 if ((val & 0xffff) == sram_dma_descs) {
16878 ret = 0;
16879 break;
16880 }
16881
16882 udelay(100);
16883 }
16884
16885 return ret;
16886}
16887
David S. Millerded73402005-05-23 13:59:47 -070016888#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016889
Matt Carlson41434702011-03-09 16:58:22 +000016890static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016891 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16892 { },
16893};
16894
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016895static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016896{
16897 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016898 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016899 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016900
Matt Carlson4bae65c2010-11-24 08:31:52 +000016901 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16902 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016903 if (!buf) {
16904 ret = -ENOMEM;
16905 goto out_nofree;
16906 }
16907
16908 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16909 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16910
David S. Miller59e6b432005-05-18 22:50:10 -070016911 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016912
Joe Perches63c3a662011-04-26 08:12:10 +000016913 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016914 goto out;
16915
Joe Perches63c3a662011-04-26 08:12:10 +000016916 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016917 /* DMA read watermark not used on PCIE */
16918 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016919 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016920 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16921 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016922 tp->dma_rwctrl |= 0x003f0000;
16923 else
16924 tp->dma_rwctrl |= 0x003f000f;
16925 } else {
Joe Perches41535772013-02-16 11:20:04 +000016926 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16927 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016928 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016929 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016930
Michael Chan4a29cc22006-03-19 13:21:12 -080016931 /* If the 5704 is behind the EPB bridge, we can
16932 * do the less restrictive ONE_DMA workaround for
16933 * better performance.
16934 */
Joe Perches63c3a662011-04-26 08:12:10 +000016935 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016936 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016937 tp->dma_rwctrl |= 0x8000;
16938 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016939 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16940
Joe Perches41535772013-02-16 11:20:04 +000016941 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016942 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016943 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016944 tp->dma_rwctrl |=
16945 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16946 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16947 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016948 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016949 /* 5780 always in PCIX mode */
16950 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016951 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016952 /* 5714 always in PCIX mode */
16953 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016954 } else {
16955 tp->dma_rwctrl |= 0x001b000f;
16956 }
16957 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016958 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16959 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016960
Joe Perches41535772013-02-16 11:20:04 +000016961 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16962 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016963 tp->dma_rwctrl &= 0xfffffff0;
16964
Joe Perches41535772013-02-16 11:20:04 +000016965 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16966 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016967 /* Remove this if it causes problems for some boards. */
16968 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16969
16970 /* On 5700/5701 chips, we need to set this bit.
16971 * Otherwise the chip will issue cacheline transactions
16972 * to streamable DMA memory with not all the byte
16973 * enables turned on. This is an error on several
16974 * RISC PCI controllers, in particular sparc64.
16975 *
16976 * On 5703/5704 chips, this bit has been reassigned
16977 * a different meaning. In particular, it is used
16978 * on those chips to enable a PCI-X workaround.
16979 */
16980 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16981 }
16982
16983 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16984
16985#if 0
16986 /* Unneeded, already done by tg3_get_invariants. */
16987 tg3_switch_clocks(tp);
16988#endif
16989
Joe Perches41535772013-02-16 11:20:04 +000016990 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16991 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016992 goto out;
16993
David S. Miller59e6b432005-05-18 22:50:10 -070016994 /* It is best to perform DMA test with maximum write burst size
16995 * to expose the 5700/5701 write DMA bug.
16996 */
16997 saved_dma_rwctrl = tp->dma_rwctrl;
16998 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16999 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017001 while (1) {
17002 u32 *p = buf, i;
17003
17004 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17005 p[i] = i;
17006
17007 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017008 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017009 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017010 dev_err(&tp->pdev->dev,
17011 "%s: Buffer write failed. err = %d\n",
17012 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017013 break;
17014 }
17015
17016#if 0
17017 /* validate data reached card RAM correctly. */
17018 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17019 u32 val;
17020 tg3_read_mem(tp, 0x2100 + (i*4), &val);
17021 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000017022 dev_err(&tp->pdev->dev,
17023 "%s: Buffer corrupted on device! "
17024 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017025 /* ret = -ENODEV here? */
17026 }
17027 p[i] = 0;
17028 }
17029#endif
17030 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017031 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017032 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017033 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17034 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017035 break;
17036 }
17037
17038 /* Verify it. */
17039 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17040 if (p[i] == i)
17041 continue;
17042
David S. Miller59e6b432005-05-18 22:50:10 -070017043 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17044 DMA_RWCTRL_WRITE_BNDRY_16) {
17045 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017046 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17047 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17048 break;
17049 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017050 dev_err(&tp->pdev->dev,
17051 "%s: Buffer corrupted on read back! "
17052 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017053 ret = -ENODEV;
17054 goto out;
17055 }
17056 }
17057
17058 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17059 /* Success. */
17060 ret = 0;
17061 break;
17062 }
17063 }
David S. Miller59e6b432005-05-18 22:50:10 -070017064 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17065 DMA_RWCTRL_WRITE_BNDRY_16) {
17066 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017067 * now look for chipsets that are known to expose the
17068 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017069 */
Matt Carlson41434702011-03-09 16:58:22 +000017070 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017071 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17072 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017073 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017074 /* Safe to use the calculated DMA boundary. */
17075 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017076 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017077
David S. Miller59e6b432005-05-18 22:50:10 -070017078 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017080
17081out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017082 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017083out_nofree:
17084 return ret;
17085}
17086
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017087static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017088{
Joe Perches63c3a662011-04-26 08:12:10 +000017089 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017090 tp->bufmgr_config.mbuf_read_dma_low_water =
17091 DEFAULT_MB_RDMA_LOW_WATER_5705;
17092 tp->bufmgr_config.mbuf_mac_rx_low_water =
17093 DEFAULT_MB_MACRX_LOW_WATER_57765;
17094 tp->bufmgr_config.mbuf_high_water =
17095 DEFAULT_MB_HIGH_WATER_57765;
17096
17097 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17098 DEFAULT_MB_RDMA_LOW_WATER_5705;
17099 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17100 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17101 tp->bufmgr_config.mbuf_high_water_jumbo =
17102 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017103 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017104 tp->bufmgr_config.mbuf_read_dma_low_water =
17105 DEFAULT_MB_RDMA_LOW_WATER_5705;
17106 tp->bufmgr_config.mbuf_mac_rx_low_water =
17107 DEFAULT_MB_MACRX_LOW_WATER_5705;
17108 tp->bufmgr_config.mbuf_high_water =
17109 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017110 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017111 tp->bufmgr_config.mbuf_mac_rx_low_water =
17112 DEFAULT_MB_MACRX_LOW_WATER_5906;
17113 tp->bufmgr_config.mbuf_high_water =
17114 DEFAULT_MB_HIGH_WATER_5906;
17115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017116
Michael Chanfdfec1722005-07-25 12:31:48 -070017117 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17118 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17119 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17120 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17121 tp->bufmgr_config.mbuf_high_water_jumbo =
17122 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17123 } else {
17124 tp->bufmgr_config.mbuf_read_dma_low_water =
17125 DEFAULT_MB_RDMA_LOW_WATER;
17126 tp->bufmgr_config.mbuf_mac_rx_low_water =
17127 DEFAULT_MB_MACRX_LOW_WATER;
17128 tp->bufmgr_config.mbuf_high_water =
17129 DEFAULT_MB_HIGH_WATER;
17130
17131 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17132 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17133 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17134 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17135 tp->bufmgr_config.mbuf_high_water_jumbo =
17136 DEFAULT_MB_HIGH_WATER_JUMBO;
17137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017138
17139 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17140 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17141}
17142
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017143static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017144{
Matt Carlson79eb6902010-02-17 15:17:03 +000017145 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17146 case TG3_PHY_ID_BCM5400: return "5400";
17147 case TG3_PHY_ID_BCM5401: return "5401";
17148 case TG3_PHY_ID_BCM5411: return "5411";
17149 case TG3_PHY_ID_BCM5701: return "5701";
17150 case TG3_PHY_ID_BCM5703: return "5703";
17151 case TG3_PHY_ID_BCM5704: return "5704";
17152 case TG3_PHY_ID_BCM5705: return "5705";
17153 case TG3_PHY_ID_BCM5750: return "5750";
17154 case TG3_PHY_ID_BCM5752: return "5752";
17155 case TG3_PHY_ID_BCM5714: return "5714";
17156 case TG3_PHY_ID_BCM5780: return "5780";
17157 case TG3_PHY_ID_BCM5755: return "5755";
17158 case TG3_PHY_ID_BCM5787: return "5787";
17159 case TG3_PHY_ID_BCM5784: return "5784";
17160 case TG3_PHY_ID_BCM5756: return "5722/5756";
17161 case TG3_PHY_ID_BCM5906: return "5906";
17162 case TG3_PHY_ID_BCM5761: return "5761";
17163 case TG3_PHY_ID_BCM5718C: return "5718C";
17164 case TG3_PHY_ID_BCM5718S: return "5718S";
17165 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017166 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017167 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017168 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017169 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017170 case 0: return "serdes";
17171 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017173}
17174
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017175static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017176{
Joe Perches63c3a662011-04-26 08:12:10 +000017177 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017178 strcpy(str, "PCI Express");
17179 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017180 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017181 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17182
17183 strcpy(str, "PCIX:");
17184
17185 if ((clock_ctrl == 7) ||
17186 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17187 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17188 strcat(str, "133MHz");
17189 else if (clock_ctrl == 0)
17190 strcat(str, "33MHz");
17191 else if (clock_ctrl == 2)
17192 strcat(str, "50MHz");
17193 else if (clock_ctrl == 4)
17194 strcat(str, "66MHz");
17195 else if (clock_ctrl == 6)
17196 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017197 } else {
17198 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017199 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017200 strcat(str, "66MHz");
17201 else
17202 strcat(str, "33MHz");
17203 }
Joe Perches63c3a662011-04-26 08:12:10 +000017204 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017205 strcat(str, ":32-bit");
17206 else
17207 strcat(str, ":64-bit");
17208 return str;
17209}
17210
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017211static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017212{
17213 struct ethtool_coalesce *ec = &tp->coal;
17214
17215 memset(ec, 0, sizeof(*ec));
17216 ec->cmd = ETHTOOL_GCOALESCE;
17217 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17218 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17219 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17220 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17221 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17222 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17223 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17224 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17225 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17226
17227 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17228 HOSTCC_MODE_CLRTICK_TXBD)) {
17229 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17230 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17231 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17232 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17233 }
Michael Chand244c892005-07-05 14:42:33 -070017234
Joe Perches63c3a662011-04-26 08:12:10 +000017235 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017236 ec->rx_coalesce_usecs_irq = 0;
17237 ec->tx_coalesce_usecs_irq = 0;
17238 ec->stats_block_coalesce_usecs = 0;
17239 }
David S. Miller15f98502005-05-18 22:49:26 -070017240}
17241
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017242static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017243 const struct pci_device_id *ent)
17244{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017245 struct net_device *dev;
17246 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017247 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017248 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017249 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017250 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017251 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017252
Joe Perches05dbe002010-02-17 19:44:19 +000017253 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017254
17255 err = pci_enable_device(pdev);
17256 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017257 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017258 return err;
17259 }
17260
Linus Torvalds1da177e2005-04-16 15:20:36 -070017261 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17262 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017263 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017264 goto err_out_disable_pdev;
17265 }
17266
17267 pci_set_master(pdev);
17268
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017269 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017270 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017271 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017272 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017273 }
17274
Linus Torvalds1da177e2005-04-16 15:20:36 -070017275 SET_NETDEV_DEV(dev, &pdev->dev);
17276
Linus Torvalds1da177e2005-04-16 15:20:36 -070017277 tp = netdev_priv(dev);
17278 tp->pdev = pdev;
17279 tp->dev = dev;
Yijing Wang5865fc12013-06-02 21:36:21 +000017280 tp->pm_cap = pdev->pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017281 tp->rx_mode = TG3_DEF_RX_MODE;
17282 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017283 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017284
Linus Torvalds1da177e2005-04-16 15:20:36 -070017285 if (tg3_debug > 0)
17286 tp->msg_enable = tg3_debug;
17287 else
17288 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17289
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017290 if (pdev_is_ssb_gige_core(pdev)) {
17291 tg3_flag_set(tp, IS_SSB_CORE);
17292 if (ssb_gige_must_flush_posted_writes(pdev))
17293 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17294 if (ssb_gige_one_dma_at_once(pdev))
17295 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
17296 if (ssb_gige_have_roboswitch(pdev))
17297 tg3_flag_set(tp, ROBOSWITCH);
17298 if (ssb_gige_is_rgmii(pdev))
17299 tg3_flag_set(tp, RGMII_MODE);
17300 }
17301
Linus Torvalds1da177e2005-04-16 15:20:36 -070017302 /* The word/byte swap controls here control register access byte
17303 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17304 * setting below.
17305 */
17306 tp->misc_host_ctrl =
17307 MISC_HOST_CTRL_MASK_PCI_INT |
17308 MISC_HOST_CTRL_WORD_SWAP |
17309 MISC_HOST_CTRL_INDIR_ACCESS |
17310 MISC_HOST_CTRL_PCISTATE_RW;
17311
17312 /* The NONFRM (non-frame) byte/word swap controls take effect
17313 * on descriptor entries, anything which isn't packet data.
17314 *
17315 * The StrongARM chips on the board (one for tx, one for rx)
17316 * are running in big-endian mode.
17317 */
17318 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17319 GRC_MODE_WSWAP_NONFRM_DATA);
17320#ifdef __BIG_ENDIAN
17321 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17322#endif
17323 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017324 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017325 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017326
Matt Carlsond5fe4882008-11-21 17:20:32 -080017327 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017328 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017329 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017330 err = -ENOMEM;
17331 goto err_out_free_dev;
17332 }
17333
Matt Carlsonc9cab242011-07-13 09:27:27 +000017334 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17335 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17336 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17337 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17338 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017339 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017340 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17341 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017342 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
17343 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17344 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
17345 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017346 tg3_flag_set(tp, ENABLE_APE);
17347 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17348 if (!tp->aperegs) {
17349 dev_err(&pdev->dev,
17350 "Cannot map APE registers, aborting\n");
17351 err = -ENOMEM;
17352 goto err_out_iounmap;
17353 }
17354 }
17355
Linus Torvalds1da177e2005-04-16 15:20:36 -070017356 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17357 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017358
Linus Torvalds1da177e2005-04-16 15:20:36 -070017359 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017360 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017361 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017362 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017363
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017364 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017365 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017366 dev_err(&pdev->dev,
17367 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017368 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017369 }
17370
Michael Chan4a29cc22006-03-19 13:21:12 -080017371 /* The EPB bridge inside 5714, 5715, and 5780 and any
17372 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017373 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17374 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17375 * do DMA address check in tg3_start_xmit().
17376 */
Joe Perches63c3a662011-04-26 08:12:10 +000017377 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017378 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017379 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017380 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017381#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017382 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017383#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017384 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017385 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017386
17387 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017388 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017389 err = pci_set_dma_mask(pdev, dma_mask);
17390 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017391 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017392 err = pci_set_consistent_dma_mask(pdev,
17393 persist_dma_mask);
17394 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017395 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17396 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017397 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017398 }
17399 }
17400 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017401 if (err || dma_mask == DMA_BIT_MASK(32)) {
17402 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017403 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017404 dev_err(&pdev->dev,
17405 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017406 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017407 }
17408 }
17409
Michael Chanfdfec1722005-07-25 12:31:48 -070017410 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017411
Patrick McHardyf6469682013-04-19 02:04:27 +000017412 features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlson0da06062011-05-19 12:12:53 +000017413
17414 /* 5700 B0 chips do not support checksumming correctly due
17415 * to hardware bugs.
17416 */
Joe Perches41535772013-02-16 11:20:04 +000017417 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017418 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17419
17420 if (tg3_flag(tp, 5755_PLUS))
17421 features |= NETIF_F_IPV6_CSUM;
17422 }
17423
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017424 /* TSO is on by default on chips that support hardware TSO.
17425 * Firmware TSO on older chips gives lower performance, so it
17426 * is off by default, but can be enabled using ethtool.
17427 */
Joe Perches63c3a662011-04-26 08:12:10 +000017428 if ((tg3_flag(tp, HW_TSO_1) ||
17429 tg3_flag(tp, HW_TSO_2) ||
17430 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017431 (features & NETIF_F_IP_CSUM))
17432 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017433 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017434 if (features & NETIF_F_IPV6_CSUM)
17435 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017436 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017437 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17438 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17439 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17440 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17441 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017442 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017444
Matt Carlsond542fe22011-05-19 16:02:43 +000017445 dev->features |= features;
17446 dev->vlan_features |= features;
17447
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017448 /*
17449 * Add loopback capability only for a subset of devices that support
17450 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17451 * loopback for the remaining devices.
17452 */
Joe Perches41535772013-02-16 11:20:04 +000017453 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017454 !tg3_flag(tp, CPMU_PRESENT))
17455 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017456 features |= NETIF_F_LOOPBACK;
17457
Matt Carlson0da06062011-05-19 12:12:53 +000017458 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017459
Joe Perches41535772013-02-16 11:20:04 +000017460 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017461 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017462 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017463 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017464 tp->rx_pending = 63;
17465 }
17466
Linus Torvalds1da177e2005-04-16 15:20:36 -070017467 err = tg3_get_device_address(tp);
17468 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017469 dev_err(&pdev->dev,
17470 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017471 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017472 }
17473
Matt Carlsonc88864d2007-11-12 21:07:01 -080017474 /*
17475 * Reset chip in case UNDI or EFI driver did not shutdown
17476 * DMA self test will enable WDMAC and we'll see (spurious)
17477 * pending DMA on the PCI bus at that point.
17478 */
17479 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17480 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17481 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17482 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17483 }
17484
17485 err = tg3_test_dma(tp);
17486 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017487 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017488 goto err_out_apeunmap;
17489 }
17490
Matt Carlson78f90dc2009-11-13 13:03:42 +000017491 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17492 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17493 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017494 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017495 struct tg3_napi *tnapi = &tp->napi[i];
17496
17497 tnapi->tp = tp;
17498 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17499
17500 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017501 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017502 intmbx += 0x8;
17503 else
17504 intmbx += 0x4;
17505
17506 tnapi->consmbox = rcvmbx;
17507 tnapi->prodmbox = sndmbx;
17508
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017509 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017510 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017511 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017512 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017513
Joe Perches63c3a662011-04-26 08:12:10 +000017514 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017515 break;
17516
17517 /*
17518 * If we support MSIX, we'll be using RSS. If we're using
17519 * RSS, the first vector only handles link interrupts and the
17520 * remaining vectors handle rx and tx interrupts. Reuse the
17521 * mailbox values for the next iteration. The values we setup
17522 * above are still useful for the single vectored mode.
17523 */
17524 if (!i)
17525 continue;
17526
17527 rcvmbx += 0x8;
17528
17529 if (sndmbx & 0x4)
17530 sndmbx -= 0x4;
17531 else
17532 sndmbx += 0xc;
17533 }
17534
Matt Carlsonc88864d2007-11-12 21:07:01 -080017535 tg3_init_coal(tp);
17536
Michael Chanc49a1562006-12-17 17:07:29 -080017537 pci_set_drvdata(pdev, dev);
17538
Joe Perches41535772013-02-16 11:20:04 +000017539 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17540 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17541 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017542 tg3_flag_set(tp, PTP_CAPABLE);
17543
Matt Carlson21f76382012-02-22 12:35:21 +000017544 tg3_timer_init(tp);
17545
Michael Chan402e1392013-02-14 12:13:41 +000017546 tg3_carrier_off(tp);
17547
Linus Torvalds1da177e2005-04-16 15:20:36 -070017548 err = register_netdev(dev);
17549 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017550 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017551 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017552 }
17553
Joe Perches05dbe002010-02-17 19:44:19 +000017554 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17555 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017556 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017557 tg3_bus_string(tp, str),
17558 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017559
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017560 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017561 struct phy_device *phydev;
17562 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017563 netdev_info(dev,
17564 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017565 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017566 } else {
17567 char *ethtype;
17568
17569 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17570 ethtype = "10/100Base-TX";
17571 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17572 ethtype = "1000Base-SX";
17573 else
17574 ethtype = "10/100/1000Base-T";
17575
Matt Carlson5129c3a2010-04-05 10:19:23 +000017576 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017577 "(WireSpeed[%d], EEE[%d])\n",
17578 tg3_phy_string(tp), ethtype,
17579 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17580 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017581 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017582
Joe Perches05dbe002010-02-17 19:44:19 +000017583 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017584 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017585 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017586 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017587 tg3_flag(tp, ENABLE_ASF) != 0,
17588 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017589 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17590 tp->dma_rwctrl,
17591 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17592 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017593
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017594 pci_save_state(pdev);
17595
Linus Torvalds1da177e2005-04-16 15:20:36 -070017596 return 0;
17597
Matt Carlson0d3031d2007-10-10 18:02:43 -070017598err_out_apeunmap:
17599 if (tp->aperegs) {
17600 iounmap(tp->aperegs);
17601 tp->aperegs = NULL;
17602 }
17603
Linus Torvalds1da177e2005-04-16 15:20:36 -070017604err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017605 if (tp->regs) {
17606 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017607 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017609
17610err_out_free_dev:
17611 free_netdev(dev);
17612
17613err_out_free_res:
17614 pci_release_regions(pdev);
17615
17616err_out_disable_pdev:
17617 pci_disable_device(pdev);
17618 pci_set_drvdata(pdev, NULL);
17619 return err;
17620}
17621
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017622static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017623{
17624 struct net_device *dev = pci_get_drvdata(pdev);
17625
17626 if (dev) {
17627 struct tg3 *tp = netdev_priv(dev);
17628
Jesper Juhle3c55302012-04-09 22:50:15 +020017629 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017630
Matt Carlsondb219972011-11-04 09:15:03 +000017631 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017632
David S. Miller1805b2f2011-10-24 18:18:09 -040017633 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017634 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017635 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017636 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017637
Linus Torvalds1da177e2005-04-16 15:20:36 -070017638 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017639 if (tp->aperegs) {
17640 iounmap(tp->aperegs);
17641 tp->aperegs = NULL;
17642 }
Michael Chan68929142005-08-09 20:17:14 -070017643 if (tp->regs) {
17644 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017645 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017647 free_netdev(dev);
17648 pci_release_regions(pdev);
17649 pci_disable_device(pdev);
17650 pci_set_drvdata(pdev, NULL);
17651 }
17652}
17653
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017654#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017655static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017656{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017657 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017658 struct net_device *dev = pci_get_drvdata(pdev);
17659 struct tg3 *tp = netdev_priv(dev);
17660 int err;
17661
17662 if (!netif_running(dev))
17663 return 0;
17664
Matt Carlsondb219972011-11-04 09:15:03 +000017665 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017666 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017667 tg3_netif_stop(tp);
17668
Matt Carlson21f76382012-02-22 12:35:21 +000017669 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017670
David S. Millerf47c11e2005-06-24 20:18:35 -070017671 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017672 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017673 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017674
17675 netif_device_detach(dev);
17676
David S. Millerf47c11e2005-06-24 20:18:35 -070017677 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017678 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017679 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017680 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017681
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017682 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017683 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017684 int err2;
17685
David S. Millerf47c11e2005-06-24 20:18:35 -070017686 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017687
Joe Perches63c3a662011-04-26 08:12:10 +000017688 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017689 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017690 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017692
Matt Carlson21f76382012-02-22 12:35:21 +000017693 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017694
17695 netif_device_attach(dev);
17696 tg3_netif_start(tp);
17697
Michael Chanb9ec6c12006-07-25 16:37:27 -070017698out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017699 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017700
17701 if (!err2)
17702 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017703 }
17704
17705 return err;
17706}
17707
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017708static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017709{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017710 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017711 struct net_device *dev = pci_get_drvdata(pdev);
17712 struct tg3 *tp = netdev_priv(dev);
17713 int err;
17714
17715 if (!netif_running(dev))
17716 return 0;
17717
Linus Torvalds1da177e2005-04-16 15:20:36 -070017718 netif_device_attach(dev);
17719
David S. Millerf47c11e2005-06-24 20:18:35 -070017720 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017721
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017722 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
17723
Joe Perches63c3a662011-04-26 08:12:10 +000017724 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017725 err = tg3_restart_hw(tp,
17726 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017727 if (err)
17728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017729
Matt Carlson21f76382012-02-22 12:35:21 +000017730 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017731
Linus Torvalds1da177e2005-04-16 15:20:36 -070017732 tg3_netif_start(tp);
17733
Michael Chanb9ec6c12006-07-25 16:37:27 -070017734out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017735 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017736
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017737 if (!err)
17738 tg3_phy_start(tp);
17739
Michael Chanb9ec6c12006-07-25 16:37:27 -070017740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017741}
Fabio Estevam42df36a2013-04-16 09:28:29 +000017742#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017743
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017744static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
17745
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017746/**
17747 * tg3_io_error_detected - called when PCI error is detected
17748 * @pdev: Pointer to PCI device
17749 * @state: The current pci connection state
17750 *
17751 * This function is called after a PCI bus error affecting
17752 * this device has been detected.
17753 */
17754static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17755 pci_channel_state_t state)
17756{
17757 struct net_device *netdev = pci_get_drvdata(pdev);
17758 struct tg3 *tp = netdev_priv(netdev);
17759 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17760
17761 netdev_info(netdev, "PCI I/O error detected\n");
17762
17763 rtnl_lock();
17764
17765 if (!netif_running(netdev))
17766 goto done;
17767
17768 tg3_phy_stop(tp);
17769
17770 tg3_netif_stop(tp);
17771
Matt Carlson21f76382012-02-22 12:35:21 +000017772 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017773
17774 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017775 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017776
17777 netif_device_detach(netdev);
17778
17779 /* Clean up software state, even if MMIO is blocked */
17780 tg3_full_lock(tp, 0);
17781 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17782 tg3_full_unlock(tp);
17783
17784done:
Michael Chan72bb72b2013-06-17 13:47:25 -070017785 if (state == pci_channel_io_perm_failure) {
17786 tg3_napi_enable(tp);
17787 dev_close(netdev);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017788 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070017789 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017790 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070017791 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017792
17793 rtnl_unlock();
17794
17795 return err;
17796}
17797
17798/**
17799 * tg3_io_slot_reset - called after the pci bus has been reset.
17800 * @pdev: Pointer to PCI device
17801 *
17802 * Restart the card from scratch, as if from a cold-boot.
17803 * At this point, the card has exprienced a hard reset,
17804 * followed by fixups by BIOS, and has its config space
17805 * set up identically to what it was at cold boot.
17806 */
17807static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17808{
17809 struct net_device *netdev = pci_get_drvdata(pdev);
17810 struct tg3 *tp = netdev_priv(netdev);
17811 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17812 int err;
17813
17814 rtnl_lock();
17815
17816 if (pci_enable_device(pdev)) {
17817 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17818 goto done;
17819 }
17820
17821 pci_set_master(pdev);
17822 pci_restore_state(pdev);
17823 pci_save_state(pdev);
17824
17825 if (!netif_running(netdev)) {
17826 rc = PCI_ERS_RESULT_RECOVERED;
17827 goto done;
17828 }
17829
17830 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017831 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017832 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017833
17834 rc = PCI_ERS_RESULT_RECOVERED;
17835
17836done:
Michael Chan72bb72b2013-06-17 13:47:25 -070017837 if (rc != PCI_ERS_RESULT_RECOVERED && netif_running(netdev)) {
17838 tg3_napi_enable(tp);
17839 dev_close(netdev);
17840 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017841 rtnl_unlock();
17842
17843 return rc;
17844}
17845
17846/**
17847 * tg3_io_resume - called when traffic can start flowing again.
17848 * @pdev: Pointer to PCI device
17849 *
17850 * This callback is called when the error recovery driver tells
17851 * us that its OK to resume normal operation.
17852 */
17853static void tg3_io_resume(struct pci_dev *pdev)
17854{
17855 struct net_device *netdev = pci_get_drvdata(pdev);
17856 struct tg3 *tp = netdev_priv(netdev);
17857 int err;
17858
17859 rtnl_lock();
17860
17861 if (!netif_running(netdev))
17862 goto done;
17863
17864 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017865 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000017866 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017867 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017868 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017869 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017870 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17871 goto done;
17872 }
17873
17874 netif_device_attach(netdev);
17875
Matt Carlson21f76382012-02-22 12:35:21 +000017876 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017877
17878 tg3_netif_start(tp);
17879
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017880 tg3_full_unlock(tp);
17881
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017882 tg3_phy_start(tp);
17883
17884done:
17885 rtnl_unlock();
17886}
17887
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017888static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017889 .error_detected = tg3_io_error_detected,
17890 .slot_reset = tg3_io_slot_reset,
17891 .resume = tg3_io_resume
17892};
17893
Linus Torvalds1da177e2005-04-16 15:20:36 -070017894static struct pci_driver tg3_driver = {
17895 .name = DRV_MODULE_NAME,
17896 .id_table = tg3_pci_tbl,
17897 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017898 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017899 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000017900 .driver.pm = &tg3_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017901};
17902
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000017903module_pci_driver(tg3_driver);