blob: bb1d34c109f5b64d748709d963edf87a77c43187 [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 Carlson66711e662009-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;
747 udelay(10);
748 }
749
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000750 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700751 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000752 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700753 ret = -EBUSY;
754 }
755
756 return ret;
757}
758
759static void tg3_ape_unlock(struct tg3 *tp, int locknum)
760{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000761 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700762
Joe Perches63c3a662011-04-26 08:12:10 +0000763 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700764 return;
765
766 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000767 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000768 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000769 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000770 case TG3_APE_LOCK_GRC:
771 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000772 if (!tp->pci_fn)
773 bit = APE_LOCK_GRANT_DRIVER;
774 else
775 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000776 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000777 case TG3_APE_LOCK_PHY0:
778 case TG3_APE_LOCK_PHY1:
779 case TG3_APE_LOCK_PHY2:
780 case TG3_APE_LOCK_PHY3:
781 bit = APE_LOCK_GRANT_DRIVER;
782 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000783 default:
784 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700785 }
786
Joe Perches41535772013-02-16 11:20:04 +0000787 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000788 gnt = TG3_APE_LOCK_GRANT;
789 else
790 gnt = TG3_APE_PER_LOCK_GRANT;
791
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000792 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700793}
794
Matt Carlsonb65a3722012-07-16 16:24:00 +0000795static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000796{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000797 u32 apedata;
798
Matt Carlsonb65a3722012-07-16 16:24:00 +0000799 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000800 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000801 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000802
803 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000804 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
805 break;
806
Matt Carlsonb65a3722012-07-16 16:24:00 +0000807 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
808
809 udelay(10);
810 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000811 }
812
Matt Carlsonb65a3722012-07-16 16:24:00 +0000813 return timeout_us ? 0 : -EBUSY;
814}
815
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000816static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
817{
818 u32 i, apedata;
819
820 for (i = 0; i < timeout_us / 10; i++) {
821 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
822
823 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
824 break;
825
826 udelay(10);
827 }
828
829 return i == timeout_us / 10;
830}
831
Michael Chan86449942012-10-02 20:31:14 -0700832static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
833 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000834{
835 int err;
836 u32 i, bufoff, msgoff, maxlen, apedata;
837
838 if (!tg3_flag(tp, APE_HAS_NCSI))
839 return 0;
840
841 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
842 if (apedata != APE_SEG_SIG_MAGIC)
843 return -ENODEV;
844
845 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
846 if (!(apedata & APE_FW_STATUS_READY))
847 return -EAGAIN;
848
849 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
850 TG3_APE_SHMEM_BASE;
851 msgoff = bufoff + 2 * sizeof(u32);
852 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
853
854 while (len) {
855 u32 length;
856
857 /* Cap xfer sizes to scratchpad limits. */
858 length = (len > maxlen) ? maxlen : len;
859 len -= length;
860
861 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
862 if (!(apedata & APE_FW_STATUS_READY))
863 return -EAGAIN;
864
865 /* Wait for up to 1 msec for APE to service previous event. */
866 err = tg3_ape_event_lock(tp, 1000);
867 if (err)
868 return err;
869
870 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
871 APE_EVENT_STATUS_SCRTCHPD_READ |
872 APE_EVENT_STATUS_EVENT_PENDING;
873 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
874
875 tg3_ape_write32(tp, bufoff, base_off);
876 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
877
878 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
879 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
880
881 base_off += length;
882
883 if (tg3_ape_wait_for_event(tp, 30000))
884 return -EAGAIN;
885
886 for (i = 0; length; i += 4, length -= 4) {
887 u32 val = tg3_ape_read32(tp, msgoff + i);
888 memcpy(data, &val, sizeof(u32));
889 data++;
890 }
891 }
892
893 return 0;
894}
895
Matt Carlsonb65a3722012-07-16 16:24:00 +0000896static int tg3_ape_send_event(struct tg3 *tp, u32 event)
897{
898 int err;
899 u32 apedata;
900
901 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
902 if (apedata != APE_SEG_SIG_MAGIC)
903 return -EAGAIN;
904
905 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
906 if (!(apedata & APE_FW_STATUS_READY))
907 return -EAGAIN;
908
909 /* Wait for up to 1 millisecond for APE to service previous event. */
910 err = tg3_ape_event_lock(tp, 1000);
911 if (err)
912 return err;
913
914 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
915 event | APE_EVENT_STATUS_EVENT_PENDING);
916
917 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
918 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
919
920 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000921}
922
923static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
924{
925 u32 event;
926 u32 apedata;
927
928 if (!tg3_flag(tp, ENABLE_APE))
929 return;
930
931 switch (kind) {
932 case RESET_KIND_INIT:
933 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
934 APE_HOST_SEG_SIG_MAGIC);
935 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
936 APE_HOST_SEG_LEN_MAGIC);
937 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
938 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
939 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
940 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
941 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
942 APE_HOST_BEHAV_NO_PHYLOCK);
943 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
944 TG3_APE_HOST_DRVR_STATE_START);
945
946 event = APE_EVENT_STATUS_STATE_START;
947 break;
948 case RESET_KIND_SHUTDOWN:
949 /* With the interface we are currently using,
950 * APE does not track driver state. Wiping
951 * out the HOST SEGMENT SIGNATURE forces
952 * the APE to assume OS absent status.
953 */
954 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
955
956 if (device_may_wakeup(&tp->pdev->dev) &&
957 tg3_flag(tp, WOL_ENABLE)) {
958 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
959 TG3_APE_HOST_WOL_SPEED_AUTO);
960 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
961 } else
962 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
963
964 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
965
966 event = APE_EVENT_STATUS_STATE_UNLOAD;
967 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000968 default:
969 return;
970 }
971
972 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
973
974 tg3_ape_send_event(tp, event);
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static void tg3_disable_ints(struct tg3 *tp)
978{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000979 int i;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 tw32(TG3PCI_MISC_HOST_CTRL,
982 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000983 for (i = 0; i < tp->irq_max; i++)
984 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987static void tg3_enable_ints(struct tg3 *tp)
988{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000989 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000990
Michael Chanbbe832c2005-06-24 20:20:04 -0700991 tp->irq_sync = 0;
992 wmb();
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 tw32(TG3PCI_MISC_HOST_CTRL,
995 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000996
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000997 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000998 for (i = 0; i < tp->irq_cnt; i++) {
999 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001000
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001002 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001003 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1004
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001005 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001006 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001007
1008 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001009 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001010 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1011 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1012 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001013 tw32(HOSTCC_MODE, tp->coal_now);
1014
1015 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Matt Carlson17375d22009-08-28 14:02:18 +00001018static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001019{
Matt Carlson17375d22009-08-28 14:02:18 +00001020 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001021 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001022 unsigned int work_exists = 0;
1023
1024 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001025 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001026 if (sblk->status & SD_STATUS_LINK_CHG)
1027 work_exists = 1;
1028 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001029
1030 /* check for TX work to do */
1031 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1032 work_exists = 1;
1033
1034 /* check for RX work to do */
1035 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001036 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001037 work_exists = 1;
1038
1039 return work_exists;
1040}
1041
Matt Carlson17375d22009-08-28 14:02:18 +00001042/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001043 * similar to tg3_enable_ints, but it accurately determines whether there
1044 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001045 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Matt Carlson17375d22009-08-28 14:02:18 +00001047static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Matt Carlson17375d22009-08-28 14:02:18 +00001049 struct tg3 *tp = tnapi->tp;
1050
Matt Carlson898a56f2009-08-28 14:02:40 +00001051 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 mmiowb();
1053
David S. Millerfac9b832005-05-18 22:46:34 -07001054 /* When doing tagged status, this work check is unnecessary.
1055 * The last_tag we write above tells the chip which piece of
1056 * work we've completed.
1057 */
Joe Perches63c3a662011-04-26 08:12:10 +00001058 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001059 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001060 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063static void tg3_switch_clocks(struct tg3 *tp)
1064{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001065 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 u32 orig_clock_ctrl;
1067
Joe Perches63c3a662011-04-26 08:12:10 +00001068 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001069 return;
1070
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001071 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 orig_clock_ctrl = clock_ctrl;
1074 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1075 CLOCK_CTRL_CLKRUN_OENABLE |
1076 0x1f);
1077 tp->pci_clock_ctrl = clock_ctrl;
1078
Joe Perches63c3a662011-04-26 08:12:10 +00001079 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001081 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1082 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001085 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1086 clock_ctrl |
1087 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1088 40);
1089 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1090 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1091 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001093 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096#define PHY_BUSY_LOOPS 5000
1097
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001098static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1099 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 u32 frame_val;
1102 unsigned int loops;
1103 int ret;
1104
1105 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1106 tw32_f(MAC_MI_MODE,
1107 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1108 udelay(80);
1109 }
1110
Michael Chan8151ad52012-07-29 19:15:41 +00001111 tg3_ape_lock(tp, tp->phy_ape_lock);
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *val = 0x0;
1114
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001115 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 MI_COM_PHY_ADDR_MASK);
1117 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1118 MI_COM_REG_ADDR_MASK);
1119 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 tw32_f(MAC_MI_COM, frame_val);
1122
1123 loops = PHY_BUSY_LOOPS;
1124 while (loops != 0) {
1125 udelay(10);
1126 frame_val = tr32(MAC_MI_COM);
1127
1128 if ((frame_val & MI_COM_BUSY) == 0) {
1129 udelay(5);
1130 frame_val = tr32(MAC_MI_COM);
1131 break;
1132 }
1133 loops -= 1;
1134 }
1135
1136 ret = -EBUSY;
1137 if (loops != 0) {
1138 *val = frame_val & MI_COM_DATA_MASK;
1139 ret = 0;
1140 }
1141
1142 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1143 tw32_f(MAC_MI_MODE, tp->mi_mode);
1144 udelay(80);
1145 }
1146
Michael Chan8151ad52012-07-29 19:15:41 +00001147 tg3_ape_unlock(tp, tp->phy_ape_lock);
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return ret;
1150}
1151
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001152static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1153{
1154 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1155}
1156
1157static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1158 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 u32 frame_val;
1161 unsigned int loops;
1162 int ret;
1163
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001164 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001165 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001166 return 0;
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1169 tw32_f(MAC_MI_MODE,
1170 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1171 udelay(80);
1172 }
1173
Michael Chan8151ad52012-07-29 19:15:41 +00001174 tg3_ape_lock(tp, tp->phy_ape_lock);
1175
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001176 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 MI_COM_PHY_ADDR_MASK);
1178 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1179 MI_COM_REG_ADDR_MASK);
1180 frame_val |= (val & MI_COM_DATA_MASK);
1181 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 tw32_f(MAC_MI_COM, frame_val);
1184
1185 loops = PHY_BUSY_LOOPS;
1186 while (loops != 0) {
1187 udelay(10);
1188 frame_val = tr32(MAC_MI_COM);
1189 if ((frame_val & MI_COM_BUSY) == 0) {
1190 udelay(5);
1191 frame_val = tr32(MAC_MI_COM);
1192 break;
1193 }
1194 loops -= 1;
1195 }
1196
1197 ret = -EBUSY;
1198 if (loops != 0)
1199 ret = 0;
1200
1201 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1202 tw32_f(MAC_MI_MODE, tp->mi_mode);
1203 udelay(80);
1204 }
1205
Michael Chan8151ad52012-07-29 19:15:41 +00001206 tg3_ape_unlock(tp, tp->phy_ape_lock);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return ret;
1209}
1210
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001211static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1212{
1213 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1214}
1215
Matt Carlsonb0988c12011-04-20 07:57:39 +00001216static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1217{
1218 int err;
1219
1220 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1221 if (err)
1222 goto done;
1223
1224 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1225 if (err)
1226 goto done;
1227
1228 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1229 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1230 if (err)
1231 goto done;
1232
1233 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1234
1235done:
1236 return err;
1237}
1238
1239static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1240{
1241 int err;
1242
1243 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1244 if (err)
1245 goto done;
1246
1247 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1248 if (err)
1249 goto done;
1250
1251 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1252 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1253 if (err)
1254 goto done;
1255
1256 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1257
1258done:
1259 return err;
1260}
1261
1262static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1263{
1264 int err;
1265
1266 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1267 if (!err)
1268 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1269
1270 return err;
1271}
1272
1273static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1274{
1275 int err;
1276
1277 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1278 if (!err)
1279 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1280
1281 return err;
1282}
1283
Matt Carlson15ee95c2011-04-20 07:57:40 +00001284static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1285{
1286 int err;
1287
1288 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1289 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1290 MII_TG3_AUXCTL_SHDWSEL_MISC);
1291 if (!err)
1292 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1293
1294 return err;
1295}
1296
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001297static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1298{
1299 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1300 set |= MII_TG3_AUXCTL_MISC_WREN;
1301
1302 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1303}
1304
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001305static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1306{
1307 u32 val;
1308 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001309
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001310 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1311
1312 if (err)
1313 return err;
1314 if (enable)
1315
1316 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1317 else
1318 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1319
1320 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1321 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1322
1323 return err;
1324}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001325
Matt Carlson95e28692008-05-25 23:44:14 -07001326static int tg3_bmcr_reset(struct tg3 *tp)
1327{
1328 u32 phy_control;
1329 int limit, err;
1330
1331 /* OK, reset it, and poll the BMCR_RESET bit until it
1332 * clears or we time out.
1333 */
1334 phy_control = BMCR_RESET;
1335 err = tg3_writephy(tp, MII_BMCR, phy_control);
1336 if (err != 0)
1337 return -EBUSY;
1338
1339 limit = 5000;
1340 while (limit--) {
1341 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1342 if (err != 0)
1343 return -EBUSY;
1344
1345 if ((phy_control & BMCR_RESET) == 0) {
1346 udelay(40);
1347 break;
1348 }
1349 udelay(10);
1350 }
Roel Kluind4675b52009-02-12 16:33:27 -08001351 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001352 return -EBUSY;
1353
1354 return 0;
1355}
1356
Matt Carlson158d7ab2008-05-29 01:37:54 -07001357static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1358{
Francois Romieu3d165432009-01-19 16:56:50 -08001359 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001360 u32 val;
1361
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001362 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001363
1364 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001365 val = -EIO;
1366
1367 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001368
1369 return val;
1370}
1371
1372static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1373{
Francois Romieu3d165432009-01-19 16:56:50 -08001374 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001375 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001376
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001377 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001378
1379 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001380 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001381
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001382 spin_unlock_bh(&tp->lock);
1383
1384 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001385}
1386
1387static int tg3_mdio_reset(struct mii_bus *bp)
1388{
1389 return 0;
1390}
1391
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001392static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001393{
1394 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001395 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001396
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001397 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001398 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001399 case PHY_ID_BCM50610:
1400 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001401 val = MAC_PHYCFG2_50610_LED_MODES;
1402 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001403 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001404 val = MAC_PHYCFG2_AC131_LED_MODES;
1405 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001406 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001407 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1408 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001409 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001410 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1411 break;
1412 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001413 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001414 }
1415
1416 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1417 tw32(MAC_PHYCFG2, val);
1418
1419 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001420 val &= ~(MAC_PHYCFG1_RGMII_INT |
1421 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1422 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001423 tw32(MAC_PHYCFG1, val);
1424
1425 return;
1426 }
1427
Joe Perches63c3a662011-04-26 08:12:10 +00001428 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001429 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1430 MAC_PHYCFG2_FMODE_MASK_MASK |
1431 MAC_PHYCFG2_GMODE_MASK_MASK |
1432 MAC_PHYCFG2_ACT_MASK_MASK |
1433 MAC_PHYCFG2_QUAL_MASK_MASK |
1434 MAC_PHYCFG2_INBAND_ENABLE;
1435
1436 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001437
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001438 val = tr32(MAC_PHYCFG1);
1439 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1440 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001441 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1442 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001443 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001444 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001445 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1446 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001447 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1448 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1449 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001450
Matt Carlsona9daf362008-05-25 23:49:44 -07001451 val = tr32(MAC_EXT_RGMII_MODE);
1452 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1453 MAC_RGMII_MODE_RX_QUALITY |
1454 MAC_RGMII_MODE_RX_ACTIVITY |
1455 MAC_RGMII_MODE_RX_ENG_DET |
1456 MAC_RGMII_MODE_TX_ENABLE |
1457 MAC_RGMII_MODE_TX_LOWPWR |
1458 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001459 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1460 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001461 val |= MAC_RGMII_MODE_RX_INT_B |
1462 MAC_RGMII_MODE_RX_QUALITY |
1463 MAC_RGMII_MODE_RX_ACTIVITY |
1464 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001465 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001466 val |= MAC_RGMII_MODE_TX_ENABLE |
1467 MAC_RGMII_MODE_TX_LOWPWR |
1468 MAC_RGMII_MODE_TX_RESET;
1469 }
1470 tw32(MAC_EXT_RGMII_MODE, val);
1471}
1472
Matt Carlson158d7ab2008-05-29 01:37:54 -07001473static void tg3_mdio_start(struct tg3 *tp)
1474{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001475 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1476 tw32_f(MAC_MI_MODE, tp->mi_mode);
1477 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001478
Joe Perches63c3a662011-04-26 08:12:10 +00001479 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001480 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001481 tg3_mdio_config_5785(tp);
1482}
1483
1484static int tg3_mdio_init(struct tg3 *tp)
1485{
1486 int i;
1487 u32 reg;
1488 struct phy_device *phydev;
1489
Joe Perches63c3a662011-04-26 08:12:10 +00001490 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001491 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001492
Matt Carlson69f11c92011-07-13 09:27:30 +00001493 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001494
Joe Perches41535772013-02-16 11:20:04 +00001495 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001496 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1497 else
1498 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1499 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001500 if (is_serdes)
1501 tp->phy_addr += 7;
1502 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001503 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001504
Matt Carlson158d7ab2008-05-29 01:37:54 -07001505 tg3_mdio_start(tp);
1506
Joe Perches63c3a662011-04-26 08:12:10 +00001507 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001508 return 0;
1509
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001510 tp->mdio_bus = mdiobus_alloc();
1511 if (tp->mdio_bus == NULL)
1512 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001513
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001514 tp->mdio_bus->name = "tg3 mdio bus";
1515 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001516 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001517 tp->mdio_bus->priv = tp;
1518 tp->mdio_bus->parent = &tp->pdev->dev;
1519 tp->mdio_bus->read = &tg3_mdio_read;
1520 tp->mdio_bus->write = &tg3_mdio_write;
1521 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001522 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001523 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001524
1525 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001526 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001527
1528 /* The bus registration will look for all the PHYs on the mdio bus.
1529 * Unfortunately, it does not ensure the PHY is powered up before
1530 * accessing the PHY ID registers. A chip reset is the
1531 * quickest way to bring the device back to an operational state..
1532 */
1533 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1534 tg3_bmcr_reset(tp);
1535
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001536 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001537 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001538 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001539 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001540 return i;
1541 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001542
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001543 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001544
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001545 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001546 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001547 mdiobus_unregister(tp->mdio_bus);
1548 mdiobus_free(tp->mdio_bus);
1549 return -ENODEV;
1550 }
1551
1552 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001553 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001554 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001555 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001556 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001557 case PHY_ID_BCM50610:
1558 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001559 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001560 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001561 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001562 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001563 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001564 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001565 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001566 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001567 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001568 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001569 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001570 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001571 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001572 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001573 case PHY_ID_RTL8201E:
1574 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001575 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001576 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001577 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001578 break;
1579 }
1580
Joe Perches63c3a662011-04-26 08:12:10 +00001581 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001582
Joe Perches41535772013-02-16 11:20:04 +00001583 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001584 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001585
1586 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001587}
1588
1589static void tg3_mdio_fini(struct tg3 *tp)
1590{
Joe Perches63c3a662011-04-26 08:12:10 +00001591 if (tg3_flag(tp, MDIOBUS_INITED)) {
1592 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001593 mdiobus_unregister(tp->mdio_bus);
1594 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001595 }
1596}
1597
Matt Carlson95e28692008-05-25 23:44:14 -07001598/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001599static inline void tg3_generate_fw_event(struct tg3 *tp)
1600{
1601 u32 val;
1602
1603 val = tr32(GRC_RX_CPU_EVENT);
1604 val |= GRC_RX_CPU_DRIVER_EVENT;
1605 tw32_f(GRC_RX_CPU_EVENT, val);
1606
1607 tp->last_event_jiffies = jiffies;
1608}
1609
1610#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1611
1612/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001613static void tg3_wait_for_event_ack(struct tg3 *tp)
1614{
1615 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001616 unsigned int delay_cnt;
1617 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001618
Matt Carlson4ba526c2008-08-15 14:10:04 -07001619 /* If enough time has passed, no wait is necessary. */
1620 time_remain = (long)(tp->last_event_jiffies + 1 +
1621 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1622 (long)jiffies;
1623 if (time_remain < 0)
1624 return;
1625
1626 /* Check if we can shorten the wait time. */
1627 delay_cnt = jiffies_to_usecs(time_remain);
1628 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1629 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1630 delay_cnt = (delay_cnt >> 3) + 1;
1631
1632 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001633 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1634 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001635 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001636 }
1637}
1638
1639/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001640static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001641{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001642 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001643
1644 val = 0;
1645 if (!tg3_readphy(tp, MII_BMCR, &reg))
1646 val = reg << 16;
1647 if (!tg3_readphy(tp, MII_BMSR, &reg))
1648 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001649 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001650
1651 val = 0;
1652 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1653 val = reg << 16;
1654 if (!tg3_readphy(tp, MII_LPA, &reg))
1655 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001656 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001657
1658 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001659 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001660 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1661 val = reg << 16;
1662 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1663 val |= (reg & 0xffff);
1664 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001665 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001666
1667 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1668 val = reg << 16;
1669 else
1670 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001671 *data++ = val;
1672}
1673
1674/* tp->lock is held. */
1675static void tg3_ump_link_report(struct tg3 *tp)
1676{
1677 u32 data[4];
1678
1679 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1680 return;
1681
1682 tg3_phy_gather_ump_data(tp, data);
1683
1684 tg3_wait_for_event_ack(tp);
1685
1686 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1687 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1688 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1689 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1690 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1691 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001692
Matt Carlson4ba526c2008-08-15 14:10:04 -07001693 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001694}
1695
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001696/* tp->lock is held. */
1697static void tg3_stop_fw(struct tg3 *tp)
1698{
1699 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1700 /* Wait for RX cpu to ACK the previous event. */
1701 tg3_wait_for_event_ack(tp);
1702
1703 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1704
1705 tg3_generate_fw_event(tp);
1706
1707 /* Wait for RX cpu to ACK this event. */
1708 tg3_wait_for_event_ack(tp);
1709 }
1710}
1711
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001712/* tp->lock is held. */
1713static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1714{
1715 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1716 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1717
1718 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1719 switch (kind) {
1720 case RESET_KIND_INIT:
1721 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1722 DRV_STATE_START);
1723 break;
1724
1725 case RESET_KIND_SHUTDOWN:
1726 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1727 DRV_STATE_UNLOAD);
1728 break;
1729
1730 case RESET_KIND_SUSPEND:
1731 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1732 DRV_STATE_SUSPEND);
1733 break;
1734
1735 default:
1736 break;
1737 }
1738 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001739}
1740
1741/* tp->lock is held. */
1742static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1743{
1744 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1745 switch (kind) {
1746 case RESET_KIND_INIT:
1747 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1748 DRV_STATE_START_DONE);
1749 break;
1750
1751 case RESET_KIND_SHUTDOWN:
1752 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1753 DRV_STATE_UNLOAD_DONE);
1754 break;
1755
1756 default:
1757 break;
1758 }
1759 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001760}
1761
1762/* tp->lock is held. */
1763static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1764{
1765 if (tg3_flag(tp, ENABLE_ASF)) {
1766 switch (kind) {
1767 case RESET_KIND_INIT:
1768 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1769 DRV_STATE_START);
1770 break;
1771
1772 case RESET_KIND_SHUTDOWN:
1773 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1774 DRV_STATE_UNLOAD);
1775 break;
1776
1777 case RESET_KIND_SUSPEND:
1778 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1779 DRV_STATE_SUSPEND);
1780 break;
1781
1782 default:
1783 break;
1784 }
1785 }
1786}
1787
1788static int tg3_poll_fw(struct tg3 *tp)
1789{
1790 int i;
1791 u32 val;
1792
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001793 if (tg3_flag(tp, IS_SSB_CORE)) {
1794 /* We don't use firmware. */
1795 return 0;
1796 }
1797
Joe Perches41535772013-02-16 11:20:04 +00001798 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001799 /* Wait up to 20ms for init done. */
1800 for (i = 0; i < 200; i++) {
1801 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1802 return 0;
1803 udelay(100);
1804 }
1805 return -ENODEV;
1806 }
1807
1808 /* Wait for firmware initialization to complete. */
1809 for (i = 0; i < 100000; i++) {
1810 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1811 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1812 break;
1813 udelay(10);
1814 }
1815
1816 /* Chip might not be fitted with firmware. Some Sun onboard
1817 * parts are configured like that. So don't signal the timeout
1818 * of the above loop as an error, but do report the lack of
1819 * running firmware once.
1820 */
1821 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1822 tg3_flag_set(tp, NO_FWARE_REPORTED);
1823
1824 netdev_info(tp->dev, "No firmware running\n");
1825 }
1826
Joe Perches41535772013-02-16 11:20:04 +00001827 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001828 /* The 57765 A0 needs a little more
1829 * time to do some important work.
1830 */
1831 mdelay(10);
1832 }
1833
1834 return 0;
1835}
1836
Matt Carlson95e28692008-05-25 23:44:14 -07001837static void tg3_link_report(struct tg3 *tp)
1838{
1839 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001840 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001841 tg3_ump_link_report(tp);
1842 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001843 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1844 (tp->link_config.active_speed == SPEED_1000 ?
1845 1000 :
1846 (tp->link_config.active_speed == SPEED_100 ?
1847 100 : 10)),
1848 (tp->link_config.active_duplex == DUPLEX_FULL ?
1849 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001850
Joe Perches05dbe002010-02-17 19:44:19 +00001851 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1852 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1853 "on" : "off",
1854 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1855 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001856
1857 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1858 netdev_info(tp->dev, "EEE is %s\n",
1859 tp->setlpicnt ? "enabled" : "disabled");
1860
Matt Carlson95e28692008-05-25 23:44:14 -07001861 tg3_ump_link_report(tp);
1862 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001863
1864 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001865}
1866
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001867static u32 tg3_decode_flowctrl_1000T(u32 adv)
1868{
1869 u32 flowctrl = 0;
1870
1871 if (adv & ADVERTISE_PAUSE_CAP) {
1872 flowctrl |= FLOW_CTRL_RX;
1873 if (!(adv & ADVERTISE_PAUSE_ASYM))
1874 flowctrl |= FLOW_CTRL_TX;
1875 } else if (adv & ADVERTISE_PAUSE_ASYM)
1876 flowctrl |= FLOW_CTRL_TX;
1877
1878 return flowctrl;
1879}
1880
Matt Carlson95e28692008-05-25 23:44:14 -07001881static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1882{
1883 u16 miireg;
1884
Steve Glendinninge18ce342008-12-16 02:00:00 -08001885 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001886 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001887 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001888 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001889 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001890 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1891 else
1892 miireg = 0;
1893
1894 return miireg;
1895}
1896
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001897static u32 tg3_decode_flowctrl_1000X(u32 adv)
1898{
1899 u32 flowctrl = 0;
1900
1901 if (adv & ADVERTISE_1000XPAUSE) {
1902 flowctrl |= FLOW_CTRL_RX;
1903 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1904 flowctrl |= FLOW_CTRL_TX;
1905 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1906 flowctrl |= FLOW_CTRL_TX;
1907
1908 return flowctrl;
1909}
1910
Matt Carlson95e28692008-05-25 23:44:14 -07001911static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1912{
1913 u8 cap = 0;
1914
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001915 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1916 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1917 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1918 if (lcladv & ADVERTISE_1000XPAUSE)
1919 cap = FLOW_CTRL_RX;
1920 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001921 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001922 }
1923
1924 return cap;
1925}
1926
Matt Carlsonf51f3562008-05-25 23:45:08 -07001927static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001928{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001929 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001930 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001931 u32 old_rx_mode = tp->rx_mode;
1932 u32 old_tx_mode = tp->tx_mode;
1933
Joe Perches63c3a662011-04-26 08:12:10 +00001934 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001935 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001936 else
1937 autoneg = tp->link_config.autoneg;
1938
Joe Perches63c3a662011-04-26 08:12:10 +00001939 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001940 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001941 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001942 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001943 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001944 } else
1945 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001946
Matt Carlsonf51f3562008-05-25 23:45:08 -07001947 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001948
Steve Glendinninge18ce342008-12-16 02:00:00 -08001949 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001950 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1951 else
1952 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1953
Matt Carlsonf51f3562008-05-25 23:45:08 -07001954 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001955 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001956
Steve Glendinninge18ce342008-12-16 02:00:00 -08001957 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001958 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1959 else
1960 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1961
Matt Carlsonf51f3562008-05-25 23:45:08 -07001962 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001963 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001964}
1965
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001966static void tg3_adjust_link(struct net_device *dev)
1967{
1968 u8 oldflowctrl, linkmesg = 0;
1969 u32 mac_mode, lcl_adv, rmt_adv;
1970 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001971 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001972
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001973 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001974
1975 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1976 MAC_MODE_HALF_DUPLEX);
1977
1978 oldflowctrl = tp->link_config.active_flowctrl;
1979
1980 if (phydev->link) {
1981 lcl_adv = 0;
1982 rmt_adv = 0;
1983
1984 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1985 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001986 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00001987 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001988 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001989 else
1990 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001991
1992 if (phydev->duplex == DUPLEX_HALF)
1993 mac_mode |= MAC_MODE_HALF_DUPLEX;
1994 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001995 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001996 tp->link_config.flowctrl);
1997
1998 if (phydev->pause)
1999 rmt_adv = LPA_PAUSE_CAP;
2000 if (phydev->asym_pause)
2001 rmt_adv |= LPA_PAUSE_ASYM;
2002 }
2003
2004 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2005 } else
2006 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2007
2008 if (mac_mode != tp->mac_mode) {
2009 tp->mac_mode = mac_mode;
2010 tw32_f(MAC_MODE, tp->mac_mode);
2011 udelay(40);
2012 }
2013
Joe Perches41535772013-02-16 11:20:04 +00002014 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002015 if (phydev->speed == SPEED_10)
2016 tw32(MAC_MI_STAT,
2017 MAC_MI_STAT_10MBPS_MODE |
2018 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2019 else
2020 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2021 }
2022
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002023 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2024 tw32(MAC_TX_LENGTHS,
2025 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2026 (6 << TX_LENGTHS_IPG_SHIFT) |
2027 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2028 else
2029 tw32(MAC_TX_LENGTHS,
2030 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2031 (6 << TX_LENGTHS_IPG_SHIFT) |
2032 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2033
Matt Carlson34655ad2012-02-22 12:35:18 +00002034 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002035 phydev->speed != tp->link_config.active_speed ||
2036 phydev->duplex != tp->link_config.active_duplex ||
2037 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002038 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002039
Matt Carlson34655ad2012-02-22 12:35:18 +00002040 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002041 tp->link_config.active_speed = phydev->speed;
2042 tp->link_config.active_duplex = phydev->duplex;
2043
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002044 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002045
2046 if (linkmesg)
2047 tg3_link_report(tp);
2048}
2049
2050static int tg3_phy_init(struct tg3 *tp)
2051{
2052 struct phy_device *phydev;
2053
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002054 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002055 return 0;
2056
2057 /* Bring the PHY back to a known state. */
2058 tg3_bmcr_reset(tp);
2059
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002060 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002061
2062 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002063 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2064 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002065 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002066 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002067 return PTR_ERR(phydev);
2068 }
2069
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002070 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002071 switch (phydev->interface) {
2072 case PHY_INTERFACE_MODE_GMII:
2073 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002074 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002075 phydev->supported &= (PHY_GBIT_FEATURES |
2076 SUPPORTED_Pause |
2077 SUPPORTED_Asym_Pause);
2078 break;
2079 }
2080 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002081 case PHY_INTERFACE_MODE_MII:
2082 phydev->supported &= (PHY_BASIC_FEATURES |
2083 SUPPORTED_Pause |
2084 SUPPORTED_Asym_Pause);
2085 break;
2086 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002087 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002088 return -EINVAL;
2089 }
2090
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002091 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002092
2093 phydev->advertising = phydev->supported;
2094
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002095 return 0;
2096}
2097
2098static void tg3_phy_start(struct tg3 *tp)
2099{
2100 struct phy_device *phydev;
2101
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002102 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002103 return;
2104
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002105 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002106
Matt Carlson80096062010-08-02 11:26:06 +00002107 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2108 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002109 phydev->speed = tp->link_config.speed;
2110 phydev->duplex = tp->link_config.duplex;
2111 phydev->autoneg = tp->link_config.autoneg;
2112 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002113 }
2114
2115 phy_start(phydev);
2116
2117 phy_start_aneg(phydev);
2118}
2119
2120static void tg3_phy_stop(struct tg3 *tp)
2121{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002122 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002123 return;
2124
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002125 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002126}
2127
2128static void tg3_phy_fini(struct tg3 *tp)
2129{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002130 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002131 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002132 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002133 }
2134}
2135
Matt Carlson941ec902011-08-19 13:58:23 +00002136static int tg3_phy_set_extloopbk(struct tg3 *tp)
2137{
2138 int err;
2139 u32 val;
2140
2141 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2142 return 0;
2143
2144 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2145 /* Cannot do read-modify-write on 5401 */
2146 err = tg3_phy_auxctl_write(tp,
2147 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2148 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2149 0x4c20);
2150 goto done;
2151 }
2152
2153 err = tg3_phy_auxctl_read(tp,
2154 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2155 if (err)
2156 return err;
2157
2158 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2159 err = tg3_phy_auxctl_write(tp,
2160 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2161
2162done:
2163 return err;
2164}
2165
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002166static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2167{
2168 u32 phytest;
2169
2170 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2171 u32 phy;
2172
2173 tg3_writephy(tp, MII_TG3_FET_TEST,
2174 phytest | MII_TG3_FET_SHADOW_EN);
2175 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2176 if (enable)
2177 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2178 else
2179 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2180 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2181 }
2182 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2183 }
2184}
2185
Matt Carlson6833c042008-11-21 17:18:59 -08002186static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2187{
2188 u32 reg;
2189
Joe Perches63c3a662011-04-26 08:12:10 +00002190 if (!tg3_flag(tp, 5705_PLUS) ||
2191 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002192 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002193 return;
2194
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002195 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002196 tg3_phy_fet_toggle_apd(tp, enable);
2197 return;
2198 }
2199
Matt Carlson6833c042008-11-21 17:18:59 -08002200 reg = MII_TG3_MISC_SHDW_WREN |
2201 MII_TG3_MISC_SHDW_SCR5_SEL |
2202 MII_TG3_MISC_SHDW_SCR5_LPED |
2203 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2204 MII_TG3_MISC_SHDW_SCR5_SDTL |
2205 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002206 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002207 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2208
2209 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2210
2211
2212 reg = MII_TG3_MISC_SHDW_WREN |
2213 MII_TG3_MISC_SHDW_APD_SEL |
2214 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2215 if (enable)
2216 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2217
2218 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2219}
2220
Joe Perches953c96e2013-04-09 10:18:14 +00002221static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002222{
2223 u32 phy;
2224
Joe Perches63c3a662011-04-26 08:12:10 +00002225 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002226 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002227 return;
2228
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002229 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002230 u32 ephy;
2231
Matt Carlson535ef6e2009-08-25 10:09:36 +00002232 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2233 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2234
2235 tg3_writephy(tp, MII_TG3_FET_TEST,
2236 ephy | MII_TG3_FET_SHADOW_EN);
2237 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002238 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002239 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002240 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002241 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2242 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002243 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002244 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002245 }
2246 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002247 int ret;
2248
2249 ret = tg3_phy_auxctl_read(tp,
2250 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2251 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002252 if (enable)
2253 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2254 else
2255 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002256 tg3_phy_auxctl_write(tp,
2257 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002258 }
2259 }
2260}
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262static void tg3_phy_set_wirespeed(struct tg3 *tp)
2263{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002264 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 u32 val;
2266
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002267 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return;
2269
Matt Carlson15ee95c2011-04-20 07:57:40 +00002270 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2271 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002272 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2273 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002276static void tg3_phy_apply_otp(struct tg3 *tp)
2277{
2278 u32 otp, phy;
2279
2280 if (!tp->phy_otp)
2281 return;
2282
2283 otp = tp->phy_otp;
2284
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002285 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002286 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002287
2288 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2289 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2290 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2291
2292 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2293 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2294 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2295
2296 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2297 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2298 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2299
2300 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2301 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2302
2303 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2304 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2305
2306 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2307 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2308 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2309
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002310 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002311}
2312
Nithin Sujir400dfba2013-05-18 06:26:53 +00002313static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2314{
2315 u32 val;
2316 struct ethtool_eee *dest = &tp->eee;
2317
2318 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2319 return;
2320
2321 if (eee)
2322 dest = eee;
2323
2324 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2325 return;
2326
2327 /* Pull eee_active */
2328 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2329 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2330 dest->eee_active = 1;
2331 } else
2332 dest->eee_active = 0;
2333
2334 /* Pull lp advertised settings */
2335 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2336 return;
2337 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2338
2339 /* Pull advertised and eee_enabled settings */
2340 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2341 return;
2342 dest->eee_enabled = !!val;
2343 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2344
2345 /* Pull tx_lpi_enabled */
2346 val = tr32(TG3_CPMU_EEE_MODE);
2347 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2348
2349 /* Pull lpi timer value */
2350 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2351}
2352
Joe Perches953c96e2013-04-09 10:18:14 +00002353static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002354{
2355 u32 val;
2356
2357 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2358 return;
2359
2360 tp->setlpicnt = 0;
2361
2362 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002363 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002364 tp->link_config.active_duplex == DUPLEX_FULL &&
2365 (tp->link_config.active_speed == SPEED_100 ||
2366 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002367 u32 eeectl;
2368
2369 if (tp->link_config.active_speed == SPEED_1000)
2370 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2371 else
2372 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2373
2374 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2375
Nithin Sujir400dfba2013-05-18 06:26:53 +00002376 tg3_eee_pull_config(tp, NULL);
2377 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002378 tp->setlpicnt = 2;
2379 }
2380
2381 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002382 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002383 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002384 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002385 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002386 }
2387
Matt Carlson52b02d02010-10-14 10:37:41 +00002388 val = tr32(TG3_CPMU_EEE_MODE);
2389 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2390 }
2391}
2392
Matt Carlsonb0c59432011-05-19 12:12:48 +00002393static void tg3_phy_eee_enable(struct tg3 *tp)
2394{
2395 u32 val;
2396
2397 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002398 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2399 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002400 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002401 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002402 val = MII_TG3_DSP_TAP26_ALNOKO |
2403 MII_TG3_DSP_TAP26_RMRXSTO;
2404 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002405 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002406 }
2407
2408 val = tr32(TG3_CPMU_EEE_MODE);
2409 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2410}
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412static int tg3_wait_macro_done(struct tg3 *tp)
2413{
2414 int limit = 100;
2415
2416 while (limit--) {
2417 u32 tmp32;
2418
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002419 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if ((tmp32 & 0x1000) == 0)
2421 break;
2422 }
2423 }
Roel Kluind4675b52009-02-12 16:33:27 -08002424 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return -EBUSY;
2426
2427 return 0;
2428}
2429
2430static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2431{
2432 static const u32 test_pat[4][6] = {
2433 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2434 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2435 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2436 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2437 };
2438 int chan;
2439
2440 for (chan = 0; chan < 4; chan++) {
2441 int i;
2442
2443 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2444 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002445 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 for (i = 0; i < 6; i++)
2448 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2449 test_pat[chan][i]);
2450
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002451 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (tg3_wait_macro_done(tp)) {
2453 *resetp = 1;
2454 return -EBUSY;
2455 }
2456
2457 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2458 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002459 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (tg3_wait_macro_done(tp)) {
2461 *resetp = 1;
2462 return -EBUSY;
2463 }
2464
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002465 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (tg3_wait_macro_done(tp)) {
2467 *resetp = 1;
2468 return -EBUSY;
2469 }
2470
2471 for (i = 0; i < 6; i += 2) {
2472 u32 low, high;
2473
2474 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2475 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2476 tg3_wait_macro_done(tp)) {
2477 *resetp = 1;
2478 return -EBUSY;
2479 }
2480 low &= 0x7fff;
2481 high &= 0x000f;
2482 if (low != test_pat[chan][i] ||
2483 high != test_pat[chan][i+1]) {
2484 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2485 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2486 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2487
2488 return -EBUSY;
2489 }
2490 }
2491 }
2492
2493 return 0;
2494}
2495
2496static int tg3_phy_reset_chanpat(struct tg3 *tp)
2497{
2498 int chan;
2499
2500 for (chan = 0; chan < 4; chan++) {
2501 int i;
2502
2503 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2504 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002505 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 for (i = 0; i < 6; i++)
2507 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002508 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (tg3_wait_macro_done(tp))
2510 return -EBUSY;
2511 }
2512
2513 return 0;
2514}
2515
2516static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2517{
2518 u32 reg32, phy9_orig;
2519 int retries, do_phy_reset, err;
2520
2521 retries = 10;
2522 do_phy_reset = 1;
2523 do {
2524 if (do_phy_reset) {
2525 err = tg3_bmcr_reset(tp);
2526 if (err)
2527 return err;
2528 do_phy_reset = 0;
2529 }
2530
2531 /* Disable transmitter and interrupt. */
2532 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2533 continue;
2534
2535 reg32 |= 0x3000;
2536 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2537
2538 /* Set full-duplex, 1000 mbps. */
2539 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002540 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002543 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 continue;
2545
Matt Carlson221c5632011-06-13 13:39:01 +00002546 tg3_writephy(tp, MII_CTRL1000,
2547 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002549 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002550 if (err)
2551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002554 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2557 if (!err)
2558 break;
2559 } while (--retries);
2560
2561 err = tg3_phy_reset_chanpat(tp);
2562 if (err)
2563 return err;
2564
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002565 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002568 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002570 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Matt Carlson221c5632011-06-13 13:39:01 +00002572 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2575 reg32 &= ~0x3000;
2576 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2577 } else if (!err)
2578 err = -EBUSY;
2579
2580 return err;
2581}
2582
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002583static void tg3_carrier_off(struct tg3 *tp)
2584{
2585 netif_carrier_off(tp->dev);
2586 tp->link_up = false;
2587}
2588
Nithin Sujirce20f162013-04-09 08:48:04 +00002589static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2590{
2591 if (tg3_flag(tp, ENABLE_ASF))
2592 netdev_warn(tp->dev,
2593 "Management side-band traffic will be interrupted during phy settings change\n");
2594}
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596/* This will reset the tigon3 PHY if there is no valid
2597 * link unless the FORCE argument is non-zero.
2598 */
2599static int tg3_phy_reset(struct tg3 *tp)
2600{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002601 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 int err;
2603
Joe Perches41535772013-02-16 11:20:04 +00002604 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002605 val = tr32(GRC_MISC_CFG);
2606 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2607 udelay(40);
2608 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002609 err = tg3_readphy(tp, MII_BMSR, &val);
2610 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (err != 0)
2612 return -EBUSY;
2613
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002614 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002615 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002616 tg3_link_report(tp);
2617 }
2618
Joe Perches41535772013-02-16 11:20:04 +00002619 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2620 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2621 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 err = tg3_phy_reset_5703_4_5(tp);
2623 if (err)
2624 return err;
2625 goto out;
2626 }
2627
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002628 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002629 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2630 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002631 cpmuctrl = tr32(TG3_CPMU_CTRL);
2632 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2633 tw32(TG3_CPMU_CTRL,
2634 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2635 }
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 err = tg3_bmcr_reset(tp);
2638 if (err)
2639 return err;
2640
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002641 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002642 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2643 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002644
2645 tw32(TG3_CPMU_CTRL, cpmuctrl);
2646 }
2647
Joe Perches41535772013-02-16 11:20:04 +00002648 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2649 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002650 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2651 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2652 CPMU_LSPD_1000MB_MACCLK_12_5) {
2653 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2654 udelay(40);
2655 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2656 }
2657 }
2658
Joe Perches63c3a662011-04-26 08:12:10 +00002659 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002660 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002661 return 0;
2662
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002663 tg3_phy_apply_otp(tp);
2664
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002665 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002666 tg3_phy_toggle_apd(tp, true);
2667 else
2668 tg3_phy_toggle_apd(tp, false);
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002671 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002672 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002673 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2674 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002675 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002677
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002678 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002679 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2680 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002682
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002683 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002684 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002685 tg3_phydsp_write(tp, 0x000a, 0x310b);
2686 tg3_phydsp_write(tp, 0x201f, 0x9506);
2687 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002688 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002689 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002690 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002691 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002692 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2693 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2694 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2695 tg3_writephy(tp, MII_TG3_TEST1,
2696 MII_TG3_TEST1_TRIM_EN | 0x4);
2697 } else
2698 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2699
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002700 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002701 }
Michael Chanc424cb22006-04-29 18:56:34 -07002702 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* Set Extended packet length bit (bit 14) on all chips that */
2705 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002706 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002708 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002709 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002711 err = tg3_phy_auxctl_read(tp,
2712 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2713 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002714 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2715 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
2717
2718 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2719 * jumbo frames transmission.
2720 */
Joe Perches63c3a662011-04-26 08:12:10 +00002721 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002722 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002723 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002724 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726
Joe Perches41535772013-02-16 11:20:04 +00002727 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002728 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002729 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002730 }
2731
Joe Perches41535772013-02-16 11:20:04 +00002732 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002733 tg3_phydsp_write(tp, 0xffb, 0x4000);
2734
Joe Perches953c96e2013-04-09 10:18:14 +00002735 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 tg3_phy_set_wirespeed(tp);
2737 return 0;
2738}
2739
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002740#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2741#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2742#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2743 TG3_GPIO_MSG_NEED_VAUX)
2744#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2745 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2746 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2747 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2748 (TG3_GPIO_MSG_DRVR_PRES << 12))
2749
2750#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2751 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2752 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2753 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2754 (TG3_GPIO_MSG_NEED_VAUX << 12))
2755
2756static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2757{
2758 u32 status, shift;
2759
Joe Perches41535772013-02-16 11:20:04 +00002760 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2761 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002762 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2763 else
2764 status = tr32(TG3_CPMU_DRV_STATUS);
2765
2766 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2767 status &= ~(TG3_GPIO_MSG_MASK << shift);
2768 status |= (newstat << shift);
2769
Joe Perches41535772013-02-16 11:20:04 +00002770 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2771 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002772 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2773 else
2774 tw32(TG3_CPMU_DRV_STATUS, status);
2775
2776 return status >> TG3_APE_GPIO_MSG_SHIFT;
2777}
2778
Matt Carlson520b2752011-06-13 13:39:02 +00002779static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2780{
2781 if (!tg3_flag(tp, IS_NIC))
2782 return 0;
2783
Joe Perches41535772013-02-16 11:20:04 +00002784 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2785 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2786 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002787 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2788 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002789
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002790 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2791
2792 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2793 TG3_GRC_LCLCTL_PWRSW_DELAY);
2794
2795 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2796 } else {
2797 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2798 TG3_GRC_LCLCTL_PWRSW_DELAY);
2799 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002800
Matt Carlson520b2752011-06-13 13:39:02 +00002801 return 0;
2802}
2803
2804static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2805{
2806 u32 grc_local_ctrl;
2807
2808 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002809 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2810 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002811 return;
2812
2813 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2814
2815 tw32_wait_f(GRC_LOCAL_CTRL,
2816 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2817 TG3_GRC_LCLCTL_PWRSW_DELAY);
2818
2819 tw32_wait_f(GRC_LOCAL_CTRL,
2820 grc_local_ctrl,
2821 TG3_GRC_LCLCTL_PWRSW_DELAY);
2822
2823 tw32_wait_f(GRC_LOCAL_CTRL,
2824 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2825 TG3_GRC_LCLCTL_PWRSW_DELAY);
2826}
2827
2828static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2829{
2830 if (!tg3_flag(tp, IS_NIC))
2831 return;
2832
Joe Perches41535772013-02-16 11:20:04 +00002833 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2834 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002835 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2836 (GRC_LCLCTRL_GPIO_OE0 |
2837 GRC_LCLCTRL_GPIO_OE1 |
2838 GRC_LCLCTRL_GPIO_OE2 |
2839 GRC_LCLCTRL_GPIO_OUTPUT0 |
2840 GRC_LCLCTRL_GPIO_OUTPUT1),
2841 TG3_GRC_LCLCTL_PWRSW_DELAY);
2842 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2843 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2844 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2845 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2846 GRC_LCLCTRL_GPIO_OE1 |
2847 GRC_LCLCTRL_GPIO_OE2 |
2848 GRC_LCLCTRL_GPIO_OUTPUT0 |
2849 GRC_LCLCTRL_GPIO_OUTPUT1 |
2850 tp->grc_local_ctrl;
2851 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2852 TG3_GRC_LCLCTL_PWRSW_DELAY);
2853
2854 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2855 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2856 TG3_GRC_LCLCTL_PWRSW_DELAY);
2857
2858 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2859 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2860 TG3_GRC_LCLCTL_PWRSW_DELAY);
2861 } else {
2862 u32 no_gpio2;
2863 u32 grc_local_ctrl = 0;
2864
2865 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002866 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002867 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2868 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2869 grc_local_ctrl,
2870 TG3_GRC_LCLCTL_PWRSW_DELAY);
2871 }
2872
2873 /* On 5753 and variants, GPIO2 cannot be used. */
2874 no_gpio2 = tp->nic_sram_data_cfg &
2875 NIC_SRAM_DATA_CFG_NO_GPIO2;
2876
2877 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2878 GRC_LCLCTRL_GPIO_OE1 |
2879 GRC_LCLCTRL_GPIO_OE2 |
2880 GRC_LCLCTRL_GPIO_OUTPUT1 |
2881 GRC_LCLCTRL_GPIO_OUTPUT2;
2882 if (no_gpio2) {
2883 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2884 GRC_LCLCTRL_GPIO_OUTPUT2);
2885 }
2886 tw32_wait_f(GRC_LOCAL_CTRL,
2887 tp->grc_local_ctrl | grc_local_ctrl,
2888 TG3_GRC_LCLCTL_PWRSW_DELAY);
2889
2890 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2891
2892 tw32_wait_f(GRC_LOCAL_CTRL,
2893 tp->grc_local_ctrl | grc_local_ctrl,
2894 TG3_GRC_LCLCTL_PWRSW_DELAY);
2895
2896 if (!no_gpio2) {
2897 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2898 tw32_wait_f(GRC_LOCAL_CTRL,
2899 tp->grc_local_ctrl | grc_local_ctrl,
2900 TG3_GRC_LCLCTL_PWRSW_DELAY);
2901 }
2902 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002903}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002904
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002905static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002906{
2907 u32 msg = 0;
2908
2909 /* Serialize power state transitions */
2910 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2911 return;
2912
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002913 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002914 msg = TG3_GPIO_MSG_NEED_VAUX;
2915
2916 msg = tg3_set_function_status(tp, msg);
2917
2918 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2919 goto done;
2920
2921 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2922 tg3_pwrsrc_switch_to_vaux(tp);
2923 else
2924 tg3_pwrsrc_die_with_vmain(tp);
2925
2926done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002927 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002928}
2929
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002930static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Matt Carlson683644b2011-03-09 16:58:23 +00002932 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Matt Carlson334355a2010-01-20 16:58:10 +00002934 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002935 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return;
2937
Joe Perches41535772013-02-16 11:20:04 +00002938 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2939 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2940 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002941 tg3_frob_aux_power_5717(tp, include_wol ?
2942 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002943 return;
2944 }
2945
2946 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002947 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002949 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002950
Michael Chanbc1c7562006-03-20 17:48:03 -08002951 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002952 if (dev_peer) {
2953 struct tg3 *tp_peer = netdev_priv(dev_peer);
2954
Joe Perches63c3a662011-04-26 08:12:10 +00002955 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002956 return;
2957
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002958 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002959 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002960 need_vaux = true;
2961 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002964 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2965 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002966 need_vaux = true;
2967
Matt Carlson520b2752011-06-13 13:39:02 +00002968 if (need_vaux)
2969 tg3_pwrsrc_switch_to_vaux(tp);
2970 else
2971 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002974static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2975{
2976 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2977 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002978 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002979 if (speed != SPEED_10)
2980 return 1;
2981 } else if (speed == SPEED_10)
2982 return 1;
2983
2984 return 0;
2985}
2986
Nithin Sujir44f3b502013-05-13 11:04:15 +00002987static bool tg3_phy_power_bug(struct tg3 *tp)
2988{
2989 switch (tg3_asic_rev(tp)) {
2990 case ASIC_REV_5700:
2991 case ASIC_REV_5704:
2992 return true;
2993 case ASIC_REV_5780:
2994 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
2995 return true;
2996 return false;
2997 case ASIC_REV_5717:
2998 if (!tp->pci_fn)
2999 return true;
3000 return false;
3001 case ASIC_REV_5719:
3002 case ASIC_REV_5720:
3003 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3004 !tp->pci_fn)
3005 return true;
3006 return false;
3007 }
3008
3009 return false;
3010}
3011
Matt Carlson0a459aa2008-11-03 16:54:15 -08003012static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003013{
Matt Carlsonce057f02007-11-12 21:08:03 -08003014 u32 val;
3015
Nithin Sujir942d1af2013-04-09 08:48:07 +00003016 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3017 return;
3018
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003019 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003020 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003021 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3022 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3023
3024 sg_dig_ctrl |=
3025 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3026 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3027 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3028 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003029 return;
Michael Chan51297242007-02-13 12:17:57 -08003030 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003031
Joe Perches41535772013-02-16 11:20:04 +00003032 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003033 tg3_bmcr_reset(tp);
3034 val = tr32(GRC_MISC_CFG);
3035 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3036 udelay(40);
3037 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003038 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003039 u32 phytest;
3040 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3041 u32 phy;
3042
3043 tg3_writephy(tp, MII_ADVERTISE, 0);
3044 tg3_writephy(tp, MII_BMCR,
3045 BMCR_ANENABLE | BMCR_ANRESTART);
3046
3047 tg3_writephy(tp, MII_TG3_FET_TEST,
3048 phytest | MII_TG3_FET_SHADOW_EN);
3049 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3050 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3051 tg3_writephy(tp,
3052 MII_TG3_FET_SHDW_AUXMODE4,
3053 phy);
3054 }
3055 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3056 }
3057 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003058 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07003059 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3060 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003061
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003062 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3063 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3064 MII_TG3_AUXCTL_PCTL_VREG_11V;
3065 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003066 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003067
Michael Chan15c3b692006-03-22 01:06:52 -08003068 /* The PHY should not be powered down on some chips because
3069 * of bugs.
3070 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003071 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003072 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003073
Joe Perches41535772013-02-16 11:20:04 +00003074 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3075 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003076 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3077 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3078 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3079 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3080 }
3081
Michael Chan15c3b692006-03-22 01:06:52 -08003082 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3083}
3084
Matt Carlson3f007892008-11-03 16:51:36 -08003085/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003086static int tg3_nvram_lock(struct tg3 *tp)
3087{
Joe Perches63c3a662011-04-26 08:12:10 +00003088 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003089 int i;
3090
3091 if (tp->nvram_lock_cnt == 0) {
3092 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3093 for (i = 0; i < 8000; i++) {
3094 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3095 break;
3096 udelay(20);
3097 }
3098 if (i == 8000) {
3099 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3100 return -ENODEV;
3101 }
3102 }
3103 tp->nvram_lock_cnt++;
3104 }
3105 return 0;
3106}
3107
3108/* tp->lock is held. */
3109static void tg3_nvram_unlock(struct tg3 *tp)
3110{
Joe Perches63c3a662011-04-26 08:12:10 +00003111 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003112 if (tp->nvram_lock_cnt > 0)
3113 tp->nvram_lock_cnt--;
3114 if (tp->nvram_lock_cnt == 0)
3115 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3116 }
3117}
3118
3119/* tp->lock is held. */
3120static void tg3_enable_nvram_access(struct tg3 *tp)
3121{
Joe Perches63c3a662011-04-26 08:12:10 +00003122 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003123 u32 nvaccess = tr32(NVRAM_ACCESS);
3124
3125 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3126 }
3127}
3128
3129/* tp->lock is held. */
3130static void tg3_disable_nvram_access(struct tg3 *tp)
3131{
Joe Perches63c3a662011-04-26 08:12:10 +00003132 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003133 u32 nvaccess = tr32(NVRAM_ACCESS);
3134
3135 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3136 }
3137}
3138
3139static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3140 u32 offset, u32 *val)
3141{
3142 u32 tmp;
3143 int i;
3144
3145 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3146 return -EINVAL;
3147
3148 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3149 EEPROM_ADDR_DEVID_MASK |
3150 EEPROM_ADDR_READ);
3151 tw32(GRC_EEPROM_ADDR,
3152 tmp |
3153 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3154 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3155 EEPROM_ADDR_ADDR_MASK) |
3156 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3157
3158 for (i = 0; i < 1000; i++) {
3159 tmp = tr32(GRC_EEPROM_ADDR);
3160
3161 if (tmp & EEPROM_ADDR_COMPLETE)
3162 break;
3163 msleep(1);
3164 }
3165 if (!(tmp & EEPROM_ADDR_COMPLETE))
3166 return -EBUSY;
3167
Matt Carlson62cedd12009-04-20 14:52:29 -07003168 tmp = tr32(GRC_EEPROM_DATA);
3169
3170 /*
3171 * The data will always be opposite the native endian
3172 * format. Perform a blind byteswap to compensate.
3173 */
3174 *val = swab32(tmp);
3175
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003176 return 0;
3177}
3178
3179#define NVRAM_CMD_TIMEOUT 10000
3180
3181static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3182{
3183 int i;
3184
3185 tw32(NVRAM_CMD, nvram_cmd);
3186 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3187 udelay(10);
3188 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3189 udelay(10);
3190 break;
3191 }
3192 }
3193
3194 if (i == NVRAM_CMD_TIMEOUT)
3195 return -EBUSY;
3196
3197 return 0;
3198}
3199
3200static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3201{
Joe Perches63c3a662011-04-26 08:12:10 +00003202 if (tg3_flag(tp, NVRAM) &&
3203 tg3_flag(tp, NVRAM_BUFFERED) &&
3204 tg3_flag(tp, FLASH) &&
3205 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003206 (tp->nvram_jedecnum == JEDEC_ATMEL))
3207
3208 addr = ((addr / tp->nvram_pagesize) <<
3209 ATMEL_AT45DB0X1B_PAGE_POS) +
3210 (addr % tp->nvram_pagesize);
3211
3212 return addr;
3213}
3214
3215static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3216{
Joe Perches63c3a662011-04-26 08:12:10 +00003217 if (tg3_flag(tp, NVRAM) &&
3218 tg3_flag(tp, NVRAM_BUFFERED) &&
3219 tg3_flag(tp, FLASH) &&
3220 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003221 (tp->nvram_jedecnum == JEDEC_ATMEL))
3222
3223 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3224 tp->nvram_pagesize) +
3225 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3226
3227 return addr;
3228}
3229
Matt Carlsone4f34112009-02-25 14:25:00 +00003230/* NOTE: Data read in from NVRAM is byteswapped according to
3231 * the byteswapping settings for all other register accesses.
3232 * tg3 devices are BE devices, so on a BE machine, the data
3233 * returned will be exactly as it is seen in NVRAM. On a LE
3234 * machine, the 32-bit value will be byteswapped.
3235 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003236static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3237{
3238 int ret;
3239
Joe Perches63c3a662011-04-26 08:12:10 +00003240 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003241 return tg3_nvram_read_using_eeprom(tp, offset, val);
3242
3243 offset = tg3_nvram_phys_addr(tp, offset);
3244
3245 if (offset > NVRAM_ADDR_MSK)
3246 return -EINVAL;
3247
3248 ret = tg3_nvram_lock(tp);
3249 if (ret)
3250 return ret;
3251
3252 tg3_enable_nvram_access(tp);
3253
3254 tw32(NVRAM_ADDR, offset);
3255 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3256 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3257
3258 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003259 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003260
3261 tg3_disable_nvram_access(tp);
3262
3263 tg3_nvram_unlock(tp);
3264
3265 return ret;
3266}
3267
Matt Carlsona9dc5292009-02-25 14:25:30 +00003268/* Ensures NVRAM data is in bytestream format. */
3269static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003270{
3271 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003272 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003273 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003274 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003275 return res;
3276}
3277
Matt Carlsondbe9b922012-02-13 10:20:09 +00003278static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3279 u32 offset, u32 len, u8 *buf)
3280{
3281 int i, j, rc = 0;
3282 u32 val;
3283
3284 for (i = 0; i < len; i += 4) {
3285 u32 addr;
3286 __be32 data;
3287
3288 addr = offset + i;
3289
3290 memcpy(&data, buf + i, 4);
3291
3292 /*
3293 * The SEEPROM interface expects the data to always be opposite
3294 * the native endian format. We accomplish this by reversing
3295 * all the operations that would have been performed on the
3296 * data from a call to tg3_nvram_read_be32().
3297 */
3298 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3299
3300 val = tr32(GRC_EEPROM_ADDR);
3301 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3302
3303 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3304 EEPROM_ADDR_READ);
3305 tw32(GRC_EEPROM_ADDR, val |
3306 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3307 (addr & EEPROM_ADDR_ADDR_MASK) |
3308 EEPROM_ADDR_START |
3309 EEPROM_ADDR_WRITE);
3310
3311 for (j = 0; j < 1000; j++) {
3312 val = tr32(GRC_EEPROM_ADDR);
3313
3314 if (val & EEPROM_ADDR_COMPLETE)
3315 break;
3316 msleep(1);
3317 }
3318 if (!(val & EEPROM_ADDR_COMPLETE)) {
3319 rc = -EBUSY;
3320 break;
3321 }
3322 }
3323
3324 return rc;
3325}
3326
3327/* offset and length are dword aligned */
3328static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3329 u8 *buf)
3330{
3331 int ret = 0;
3332 u32 pagesize = tp->nvram_pagesize;
3333 u32 pagemask = pagesize - 1;
3334 u32 nvram_cmd;
3335 u8 *tmp;
3336
3337 tmp = kmalloc(pagesize, GFP_KERNEL);
3338 if (tmp == NULL)
3339 return -ENOMEM;
3340
3341 while (len) {
3342 int j;
3343 u32 phy_addr, page_off, size;
3344
3345 phy_addr = offset & ~pagemask;
3346
3347 for (j = 0; j < pagesize; j += 4) {
3348 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3349 (__be32 *) (tmp + j));
3350 if (ret)
3351 break;
3352 }
3353 if (ret)
3354 break;
3355
3356 page_off = offset & pagemask;
3357 size = pagesize;
3358 if (len < size)
3359 size = len;
3360
3361 len -= size;
3362
3363 memcpy(tmp + page_off, buf, size);
3364
3365 offset = offset + (pagesize - page_off);
3366
3367 tg3_enable_nvram_access(tp);
3368
3369 /*
3370 * Before we can erase the flash page, we need
3371 * to issue a special "write enable" command.
3372 */
3373 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3374
3375 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3376 break;
3377
3378 /* Erase the target page */
3379 tw32(NVRAM_ADDR, phy_addr);
3380
3381 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3382 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3383
3384 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3385 break;
3386
3387 /* Issue another write enable to start the write. */
3388 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3389
3390 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3391 break;
3392
3393 for (j = 0; j < pagesize; j += 4) {
3394 __be32 data;
3395
3396 data = *((__be32 *) (tmp + j));
3397
3398 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3399
3400 tw32(NVRAM_ADDR, phy_addr + j);
3401
3402 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3403 NVRAM_CMD_WR;
3404
3405 if (j == 0)
3406 nvram_cmd |= NVRAM_CMD_FIRST;
3407 else if (j == (pagesize - 4))
3408 nvram_cmd |= NVRAM_CMD_LAST;
3409
3410 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3411 if (ret)
3412 break;
3413 }
3414 if (ret)
3415 break;
3416 }
3417
3418 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3419 tg3_nvram_exec_cmd(tp, nvram_cmd);
3420
3421 kfree(tmp);
3422
3423 return ret;
3424}
3425
3426/* offset and length are dword aligned */
3427static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3428 u8 *buf)
3429{
3430 int i, ret = 0;
3431
3432 for (i = 0; i < len; i += 4, offset += 4) {
3433 u32 page_off, phy_addr, nvram_cmd;
3434 __be32 data;
3435
3436 memcpy(&data, buf + i, 4);
3437 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3438
3439 page_off = offset % tp->nvram_pagesize;
3440
3441 phy_addr = tg3_nvram_phys_addr(tp, offset);
3442
Matt Carlsondbe9b922012-02-13 10:20:09 +00003443 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3444
3445 if (page_off == 0 || i == 0)
3446 nvram_cmd |= NVRAM_CMD_FIRST;
3447 if (page_off == (tp->nvram_pagesize - 4))
3448 nvram_cmd |= NVRAM_CMD_LAST;
3449
3450 if (i == (len - 4))
3451 nvram_cmd |= NVRAM_CMD_LAST;
3452
Matt Carlson42278222012-02-13 15:20:11 +00003453 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3454 !tg3_flag(tp, FLASH) ||
3455 !tg3_flag(tp, 57765_PLUS))
3456 tw32(NVRAM_ADDR, phy_addr);
3457
Joe Perches41535772013-02-16 11:20:04 +00003458 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003459 !tg3_flag(tp, 5755_PLUS) &&
3460 (tp->nvram_jedecnum == JEDEC_ST) &&
3461 (nvram_cmd & NVRAM_CMD_FIRST)) {
3462 u32 cmd;
3463
3464 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3465 ret = tg3_nvram_exec_cmd(tp, cmd);
3466 if (ret)
3467 break;
3468 }
3469 if (!tg3_flag(tp, FLASH)) {
3470 /* We always do complete word writes to eeprom. */
3471 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3472 }
3473
3474 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3475 if (ret)
3476 break;
3477 }
3478 return ret;
3479}
3480
3481/* offset and length are dword aligned */
3482static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3483{
3484 int ret;
3485
3486 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3487 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3488 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3489 udelay(40);
3490 }
3491
3492 if (!tg3_flag(tp, NVRAM)) {
3493 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3494 } else {
3495 u32 grc_mode;
3496
3497 ret = tg3_nvram_lock(tp);
3498 if (ret)
3499 return ret;
3500
3501 tg3_enable_nvram_access(tp);
3502 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3503 tw32(NVRAM_WRITE1, 0x406);
3504
3505 grc_mode = tr32(GRC_MODE);
3506 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3507
3508 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3509 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3510 buf);
3511 } else {
3512 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3513 buf);
3514 }
3515
3516 grc_mode = tr32(GRC_MODE);
3517 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3518
3519 tg3_disable_nvram_access(tp);
3520 tg3_nvram_unlock(tp);
3521 }
3522
3523 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3524 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3525 udelay(40);
3526 }
3527
3528 return ret;
3529}
3530
Matt Carlson997b4f12011-08-31 11:44:53 +00003531#define RX_CPU_SCRATCH_BASE 0x30000
3532#define RX_CPU_SCRATCH_SIZE 0x04000
3533#define TX_CPU_SCRATCH_BASE 0x34000
3534#define TX_CPU_SCRATCH_SIZE 0x04000
3535
3536/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003537static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003538{
3539 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003540 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003541
Nithin Sujir837c45b2013-03-06 17:02:30 +00003542 for (i = 0; i < iters; i++) {
3543 tw32(cpu_base + CPU_STATE, 0xffffffff);
3544 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3545 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3546 break;
3547 }
3548
3549 return (i == iters) ? -EBUSY : 0;
3550}
3551
3552/* tp->lock is held. */
3553static int tg3_rxcpu_pause(struct tg3 *tp)
3554{
3555 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3556
3557 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3558 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3559 udelay(10);
3560
3561 return rc;
3562}
3563
3564/* tp->lock is held. */
3565static int tg3_txcpu_pause(struct tg3 *tp)
3566{
3567 return tg3_pause_cpu(tp, TX_CPU_BASE);
3568}
3569
3570/* tp->lock is held. */
3571static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3572{
3573 tw32(cpu_base + CPU_STATE, 0xffffffff);
3574 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3575}
3576
3577/* tp->lock is held. */
3578static void tg3_rxcpu_resume(struct tg3 *tp)
3579{
3580 tg3_resume_cpu(tp, RX_CPU_BASE);
3581}
3582
3583/* tp->lock is held. */
3584static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3585{
3586 int rc;
3587
3588 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003589
Joe Perches41535772013-02-16 11:20:04 +00003590 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003591 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3592
3593 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3594 return 0;
3595 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003596 if (cpu_base == RX_CPU_BASE) {
3597 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003598 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003599 /*
3600 * There is only an Rx CPU for the 5750 derivative in the
3601 * BCM4785.
3602 */
3603 if (tg3_flag(tp, IS_SSB_CORE))
3604 return 0;
3605
Nithin Sujir837c45b2013-03-06 17:02:30 +00003606 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003607 }
3608
Nithin Sujir837c45b2013-03-06 17:02:30 +00003609 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003610 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003611 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003612 return -ENODEV;
3613 }
3614
3615 /* Clear firmware's nvram arbitration. */
3616 if (tg3_flag(tp, NVRAM))
3617 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3618 return 0;
3619}
3620
Nithin Sujir31f11a92013-03-06 17:02:33 +00003621static int tg3_fw_data_len(struct tg3 *tp,
3622 const struct tg3_firmware_hdr *fw_hdr)
3623{
3624 int fw_len;
3625
3626 /* Non fragmented firmware have one firmware header followed by a
3627 * contiguous chunk of data to be written. The length field in that
3628 * header is not the length of data to be written but the complete
3629 * length of the bss. The data length is determined based on
3630 * tp->fw->size minus headers.
3631 *
3632 * Fragmented firmware have a main header followed by multiple
3633 * fragments. Each fragment is identical to non fragmented firmware
3634 * with a firmware header followed by a contiguous chunk of data. In
3635 * the main header, the length field is unused and set to 0xffffffff.
3636 * In each fragment header the length is the entire size of that
3637 * fragment i.e. fragment data + header length. Data length is
3638 * therefore length field in the header minus TG3_FW_HDR_LEN.
3639 */
3640 if (tp->fw_len == 0xffffffff)
3641 fw_len = be32_to_cpu(fw_hdr->len);
3642 else
3643 fw_len = tp->fw->size;
3644
3645 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3646}
3647
Matt Carlson997b4f12011-08-31 11:44:53 +00003648/* tp->lock is held. */
3649static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3650 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003651 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003652{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003653 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003654 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003655 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003656
3657 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3658 netdev_err(tp->dev,
3659 "%s: Trying to load TX cpu firmware which is 5705\n",
3660 __func__);
3661 return -EINVAL;
3662 }
3663
Nithin Sujirc4dab502013-03-06 17:02:34 +00003664 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003665 write_op = tg3_write_mem;
3666 else
3667 write_op = tg3_write_indirect_reg32;
3668
Nithin Sujirc4dab502013-03-06 17:02:34 +00003669 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3670 /* It is possible that bootcode is still loading at this point.
3671 * Get the nvram lock first before halting the cpu.
3672 */
3673 int lock_err = tg3_nvram_lock(tp);
3674 err = tg3_halt_cpu(tp, cpu_base);
3675 if (!lock_err)
3676 tg3_nvram_unlock(tp);
3677 if (err)
3678 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003679
Nithin Sujirc4dab502013-03-06 17:02:34 +00003680 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3681 write_op(tp, cpu_scratch_base + i, 0);
3682 tw32(cpu_base + CPU_STATE, 0xffffffff);
3683 tw32(cpu_base + CPU_MODE,
3684 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3685 } else {
3686 /* Subtract additional main header for fragmented firmware and
3687 * advance to the first fragment
3688 */
3689 total_len -= TG3_FW_HDR_LEN;
3690 fw_hdr++;
3691 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003692
Nithin Sujir31f11a92013-03-06 17:02:33 +00003693 do {
3694 u32 *fw_data = (u32 *)(fw_hdr + 1);
3695 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3696 write_op(tp, cpu_scratch_base +
3697 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3698 (i * sizeof(u32)),
3699 be32_to_cpu(fw_data[i]));
3700
3701 total_len -= be32_to_cpu(fw_hdr->len);
3702
3703 /* Advance to next fragment */
3704 fw_hdr = (struct tg3_firmware_hdr *)
3705 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3706 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003707
3708 err = 0;
3709
3710out:
3711 return err;
3712}
3713
3714/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003715static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3716{
3717 int i;
3718 const int iters = 5;
3719
3720 tw32(cpu_base + CPU_STATE, 0xffffffff);
3721 tw32_f(cpu_base + CPU_PC, pc);
3722
3723 for (i = 0; i < iters; i++) {
3724 if (tr32(cpu_base + CPU_PC) == pc)
3725 break;
3726 tw32(cpu_base + CPU_STATE, 0xffffffff);
3727 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3728 tw32_f(cpu_base + CPU_PC, pc);
3729 udelay(1000);
3730 }
3731
3732 return (i == iters) ? -EBUSY : 0;
3733}
3734
3735/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003736static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3737{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003738 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003739 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003740
Nithin Sujir77997ea2013-03-06 17:02:32 +00003741 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003742
3743 /* Firmware blob starts with version numbers, followed by
3744 start address and length. We are setting complete length.
3745 length = end_address_of_bss - start_address_of_text.
3746 Remainder is the blob to be loaded contiguously
3747 from start address. */
3748
Matt Carlson997b4f12011-08-31 11:44:53 +00003749 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3750 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003751 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003752 if (err)
3753 return err;
3754
3755 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3756 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003757 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003758 if (err)
3759 return err;
3760
3761 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003762 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3763 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003764 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003765 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3766 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003767 tr32(RX_CPU_BASE + CPU_PC),
3768 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003769 return -ENODEV;
3770 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003771
3772 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003773
3774 return 0;
3775}
3776
Nithin Sujirc4dab502013-03-06 17:02:34 +00003777static int tg3_validate_rxcpu_state(struct tg3 *tp)
3778{
3779 const int iters = 1000;
3780 int i;
3781 u32 val;
3782
3783 /* Wait for boot code to complete initialization and enter service
3784 * loop. It is then safe to download service patches
3785 */
3786 for (i = 0; i < iters; i++) {
3787 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3788 break;
3789
3790 udelay(10);
3791 }
3792
3793 if (i == iters) {
3794 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3795 return -EBUSY;
3796 }
3797
3798 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3799 if (val & 0xff) {
3800 netdev_warn(tp->dev,
3801 "Other patches exist. Not downloading EEE patch\n");
3802 return -EEXIST;
3803 }
3804
3805 return 0;
3806}
3807
3808/* tp->lock is held. */
3809static void tg3_load_57766_firmware(struct tg3 *tp)
3810{
3811 struct tg3_firmware_hdr *fw_hdr;
3812
3813 if (!tg3_flag(tp, NO_NVRAM))
3814 return;
3815
3816 if (tg3_validate_rxcpu_state(tp))
3817 return;
3818
3819 if (!tp->fw)
3820 return;
3821
3822 /* This firmware blob has a different format than older firmware
3823 * releases as given below. The main difference is we have fragmented
3824 * data to be written to non-contiguous locations.
3825 *
3826 * In the beginning we have a firmware header identical to other
3827 * firmware which consists of version, base addr and length. The length
3828 * here is unused and set to 0xffffffff.
3829 *
3830 * This is followed by a series of firmware fragments which are
3831 * individually identical to previous firmware. i.e. they have the
3832 * firmware header and followed by data for that fragment. The version
3833 * field of the individual fragment header is unused.
3834 */
3835
3836 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3837 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3838 return;
3839
3840 if (tg3_rxcpu_pause(tp))
3841 return;
3842
3843 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3844 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3845
3846 tg3_rxcpu_resume(tp);
3847}
3848
Matt Carlson997b4f12011-08-31 11:44:53 +00003849/* tp->lock is held. */
3850static int tg3_load_tso_firmware(struct tg3 *tp)
3851{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003852 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003853 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003854 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003855
Matt Carlson1caf13e2013-03-06 17:02:29 +00003856 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003857 return 0;
3858
Nithin Sujir77997ea2013-03-06 17:02:32 +00003859 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003860
3861 /* Firmware blob starts with version numbers, followed by
3862 start address and length. We are setting complete length.
3863 length = end_address_of_bss - start_address_of_text.
3864 Remainder is the blob to be loaded contiguously
3865 from start address. */
3866
Matt Carlson997b4f12011-08-31 11:44:53 +00003867 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003868
Joe Perches41535772013-02-16 11:20:04 +00003869 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003870 cpu_base = RX_CPU_BASE;
3871 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3872 } else {
3873 cpu_base = TX_CPU_BASE;
3874 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3875 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3876 }
3877
3878 err = tg3_load_firmware_cpu(tp, cpu_base,
3879 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003880 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003881 if (err)
3882 return err;
3883
3884 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003885 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3886 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003887 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003888 netdev_err(tp->dev,
3889 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003890 __func__, tr32(cpu_base + CPU_PC),
3891 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003892 return -ENODEV;
3893 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003894
3895 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003896 return 0;
3897}
3898
3899
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003900/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003901static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003902{
3903 u32 addr_high, addr_low;
3904 int i;
3905
3906 addr_high = ((tp->dev->dev_addr[0] << 8) |
3907 tp->dev->dev_addr[1]);
3908 addr_low = ((tp->dev->dev_addr[2] << 24) |
3909 (tp->dev->dev_addr[3] << 16) |
3910 (tp->dev->dev_addr[4] << 8) |
3911 (tp->dev->dev_addr[5] << 0));
3912 for (i = 0; i < 4; i++) {
3913 if (i == 1 && skip_mac_1)
3914 continue;
3915 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3916 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3917 }
3918
Joe Perches41535772013-02-16 11:20:04 +00003919 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3920 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003921 for (i = 0; i < 12; i++) {
3922 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3923 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3924 }
3925 }
3926
3927 addr_high = (tp->dev->dev_addr[0] +
3928 tp->dev->dev_addr[1] +
3929 tp->dev->dev_addr[2] +
3930 tp->dev->dev_addr[3] +
3931 tp->dev->dev_addr[4] +
3932 tp->dev->dev_addr[5]) &
3933 TX_BACKOFF_SEED_MASK;
3934 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3935}
3936
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003937static void tg3_enable_register_access(struct tg3 *tp)
3938{
3939 /*
3940 * Make sure register accesses (indirect or otherwise) will function
3941 * correctly.
3942 */
3943 pci_write_config_dword(tp->pdev,
3944 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3945}
3946
3947static int tg3_power_up(struct tg3 *tp)
3948{
Matt Carlsonbed98292011-07-13 09:27:29 +00003949 int err;
3950
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003951 tg3_enable_register_access(tp);
3952
Matt Carlsonbed98292011-07-13 09:27:29 +00003953 err = pci_set_power_state(tp->pdev, PCI_D0);
3954 if (!err) {
3955 /* Switch out of Vaux if it is a NIC */
3956 tg3_pwrsrc_switch_to_vmain(tp);
3957 } else {
3958 netdev_err(tp->dev, "Transition to D0 failed\n");
3959 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003960
Matt Carlsonbed98292011-07-13 09:27:29 +00003961 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003962}
3963
Joe Perches953c96e2013-04-09 10:18:14 +00003964static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00003965
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003966static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
3968 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003969 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003971 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003972
3973 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003974 if (tg3_flag(tp, CLKREQ_BUG))
3975 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3976 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3979 tw32(TG3PCI_MISC_HOST_CTRL,
3980 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3981
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003982 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003983 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003984
Joe Perches63c3a662011-04-26 08:12:10 +00003985 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003986 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003987 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003988 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003989 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003990 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003991
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003992 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003993
Matt Carlson80096062010-08-02 11:26:06 +00003994 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003995
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003996 tp->link_config.speed = phydev->speed;
3997 tp->link_config.duplex = phydev->duplex;
3998 tp->link_config.autoneg = phydev->autoneg;
3999 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004000
4001 advertising = ADVERTISED_TP |
4002 ADVERTISED_Pause |
4003 ADVERTISED_Autoneg |
4004 ADVERTISED_10baseT_Half;
4005
Joe Perches63c3a662011-04-26 08:12:10 +00004006 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4007 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004008 advertising |=
4009 ADVERTISED_100baseT_Half |
4010 ADVERTISED_100baseT_Full |
4011 ADVERTISED_10baseT_Full;
4012 else
4013 advertising |= ADVERTISED_10baseT_Full;
4014 }
4015
4016 phydev->advertising = advertising;
4017
4018 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004019
4020 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004021 if (phyid != PHY_ID_BCMAC131) {
4022 phyid &= PHY_BCM_OUI_MASK;
4023 if (phyid == PHY_BCM_OUI_1 ||
4024 phyid == PHY_BCM_OUI_2 ||
4025 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004026 do_low_power = true;
4027 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004028 }
Matt Carlsondd477002008-05-25 23:45:58 -07004029 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004030 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004031
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004032 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004033 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Matt Carlson2855b9f2012-02-13 15:20:14 +00004035 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004036 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
4038
Joe Perches41535772013-02-16 11:20:04 +00004039 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004040 u32 val;
4041
4042 val = tr32(GRC_VCPU_EXT_CTRL);
4043 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004044 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004045 int i;
4046 u32 val;
4047
4048 for (i = 0; i < 200; i++) {
4049 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4050 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4051 break;
4052 msleep(1);
4053 }
4054 }
Joe Perches63c3a662011-04-26 08:12:10 +00004055 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004056 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4057 WOL_DRV_STATE_SHUTDOWN |
4058 WOL_DRV_WOL |
4059 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004060
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004061 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 u32 mac_mode;
4063
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004064 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004065 if (do_low_power &&
4066 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4067 tg3_phy_auxctl_write(tp,
4068 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4069 MII_TG3_AUXCTL_PCTL_WOL_EN |
4070 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4071 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004072 udelay(40);
4073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004075 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004076 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004077 else if (tp->phy_flags &
4078 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4079 if (tp->link_config.active_speed == SPEED_1000)
4080 mac_mode = MAC_MODE_PORT_MODE_GMII;
4081 else
4082 mac_mode = MAC_MODE_PORT_MODE_MII;
4083 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004084 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004086 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004087 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004088 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004089 SPEED_100 : SPEED_10;
4090 if (tg3_5700_link_polarity(tp, speed))
4091 mac_mode |= MAC_MODE_LINK_POLARITY;
4092 else
4093 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 } else {
4096 mac_mode = MAC_MODE_PORT_MODE_TBI;
4097 }
4098
Joe Perches63c3a662011-04-26 08:12:10 +00004099 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 tw32(MAC_LED_CTRL, tp->led_ctrl);
4101
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004102 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004103 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4104 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004105 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Joe Perches63c3a662011-04-26 08:12:10 +00004107 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004108 mac_mode |= MAC_MODE_APE_TX_EN |
4109 MAC_MODE_APE_RX_EN |
4110 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 tw32_f(MAC_MODE, mac_mode);
4113 udelay(100);
4114
4115 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4116 udelay(10);
4117 }
4118
Joe Perches63c3a662011-04-26 08:12:10 +00004119 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004120 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4121 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 u32 base_val;
4123
4124 base_val = tp->pci_clock_ctrl;
4125 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4126 CLOCK_CTRL_TXCLK_DISABLE);
4127
Michael Chanb401e9e2005-12-19 16:27:04 -08004128 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4129 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004130 } else if (tg3_flag(tp, 5780_CLASS) ||
4131 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004132 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004133 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004134 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 u32 newbits1, newbits2;
4136
Joe Perches41535772013-02-16 11:20:04 +00004137 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4138 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4140 CLOCK_CTRL_TXCLK_DISABLE |
4141 CLOCK_CTRL_ALTCLK);
4142 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004143 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 newbits1 = CLOCK_CTRL_625_CORE;
4145 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4146 } else {
4147 newbits1 = CLOCK_CTRL_ALTCLK;
4148 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4149 }
4150
Michael Chanb401e9e2005-12-19 16:27:04 -08004151 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4152 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Michael Chanb401e9e2005-12-19 16:27:04 -08004154 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4155 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Joe Perches63c3a662011-04-26 08:12:10 +00004157 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 u32 newbits3;
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 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4163 CLOCK_CTRL_TXCLK_DISABLE |
4164 CLOCK_CTRL_44MHZ_CORE);
4165 } else {
4166 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4167 }
4168
Michael Chanb401e9e2005-12-19 16:27:04 -08004169 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4170 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 }
4172 }
4173
Joe Perches63c3a662011-04-26 08:12:10 +00004174 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004175 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004176
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004177 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
4179 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004180 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004181 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4182 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 u32 val = tr32(0x7d00);
4184
4185 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4186 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004187 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004188 int err;
4189
4190 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004192 if (!err)
4193 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 }
4196
Michael Chanbbadf502006-04-06 21:46:34 -07004197 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4198
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004199 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 return 0;
4202}
4203
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004204static void tg3_power_down(struct tg3 *tp)
4205{
4206 tg3_power_down_prepare(tp);
4207
Joe Perches63c3a662011-04-26 08:12:10 +00004208 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004209 pci_set_power_state(tp->pdev, PCI_D3hot);
4210}
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4213{
4214 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4215 case MII_TG3_AUX_STAT_10HALF:
4216 *speed = SPEED_10;
4217 *duplex = DUPLEX_HALF;
4218 break;
4219
4220 case MII_TG3_AUX_STAT_10FULL:
4221 *speed = SPEED_10;
4222 *duplex = DUPLEX_FULL;
4223 break;
4224
4225 case MII_TG3_AUX_STAT_100HALF:
4226 *speed = SPEED_100;
4227 *duplex = DUPLEX_HALF;
4228 break;
4229
4230 case MII_TG3_AUX_STAT_100FULL:
4231 *speed = SPEED_100;
4232 *duplex = DUPLEX_FULL;
4233 break;
4234
4235 case MII_TG3_AUX_STAT_1000HALF:
4236 *speed = SPEED_1000;
4237 *duplex = DUPLEX_HALF;
4238 break;
4239
4240 case MII_TG3_AUX_STAT_1000FULL:
4241 *speed = SPEED_1000;
4242 *duplex = DUPLEX_FULL;
4243 break;
4244
4245 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004246 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004247 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4248 SPEED_10;
4249 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4250 DUPLEX_HALF;
4251 break;
4252 }
Matt Carlsone7405222012-02-13 15:20:16 +00004253 *speed = SPEED_UNKNOWN;
4254 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257}
4258
Matt Carlson42b64a42011-05-19 12:12:49 +00004259static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Matt Carlson42b64a42011-05-19 12:12:49 +00004261 int err = 0;
4262 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Matt Carlson42b64a42011-05-19 12:12:49 +00004264 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004265 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004266 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Matt Carlson42b64a42011-05-19 12:12:49 +00004268 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4269 if (err)
4270 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Matt Carlson4f272092011-12-14 11:09:57 +00004272 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4273 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004274
Joe Perches41535772013-02-16 11:20:04 +00004275 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4276 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004277 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004278
Matt Carlson4f272092011-12-14 11:09:57 +00004279 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4280 if (err)
4281 goto done;
4282 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004283
Matt Carlson42b64a42011-05-19 12:12:49 +00004284 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4285 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Matt Carlson42b64a42011-05-19 12:12:49 +00004287 tw32(TG3_CPMU_EEE_MODE,
4288 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004289
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004290 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004291 if (!err) {
4292 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004293
Matt Carlsona6b68da2010-12-06 08:28:52 +00004294 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004295 /* Advertise 100-BaseTX EEE ability */
4296 if (advertise & ADVERTISED_100baseT_Full)
4297 val |= MDIO_AN_EEE_ADV_100TX;
4298 /* Advertise 1000-BaseT EEE ability */
4299 if (advertise & ADVERTISED_1000baseT_Full)
4300 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004301
4302 if (!tp->eee.eee_enabled) {
4303 val = 0;
4304 tp->eee.advertised = 0;
4305 } else {
4306 tp->eee.advertised = advertise &
4307 (ADVERTISED_100baseT_Full |
4308 ADVERTISED_1000baseT_Full);
4309 }
4310
Matt Carlson42b64a42011-05-19 12:12:49 +00004311 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004312 if (err)
4313 val = 0;
4314
Joe Perches41535772013-02-16 11:20:04 +00004315 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004316 case ASIC_REV_5717:
4317 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004318 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004319 case ASIC_REV_5719:
4320 /* If we advertised any eee advertisements above... */
4321 if (val)
4322 val = MII_TG3_DSP_TAP26_ALNOKO |
4323 MII_TG3_DSP_TAP26_RMRXSTO |
4324 MII_TG3_DSP_TAP26_OPCSINPT;
4325 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4326 /* Fall through */
4327 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004328 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004329 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4330 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4331 MII_TG3_DSP_CH34TP2_HIBW01);
4332 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004333
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004334 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004335 if (!err)
4336 err = err2;
4337 }
4338
4339done:
4340 return err;
4341}
4342
4343static void tg3_phy_copper_begin(struct tg3 *tp)
4344{
Matt Carlsond13ba512012-02-22 12:35:19 +00004345 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4346 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4347 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004348
Nithin Sujir942d1af2013-04-09 08:48:07 +00004349 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4350 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004351 adv = ADVERTISED_10baseT_Half |
4352 ADVERTISED_10baseT_Full;
4353 if (tg3_flag(tp, WOL_SPEED_100MB))
4354 adv |= ADVERTISED_100baseT_Half |
4355 ADVERTISED_100baseT_Full;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004356 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK)
4357 adv |= ADVERTISED_1000baseT_Half |
4358 ADVERTISED_1000baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004359
Matt Carlsond13ba512012-02-22 12:35:19 +00004360 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004361 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004362 adv = tp->link_config.advertising;
4363 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4364 adv &= ~(ADVERTISED_1000baseT_Half |
4365 ADVERTISED_1000baseT_Full);
4366
4367 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004368 }
4369
Matt Carlsond13ba512012-02-22 12:35:19 +00004370 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +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)) {
4374 /* Normally during power down we want to autonegotiate
4375 * the lowest possible speed for WOL. However, to avoid
4376 * link flap, we leave it untouched.
4377 */
4378 return;
4379 }
4380
Matt Carlsond13ba512012-02-22 12:35:19 +00004381 tg3_writephy(tp, MII_BMCR,
4382 BMCR_ANENABLE | BMCR_ANRESTART);
4383 } else {
4384 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 u32 bmcr, orig_bmcr;
4386
4387 tp->link_config.active_speed = tp->link_config.speed;
4388 tp->link_config.active_duplex = tp->link_config.duplex;
4389
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004390 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4391 /* With autoneg disabled, 5715 only links up when the
4392 * advertisement register has the configured speed
4393 * enabled.
4394 */
4395 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4396 }
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 bmcr = 0;
4399 switch (tp->link_config.speed) {
4400 default:
4401 case SPEED_10:
4402 break;
4403
4404 case SPEED_100:
4405 bmcr |= BMCR_SPEED100;
4406 break;
4407
4408 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004409 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
4413 if (tp->link_config.duplex == DUPLEX_FULL)
4414 bmcr |= BMCR_FULLDPLX;
4415
4416 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4417 (bmcr != orig_bmcr)) {
4418 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4419 for (i = 0; i < 1500; i++) {
4420 u32 tmp;
4421
4422 udelay(10);
4423 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4424 tg3_readphy(tp, MII_BMSR, &tmp))
4425 continue;
4426 if (!(tmp & BMSR_LSTATUS)) {
4427 udelay(40);
4428 break;
4429 }
4430 }
4431 tg3_writephy(tp, MII_BMCR, bmcr);
4432 udelay(40);
4433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
4435}
4436
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004437static int tg3_phy_pull_config(struct tg3 *tp)
4438{
4439 int err;
4440 u32 val;
4441
4442 err = tg3_readphy(tp, MII_BMCR, &val);
4443 if (err)
4444 goto done;
4445
4446 if (!(val & BMCR_ANENABLE)) {
4447 tp->link_config.autoneg = AUTONEG_DISABLE;
4448 tp->link_config.advertising = 0;
4449 tg3_flag_clear(tp, PAUSE_AUTONEG);
4450
4451 err = -EIO;
4452
4453 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4454 case 0:
4455 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4456 goto done;
4457
4458 tp->link_config.speed = SPEED_10;
4459 break;
4460 case BMCR_SPEED100:
4461 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4462 goto done;
4463
4464 tp->link_config.speed = SPEED_100;
4465 break;
4466 case BMCR_SPEED1000:
4467 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4468 tp->link_config.speed = SPEED_1000;
4469 break;
4470 }
4471 /* Fall through */
4472 default:
4473 goto done;
4474 }
4475
4476 if (val & BMCR_FULLDPLX)
4477 tp->link_config.duplex = DUPLEX_FULL;
4478 else
4479 tp->link_config.duplex = DUPLEX_HALF;
4480
4481 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4482
4483 err = 0;
4484 goto done;
4485 }
4486
4487 tp->link_config.autoneg = AUTONEG_ENABLE;
4488 tp->link_config.advertising = ADVERTISED_Autoneg;
4489 tg3_flag_set(tp, PAUSE_AUTONEG);
4490
4491 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4492 u32 adv;
4493
4494 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4495 if (err)
4496 goto done;
4497
4498 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4499 tp->link_config.advertising |= adv | ADVERTISED_TP;
4500
4501 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4502 } else {
4503 tp->link_config.advertising |= ADVERTISED_FIBRE;
4504 }
4505
4506 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4507 u32 adv;
4508
4509 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4510 err = tg3_readphy(tp, MII_CTRL1000, &val);
4511 if (err)
4512 goto done;
4513
4514 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4515 } else {
4516 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4517 if (err)
4518 goto done;
4519
4520 adv = tg3_decode_flowctrl_1000X(val);
4521 tp->link_config.flowctrl = adv;
4522
4523 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4524 adv = mii_adv_to_ethtool_adv_x(val);
4525 }
4526
4527 tp->link_config.advertising |= adv;
4528 }
4529
4530done:
4531 return err;
4532}
4533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534static int tg3_init_5401phy_dsp(struct tg3 *tp)
4535{
4536 int err;
4537
4538 /* Turn off tap power management. */
4539 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004540 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004542 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4543 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4544 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4545 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4546 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
4548 udelay(40);
4549
4550 return err;
4551}
4552
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004553static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4554{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004555 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004556
4557 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4558 return true;
4559
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004560 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004561
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004562 if (tp->eee.eee_enabled) {
4563 if (tp->eee.advertised != eee.advertised ||
4564 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4565 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4566 return false;
4567 } else {
4568 /* EEE is disabled but we're advertising */
4569 if (eee.advertised)
4570 return false;
4571 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004572
4573 return true;
4574}
4575
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004576static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004578 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004579
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004580 advertising = tp->link_config.advertising;
4581 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004583 advmsk = ADVERTISE_ALL;
4584 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004585 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004586 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004589 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4590 return false;
4591
4592 if ((*lcladv & advmsk) != tgtadv)
4593 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004594
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004595 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 u32 tg3_ctrl;
4597
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004598 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004599
Matt Carlson221c5632011-06-13 13:39:01 +00004600 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004601 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
Matt Carlson3198e072012-02-13 15:20:10 +00004603 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004604 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4605 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004606 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4607 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4608 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4609 } else {
4610 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4611 }
4612
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004613 if (tg3_ctrl != tgtadv)
4614 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004616
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004617 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004618}
4619
Matt Carlson859edb22011-12-08 14:40:16 +00004620static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4621{
4622 u32 lpeth = 0;
4623
4624 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4625 u32 val;
4626
4627 if (tg3_readphy(tp, MII_STAT1000, &val))
4628 return false;
4629
4630 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4631 }
4632
4633 if (tg3_readphy(tp, MII_LPA, rmtadv))
4634 return false;
4635
4636 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4637 tp->link_config.rmt_adv = lpeth;
4638
4639 return true;
4640}
4641
Joe Perches953c96e2013-04-09 10:18:14 +00004642static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004643{
4644 if (curr_link_up != tp->link_up) {
4645 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004646 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004647 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004648 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004649 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4650 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4651 }
4652
4653 tg3_link_report(tp);
4654 return true;
4655 }
4656
4657 return false;
4658}
4659
Michael Chan3310e242013-04-09 08:48:05 +00004660static void tg3_clear_mac_status(struct tg3 *tp)
4661{
4662 tw32(MAC_EVENT, 0);
4663
4664 tw32_f(MAC_STATUS,
4665 MAC_STATUS_SYNC_CHANGED |
4666 MAC_STATUS_CFG_CHANGED |
4667 MAC_STATUS_MI_COMPLETION |
4668 MAC_STATUS_LNKSTATE_CHANGED);
4669 udelay(40);
4670}
4671
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004672static void tg3_setup_eee(struct tg3 *tp)
4673{
4674 u32 val;
4675
4676 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4677 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4678 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4679 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4680
4681 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4682
4683 tw32_f(TG3_CPMU_EEE_CTRL,
4684 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4685
4686 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4687 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4688 TG3_CPMU_EEEMD_LPI_IN_RX |
4689 TG3_CPMU_EEEMD_EEE_ENABLE;
4690
4691 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4692 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4693
4694 if (tg3_flag(tp, ENABLE_APE))
4695 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4696
4697 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4698
4699 tw32_f(TG3_CPMU_EEE_DBTMR1,
4700 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4701 (tp->eee.tx_lpi_timer & 0xffff));
4702
4703 tw32_f(TG3_CPMU_EEE_DBTMR2,
4704 TG3_CPMU_DBTMR2_APE_TX_2047US |
4705 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4706}
4707
Joe Perches953c96e2013-04-09 10:18:14 +00004708static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Joe Perches953c96e2013-04-09 10:18:14 +00004710 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004711 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004712 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 u16 current_speed;
4714 u8 current_duplex;
4715 int i, err;
4716
Michael Chan3310e242013-04-09 08:48:05 +00004717 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Matt Carlson8ef21422008-05-02 16:47:53 -07004719 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4720 tw32_f(MAC_MI_MODE,
4721 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4722 udelay(80);
4723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004725 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 /* Some third-party PHYs need to be reset on link going
4728 * down.
4729 */
Joe Perches41535772013-02-16 11:20:04 +00004730 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4731 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4732 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004733 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 tg3_readphy(tp, MII_BMSR, &bmsr);
4735 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4736 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004737 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 }
4739 if (force_reset)
4740 tg3_phy_reset(tp);
4741
Matt Carlson79eb6902010-02-17 15:17:03 +00004742 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 tg3_readphy(tp, MII_BMSR, &bmsr);
4744 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004745 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 bmsr = 0;
4747
4748 if (!(bmsr & BMSR_LSTATUS)) {
4749 err = tg3_init_5401phy_dsp(tp);
4750 if (err)
4751 return err;
4752
4753 tg3_readphy(tp, MII_BMSR, &bmsr);
4754 for (i = 0; i < 1000; i++) {
4755 udelay(10);
4756 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4757 (bmsr & BMSR_LSTATUS)) {
4758 udelay(40);
4759 break;
4760 }
4761 }
4762
Matt Carlson79eb6902010-02-17 15:17:03 +00004763 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4764 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 !(bmsr & BMSR_LSTATUS) &&
4766 tp->link_config.active_speed == SPEED_1000) {
4767 err = tg3_phy_reset(tp);
4768 if (!err)
4769 err = tg3_init_5401phy_dsp(tp);
4770 if (err)
4771 return err;
4772 }
4773 }
Joe Perches41535772013-02-16 11:20:04 +00004774 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4775 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 /* 5701 {A0,B0} CRC bug workaround */
4777 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004778 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4779 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4780 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
4782
4783 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004784 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4785 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004787 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004789 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4791
Joe Perches41535772013-02-16 11:20:04 +00004792 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4793 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4795 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4796 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4797 else
4798 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4799 }
4800
Joe Perches953c96e2013-04-09 10:18:14 +00004801 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004802 current_speed = SPEED_UNKNOWN;
4803 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004804 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004805 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004807 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004808 err = tg3_phy_auxctl_read(tp,
4809 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4810 &val);
4811 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004812 tg3_phy_auxctl_write(tp,
4813 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4814 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 goto relink;
4816 }
4817 }
4818
4819 bmsr = 0;
4820 for (i = 0; i < 100; i++) {
4821 tg3_readphy(tp, MII_BMSR, &bmsr);
4822 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4823 (bmsr & BMSR_LSTATUS))
4824 break;
4825 udelay(40);
4826 }
4827
4828 if (bmsr & BMSR_LSTATUS) {
4829 u32 aux_stat, bmcr;
4830
4831 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4832 for (i = 0; i < 2000; i++) {
4833 udelay(10);
4834 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4835 aux_stat)
4836 break;
4837 }
4838
4839 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4840 &current_speed,
4841 &current_duplex);
4842
4843 bmcr = 0;
4844 for (i = 0; i < 200; i++) {
4845 tg3_readphy(tp, MII_BMCR, &bmcr);
4846 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4847 continue;
4848 if (bmcr && bmcr != 0x7fff)
4849 break;
4850 udelay(10);
4851 }
4852
Matt Carlsonef167e22007-12-20 20:10:01 -08004853 lcl_adv = 0;
4854 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Matt Carlsonef167e22007-12-20 20:10:01 -08004856 tp->link_config.active_speed = current_speed;
4857 tp->link_config.active_duplex = current_duplex;
4858
4859 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004860 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4861
Matt Carlsonef167e22007-12-20 20:10:01 -08004862 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004863 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004864 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004865 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004866 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004867
4868 /* EEE settings changes take effect only after a phy
4869 * reset. If we have skipped a reset due to Link Flap
4870 * Avoidance being enabled, do it now.
4871 */
4872 if (!eee_config_ok &&
4873 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004874 !force_reset) {
4875 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004876 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 } else {
4879 if (!(bmcr & BMCR_ANENABLE) &&
4880 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004881 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004882 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 }
4884 }
4885
Joe Perches953c96e2013-04-09 10:18:14 +00004886 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004887 tp->link_config.active_duplex == DUPLEX_FULL) {
4888 u32 reg, bit;
4889
4890 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4891 reg = MII_TG3_FET_GEN_STAT;
4892 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4893 } else {
4894 reg = MII_TG3_EXT_STAT;
4895 bit = MII_TG3_EXT_STAT_MDIX;
4896 }
4897
4898 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4899 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4900
Matt Carlsonef167e22007-12-20 20:10:01 -08004901 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004906 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 tg3_phy_copper_begin(tp);
4908
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004909 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004910 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004911 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4912 current_speed = SPEED_1000;
4913 current_duplex = DUPLEX_FULL;
4914 tp->link_config.active_speed = current_speed;
4915 tp->link_config.active_duplex = current_duplex;
4916 }
4917
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004918 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004919 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4920 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004921 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 }
4923
4924 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004925 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 if (tp->link_config.active_speed == SPEED_100 ||
4927 tp->link_config.active_speed == SPEED_10)
4928 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4929 else
4930 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004931 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004932 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4933 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4935
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004936 /* In order for the 5750 core in BCM4785 chip to work properly
4937 * in RGMII mode, the Led Control Register must be set up.
4938 */
4939 if (tg3_flag(tp, RGMII_MODE)) {
4940 u32 led_ctrl = tr32(MAC_LED_CTRL);
4941 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4942
4943 if (tp->link_config.active_speed == SPEED_10)
4944 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4945 else if (tp->link_config.active_speed == SPEED_100)
4946 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4947 LED_CTRL_100MBPS_ON);
4948 else if (tp->link_config.active_speed == SPEED_1000)
4949 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4950 LED_CTRL_1000MBPS_ON);
4951
4952 tw32(MAC_LED_CTRL, led_ctrl);
4953 udelay(40);
4954 }
4955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4957 if (tp->link_config.active_duplex == DUPLEX_HALF)
4958 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4959
Joe Perches41535772013-02-16 11:20:04 +00004960 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00004961 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004962 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004964 else
4965 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 }
4967
4968 /* ??? Without this setting Netgear GA302T PHY does not
4969 * ??? send/receive packets...
4970 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004971 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004972 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4974 tw32_f(MAC_MI_MODE, tp->mi_mode);
4975 udelay(80);
4976 }
4977
4978 tw32_f(MAC_MODE, tp->mac_mode);
4979 udelay(40);
4980
Matt Carlson52b02d02010-10-14 10:37:41 +00004981 tg3_phy_eee_adjust(tp, current_link_up);
4982
Joe Perches63c3a662011-04-26 08:12:10 +00004983 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 /* Polled via timer. */
4985 tw32_f(MAC_EVENT, 0);
4986 } else {
4987 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4988 }
4989 udelay(40);
4990
Joe Perches41535772013-02-16 11:20:04 +00004991 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00004992 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004994 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 udelay(120);
4996 tw32_f(MAC_STATUS,
4997 (MAC_STATUS_SYNC_CHANGED |
4998 MAC_STATUS_CFG_CHANGED));
4999 udelay(40);
5000 tg3_write_mem(tp,
5001 NIC_SRAM_FIRMWARE_MBOX,
5002 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5003 }
5004
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005005 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005006 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005007 if (tp->link_config.active_speed == SPEED_100 ||
5008 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005009 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5010 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005011 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005012 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5013 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005014 }
5015
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005016 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 return 0;
5019}
5020
5021struct tg3_fiber_aneginfo {
5022 int state;
5023#define ANEG_STATE_UNKNOWN 0
5024#define ANEG_STATE_AN_ENABLE 1
5025#define ANEG_STATE_RESTART_INIT 2
5026#define ANEG_STATE_RESTART 3
5027#define ANEG_STATE_DISABLE_LINK_OK 4
5028#define ANEG_STATE_ABILITY_DETECT_INIT 5
5029#define ANEG_STATE_ABILITY_DETECT 6
5030#define ANEG_STATE_ACK_DETECT_INIT 7
5031#define ANEG_STATE_ACK_DETECT 8
5032#define ANEG_STATE_COMPLETE_ACK_INIT 9
5033#define ANEG_STATE_COMPLETE_ACK 10
5034#define ANEG_STATE_IDLE_DETECT_INIT 11
5035#define ANEG_STATE_IDLE_DETECT 12
5036#define ANEG_STATE_LINK_OK 13
5037#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5038#define ANEG_STATE_NEXT_PAGE_WAIT 15
5039
5040 u32 flags;
5041#define MR_AN_ENABLE 0x00000001
5042#define MR_RESTART_AN 0x00000002
5043#define MR_AN_COMPLETE 0x00000004
5044#define MR_PAGE_RX 0x00000008
5045#define MR_NP_LOADED 0x00000010
5046#define MR_TOGGLE_TX 0x00000020
5047#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5048#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5049#define MR_LP_ADV_SYM_PAUSE 0x00000100
5050#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5051#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5052#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5053#define MR_LP_ADV_NEXT_PAGE 0x00001000
5054#define MR_TOGGLE_RX 0x00002000
5055#define MR_NP_RX 0x00004000
5056
5057#define MR_LINK_OK 0x80000000
5058
5059 unsigned long link_time, cur_time;
5060
5061 u32 ability_match_cfg;
5062 int ability_match_count;
5063
5064 char ability_match, idle_match, ack_match;
5065
5066 u32 txconfig, rxconfig;
5067#define ANEG_CFG_NP 0x00000080
5068#define ANEG_CFG_ACK 0x00000040
5069#define ANEG_CFG_RF2 0x00000020
5070#define ANEG_CFG_RF1 0x00000010
5071#define ANEG_CFG_PS2 0x00000001
5072#define ANEG_CFG_PS1 0x00008000
5073#define ANEG_CFG_HD 0x00004000
5074#define ANEG_CFG_FD 0x00002000
5075#define ANEG_CFG_INVAL 0x00001f06
5076
5077};
5078#define ANEG_OK 0
5079#define ANEG_DONE 1
5080#define ANEG_TIMER_ENAB 2
5081#define ANEG_FAILED -1
5082
5083#define ANEG_STATE_SETTLE_TIME 10000
5084
5085static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5086 struct tg3_fiber_aneginfo *ap)
5087{
Matt Carlson5be73b42007-12-20 20:09:29 -08005088 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 unsigned long delta;
5090 u32 rx_cfg_reg;
5091 int ret;
5092
5093 if (ap->state == ANEG_STATE_UNKNOWN) {
5094 ap->rxconfig = 0;
5095 ap->link_time = 0;
5096 ap->cur_time = 0;
5097 ap->ability_match_cfg = 0;
5098 ap->ability_match_count = 0;
5099 ap->ability_match = 0;
5100 ap->idle_match = 0;
5101 ap->ack_match = 0;
5102 }
5103 ap->cur_time++;
5104
5105 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5106 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5107
5108 if (rx_cfg_reg != ap->ability_match_cfg) {
5109 ap->ability_match_cfg = rx_cfg_reg;
5110 ap->ability_match = 0;
5111 ap->ability_match_count = 0;
5112 } else {
5113 if (++ap->ability_match_count > 1) {
5114 ap->ability_match = 1;
5115 ap->ability_match_cfg = rx_cfg_reg;
5116 }
5117 }
5118 if (rx_cfg_reg & ANEG_CFG_ACK)
5119 ap->ack_match = 1;
5120 else
5121 ap->ack_match = 0;
5122
5123 ap->idle_match = 0;
5124 } else {
5125 ap->idle_match = 1;
5126 ap->ability_match_cfg = 0;
5127 ap->ability_match_count = 0;
5128 ap->ability_match = 0;
5129 ap->ack_match = 0;
5130
5131 rx_cfg_reg = 0;
5132 }
5133
5134 ap->rxconfig = rx_cfg_reg;
5135 ret = ANEG_OK;
5136
Matt Carlson33f401a2010-04-05 10:19:27 +00005137 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 case ANEG_STATE_UNKNOWN:
5139 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5140 ap->state = ANEG_STATE_AN_ENABLE;
5141
5142 /* fallthru */
5143 case ANEG_STATE_AN_ENABLE:
5144 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5145 if (ap->flags & MR_AN_ENABLE) {
5146 ap->link_time = 0;
5147 ap->cur_time = 0;
5148 ap->ability_match_cfg = 0;
5149 ap->ability_match_count = 0;
5150 ap->ability_match = 0;
5151 ap->idle_match = 0;
5152 ap->ack_match = 0;
5153
5154 ap->state = ANEG_STATE_RESTART_INIT;
5155 } else {
5156 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5157 }
5158 break;
5159
5160 case ANEG_STATE_RESTART_INIT:
5161 ap->link_time = ap->cur_time;
5162 ap->flags &= ~(MR_NP_LOADED);
5163 ap->txconfig = 0;
5164 tw32(MAC_TX_AUTO_NEG, 0);
5165 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5166 tw32_f(MAC_MODE, tp->mac_mode);
5167 udelay(40);
5168
5169 ret = ANEG_TIMER_ENAB;
5170 ap->state = ANEG_STATE_RESTART;
5171
5172 /* fallthru */
5173 case ANEG_STATE_RESTART:
5174 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005175 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005177 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 break;
5180
5181 case ANEG_STATE_DISABLE_LINK_OK:
5182 ret = ANEG_DONE;
5183 break;
5184
5185 case ANEG_STATE_ABILITY_DETECT_INIT:
5186 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005187 ap->txconfig = ANEG_CFG_FD;
5188 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5189 if (flowctrl & ADVERTISE_1000XPAUSE)
5190 ap->txconfig |= ANEG_CFG_PS1;
5191 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5192 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5194 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5195 tw32_f(MAC_MODE, tp->mac_mode);
5196 udelay(40);
5197
5198 ap->state = ANEG_STATE_ABILITY_DETECT;
5199 break;
5200
5201 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005202 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 break;
5205
5206 case ANEG_STATE_ACK_DETECT_INIT:
5207 ap->txconfig |= ANEG_CFG_ACK;
5208 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5209 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5210 tw32_f(MAC_MODE, tp->mac_mode);
5211 udelay(40);
5212
5213 ap->state = ANEG_STATE_ACK_DETECT;
5214
5215 /* fallthru */
5216 case ANEG_STATE_ACK_DETECT:
5217 if (ap->ack_match != 0) {
5218 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5219 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5220 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5221 } else {
5222 ap->state = ANEG_STATE_AN_ENABLE;
5223 }
5224 } else if (ap->ability_match != 0 &&
5225 ap->rxconfig == 0) {
5226 ap->state = ANEG_STATE_AN_ENABLE;
5227 }
5228 break;
5229
5230 case ANEG_STATE_COMPLETE_ACK_INIT:
5231 if (ap->rxconfig & ANEG_CFG_INVAL) {
5232 ret = ANEG_FAILED;
5233 break;
5234 }
5235 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5236 MR_LP_ADV_HALF_DUPLEX |
5237 MR_LP_ADV_SYM_PAUSE |
5238 MR_LP_ADV_ASYM_PAUSE |
5239 MR_LP_ADV_REMOTE_FAULT1 |
5240 MR_LP_ADV_REMOTE_FAULT2 |
5241 MR_LP_ADV_NEXT_PAGE |
5242 MR_TOGGLE_RX |
5243 MR_NP_RX);
5244 if (ap->rxconfig & ANEG_CFG_FD)
5245 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5246 if (ap->rxconfig & ANEG_CFG_HD)
5247 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5248 if (ap->rxconfig & ANEG_CFG_PS1)
5249 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5250 if (ap->rxconfig & ANEG_CFG_PS2)
5251 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5252 if (ap->rxconfig & ANEG_CFG_RF1)
5253 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5254 if (ap->rxconfig & ANEG_CFG_RF2)
5255 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5256 if (ap->rxconfig & ANEG_CFG_NP)
5257 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5258
5259 ap->link_time = ap->cur_time;
5260
5261 ap->flags ^= (MR_TOGGLE_TX);
5262 if (ap->rxconfig & 0x0008)
5263 ap->flags |= MR_TOGGLE_RX;
5264 if (ap->rxconfig & ANEG_CFG_NP)
5265 ap->flags |= MR_NP_RX;
5266 ap->flags |= MR_PAGE_RX;
5267
5268 ap->state = ANEG_STATE_COMPLETE_ACK;
5269 ret = ANEG_TIMER_ENAB;
5270 break;
5271
5272 case ANEG_STATE_COMPLETE_ACK:
5273 if (ap->ability_match != 0 &&
5274 ap->rxconfig == 0) {
5275 ap->state = ANEG_STATE_AN_ENABLE;
5276 break;
5277 }
5278 delta = ap->cur_time - ap->link_time;
5279 if (delta > ANEG_STATE_SETTLE_TIME) {
5280 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5281 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5282 } else {
5283 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5284 !(ap->flags & MR_NP_RX)) {
5285 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5286 } else {
5287 ret = ANEG_FAILED;
5288 }
5289 }
5290 }
5291 break;
5292
5293 case ANEG_STATE_IDLE_DETECT_INIT:
5294 ap->link_time = ap->cur_time;
5295 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5296 tw32_f(MAC_MODE, tp->mac_mode);
5297 udelay(40);
5298
5299 ap->state = ANEG_STATE_IDLE_DETECT;
5300 ret = ANEG_TIMER_ENAB;
5301 break;
5302
5303 case ANEG_STATE_IDLE_DETECT:
5304 if (ap->ability_match != 0 &&
5305 ap->rxconfig == 0) {
5306 ap->state = ANEG_STATE_AN_ENABLE;
5307 break;
5308 }
5309 delta = ap->cur_time - ap->link_time;
5310 if (delta > ANEG_STATE_SETTLE_TIME) {
5311 /* XXX another gem from the Broadcom driver :( */
5312 ap->state = ANEG_STATE_LINK_OK;
5313 }
5314 break;
5315
5316 case ANEG_STATE_LINK_OK:
5317 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5318 ret = ANEG_DONE;
5319 break;
5320
5321 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5322 /* ??? unimplemented */
5323 break;
5324
5325 case ANEG_STATE_NEXT_PAGE_WAIT:
5326 /* ??? unimplemented */
5327 break;
5328
5329 default:
5330 ret = ANEG_FAILED;
5331 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
5334 return ret;
5335}
5336
Matt Carlson5be73b42007-12-20 20:09:29 -08005337static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
5339 int res = 0;
5340 struct tg3_fiber_aneginfo aninfo;
5341 int status = ANEG_FAILED;
5342 unsigned int tick;
5343 u32 tmp;
5344
5345 tw32_f(MAC_TX_AUTO_NEG, 0);
5346
5347 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5348 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5349 udelay(40);
5350
5351 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5352 udelay(40);
5353
5354 memset(&aninfo, 0, sizeof(aninfo));
5355 aninfo.flags |= MR_AN_ENABLE;
5356 aninfo.state = ANEG_STATE_UNKNOWN;
5357 aninfo.cur_time = 0;
5358 tick = 0;
5359 while (++tick < 195000) {
5360 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5361 if (status == ANEG_DONE || status == ANEG_FAILED)
5362 break;
5363
5364 udelay(1);
5365 }
5366
5367 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5368 tw32_f(MAC_MODE, tp->mac_mode);
5369 udelay(40);
5370
Matt Carlson5be73b42007-12-20 20:09:29 -08005371 *txflags = aninfo.txconfig;
5372 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
5374 if (status == ANEG_DONE &&
5375 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5376 MR_LP_ADV_FULL_DUPLEX)))
5377 res = 1;
5378
5379 return res;
5380}
5381
5382static void tg3_init_bcm8002(struct tg3 *tp)
5383{
5384 u32 mac_status = tr32(MAC_STATUS);
5385 int i;
5386
5387 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005388 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 !(mac_status & MAC_STATUS_PCS_SYNCED))
5390 return;
5391
5392 /* Set PLL lock range. */
5393 tg3_writephy(tp, 0x16, 0x8007);
5394
5395 /* SW reset */
5396 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5397
5398 /* Wait for reset to complete. */
5399 /* XXX schedule_timeout() ... */
5400 for (i = 0; i < 500; i++)
5401 udelay(10);
5402
5403 /* Config mode; select PMA/Ch 1 regs. */
5404 tg3_writephy(tp, 0x10, 0x8411);
5405
5406 /* Enable auto-lock and comdet, select txclk for tx. */
5407 tg3_writephy(tp, 0x11, 0x0a10);
5408
5409 tg3_writephy(tp, 0x18, 0x00a0);
5410 tg3_writephy(tp, 0x16, 0x41ff);
5411
5412 /* Assert and deassert POR. */
5413 tg3_writephy(tp, 0x13, 0x0400);
5414 udelay(40);
5415 tg3_writephy(tp, 0x13, 0x0000);
5416
5417 tg3_writephy(tp, 0x11, 0x0a50);
5418 udelay(40);
5419 tg3_writephy(tp, 0x11, 0x0a10);
5420
5421 /* Wait for signal to stabilize */
5422 /* XXX schedule_timeout() ... */
5423 for (i = 0; i < 15000; i++)
5424 udelay(10);
5425
5426 /* Deselect the channel register so we can read the PHYID
5427 * later.
5428 */
5429 tg3_writephy(tp, 0x10, 0x8011);
5430}
5431
Joe Perches953c96e2013-04-09 10:18:14 +00005432static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005434 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005435 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 u32 sg_dig_ctrl, sg_dig_status;
5437 u32 serdes_cfg, expected_sg_dig_ctrl;
5438 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
5440 serdes_cfg = 0;
5441 expected_sg_dig_ctrl = 0;
5442 workaround = 0;
5443 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005444 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Joe Perches41535772013-02-16 11:20:04 +00005446 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5447 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 workaround = 1;
5449 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5450 port_a = 0;
5451
5452 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5453 /* preserve bits 20-23 for voltage regulator */
5454 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5455 }
5456
5457 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5458
5459 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005460 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 if (workaround) {
5462 u32 val = serdes_cfg;
5463
5464 if (port_a)
5465 val |= 0xc010000;
5466 else
5467 val |= 0x4010000;
5468 tw32_f(MAC_SERDES_CFG, val);
5469 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005470
5471 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 }
5473 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5474 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005475 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 }
5477 goto out;
5478 }
5479
5480 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005481 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
Matt Carlson82cd3d12007-12-20 20:09:00 -08005483 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5484 if (flowctrl & ADVERTISE_1000XPAUSE)
5485 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5486 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5487 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
5489 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005490 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005491 tp->serdes_counter &&
5492 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5493 MAC_STATUS_RCVD_CFG)) ==
5494 MAC_STATUS_PCS_SYNCED)) {
5495 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005496 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005497 goto out;
5498 }
5499restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 if (workaround)
5501 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005502 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 udelay(5);
5504 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5505
Michael Chan3d3ebe72006-09-27 15:59:15 -07005506 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005507 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5509 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005510 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 mac_status = tr32(MAC_STATUS);
5512
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005513 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005515 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Matt Carlson82cd3d12007-12-20 20:09:00 -08005517 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5518 local_adv |= ADVERTISE_1000XPAUSE;
5519 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5520 local_adv |= ADVERTISE_1000XPSE_ASYM;
5521
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005522 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005523 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005524 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005525 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
Matt Carlson859edb22011-12-08 14:40:16 +00005527 tp->link_config.rmt_adv =
5528 mii_adv_to_ethtool_adv_x(remote_adv);
5529
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005531 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005532 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005533 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005534 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005535 if (tp->serdes_counter)
5536 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 else {
5538 if (workaround) {
5539 u32 val = serdes_cfg;
5540
5541 if (port_a)
5542 val |= 0xc010000;
5543 else
5544 val |= 0x4010000;
5545
5546 tw32_f(MAC_SERDES_CFG, val);
5547 }
5548
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005549 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 udelay(40);
5551
5552 /* Link parallel detection - link is up */
5553 /* only if we have PCS_SYNC and not */
5554 /* receiving config code words */
5555 mac_status = tr32(MAC_STATUS);
5556 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5557 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5558 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005559 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005560 tp->phy_flags |=
5561 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005562 tp->serdes_counter =
5563 SERDES_PARALLEL_DET_TIMEOUT;
5564 } else
5565 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 }
5567 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005568 } else {
5569 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005570 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 }
5572
5573out:
5574 return current_link_up;
5575}
5576
Joe Perches953c96e2013-04-09 10:18:14 +00005577static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578{
Joe Perches953c96e2013-04-09 10:18:14 +00005579 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005581 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
5584 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005585 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005587
Matt Carlson5be73b42007-12-20 20:09:29 -08005588 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5589 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
Matt Carlson5be73b42007-12-20 20:09:29 -08005591 if (txflags & ANEG_CFG_PS1)
5592 local_adv |= ADVERTISE_1000XPAUSE;
5593 if (txflags & ANEG_CFG_PS2)
5594 local_adv |= ADVERTISE_1000XPSE_ASYM;
5595
5596 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5597 remote_adv |= LPA_1000XPAUSE;
5598 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5599 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Matt Carlson859edb22011-12-08 14:40:16 +00005601 tp->link_config.rmt_adv =
5602 mii_adv_to_ethtool_adv_x(remote_adv);
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 tg3_setup_flow_control(tp, local_adv, remote_adv);
5605
Joe Perches953c96e2013-04-09 10:18:14 +00005606 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 }
5608 for (i = 0; i < 30; i++) {
5609 udelay(20);
5610 tw32_f(MAC_STATUS,
5611 (MAC_STATUS_SYNC_CHANGED |
5612 MAC_STATUS_CFG_CHANGED));
5613 udelay(40);
5614 if ((tr32(MAC_STATUS) &
5615 (MAC_STATUS_SYNC_CHANGED |
5616 MAC_STATUS_CFG_CHANGED)) == 0)
5617 break;
5618 }
5619
5620 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005621 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5623 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005624 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005626 tg3_setup_flow_control(tp, 0, 0);
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005629 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
5631 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5632 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005633
5634 tw32_f(MAC_MODE, tp->mac_mode);
5635 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 }
5637
5638out:
5639 return current_link_up;
5640}
5641
Joe Perches953c96e2013-04-09 10:18:14 +00005642static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
5644 u32 orig_pause_cfg;
5645 u16 orig_active_speed;
5646 u8 orig_active_duplex;
5647 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005648 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 int i;
5650
Matt Carlson8d018622007-12-20 20:05:44 -08005651 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 orig_active_speed = tp->link_config.active_speed;
5653 orig_active_duplex = tp->link_config.active_duplex;
5654
Joe Perches63c3a662011-04-26 08:12:10 +00005655 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005656 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005657 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 mac_status = tr32(MAC_STATUS);
5659 mac_status &= (MAC_STATUS_PCS_SYNCED |
5660 MAC_STATUS_SIGNAL_DET |
5661 MAC_STATUS_CFG_CHANGED |
5662 MAC_STATUS_RCVD_CFG);
5663 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5664 MAC_STATUS_SIGNAL_DET)) {
5665 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5666 MAC_STATUS_CFG_CHANGED));
5667 return 0;
5668 }
5669 }
5670
5671 tw32_f(MAC_TX_AUTO_NEG, 0);
5672
5673 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5674 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5675 tw32_f(MAC_MODE, tp->mac_mode);
5676 udelay(40);
5677
Matt Carlson79eb6902010-02-17 15:17:03 +00005678 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 tg3_init_bcm8002(tp);
5680
5681 /* Enable link change event even when serdes polling. */
5682 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5683 udelay(40);
5684
Joe Perches953c96e2013-04-09 10:18:14 +00005685 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005686 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 mac_status = tr32(MAC_STATUS);
5688
Joe Perches63c3a662011-04-26 08:12:10 +00005689 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5691 else
5692 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5693
Matt Carlson898a56f2009-08-28 14:02:40 +00005694 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005696 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 for (i = 0; i < 100; i++) {
5699 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5700 MAC_STATUS_CFG_CHANGED));
5701 udelay(5);
5702 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005703 MAC_STATUS_CFG_CHANGED |
5704 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 break;
5706 }
5707
5708 mac_status = tr32(MAC_STATUS);
5709 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005710 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005711 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5712 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 tw32_f(MAC_MODE, (tp->mac_mode |
5714 MAC_MODE_SEND_CONFIGS));
5715 udelay(1);
5716 tw32_f(MAC_MODE, tp->mac_mode);
5717 }
5718 }
5719
Joe Perches953c96e2013-04-09 10:18:14 +00005720 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 tp->link_config.active_speed = SPEED_1000;
5722 tp->link_config.active_duplex = DUPLEX_FULL;
5723 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5724 LED_CTRL_LNKLED_OVERRIDE |
5725 LED_CTRL_1000MBPS_ON));
5726 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005727 tp->link_config.active_speed = SPEED_UNKNOWN;
5728 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5730 LED_CTRL_LNKLED_OVERRIDE |
5731 LED_CTRL_TRAFFIC_OVERRIDE));
5732 }
5733
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005734 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005735 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 if (orig_pause_cfg != now_pause_cfg ||
5737 orig_active_speed != tp->link_config.active_speed ||
5738 orig_active_duplex != tp->link_config.active_duplex)
5739 tg3_link_report(tp);
5740 }
5741
5742 return 0;
5743}
5744
Joe Perches953c96e2013-04-09 10:18:14 +00005745static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005746{
Joe Perches953c96e2013-04-09 10:18:14 +00005747 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005748 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005749 u16 current_speed = SPEED_UNKNOWN;
5750 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005751 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005752 u32 local_adv, remote_adv, sgsr;
5753
5754 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5755 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5756 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5757 (sgsr & SERDES_TG3_SGMII_MODE)) {
5758
5759 if (force_reset)
5760 tg3_phy_reset(tp);
5761
5762 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5763
5764 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5765 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5766 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005767 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005768 if (sgsr & SERDES_TG3_SPEED_1000) {
5769 current_speed = SPEED_1000;
5770 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5771 } else if (sgsr & SERDES_TG3_SPEED_100) {
5772 current_speed = SPEED_100;
5773 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5774 } else {
5775 current_speed = SPEED_10;
5776 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5777 }
5778
5779 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5780 current_duplex = DUPLEX_FULL;
5781 else
5782 current_duplex = DUPLEX_HALF;
5783 }
5784
5785 tw32_f(MAC_MODE, tp->mac_mode);
5786 udelay(40);
5787
5788 tg3_clear_mac_status(tp);
5789
5790 goto fiber_setup_done;
5791 }
Michael Chan747e8f82005-07-25 12:33:22 -07005792
5793 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5794 tw32_f(MAC_MODE, tp->mac_mode);
5795 udelay(40);
5796
Michael Chan3310e242013-04-09 08:48:05 +00005797 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005798
5799 if (force_reset)
5800 tg3_phy_reset(tp);
5801
Matt Carlson859edb22011-12-08 14:40:16 +00005802 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005803
5804 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5805 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005806 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005807 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5808 bmsr |= BMSR_LSTATUS;
5809 else
5810 bmsr &= ~BMSR_LSTATUS;
5811 }
Michael Chan747e8f82005-07-25 12:33:22 -07005812
5813 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5814
5815 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005816 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005817 /* do nothing, just check for link up at the end */
5818 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005819 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005820
5821 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005822 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5823 ADVERTISE_1000XPAUSE |
5824 ADVERTISE_1000XPSE_ASYM |
5825 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005826
Matt Carlson28011cf2011-11-16 18:36:59 -05005827 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005828 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005829
Matt Carlson28011cf2011-11-16 18:36:59 -05005830 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5831 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005832 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5833 tg3_writephy(tp, MII_BMCR, bmcr);
5834
5835 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005836 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005837 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005838
5839 return err;
5840 }
5841 } else {
5842 u32 new_bmcr;
5843
5844 bmcr &= ~BMCR_SPEED1000;
5845 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5846
5847 if (tp->link_config.duplex == DUPLEX_FULL)
5848 new_bmcr |= BMCR_FULLDPLX;
5849
5850 if (new_bmcr != bmcr) {
5851 /* BMCR_SPEED1000 is a reserved bit that needs
5852 * to be set on write.
5853 */
5854 new_bmcr |= BMCR_SPEED1000;
5855
5856 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005857 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005858 u32 adv;
5859
5860 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5861 adv &= ~(ADVERTISE_1000XFULL |
5862 ADVERTISE_1000XHALF |
5863 ADVERTISE_SLCT);
5864 tg3_writephy(tp, MII_ADVERTISE, adv);
5865 tg3_writephy(tp, MII_BMCR, bmcr |
5866 BMCR_ANRESTART |
5867 BMCR_ANENABLE);
5868 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005869 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005870 }
5871 tg3_writephy(tp, MII_BMCR, new_bmcr);
5872 bmcr = new_bmcr;
5873 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5874 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005875 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005876 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5877 bmsr |= BMSR_LSTATUS;
5878 else
5879 bmsr &= ~BMSR_LSTATUS;
5880 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005881 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005882 }
5883 }
5884
5885 if (bmsr & BMSR_LSTATUS) {
5886 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005887 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005888 if (bmcr & BMCR_FULLDPLX)
5889 current_duplex = DUPLEX_FULL;
5890 else
5891 current_duplex = DUPLEX_HALF;
5892
Matt Carlsonef167e22007-12-20 20:10:01 -08005893 local_adv = 0;
5894 remote_adv = 0;
5895
Michael Chan747e8f82005-07-25 12:33:22 -07005896 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005897 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005898
5899 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5900 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5901 common = local_adv & remote_adv;
5902 if (common & (ADVERTISE_1000XHALF |
5903 ADVERTISE_1000XFULL)) {
5904 if (common & ADVERTISE_1000XFULL)
5905 current_duplex = DUPLEX_FULL;
5906 else
5907 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005908
5909 tp->link_config.rmt_adv =
5910 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005911 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005912 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005913 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005914 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005915 }
Michael Chan747e8f82005-07-25 12:33:22 -07005916 }
5917 }
5918
Michael Chan85730a62013-04-09 08:48:06 +00005919fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005920 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005921 tg3_setup_flow_control(tp, local_adv, remote_adv);
5922
Michael Chan747e8f82005-07-25 12:33:22 -07005923 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5924 if (tp->link_config.active_duplex == DUPLEX_HALF)
5925 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5926
5927 tw32_f(MAC_MODE, tp->mac_mode);
5928 udelay(40);
5929
5930 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5931
5932 tp->link_config.active_speed = current_speed;
5933 tp->link_config.active_duplex = current_duplex;
5934
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005935 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005936 return err;
5937}
5938
5939static void tg3_serdes_parallel_detect(struct tg3 *tp)
5940{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005941 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005942 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005943 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005944 return;
5945 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005946
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005947 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005948 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5949 u32 bmcr;
5950
5951 tg3_readphy(tp, MII_BMCR, &bmcr);
5952 if (bmcr & BMCR_ANENABLE) {
5953 u32 phy1, phy2;
5954
5955 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005956 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5957 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005958
5959 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005960 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5961 MII_TG3_DSP_EXP1_INT_STAT);
5962 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5963 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005964
5965 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5966 /* We have signal detect and not receiving
5967 * config code words, link is up by parallel
5968 * detection.
5969 */
5970
5971 bmcr &= ~BMCR_ANENABLE;
5972 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5973 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005974 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005975 }
5976 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005977 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005978 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005979 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005980 u32 phy2;
5981
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);
Michael Chan747e8f82005-07-25 12:33:22 -07005986 if (phy2 & 0x20) {
5987 u32 bmcr;
5988
5989 /* Config code words received, turn on autoneg. */
5990 tg3_readphy(tp, MII_BMCR, &bmcr);
5991 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5992
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005993 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005994
5995 }
5996 }
5997}
5998
Joe Perches953c96e2013-04-09 10:18:14 +00005999static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006001 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 int err;
6003
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006004 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006006 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006007 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006008 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Joe Perches41535772013-02-16 11:20:04 +00006011 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006012 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006013
6014 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6015 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6016 scale = 65;
6017 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6018 scale = 6;
6019 else
6020 scale = 12;
6021
6022 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6023 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6024 tw32(GRC_MISC_CFG, val);
6025 }
6026
Matt Carlsonf2096f92011-04-05 14:22:48 +00006027 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6028 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006029 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6030 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006031 val |= tr32(MAC_TX_LENGTHS) &
6032 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6033 TX_LENGTHS_CNT_DWN_VAL_MSK);
6034
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 if (tp->link_config.active_speed == SPEED_1000 &&
6036 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006037 tw32(MAC_TX_LENGTHS, val |
6038 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006040 tw32(MAC_TX_LENGTHS, val |
6041 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
Joe Perches63c3a662011-04-26 08:12:10 +00006043 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006044 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006046 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 } else {
6048 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6049 }
6050 }
6051
Joe Perches63c3a662011-04-26 08:12:10 +00006052 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006053 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006054 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006055 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6056 tp->pwrmgmt_thresh;
6057 else
6058 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6059 tw32(PCIE_PWR_MGMT_THRESH, val);
6060 }
6061
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 return err;
6063}
6064
Matt Carlsonbe947302012-12-03 19:36:57 +00006065/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006066static u64 tg3_refclk_read(struct tg3 *tp)
6067{
6068 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6069 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6070}
6071
6072/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006073static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6074{
6075 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
6076 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6077 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
6078 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
6079}
6080
Matt Carlson7d41e492012-12-03 19:36:58 +00006081static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6082static inline void tg3_full_unlock(struct tg3 *tp);
6083static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6084{
6085 struct tg3 *tp = netdev_priv(dev);
6086
6087 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6088 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006089 SOF_TIMESTAMPING_SOFTWARE;
6090
6091 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006092 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006093 SOF_TIMESTAMPING_RX_HARDWARE |
6094 SOF_TIMESTAMPING_RAW_HARDWARE;
6095 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006096
6097 if (tp->ptp_clock)
6098 info->phc_index = ptp_clock_index(tp->ptp_clock);
6099 else
6100 info->phc_index = -1;
6101
6102 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6103
6104 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6105 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6106 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6107 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6108 return 0;
6109}
6110
6111static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6112{
6113 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6114 bool neg_adj = false;
6115 u32 correction = 0;
6116
6117 if (ppb < 0) {
6118 neg_adj = true;
6119 ppb = -ppb;
6120 }
6121
6122 /* Frequency adjustment is performed using hardware with a 24 bit
6123 * accumulator and a programmable correction value. On each clk, the
6124 * correction value gets added to the accumulator and when it
6125 * overflows, the time counter is incremented/decremented.
6126 *
6127 * So conversion from ppb to correction value is
6128 * ppb * (1 << 24) / 1000000000
6129 */
6130 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6131 TG3_EAV_REF_CLK_CORRECT_MASK;
6132
6133 tg3_full_lock(tp, 0);
6134
6135 if (correction)
6136 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6137 TG3_EAV_REF_CLK_CORRECT_EN |
6138 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6139 else
6140 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6141
6142 tg3_full_unlock(tp);
6143
6144 return 0;
6145}
6146
6147static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6148{
6149 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6150
6151 tg3_full_lock(tp, 0);
6152 tp->ptp_adjust += delta;
6153 tg3_full_unlock(tp);
6154
6155 return 0;
6156}
6157
6158static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6159{
6160 u64 ns;
6161 u32 remainder;
6162 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6163
6164 tg3_full_lock(tp, 0);
6165 ns = tg3_refclk_read(tp);
6166 ns += tp->ptp_adjust;
6167 tg3_full_unlock(tp);
6168
6169 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6170 ts->tv_nsec = remainder;
6171
6172 return 0;
6173}
6174
6175static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6176 const struct timespec *ts)
6177{
6178 u64 ns;
6179 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6180
6181 ns = timespec_to_ns(ts);
6182
6183 tg3_full_lock(tp, 0);
6184 tg3_refclk_write(tp, ns);
6185 tp->ptp_adjust = 0;
6186 tg3_full_unlock(tp);
6187
6188 return 0;
6189}
6190
6191static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6192 struct ptp_clock_request *rq, int on)
6193{
6194 return -EOPNOTSUPP;
6195}
6196
6197static const struct ptp_clock_info tg3_ptp_caps = {
6198 .owner = THIS_MODULE,
6199 .name = "tg3 clock",
6200 .max_adj = 250000000,
6201 .n_alarm = 0,
6202 .n_ext_ts = 0,
6203 .n_per_out = 0,
6204 .pps = 0,
6205 .adjfreq = tg3_ptp_adjfreq,
6206 .adjtime = tg3_ptp_adjtime,
6207 .gettime = tg3_ptp_gettime,
6208 .settime = tg3_ptp_settime,
6209 .enable = tg3_ptp_enable,
6210};
6211
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006212static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6213 struct skb_shared_hwtstamps *timestamp)
6214{
6215 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6216 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6217 tp->ptp_adjust);
6218}
6219
Matt Carlsonbe947302012-12-03 19:36:57 +00006220/* tp->lock must be held */
6221static void tg3_ptp_init(struct tg3 *tp)
6222{
6223 if (!tg3_flag(tp, PTP_CAPABLE))
6224 return;
6225
6226 /* Initialize the hardware clock to the system time. */
6227 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6228 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006229 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006230}
6231
6232/* tp->lock must be held */
6233static void tg3_ptp_resume(struct tg3 *tp)
6234{
6235 if (!tg3_flag(tp, PTP_CAPABLE))
6236 return;
6237
6238 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6239 tp->ptp_adjust = 0;
6240}
6241
6242static void tg3_ptp_fini(struct tg3 *tp)
6243{
6244 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6245 return;
6246
Matt Carlson7d41e492012-12-03 19:36:58 +00006247 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006248 tp->ptp_clock = NULL;
6249 tp->ptp_adjust = 0;
6250}
6251
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006252static inline int tg3_irq_sync(struct tg3 *tp)
6253{
6254 return tp->irq_sync;
6255}
6256
Matt Carlson97bd8e42011-04-13 11:05:04 +00006257static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6258{
6259 int i;
6260
6261 dst = (u32 *)((u8 *)dst + off);
6262 for (i = 0; i < len; i += sizeof(u32))
6263 *dst++ = tr32(off + i);
6264}
6265
6266static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6267{
6268 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6269 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6270 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6271 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6272 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6273 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6274 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6275 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6276 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6277 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6278 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6279 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6280 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6281 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6282 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6283 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6284 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6285 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6286 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6287
Joe Perches63c3a662011-04-26 08:12:10 +00006288 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006289 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6290
6291 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6292 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6293 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6294 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6295 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6296 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6297 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6298 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6299
Joe Perches63c3a662011-04-26 08:12:10 +00006300 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006301 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6302 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6303 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6304 }
6305
6306 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6307 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6308 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6309 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6310 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6311
Joe Perches63c3a662011-04-26 08:12:10 +00006312 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006313 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6314}
6315
6316static void tg3_dump_state(struct tg3 *tp)
6317{
6318 int i;
6319 u32 *regs;
6320
6321 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006322 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006323 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006324
Joe Perches63c3a662011-04-26 08:12:10 +00006325 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006326 /* Read up to but not including private PCI registers */
6327 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6328 regs[i / sizeof(u32)] = tr32(i);
6329 } else
6330 tg3_dump_legacy_regs(tp, regs);
6331
6332 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6333 if (!regs[i + 0] && !regs[i + 1] &&
6334 !regs[i + 2] && !regs[i + 3])
6335 continue;
6336
6337 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6338 i * 4,
6339 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6340 }
6341
6342 kfree(regs);
6343
6344 for (i = 0; i < tp->irq_cnt; i++) {
6345 struct tg3_napi *tnapi = &tp->napi[i];
6346
6347 /* SW status block */
6348 netdev_err(tp->dev,
6349 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6350 i,
6351 tnapi->hw_status->status,
6352 tnapi->hw_status->status_tag,
6353 tnapi->hw_status->rx_jumbo_consumer,
6354 tnapi->hw_status->rx_consumer,
6355 tnapi->hw_status->rx_mini_consumer,
6356 tnapi->hw_status->idx[0].rx_producer,
6357 tnapi->hw_status->idx[0].tx_consumer);
6358
6359 netdev_err(tp->dev,
6360 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6361 i,
6362 tnapi->last_tag, tnapi->last_irq_tag,
6363 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6364 tnapi->rx_rcb_ptr,
6365 tnapi->prodring.rx_std_prod_idx,
6366 tnapi->prodring.rx_std_cons_idx,
6367 tnapi->prodring.rx_jmb_prod_idx,
6368 tnapi->prodring.rx_jmb_cons_idx);
6369 }
6370}
6371
Michael Chandf3e6542006-05-26 17:48:07 -07006372/* This is called whenever we suspect that the system chipset is re-
6373 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6374 * is bogus tx completions. We try to recover by setting the
6375 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6376 * in the workqueue.
6377 */
6378static void tg3_tx_recover(struct tg3 *tp)
6379{
Joe Perches63c3a662011-04-26 08:12:10 +00006380 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006381 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6382
Matt Carlson5129c3a2010-04-05 10:19:23 +00006383 netdev_warn(tp->dev,
6384 "The system may be re-ordering memory-mapped I/O "
6385 "cycles to the network device, attempting to recover. "
6386 "Please report the problem to the driver maintainer "
6387 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006388
6389 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006390 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006391 spin_unlock(&tp->lock);
6392}
6393
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006394static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006395{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006396 /* Tell compiler to fetch tx indices from memory. */
6397 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006398 return tnapi->tx_pending -
6399 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006400}
6401
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402/* Tigon3 never reports partial packet sends. So we do not
6403 * need special logic to handle SKBs that have not had all
6404 * of their frags sent yet, like SunGEM does.
6405 */
Matt Carlson17375d22009-08-28 14:02:18 +00006406static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407{
Matt Carlson17375d22009-08-28 14:02:18 +00006408 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006409 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006410 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006411 struct netdev_queue *txq;
6412 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006413 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006414
Joe Perches63c3a662011-04-26 08:12:10 +00006415 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006416 index--;
6417
6418 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419
6420 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006421 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006423 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424
Michael Chandf3e6542006-05-26 17:48:07 -07006425 if (unlikely(skb == NULL)) {
6426 tg3_tx_recover(tp);
6427 return;
6428 }
6429
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006430 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6431 struct skb_shared_hwtstamps timestamp;
6432 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6433 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6434
6435 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6436
6437 skb_tstamp_tx(skb, &timestamp);
6438 }
6439
Alexander Duyckf4188d82009-12-02 16:48:38 +00006440 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006441 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006442 skb_headlen(skb),
6443 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444
6445 ri->skb = NULL;
6446
Matt Carlsone01ee142011-07-27 14:20:50 +00006447 while (ri->fragmented) {
6448 ri->fragmented = false;
6449 sw_idx = NEXT_TX(sw_idx);
6450 ri = &tnapi->tx_buffers[sw_idx];
6451 }
6452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 sw_idx = NEXT_TX(sw_idx);
6454
6455 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006456 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006457 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6458 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006459
6460 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006461 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006462 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006463 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006464
6465 while (ri->fragmented) {
6466 ri->fragmented = false;
6467 sw_idx = NEXT_TX(sw_idx);
6468 ri = &tnapi->tx_buffers[sw_idx];
6469 }
6470
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 sw_idx = NEXT_TX(sw_idx);
6472 }
6473
Tom Herbert298376d2011-11-28 16:33:30 +00006474 pkts_compl++;
6475 bytes_compl += skb->len;
6476
David S. Millerf47c11e2005-06-24 20:18:35 -07006477 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006478
6479 if (unlikely(tx_bug)) {
6480 tg3_tx_recover(tp);
6481 return;
6482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 }
6484
Tom Herbert5cb917b2012-03-05 19:53:50 +00006485 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006486
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006487 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
Michael Chan1b2a7202006-08-07 21:46:02 -07006489 /* Need to make the tx_cons update visible to tg3_start_xmit()
6490 * before checking for netif_queue_stopped(). Without the
6491 * memory barrier, there is a small possibility that tg3_start_xmit()
6492 * will miss it and cause the queue to be stopped forever.
6493 */
6494 smp_mb();
6495
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006496 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006497 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006498 __netif_tx_lock(txq, smp_processor_id());
6499 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006500 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006501 netif_tx_wake_queue(txq);
6502 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504}
6505
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006506static void tg3_frag_free(bool is_frag, void *data)
6507{
6508 if (is_frag)
6509 put_page(virt_to_head_page(data));
6510 else
6511 kfree(data);
6512}
6513
Eric Dumazet9205fd92011-11-18 06:47:01 +00006514static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006515{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006516 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6517 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6518
Eric Dumazet9205fd92011-11-18 06:47:01 +00006519 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006520 return;
6521
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006522 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006523 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006524 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006525 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006526}
6527
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006528
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529/* Returns size of skb allocated or < 0 on error.
6530 *
6531 * We only need to fill in the address because the other members
6532 * of the RX descriptor are invariant, see tg3_init_rings.
6533 *
6534 * Note the purposeful assymetry of cpu vs. chip accesses. For
6535 * posting buffers we only dirty the first cache line of the RX
6536 * descriptor (containing the address). Whereas for the RX status
6537 * buffers the cpu only reads the last cacheline of the RX descriptor
6538 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6539 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006540static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006541 u32 opaque_key, u32 dest_idx_unmasked,
6542 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543{
6544 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006545 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006546 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006548 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 switch (opaque_key) {
6551 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006552 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006553 desc = &tpr->rx_std[dest_idx];
6554 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006555 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 break;
6557
6558 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006559 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006560 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006561 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006562 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 break;
6564
6565 default:
6566 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568
6569 /* Do not overwrite any of the map or rp information
6570 * until we are sure we can commit to a new buffer.
6571 *
6572 * Callers depend upon this behavior and assume that
6573 * we leave everything unchanged if we fail.
6574 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006575 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6576 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006577 if (skb_size <= PAGE_SIZE) {
6578 data = netdev_alloc_frag(skb_size);
6579 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006580 } else {
6581 data = kmalloc(skb_size, GFP_ATOMIC);
6582 *frag_size = 0;
6583 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006584 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 return -ENOMEM;
6586
Eric Dumazet9205fd92011-11-18 06:47:01 +00006587 mapping = pci_map_single(tp->pdev,
6588 data + TG3_RX_OFFSET(tp),
6589 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006591 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006592 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006593 return -EIO;
6594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
Eric Dumazet9205fd92011-11-18 06:47:01 +00006596 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006597 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 desc->addr_hi = ((u64)mapping >> 32);
6600 desc->addr_lo = ((u64)mapping & 0xffffffff);
6601
Eric Dumazet9205fd92011-11-18 06:47:01 +00006602 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603}
6604
6605/* We only need to move over in the address because the other
6606 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006607 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 */
Matt Carlsona3896162009-11-13 13:03:44 +00006609static void tg3_recycle_rx(struct tg3_napi *tnapi,
6610 struct tg3_rx_prodring_set *dpr,
6611 u32 opaque_key, int src_idx,
6612 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613{
Matt Carlson17375d22009-08-28 14:02:18 +00006614 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6616 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006617 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006618 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619
6620 switch (opaque_key) {
6621 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006622 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006623 dest_desc = &dpr->rx_std[dest_idx];
6624 dest_map = &dpr->rx_std_buffers[dest_idx];
6625 src_desc = &spr->rx_std[src_idx];
6626 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 break;
6628
6629 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006630 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006631 dest_desc = &dpr->rx_jmb[dest_idx].std;
6632 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6633 src_desc = &spr->rx_jmb[src_idx].std;
6634 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 break;
6636
6637 default:
6638 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
Eric Dumazet9205fd92011-11-18 06:47:01 +00006641 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006642 dma_unmap_addr_set(dest_map, mapping,
6643 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 dest_desc->addr_hi = src_desc->addr_hi;
6645 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006646
6647 /* Ensure that the update to the skb happens after the physical
6648 * addresses have been transferred to the new BD location.
6649 */
6650 smp_wmb();
6651
Eric Dumazet9205fd92011-11-18 06:47:01 +00006652 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653}
6654
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655/* The RX ring scheme is composed of multiple rings which post fresh
6656 * buffers to the chip, and one special ring the chip uses to report
6657 * status back to the host.
6658 *
6659 * The special ring reports the status of received packets to the
6660 * host. The chip does not write into the original descriptor the
6661 * RX buffer was obtained from. The chip simply takes the original
6662 * descriptor as provided by the host, updates the status and length
6663 * field, then writes this into the next status ring entry.
6664 *
6665 * Each ring the host uses to post buffers to the chip is described
6666 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6667 * it is first placed into the on-chip ram. When the packet's length
6668 * is known, it walks down the TG3_BDINFO entries to select the ring.
6669 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6670 * which is within the range of the new packet's length is chosen.
6671 *
6672 * The "separate ring for rx status" scheme may sound queer, but it makes
6673 * sense from a cache coherency perspective. If only the host writes
6674 * to the buffer post rings, and only the chip writes to the rx status
6675 * rings, then cache lines never move beyond shared-modified state.
6676 * If both the host and chip were to write into the same ring, cache line
6677 * eviction could occur since both entities want it in an exclusive state.
6678 */
Matt Carlson17375d22009-08-28 14:02:18 +00006679static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680{
Matt Carlson17375d22009-08-28 14:02:18 +00006681 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006682 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006683 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006684 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006685 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006687 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006689 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 /*
6691 * We need to order the read of hw_idx and the read of
6692 * the opaque cookie.
6693 */
6694 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 work_mask = 0;
6696 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006697 std_prod_idx = tpr->rx_std_prod_idx;
6698 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006700 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006701 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 unsigned int len;
6703 struct sk_buff *skb;
6704 dma_addr_t dma_addr;
6705 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006706 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006707 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
6709 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6710 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6711 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006712 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006713 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006714 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006715 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006716 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006718 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006719 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006720 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006721 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006722 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
6725 work_mask |= opaque_key;
6726
6727 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6728 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6729 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006730 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 desc_idx, *post_ptr);
6732 drop_it_no_recycle:
6733 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006734 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 goto next_pkt;
6736 }
6737
Eric Dumazet9205fd92011-11-18 06:47:01 +00006738 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006739 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6740 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006742 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6743 RXD_FLAG_PTPSTAT_PTPV1 ||
6744 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6745 RXD_FLAG_PTPSTAT_PTPV2) {
6746 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6747 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6748 }
6749
Matt Carlsond2757fc2010-04-12 06:58:27 +00006750 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006752 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
Eric Dumazet9205fd92011-11-18 06:47:01 +00006754 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006755 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 if (skb_size < 0)
6757 goto drop_it;
6758
Matt Carlson287be122009-08-28 13:58:46 +00006759 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 PCI_DMA_FROMDEVICE);
6761
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006762 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006763 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006764 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006765 goto drop_it_no_recycle;
6766 }
6767 skb_reserve(skb, TG3_RX_OFFSET(tp));
6768 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006769 * after the usage of the old DMA mapping.
6770 */
6771 smp_wmb();
6772
Eric Dumazet9205fd92011-11-18 06:47:01 +00006773 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006774
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006776 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 desc_idx, *post_ptr);
6778
Eric Dumazet9205fd92011-11-18 06:47:01 +00006779 skb = netdev_alloc_skb(tp->dev,
6780 len + TG3_RAW_IP_ALIGN);
6781 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 goto drop_it_no_recycle;
6783
Eric Dumazet9205fd92011-11-18 06:47:01 +00006784 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006786 memcpy(skb->data,
6787 data + TG3_RX_OFFSET(tp),
6788 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 }
6791
Eric Dumazet9205fd92011-11-18 06:47:01 +00006792 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006793 if (tstamp)
6794 tg3_hwclock_to_timestamp(tp, tstamp,
6795 skb_hwtstamps(skb));
6796
Michał Mirosławdc668912011-04-07 03:35:07 +00006797 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6799 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6800 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6801 skb->ip_summed = CHECKSUM_UNNECESSARY;
6802 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006803 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
6805 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006806
6807 if (len > (tp->dev->mtu + ETH_HLEN) &&
6808 skb->protocol != htons(ETH_P_8021Q)) {
6809 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006810 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006811 }
6812
Matt Carlson9dc7a112010-04-12 06:58:28 +00006813 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006814 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006815 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006816 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006817
Matt Carlsonbf933c82011-01-25 15:58:49 +00006818 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 received++;
6821 budget--;
6822
6823next_pkt:
6824 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006825
6826 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006827 tpr->rx_std_prod_idx = std_prod_idx &
6828 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006829 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6830 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006831 work_mask &= ~RXD_OPAQUE_RING_STD;
6832 rx_std_posted = 0;
6833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006835 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006836 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006837
6838 /* Refresh hw_idx to see if there is new work */
6839 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006840 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006841 rmb();
6842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 }
6844
6845 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006846 tnapi->rx_rcb_ptr = sw_idx;
6847 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
6849 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006850 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006851 /* Sync BD data before updating mailbox */
6852 wmb();
6853
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006854 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006855 tpr->rx_std_prod_idx = std_prod_idx &
6856 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006857 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6858 tpr->rx_std_prod_idx);
6859 }
6860 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006861 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6862 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006863 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6864 tpr->rx_jmb_prod_idx);
6865 }
6866 mmiowb();
6867 } else if (work_mask) {
6868 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6869 * updated before the producer indices can be updated.
6870 */
6871 smp_wmb();
6872
Matt Carlson2c49a442010-09-30 10:34:35 +00006873 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6874 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006875
Michael Chan7ae52892012-03-21 15:38:33 +00006876 if (tnapi != &tp->napi[1]) {
6877 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006878 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
6882 return received;
6883}
6884
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006885static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006888 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006889 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6890
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 if (sblk->status & SD_STATUS_LINK_CHG) {
6892 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006893 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006894 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006895 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006896 tw32_f(MAC_STATUS,
6897 (MAC_STATUS_SYNC_CHANGED |
6898 MAC_STATUS_CFG_CHANGED |
6899 MAC_STATUS_MI_COMPLETION |
6900 MAC_STATUS_LNKSTATE_CHANGED));
6901 udelay(40);
6902 } else
Joe Perches953c96e2013-04-09 10:18:14 +00006903 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07006904 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 }
6906 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006907}
6908
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006909static int tg3_rx_prodring_xfer(struct tg3 *tp,
6910 struct tg3_rx_prodring_set *dpr,
6911 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006912{
6913 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006914 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006915
6916 while (1) {
6917 src_prod_idx = spr->rx_std_prod_idx;
6918
6919 /* Make sure updates to the rx_std_buffers[] entries and the
6920 * standard producer index are seen in the correct order.
6921 */
6922 smp_rmb();
6923
6924 if (spr->rx_std_cons_idx == src_prod_idx)
6925 break;
6926
6927 if (spr->rx_std_cons_idx < src_prod_idx)
6928 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6929 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006930 cpycnt = tp->rx_std_ring_mask + 1 -
6931 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006932
Matt Carlson2c49a442010-09-30 10:34:35 +00006933 cpycnt = min(cpycnt,
6934 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006935
6936 si = spr->rx_std_cons_idx;
6937 di = dpr->rx_std_prod_idx;
6938
Matt Carlsone92967b2010-02-12 14:47:06 +00006939 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006940 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006941 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006942 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006943 break;
6944 }
6945 }
6946
6947 if (!cpycnt)
6948 break;
6949
6950 /* Ensure that updates to the rx_std_buffers ring and the
6951 * shadowed hardware producer ring from tg3_recycle_skb() are
6952 * ordered correctly WRT the skb check above.
6953 */
6954 smp_rmb();
6955
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006956 memcpy(&dpr->rx_std_buffers[di],
6957 &spr->rx_std_buffers[si],
6958 cpycnt * sizeof(struct ring_info));
6959
6960 for (i = 0; i < cpycnt; i++, di++, si++) {
6961 struct tg3_rx_buffer_desc *sbd, *dbd;
6962 sbd = &spr->rx_std[si];
6963 dbd = &dpr->rx_std[di];
6964 dbd->addr_hi = sbd->addr_hi;
6965 dbd->addr_lo = sbd->addr_lo;
6966 }
6967
Matt Carlson2c49a442010-09-30 10:34:35 +00006968 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6969 tp->rx_std_ring_mask;
6970 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6971 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006972 }
6973
6974 while (1) {
6975 src_prod_idx = spr->rx_jmb_prod_idx;
6976
6977 /* Make sure updates to the rx_jmb_buffers[] entries and
6978 * the jumbo producer index are seen in the correct order.
6979 */
6980 smp_rmb();
6981
6982 if (spr->rx_jmb_cons_idx == src_prod_idx)
6983 break;
6984
6985 if (spr->rx_jmb_cons_idx < src_prod_idx)
6986 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6987 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006988 cpycnt = tp->rx_jmb_ring_mask + 1 -
6989 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006990
6991 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006992 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006993
6994 si = spr->rx_jmb_cons_idx;
6995 di = dpr->rx_jmb_prod_idx;
6996
Matt Carlsone92967b2010-02-12 14:47:06 +00006997 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006998 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006999 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007000 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007001 break;
7002 }
7003 }
7004
7005 if (!cpycnt)
7006 break;
7007
7008 /* Ensure that updates to the rx_jmb_buffers ring and the
7009 * shadowed hardware producer ring from tg3_recycle_skb() are
7010 * ordered correctly WRT the skb check above.
7011 */
7012 smp_rmb();
7013
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007014 memcpy(&dpr->rx_jmb_buffers[di],
7015 &spr->rx_jmb_buffers[si],
7016 cpycnt * sizeof(struct ring_info));
7017
7018 for (i = 0; i < cpycnt; i++, di++, si++) {
7019 struct tg3_rx_buffer_desc *sbd, *dbd;
7020 sbd = &spr->rx_jmb[si].std;
7021 dbd = &dpr->rx_jmb[di].std;
7022 dbd->addr_hi = sbd->addr_hi;
7023 dbd->addr_lo = sbd->addr_lo;
7024 }
7025
Matt Carlson2c49a442010-09-30 10:34:35 +00007026 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7027 tp->rx_jmb_ring_mask;
7028 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7029 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007030 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007031
7032 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007033}
7034
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007035static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7036{
7037 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038
7039 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007040 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007041 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007042 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007043 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 }
7045
Matt Carlsonf891ea12012-04-24 13:37:01 +00007046 if (!tnapi->rx_rcb_prod_idx)
7047 return work_done;
7048
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 /* run RX thread, within the bounds set by NAPI.
7050 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007051 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007053 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007054 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055
Joe Perches63c3a662011-04-26 08:12:10 +00007056 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007057 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007058 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007059 u32 std_prod_idx = dpr->rx_std_prod_idx;
7060 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007061
Michael Chan7ae52892012-03-21 15:38:33 +00007062 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007063 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007064 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007065 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007066
7067 wmb();
7068
Matt Carlsone4af1af2010-02-12 14:47:05 +00007069 if (std_prod_idx != dpr->rx_std_prod_idx)
7070 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7071 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007072
Matt Carlsone4af1af2010-02-12 14:47:05 +00007073 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7074 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7075 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007076
7077 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007078
7079 if (err)
7080 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007081 }
7082
David S. Miller6f535762007-10-11 18:08:29 -07007083 return work_done;
7084}
David S. Millerf7383c22005-05-18 22:50:53 -07007085
Matt Carlsondb219972011-11-04 09:15:03 +00007086static inline void tg3_reset_task_schedule(struct tg3 *tp)
7087{
7088 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7089 schedule_work(&tp->reset_task);
7090}
7091
7092static inline void tg3_reset_task_cancel(struct tg3 *tp)
7093{
7094 cancel_work_sync(&tp->reset_task);
7095 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007096 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007097}
7098
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007099static int tg3_poll_msix(struct napi_struct *napi, int budget)
7100{
7101 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7102 struct tg3 *tp = tnapi->tp;
7103 int work_done = 0;
7104 struct tg3_hw_status *sblk = tnapi->hw_status;
7105
7106 while (1) {
7107 work_done = tg3_poll_work(tnapi, work_done, budget);
7108
Joe Perches63c3a662011-04-26 08:12:10 +00007109 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007110 goto tx_recovery;
7111
7112 if (unlikely(work_done >= budget))
7113 break;
7114
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007115 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007116 * to tell the hw how much work has been processed,
7117 * so we must read it before checking for more work.
7118 */
7119 tnapi->last_tag = sblk->status_tag;
7120 tnapi->last_irq_tag = tnapi->last_tag;
7121 rmb();
7122
7123 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007124 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7125 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007126
7127 /* This test here is not race free, but will reduce
7128 * the number of interrupts by looping again.
7129 */
7130 if (tnapi == &tp->napi[1] && tp->rx_refill)
7131 continue;
7132
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007133 napi_complete(napi);
7134 /* Reenable interrupts. */
7135 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007136
7137 /* This test here is synchronized by napi_schedule()
7138 * and napi_complete() to close the race condition.
7139 */
7140 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7141 tw32(HOSTCC_MODE, tp->coalesce_mode |
7142 HOSTCC_MODE_ENABLE |
7143 tnapi->coal_now);
7144 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007145 mmiowb();
7146 break;
7147 }
7148 }
7149
7150 return work_done;
7151
7152tx_recovery:
7153 /* work_done is guaranteed to be less than budget. */
7154 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007155 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007156 return work_done;
7157}
7158
Matt Carlsone64de4e2011-04-13 11:05:05 +00007159static void tg3_process_error(struct tg3 *tp)
7160{
7161 u32 val;
7162 bool real_error = false;
7163
Joe Perches63c3a662011-04-26 08:12:10 +00007164 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007165 return;
7166
7167 /* Check Flow Attention register */
7168 val = tr32(HOSTCC_FLOW_ATTN);
7169 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7170 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7171 real_error = true;
7172 }
7173
7174 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7175 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7176 real_error = true;
7177 }
7178
7179 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7180 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7181 real_error = true;
7182 }
7183
7184 if (!real_error)
7185 return;
7186
7187 tg3_dump_state(tp);
7188
Joe Perches63c3a662011-04-26 08:12:10 +00007189 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007190 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007191}
7192
David S. Miller6f535762007-10-11 18:08:29 -07007193static int tg3_poll(struct napi_struct *napi, int budget)
7194{
Matt Carlson8ef04422009-08-28 14:01:37 +00007195 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7196 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007197 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007198 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007199
7200 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007201 if (sblk->status & SD_STATUS_ERROR)
7202 tg3_process_error(tp);
7203
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007204 tg3_poll_link(tp);
7205
Matt Carlson17375d22009-08-28 14:02:18 +00007206 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007207
Joe Perches63c3a662011-04-26 08:12:10 +00007208 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007209 goto tx_recovery;
7210
7211 if (unlikely(work_done >= budget))
7212 break;
7213
Joe Perches63c3a662011-04-26 08:12:10 +00007214 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007215 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007216 * to tell the hw how much work has been processed,
7217 * so we must read it before checking for more work.
7218 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007219 tnapi->last_tag = sblk->status_tag;
7220 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007221 rmb();
7222 } else
7223 sblk->status &= ~SD_STATUS_UPDATED;
7224
Matt Carlson17375d22009-08-28 14:02:18 +00007225 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007226 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007227 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007228 break;
7229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 }
7231
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007232 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007233
7234tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007235 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007236 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007237 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007238 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239}
7240
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007241static void tg3_napi_disable(struct tg3 *tp)
7242{
7243 int i;
7244
7245 for (i = tp->irq_cnt - 1; i >= 0; i--)
7246 napi_disable(&tp->napi[i].napi);
7247}
7248
7249static void tg3_napi_enable(struct tg3 *tp)
7250{
7251 int i;
7252
7253 for (i = 0; i < tp->irq_cnt; i++)
7254 napi_enable(&tp->napi[i].napi);
7255}
7256
7257static void tg3_napi_init(struct tg3 *tp)
7258{
7259 int i;
7260
7261 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7262 for (i = 1; i < tp->irq_cnt; i++)
7263 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7264}
7265
7266static void tg3_napi_fini(struct tg3 *tp)
7267{
7268 int i;
7269
7270 for (i = 0; i < tp->irq_cnt; i++)
7271 netif_napi_del(&tp->napi[i].napi);
7272}
7273
7274static inline void tg3_netif_stop(struct tg3 *tp)
7275{
7276 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7277 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007278 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007279 netif_tx_disable(tp->dev);
7280}
7281
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007282/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007283static inline void tg3_netif_start(struct tg3 *tp)
7284{
Matt Carlsonbe947302012-12-03 19:36:57 +00007285 tg3_ptp_resume(tp);
7286
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007287 /* NOTE: unconditional netif_tx_wake_all_queues is only
7288 * appropriate so long as all callers are assured to
7289 * have free tx slots (such as after tg3_init_hw)
7290 */
7291 netif_tx_wake_all_queues(tp->dev);
7292
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007293 if (tp->link_up)
7294 netif_carrier_on(tp->dev);
7295
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007296 tg3_napi_enable(tp);
7297 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7298 tg3_enable_ints(tp);
7299}
7300
David S. Millerf47c11e2005-06-24 20:18:35 -07007301static void tg3_irq_quiesce(struct tg3 *tp)
7302{
Matt Carlson4f125f42009-09-01 12:55:02 +00007303 int i;
7304
David S. Millerf47c11e2005-06-24 20:18:35 -07007305 BUG_ON(tp->irq_sync);
7306
7307 tp->irq_sync = 1;
7308 smp_mb();
7309
Matt Carlson4f125f42009-09-01 12:55:02 +00007310 for (i = 0; i < tp->irq_cnt; i++)
7311 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007312}
7313
David S. Millerf47c11e2005-06-24 20:18:35 -07007314/* Fully shutdown all tg3 driver activity elsewhere in the system.
7315 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7316 * with as well. Most of the time, this is not necessary except when
7317 * shutting down the device.
7318 */
7319static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7320{
Michael Chan46966542007-07-11 19:47:19 -07007321 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007322 if (irq_sync)
7323 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007324}
7325
7326static inline void tg3_full_unlock(struct tg3 *tp)
7327{
David S. Millerf47c11e2005-06-24 20:18:35 -07007328 spin_unlock_bh(&tp->lock);
7329}
7330
Michael Chanfcfa0a32006-03-20 22:28:41 -08007331/* One-shot MSI handler - Chip automatically disables interrupt
7332 * after sending MSI so driver doesn't have to do it.
7333 */
David Howells7d12e782006-10-05 14:55:46 +01007334static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007335{
Matt Carlson09943a12009-08-28 14:01:57 +00007336 struct tg3_napi *tnapi = dev_id;
7337 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007338
Matt Carlson898a56f2009-08-28 14:02:40 +00007339 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007340 if (tnapi->rx_rcb)
7341 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007342
7343 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007344 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007345
7346 return IRQ_HANDLED;
7347}
7348
Michael Chan88b06bc22005-04-21 17:13:25 -07007349/* MSI ISR - No need to check for interrupt sharing and no need to
7350 * flush status block and interrupt mailbox. PCI ordering rules
7351 * guarantee that MSI will arrive after the status block.
7352 */
David Howells7d12e782006-10-05 14:55:46 +01007353static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007354{
Matt Carlson09943a12009-08-28 14:01:57 +00007355 struct tg3_napi *tnapi = dev_id;
7356 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007357
Matt Carlson898a56f2009-08-28 14:02:40 +00007358 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007359 if (tnapi->rx_rcb)
7360 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007361 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007362 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007363 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007364 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007365 * NIC to stop sending us irqs, engaging "in-intr-handler"
7366 * event coalescing.
7367 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007368 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007369 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007370 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007371
Michael Chan88b06bc22005-04-21 17:13:25 -07007372 return IRQ_RETVAL(1);
7373}
7374
David Howells7d12e782006-10-05 14:55:46 +01007375static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376{
Matt Carlson09943a12009-08-28 14:01:57 +00007377 struct tg3_napi *tnapi = dev_id;
7378 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007379 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 unsigned int handled = 1;
7381
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 /* In INTx mode, it is possible for the interrupt to arrive at
7383 * the CPU before the status block posted prior to the interrupt.
7384 * Reading the PCI State register will confirm whether the
7385 * interrupt is ours and will flush the status block.
7386 */
Michael Chand18edcb2007-03-24 20:57:11 -07007387 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007388 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007389 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7390 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007391 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007392 }
Michael Chand18edcb2007-03-24 20:57:11 -07007393 }
7394
7395 /*
7396 * Writing any value to intr-mbox-0 clears PCI INTA# and
7397 * chip-internal interrupt pending events.
7398 * Writing non-zero to intr-mbox-0 additional tells the
7399 * NIC to stop sending us irqs, engaging "in-intr-handler"
7400 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007401 *
7402 * Flush the mailbox to de-assert the IRQ immediately to prevent
7403 * spurious interrupts. The flush impacts performance but
7404 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007405 */
Michael Chanc04cb342007-05-07 00:26:15 -07007406 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007407 if (tg3_irq_sync(tp))
7408 goto out;
7409 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007410 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007411 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007412 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007413 } else {
7414 /* No work, shared interrupt perhaps? re-enable
7415 * interrupts, and flush that PCI write
7416 */
7417 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7418 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007419 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007420out:
David S. Millerfac9b832005-05-18 22:46:34 -07007421 return IRQ_RETVAL(handled);
7422}
7423
David Howells7d12e782006-10-05 14:55:46 +01007424static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007425{
Matt Carlson09943a12009-08-28 14:01:57 +00007426 struct tg3_napi *tnapi = dev_id;
7427 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007428 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007429 unsigned int handled = 1;
7430
David S. Millerfac9b832005-05-18 22:46:34 -07007431 /* In INTx mode, it is possible for the interrupt to arrive at
7432 * the CPU before the status block posted prior to the interrupt.
7433 * Reading the PCI State register will confirm whether the
7434 * interrupt is ours and will flush the status block.
7435 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007436 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007437 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007438 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7439 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 }
Michael Chand18edcb2007-03-24 20:57:11 -07007442 }
7443
7444 /*
7445 * writing any value to intr-mbox-0 clears PCI INTA# and
7446 * chip-internal interrupt pending events.
7447 * writing non-zero to intr-mbox-0 additional tells the
7448 * NIC to stop sending us irqs, engaging "in-intr-handler"
7449 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007450 *
7451 * Flush the mailbox to de-assert the IRQ immediately to prevent
7452 * spurious interrupts. The flush impacts performance but
7453 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007454 */
Michael Chanc04cb342007-05-07 00:26:15 -07007455 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007456
7457 /*
7458 * In a shared interrupt configuration, sometimes other devices'
7459 * interrupts will scream. We record the current status tag here
7460 * so that the above check can report that the screaming interrupts
7461 * are unhandled. Eventually they will be silenced.
7462 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007463 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007464
Michael Chand18edcb2007-03-24 20:57:11 -07007465 if (tg3_irq_sync(tp))
7466 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007467
Matt Carlson72334482009-08-28 14:03:01 +00007468 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007469
Matt Carlson09943a12009-08-28 14:01:57 +00007470 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007471
David S. Millerf47c11e2005-06-24 20:18:35 -07007472out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 return IRQ_RETVAL(handled);
7474}
7475
Michael Chan79381092005-04-21 17:13:59 -07007476/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007477static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007478{
Matt Carlson09943a12009-08-28 14:01:57 +00007479 struct tg3_napi *tnapi = dev_id;
7480 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007481 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007482
Michael Chanf9804dd2005-09-27 12:13:10 -07007483 if ((sblk->status & SD_STATUS_UPDATED) ||
7484 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007485 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007486 return IRQ_RETVAL(1);
7487 }
7488 return IRQ_RETVAL(0);
7489}
7490
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491#ifdef CONFIG_NET_POLL_CONTROLLER
7492static void tg3_poll_controller(struct net_device *dev)
7493{
Matt Carlson4f125f42009-09-01 12:55:02 +00007494 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007495 struct tg3 *tp = netdev_priv(dev);
7496
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007497 if (tg3_irq_sync(tp))
7498 return;
7499
Matt Carlson4f125f42009-09-01 12:55:02 +00007500 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007501 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502}
7503#endif
7504
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505static void tg3_tx_timeout(struct net_device *dev)
7506{
7507 struct tg3 *tp = netdev_priv(dev);
7508
Michael Chanb0408752007-02-13 12:18:30 -08007509 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007510 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007511 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513
Matt Carlsondb219972011-11-04 09:15:03 +00007514 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515}
7516
Michael Chanc58ec932005-09-17 00:46:27 -07007517/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7518static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7519{
7520 u32 base = (u32) mapping & 0xffffffff;
7521
Eric Dumazet807540b2010-09-23 05:40:09 +00007522 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007523}
7524
Michael Chan0f0d1512013-05-13 11:04:16 +00007525/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7526 * of any 4GB boundaries: 4G, 8G, etc
7527 */
7528static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7529 u32 len, u32 mss)
7530{
7531 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7532 u32 base = (u32) mapping & 0xffffffff;
7533
7534 return ((base + len + (mss & 0x3fff)) < base);
7535 }
7536 return 0;
7537}
7538
Michael Chan72f2afb2006-03-06 19:28:35 -08007539/* Test for DMA addresses > 40-bit */
7540static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7541 int len)
7542{
7543#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007544 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007545 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007546 return 0;
7547#else
7548 return 0;
7549#endif
7550}
7551
Matt Carlsond1a3b732011-07-27 14:20:51 +00007552static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007553 dma_addr_t mapping, u32 len, u32 flags,
7554 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007555{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007556 txbd->addr_hi = ((u64) mapping >> 32);
7557 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7558 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7559 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561
Matt Carlson84b67b22011-07-27 14:20:52 +00007562static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007563 dma_addr_t map, u32 len, u32 flags,
7564 u32 mss, u32 vlan)
7565{
7566 struct tg3 *tp = tnapi->tp;
7567 bool hwbug = false;
7568
7569 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007570 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007571
7572 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007573 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007574
Michael Chan0f0d1512013-05-13 11:04:16 +00007575 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7576 hwbug = true;
7577
Matt Carlsond1a3b732011-07-27 14:20:51 +00007578 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007579 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007580
Matt Carlsona4cb4282011-12-14 11:09:58 +00007581 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007582 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007583 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007584 while (len > tp->dma_limit && *budget) {
7585 u32 frag_len = tp->dma_limit;
7586 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007587
Matt Carlsonb9e45482011-11-04 09:14:59 +00007588 /* Avoid the 8byte DMA problem */
7589 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007590 len += tp->dma_limit / 2;
7591 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007592 }
7593
Matt Carlsonb9e45482011-11-04 09:14:59 +00007594 tnapi->tx_buffers[*entry].fragmented = true;
7595
7596 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7597 frag_len, tmp_flag, mss, vlan);
7598 *budget -= 1;
7599 prvidx = *entry;
7600 *entry = NEXT_TX(*entry);
7601
Matt Carlsone31aa982011-07-27 14:20:53 +00007602 map += frag_len;
7603 }
7604
7605 if (len) {
7606 if (*budget) {
7607 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7608 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007609 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007610 *entry = NEXT_TX(*entry);
7611 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007612 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007613 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007614 }
7615 }
7616 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007617 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7618 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007619 *entry = NEXT_TX(*entry);
7620 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007621
7622 return hwbug;
7623}
7624
Matt Carlson0d681b22011-07-27 14:20:49 +00007625static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007626{
7627 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007628 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007629 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007630
Matt Carlson0d681b22011-07-27 14:20:49 +00007631 skb = txb->skb;
7632 txb->skb = NULL;
7633
Matt Carlson432aa7e2011-05-19 12:12:45 +00007634 pci_unmap_single(tnapi->tp->pdev,
7635 dma_unmap_addr(txb, mapping),
7636 skb_headlen(skb),
7637 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007638
7639 while (txb->fragmented) {
7640 txb->fragmented = false;
7641 entry = NEXT_TX(entry);
7642 txb = &tnapi->tx_buffers[entry];
7643 }
7644
Matt Carlsonba1142e2011-11-04 09:15:00 +00007645 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007646 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007647
7648 entry = NEXT_TX(entry);
7649 txb = &tnapi->tx_buffers[entry];
7650
7651 pci_unmap_page(tnapi->tp->pdev,
7652 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007653 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007654
7655 while (txb->fragmented) {
7656 txb->fragmented = false;
7657 entry = NEXT_TX(entry);
7658 txb = &tnapi->tx_buffers[entry];
7659 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007660 }
7661}
7662
Michael Chan72f2afb2006-03-06 19:28:35 -08007663/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007664static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007665 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007666 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007667 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007669 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007670 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007671 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007672 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673
Joe Perches41535772013-02-16 11:20:04 +00007674 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007675 new_skb = skb_copy(skb, GFP_ATOMIC);
7676 else {
7677 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7678
7679 new_skb = skb_copy_expand(skb,
7680 skb_headroom(skb) + more_headroom,
7681 skb_tailroom(skb), GFP_ATOMIC);
7682 }
7683
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007685 ret = -1;
7686 } else {
7687 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007688 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7689 PCI_DMA_TODEVICE);
7690 /* Make sure the mapping succeeded */
7691 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007692 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007693 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007694 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007695 u32 save_entry = *entry;
7696
Matt Carlson92cd3a12011-07-27 14:20:47 +00007697 base_flags |= TXD_FLAG_END;
7698
Matt Carlson84b67b22011-07-27 14:20:52 +00007699 tnapi->tx_buffers[*entry].skb = new_skb;
7700 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007701 mapping, new_addr);
7702
Matt Carlson84b67b22011-07-27 14:20:52 +00007703 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007704 new_skb->len, base_flags,
7705 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007706 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007707 dev_kfree_skb(new_skb);
7708 ret = -1;
7709 }
Michael Chanc58ec932005-09-17 00:46:27 -07007710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 }
7712
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007714 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007715 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716}
7717
Matt Carlson2ffcc982011-05-19 12:12:44 +00007718static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007719
7720/* Use GSO to workaround a rare TSO bug that may be triggered when the
7721 * TSO header is greater than 80 bytes.
7722 */
7723static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7724{
7725 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007726 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007727
7728 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007729 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007730 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007731
7732 /* netif_tx_stop_queue() must be done before checking
7733 * checking tx index in tg3_tx_avail() below, because in
7734 * tg3_tx(), we update tx index before checking for
7735 * netif_tx_queue_stopped().
7736 */
7737 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007738 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007739 return NETDEV_TX_BUSY;
7740
7741 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007742 }
7743
7744 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007745 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007746 goto tg3_tso_bug_end;
7747
7748 do {
7749 nskb = segs;
7750 segs = segs->next;
7751 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007752 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007753 } while (segs);
7754
7755tg3_tso_bug_end:
7756 dev_kfree_skb(skb);
7757
7758 return NETDEV_TX_OK;
7759}
Michael Chan52c0fd82006-06-29 20:15:54 -07007760
Michael Chan5a6f3072006-03-20 22:28:05 -08007761/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007762 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007763 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007764static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007765{
7766 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007767 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007768 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007769 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007770 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007771 struct tg3_napi *tnapi;
7772 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007773 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007774
Matt Carlson24f4efd2009-11-13 13:03:35 +00007775 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7776 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007777 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007778 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779
Matt Carlson84b67b22011-07-27 14:20:52 +00007780 budget = tg3_tx_avail(tnapi);
7781
Michael Chan00b70502006-06-17 21:58:45 -07007782 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007783 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007784 * interrupt. Furthermore, IRQ processing runs lockless so we have
7785 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007787 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007788 if (!netif_tx_queue_stopped(txq)) {
7789 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007790
7791 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007792 netdev_err(dev,
7793 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 return NETDEV_TX_BUSY;
7796 }
7797
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007798 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007800 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007802
Matt Carlsonbe98da62010-07-11 09:31:46 +00007803 mss = skb_shinfo(skb)->gso_size;
7804 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007805 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007806 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807
7808 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007809 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7810 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811
Matt Carlson34195c32010-07-11 09:31:42 +00007812 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007813 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814
Eric Dumazeta5a11952012-01-23 01:22:09 +00007815 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007816
Eric Dumazeta5a11952012-01-23 01:22:09 +00007817 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007818 iph->check = 0;
7819 iph->tot_len = htons(mss + hdr_len);
7820 }
7821
Michael Chan52c0fd82006-06-29 20:15:54 -07007822 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007823 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007824 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007825
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7827 TXD_FLAG_CPU_POST_DMA);
7828
Joe Perches63c3a662011-04-26 08:12:10 +00007829 if (tg3_flag(tp, HW_TSO_1) ||
7830 tg3_flag(tp, HW_TSO_2) ||
7831 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007832 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007834 } else
7835 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7836 iph->daddr, 0,
7837 IPPROTO_TCP,
7838 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839
Joe Perches63c3a662011-04-26 08:12:10 +00007840 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007841 mss |= (hdr_len & 0xc) << 12;
7842 if (hdr_len & 0x10)
7843 base_flags |= 0x00000010;
7844 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007845 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007846 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007847 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007848 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007849 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 int tsflags;
7851
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007852 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 mss |= (tsflags << 11);
7854 }
7855 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007856 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 int tsflags;
7858
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007859 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 base_flags |= tsflags << 12;
7861 }
7862 }
7863 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007864
Matt Carlson93a700a2011-08-31 11:44:54 +00007865 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7866 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7867 base_flags |= TXD_FLAG_JMB_PKT;
7868
Matt Carlson92cd3a12011-07-27 14:20:47 +00007869 if (vlan_tx_tag_present(skb)) {
7870 base_flags |= TXD_FLAG_VLAN;
7871 vlan = vlan_tx_tag_get(skb);
7872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007874 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7875 tg3_flag(tp, TX_TSTAMP_EN)) {
7876 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7877 base_flags |= TXD_FLAG_HWTSTAMP;
7878 }
7879
Alexander Duyckf4188d82009-12-02 16:48:38 +00007880 len = skb_headlen(skb);
7881
7882 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007883 if (pci_dma_mapping_error(tp->pdev, mapping))
7884 goto drop;
7885
David S. Miller90079ce2008-09-11 04:52:51 -07007886
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007887 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007888 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889
7890 would_hit_hwbug = 0;
7891
Joe Perches63c3a662011-04-26 08:12:10 +00007892 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007893 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
Matt Carlson84b67b22011-07-27 14:20:52 +00007895 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007896 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007897 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007898 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007899 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007900 u32 tmp_mss = mss;
7901
7902 if (!tg3_flag(tp, HW_TSO_1) &&
7903 !tg3_flag(tp, HW_TSO_2) &&
7904 !tg3_flag(tp, HW_TSO_3))
7905 tmp_mss = 0;
7906
Matt Carlsonc5665a52012-02-13 10:20:12 +00007907 /* Now loop through additional data
7908 * fragments, and queue them.
7909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 last = skb_shinfo(skb)->nr_frags - 1;
7911 for (i = 0; i <= last; i++) {
7912 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7913
Eric Dumazet9e903e02011-10-18 21:00:24 +00007914 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007915 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007916 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007918 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007919 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007920 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007921 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007922 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923
Matt Carlsonb9e45482011-11-04 09:14:59 +00007924 if (!budget ||
7925 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007926 len, base_flags |
7927 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007928 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007929 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007930 break;
7931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932 }
7933 }
7934
7935 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007936 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937
7938 /* If the workaround fails due to memory/mapping
7939 * failure, silently drop this packet.
7940 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007941 entry = tnapi->tx_prod;
7942 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007943 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007944 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007945 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 }
7947
Richard Cochrand515b452011-06-19 03:31:41 +00007948 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007949 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007950
Michael Chan6541b802012-03-04 14:48:14 +00007951 /* Sync BD data before updating mailbox */
7952 wmb();
7953
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007955 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007957 tnapi->tx_prod = entry;
7958 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007959 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007960
7961 /* netif_tx_stop_queue() must be done before checking
7962 * checking tx index in tg3_tx_avail() below, because in
7963 * tg3_tx(), we update tx index before checking for
7964 * netif_tx_queue_stopped().
7965 */
7966 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007967 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007968 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007971 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007973
7974dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007975 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007976 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007977drop:
7978 dev_kfree_skb(skb);
7979drop_nofree:
7980 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007981 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982}
7983
Matt Carlson6e01b202011-08-19 13:58:20 +00007984static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7985{
7986 if (enable) {
7987 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7988 MAC_MODE_PORT_MODE_MASK);
7989
7990 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7991
7992 if (!tg3_flag(tp, 5705_PLUS))
7993 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7994
7995 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7996 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7997 else
7998 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7999 } else {
8000 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8001
8002 if (tg3_flag(tp, 5705_PLUS) ||
8003 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008004 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008005 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8006 }
8007
8008 tw32(MAC_MODE, tp->mac_mode);
8009 udelay(40);
8010}
8011
Matt Carlson941ec902011-08-19 13:58:23 +00008012static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008013{
Matt Carlson941ec902011-08-19 13:58:23 +00008014 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008015
8016 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008017 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008018
Matt Carlson941ec902011-08-19 13:58:23 +00008019 if (extlpbk && tg3_phy_set_extloopbk(tp))
8020 return -EIO;
8021
8022 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008023 switch (speed) {
8024 case SPEED_10:
8025 break;
8026 case SPEED_100:
8027 bmcr |= BMCR_SPEED100;
8028 break;
8029 case SPEED_1000:
8030 default:
8031 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8032 speed = SPEED_100;
8033 bmcr |= BMCR_SPEED100;
8034 } else {
8035 speed = SPEED_1000;
8036 bmcr |= BMCR_SPEED1000;
8037 }
8038 }
8039
Matt Carlson941ec902011-08-19 13:58:23 +00008040 if (extlpbk) {
8041 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8042 tg3_readphy(tp, MII_CTRL1000, &val);
8043 val |= CTL1000_AS_MASTER |
8044 CTL1000_ENABLE_MASTER;
8045 tg3_writephy(tp, MII_CTRL1000, val);
8046 } else {
8047 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8048 MII_TG3_FET_PTEST_TRIM_2;
8049 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8050 }
8051 } else
8052 bmcr |= BMCR_LOOPBACK;
8053
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008054 tg3_writephy(tp, MII_BMCR, bmcr);
8055
8056 /* The write needs to be flushed for the FETs */
8057 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8058 tg3_readphy(tp, MII_BMCR, &bmcr);
8059
8060 udelay(40);
8061
8062 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008063 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008064 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008065 MII_TG3_FET_PTEST_FRC_TX_LINK |
8066 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8067
8068 /* The write needs to be flushed for the AC131 */
8069 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8070 }
8071
8072 /* Reset to prevent losing 1st rx packet intermittently */
8073 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8074 tg3_flag(tp, 5780_CLASS)) {
8075 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8076 udelay(10);
8077 tw32_f(MAC_RX_MODE, tp->rx_mode);
8078 }
8079
8080 mac_mode = tp->mac_mode &
8081 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8082 if (speed == SPEED_1000)
8083 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8084 else
8085 mac_mode |= MAC_MODE_PORT_MODE_MII;
8086
Joe Perches41535772013-02-16 11:20:04 +00008087 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008088 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8089
8090 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8091 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8092 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8093 mac_mode |= MAC_MODE_LINK_POLARITY;
8094
8095 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8096 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8097 }
8098
8099 tw32(MAC_MODE, mac_mode);
8100 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008101
8102 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008103}
8104
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008105static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008106{
8107 struct tg3 *tp = netdev_priv(dev);
8108
8109 if (features & NETIF_F_LOOPBACK) {
8110 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8111 return;
8112
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008113 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008114 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008115 netif_carrier_on(tp->dev);
8116 spin_unlock_bh(&tp->lock);
8117 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8118 } else {
8119 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8120 return;
8121
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008122 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008123 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008124 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008125 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008126 spin_unlock_bh(&tp->lock);
8127 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8128 }
8129}
8130
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008131static netdev_features_t tg3_fix_features(struct net_device *dev,
8132 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008133{
8134 struct tg3 *tp = netdev_priv(dev);
8135
Joe Perches63c3a662011-04-26 08:12:10 +00008136 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008137 features &= ~NETIF_F_ALL_TSO;
8138
8139 return features;
8140}
8141
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008142static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008143{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008144 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008145
8146 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8147 tg3_set_loopback(dev, features);
8148
8149 return 0;
8150}
8151
Matt Carlson21f581a2009-08-28 14:00:25 +00008152static void tg3_rx_prodring_free(struct tg3 *tp,
8153 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 int i;
8156
Matt Carlson8fea32b2010-09-15 08:59:58 +00008157 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008158 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008159 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008160 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008161 tp->rx_pkt_map_sz);
8162
Joe Perches63c3a662011-04-26 08:12:10 +00008163 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008164 for (i = tpr->rx_jmb_cons_idx;
8165 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008166 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008167 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008168 TG3_RX_JMB_MAP_SZ);
8169 }
8170 }
8171
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008172 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174
Matt Carlson2c49a442010-09-30 10:34:35 +00008175 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008176 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008177 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178
Joe Perches63c3a662011-04-26 08:12:10 +00008179 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008180 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008181 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008182 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 }
8184}
8185
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008186/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008187 *
8188 * The chip has been shut down and the driver detached from
8189 * the networking, so no interrupts or new tx packets will
8190 * end up in the driver. tp->{tx,}lock are held and thus
8191 * we may not sleep.
8192 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008193static int tg3_rx_prodring_alloc(struct tg3 *tp,
8194 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195{
Matt Carlson287be122009-08-28 13:58:46 +00008196 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008198 tpr->rx_std_cons_idx = 0;
8199 tpr->rx_std_prod_idx = 0;
8200 tpr->rx_jmb_cons_idx = 0;
8201 tpr->rx_jmb_prod_idx = 0;
8202
Matt Carlson8fea32b2010-09-15 08:59:58 +00008203 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008204 memset(&tpr->rx_std_buffers[0], 0,
8205 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008206 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008207 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008208 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008209 goto done;
8210 }
8211
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008213 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214
Matt Carlson287be122009-08-28 13:58:46 +00008215 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008216 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008217 tp->dev->mtu > ETH_DATA_LEN)
8218 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8219 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008220
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 /* Initialize invariants of the rings, we only set this
8222 * stuff once. This works because the card does not
8223 * write into the rx buffer posting rings.
8224 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008225 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226 struct tg3_rx_buffer_desc *rxd;
8227
Matt Carlson21f581a2009-08-28 14:00:25 +00008228 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008229 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8231 rxd->opaque = (RXD_OPAQUE_RING_STD |
8232 (i << RXD_OPAQUE_INDEX_SHIFT));
8233 }
8234
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008235 /* Now allocate fresh SKBs for each rx ring. */
8236 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008237 unsigned int frag_size;
8238
8239 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8240 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008241 netdev_warn(tp->dev,
8242 "Using a smaller RX standard ring. Only "
8243 "%d out of %d buffers were allocated "
8244 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008245 if (i == 0)
8246 goto initfail;
8247 tp->rx_pending = i;
8248 break;
8249 }
8250 }
8251
Joe Perches63c3a662011-04-26 08:12:10 +00008252 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008253 goto done;
8254
Matt Carlson2c49a442010-09-30 10:34:35 +00008255 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008256
Joe Perches63c3a662011-04-26 08:12:10 +00008257 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008258 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259
Matt Carlson2c49a442010-09-30 10:34:35 +00008260 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008261 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262
Matt Carlson0d86df82010-02-17 15:17:00 +00008263 rxd = &tpr->rx_jmb[i].std;
8264 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8265 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8266 RXD_FLAG_JUMBO;
8267 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8268 (i << RXD_OPAQUE_INDEX_SHIFT));
8269 }
8270
8271 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008272 unsigned int frag_size;
8273
8274 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8275 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008276 netdev_warn(tp->dev,
8277 "Using a smaller RX jumbo ring. Only %d "
8278 "out of %d buffers were allocated "
8279 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008280 if (i == 0)
8281 goto initfail;
8282 tp->rx_jumbo_pending = i;
8283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284 }
8285 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008286
8287done:
Michael Chan32d8c572006-07-25 16:38:29 -07008288 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008289
8290initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008291 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008292 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293}
8294
Matt Carlson21f581a2009-08-28 14:00:25 +00008295static void tg3_rx_prodring_fini(struct tg3 *tp,
8296 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297{
Matt Carlson21f581a2009-08-28 14:00:25 +00008298 kfree(tpr->rx_std_buffers);
8299 tpr->rx_std_buffers = NULL;
8300 kfree(tpr->rx_jmb_buffers);
8301 tpr->rx_jmb_buffers = NULL;
8302 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008303 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8304 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008305 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008307 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008308 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8309 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008310 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008312}
8313
Matt Carlson21f581a2009-08-28 14:00:25 +00008314static int tg3_rx_prodring_init(struct tg3 *tp,
8315 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008316{
Matt Carlson2c49a442010-09-30 10:34:35 +00008317 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8318 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008319 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008320 return -ENOMEM;
8321
Matt Carlson4bae65c2010-11-24 08:31:52 +00008322 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8323 TG3_RX_STD_RING_BYTES(tp),
8324 &tpr->rx_std_mapping,
8325 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008326 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008327 goto err_out;
8328
Joe Perches63c3a662011-04-26 08:12:10 +00008329 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008330 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008331 GFP_KERNEL);
8332 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008333 goto err_out;
8334
Matt Carlson4bae65c2010-11-24 08:31:52 +00008335 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8336 TG3_RX_JMB_RING_BYTES(tp),
8337 &tpr->rx_jmb_mapping,
8338 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008339 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008340 goto err_out;
8341 }
8342
8343 return 0;
8344
8345err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008346 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008347 return -ENOMEM;
8348}
8349
8350/* Free up pending packets in all rx/tx rings.
8351 *
8352 * The chip has been shut down and the driver detached from
8353 * the networking, so no interrupts or new tx packets will
8354 * end up in the driver. tp->{tx,}lock is not held and we are not
8355 * in an interrupt context and thus may sleep.
8356 */
8357static void tg3_free_rings(struct tg3 *tp)
8358{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008359 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008360
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008361 for (j = 0; j < tp->irq_cnt; j++) {
8362 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008363
Matt Carlson8fea32b2010-09-15 08:59:58 +00008364 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008365
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008366 if (!tnapi->tx_buffers)
8367 continue;
8368
Matt Carlson0d681b22011-07-27 14:20:49 +00008369 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8370 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008371
Matt Carlson0d681b22011-07-27 14:20:49 +00008372 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008373 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008374
Matt Carlsonba1142e2011-11-04 09:15:00 +00008375 tg3_tx_skb_unmap(tnapi, i,
8376 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008377
8378 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008379 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008380 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008381 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008382}
8383
8384/* Initialize tx/rx rings for packet processing.
8385 *
8386 * The chip has been shut down and the driver detached from
8387 * the networking, so no interrupts or new tx packets will
8388 * end up in the driver. tp->{tx,}lock are held and thus
8389 * we may not sleep.
8390 */
8391static int tg3_init_rings(struct tg3 *tp)
8392{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008393 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008394
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008395 /* Free up all the SKBs. */
8396 tg3_free_rings(tp);
8397
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008398 for (i = 0; i < tp->irq_cnt; i++) {
8399 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008400
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008401 tnapi->last_tag = 0;
8402 tnapi->last_irq_tag = 0;
8403 tnapi->hw_status->status = 0;
8404 tnapi->hw_status->status_tag = 0;
8405 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8406
8407 tnapi->tx_prod = 0;
8408 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008409 if (tnapi->tx_ring)
8410 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008411
8412 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008413 if (tnapi->rx_rcb)
8414 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008415
Matt Carlson8fea32b2010-09-15 08:59:58 +00008416 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008417 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008418 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008419 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008420 }
Matt Carlson72334482009-08-28 14:03:01 +00008421
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008422 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008423}
8424
Michael Chan49a359e2012-09-28 07:12:37 +00008425static void tg3_mem_tx_release(struct tg3 *tp)
8426{
8427 int i;
8428
8429 for (i = 0; i < tp->irq_max; i++) {
8430 struct tg3_napi *tnapi = &tp->napi[i];
8431
8432 if (tnapi->tx_ring) {
8433 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8434 tnapi->tx_ring, tnapi->tx_desc_mapping);
8435 tnapi->tx_ring = NULL;
8436 }
8437
8438 kfree(tnapi->tx_buffers);
8439 tnapi->tx_buffers = NULL;
8440 }
8441}
8442
8443static int tg3_mem_tx_acquire(struct tg3 *tp)
8444{
8445 int i;
8446 struct tg3_napi *tnapi = &tp->napi[0];
8447
8448 /* If multivector TSS is enabled, vector 0 does not handle
8449 * tx interrupts. Don't allocate any resources for it.
8450 */
8451 if (tg3_flag(tp, ENABLE_TSS))
8452 tnapi++;
8453
8454 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8455 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8456 TG3_TX_RING_SIZE, GFP_KERNEL);
8457 if (!tnapi->tx_buffers)
8458 goto err_out;
8459
8460 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8461 TG3_TX_RING_BYTES,
8462 &tnapi->tx_desc_mapping,
8463 GFP_KERNEL);
8464 if (!tnapi->tx_ring)
8465 goto err_out;
8466 }
8467
8468 return 0;
8469
8470err_out:
8471 tg3_mem_tx_release(tp);
8472 return -ENOMEM;
8473}
8474
8475static void tg3_mem_rx_release(struct tg3 *tp)
8476{
8477 int i;
8478
8479 for (i = 0; i < tp->irq_max; i++) {
8480 struct tg3_napi *tnapi = &tp->napi[i];
8481
8482 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8483
8484 if (!tnapi->rx_rcb)
8485 continue;
8486
8487 dma_free_coherent(&tp->pdev->dev,
8488 TG3_RX_RCB_RING_BYTES(tp),
8489 tnapi->rx_rcb,
8490 tnapi->rx_rcb_mapping);
8491 tnapi->rx_rcb = NULL;
8492 }
8493}
8494
8495static int tg3_mem_rx_acquire(struct tg3 *tp)
8496{
8497 unsigned int i, limit;
8498
8499 limit = tp->rxq_cnt;
8500
8501 /* If RSS is enabled, we need a (dummy) producer ring
8502 * set on vector zero. This is the true hw prodring.
8503 */
8504 if (tg3_flag(tp, ENABLE_RSS))
8505 limit++;
8506
8507 for (i = 0; i < limit; i++) {
8508 struct tg3_napi *tnapi = &tp->napi[i];
8509
8510 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8511 goto err_out;
8512
8513 /* If multivector RSS is enabled, vector 0
8514 * does not handle rx or tx interrupts.
8515 * Don't allocate any resources for it.
8516 */
8517 if (!i && tg3_flag(tp, ENABLE_RSS))
8518 continue;
8519
8520 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8521 TG3_RX_RCB_RING_BYTES(tp),
8522 &tnapi->rx_rcb_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008523 GFP_KERNEL | __GFP_ZERO);
Michael Chan49a359e2012-09-28 07:12:37 +00008524 if (!tnapi->rx_rcb)
8525 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008526 }
8527
8528 return 0;
8529
8530err_out:
8531 tg3_mem_rx_release(tp);
8532 return -ENOMEM;
8533}
8534
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008535/*
8536 * Must not be invoked with interrupt sources disabled and
8537 * the hardware shutdown down.
8538 */
8539static void tg3_free_consistent(struct tg3 *tp)
8540{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008541 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008542
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008543 for (i = 0; i < tp->irq_cnt; i++) {
8544 struct tg3_napi *tnapi = &tp->napi[i];
8545
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008546 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008547 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8548 tnapi->hw_status,
8549 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008550 tnapi->hw_status = NULL;
8551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008553
Michael Chan49a359e2012-09-28 07:12:37 +00008554 tg3_mem_rx_release(tp);
8555 tg3_mem_tx_release(tp);
8556
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008558 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8559 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560 tp->hw_stats = NULL;
8561 }
8562}
8563
8564/*
8565 * Must not be invoked with interrupt sources disabled and
8566 * the hardware shutdown down. Can sleep.
8567 */
8568static int tg3_alloc_consistent(struct tg3 *tp)
8569{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008570 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008571
Matt Carlson4bae65c2010-11-24 08:31:52 +00008572 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8573 sizeof(struct tg3_hw_stats),
8574 &tp->stats_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008575 GFP_KERNEL | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576 if (!tp->hw_stats)
8577 goto err_out;
8578
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008579 for (i = 0; i < tp->irq_cnt; i++) {
8580 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008581 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008582
Matt Carlson4bae65c2010-11-24 08:31:52 +00008583 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8584 TG3_HW_STATUS_SIZE,
8585 &tnapi->status_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008586 GFP_KERNEL | __GFP_ZERO);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008587 if (!tnapi->hw_status)
8588 goto err_out;
8589
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008590 sblk = tnapi->hw_status;
8591
Michael Chan49a359e2012-09-28 07:12:37 +00008592 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008593 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008594
Michael Chan49a359e2012-09-28 07:12:37 +00008595 /*
8596 * When RSS is enabled, the status block format changes
8597 * slightly. The "rx_jumbo_consumer", "reserved",
8598 * and "rx_mini_consumer" members get mapped to the
8599 * other three rx return ring producer indexes.
8600 */
8601 switch (i) {
8602 case 1:
8603 prodptr = &sblk->idx[0].rx_producer;
8604 break;
8605 case 2:
8606 prodptr = &sblk->rx_jumbo_consumer;
8607 break;
8608 case 3:
8609 prodptr = &sblk->reserved;
8610 break;
8611 case 4:
8612 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008613 break;
8614 }
Michael Chan49a359e2012-09-28 07:12:37 +00008615 tnapi->rx_rcb_prod_idx = prodptr;
8616 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008617 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008618 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008619 }
8620
Michael Chan49a359e2012-09-28 07:12:37 +00008621 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8622 goto err_out;
8623
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624 return 0;
8625
8626err_out:
8627 tg3_free_consistent(tp);
8628 return -ENOMEM;
8629}
8630
8631#define MAX_WAIT_CNT 1000
8632
8633/* To stop a block, clear the enable bit and poll till it
8634 * clears. tp->lock is held.
8635 */
Joe Perches953c96e2013-04-09 10:18:14 +00008636static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637{
8638 unsigned int i;
8639 u32 val;
8640
Joe Perches63c3a662011-04-26 08:12:10 +00008641 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642 switch (ofs) {
8643 case RCVLSC_MODE:
8644 case DMAC_MODE:
8645 case MBFREE_MODE:
8646 case BUFMGR_MODE:
8647 case MEMARB_MODE:
8648 /* We can't enable/disable these bits of the
8649 * 5705/5750, just say success.
8650 */
8651 return 0;
8652
8653 default:
8654 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656 }
8657
8658 val = tr32(ofs);
8659 val &= ~enable_bit;
8660 tw32_f(ofs, val);
8661
8662 for (i = 0; i < MAX_WAIT_CNT; i++) {
8663 udelay(100);
8664 val = tr32(ofs);
8665 if ((val & enable_bit) == 0)
8666 break;
8667 }
8668
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008669 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008670 dev_err(&tp->pdev->dev,
8671 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8672 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673 return -ENODEV;
8674 }
8675
8676 return 0;
8677}
8678
8679/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008680static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681{
8682 int i, err;
8683
8684 tg3_disable_ints(tp);
8685
8686 tp->rx_mode &= ~RX_MODE_ENABLE;
8687 tw32_f(MAC_RX_MODE, tp->rx_mode);
8688 udelay(10);
8689
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008690 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8691 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8692 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8693 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8694 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8695 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008697 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8698 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8699 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8700 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8701 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8702 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8703 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704
8705 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8706 tw32_f(MAC_MODE, tp->mac_mode);
8707 udelay(40);
8708
8709 tp->tx_mode &= ~TX_MODE_ENABLE;
8710 tw32_f(MAC_TX_MODE, tp->tx_mode);
8711
8712 for (i = 0; i < MAX_WAIT_CNT; i++) {
8713 udelay(100);
8714 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8715 break;
8716 }
8717 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008718 dev_err(&tp->pdev->dev,
8719 "%s timed out, TX_MODE_ENABLE will not clear "
8720 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008721 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722 }
8723
Michael Chane6de8ad2005-05-05 14:42:41 -07008724 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008725 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8726 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727
8728 tw32(FTQ_RESET, 0xffffffff);
8729 tw32(FTQ_RESET, 0x00000000);
8730
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008731 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8732 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008734 for (i = 0; i < tp->irq_cnt; i++) {
8735 struct tg3_napi *tnapi = &tp->napi[i];
8736 if (tnapi->hw_status)
8737 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 return err;
8741}
8742
Michael Chanee6a99b2007-07-18 21:49:10 -07008743/* Save PCI command register before chip reset */
8744static void tg3_save_pci_state(struct tg3 *tp)
8745{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008746 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008747}
8748
8749/* Restore PCI state after chip reset */
8750static void tg3_restore_pci_state(struct tg3 *tp)
8751{
8752 u32 val;
8753
8754 /* Re-enable indirect register accesses. */
8755 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8756 tp->misc_host_ctrl);
8757
8758 /* Set MAX PCI retry to zero. */
8759 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008760 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008761 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008762 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008763 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008764 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008765 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008766 PCISTATE_ALLOW_APE_SHMEM_WR |
8767 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008768 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8769
Matt Carlson8a6eac92007-10-21 16:17:55 -07008770 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008771
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008772 if (!tg3_flag(tp, PCI_EXPRESS)) {
8773 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8774 tp->pci_cacheline_sz);
8775 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8776 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008777 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008778
Michael Chanee6a99b2007-07-18 21:49:10 -07008779 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008780 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008781 u16 pcix_cmd;
8782
8783 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8784 &pcix_cmd);
8785 pcix_cmd &= ~PCI_X_CMD_ERO;
8786 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8787 pcix_cmd);
8788 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008789
Joe Perches63c3a662011-04-26 08:12:10 +00008790 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008791
8792 /* Chip reset on 5780 will reset MSI enable bit,
8793 * so need to restore it.
8794 */
Joe Perches63c3a662011-04-26 08:12:10 +00008795 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008796 u16 ctrl;
8797
8798 pci_read_config_word(tp->pdev,
8799 tp->msi_cap + PCI_MSI_FLAGS,
8800 &ctrl);
8801 pci_write_config_word(tp->pdev,
8802 tp->msi_cap + PCI_MSI_FLAGS,
8803 ctrl | PCI_MSI_FLAGS_ENABLE);
8804 val = tr32(MSGINT_MODE);
8805 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8806 }
8807 }
8808}
8809
Linus Torvalds1da177e2005-04-16 15:20:36 -07008810/* tp->lock is held. */
8811static int tg3_chip_reset(struct tg3 *tp)
8812{
8813 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008814 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008815 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008816
David S. Millerf49639e2006-06-09 11:58:36 -07008817 tg3_nvram_lock(tp);
8818
Matt Carlson77b483f2008-08-15 14:07:24 -07008819 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8820
David S. Millerf49639e2006-06-09 11:58:36 -07008821 /* No matching tg3_nvram_unlock() after this because
8822 * chip reset below will undo the nvram lock.
8823 */
8824 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825
Michael Chanee6a99b2007-07-18 21:49:10 -07008826 /* GRC_MISC_CFG core clock reset will clear the memory
8827 * enable bit in PCI register 4 and the MSI enable bit
8828 * on some chips, so we save relevant registers here.
8829 */
8830 tg3_save_pci_state(tp);
8831
Joe Perches41535772013-02-16 11:20:04 +00008832 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008833 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008834 tw32(GRC_FASTBOOT_PC, 0);
8835
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 /*
8837 * We must avoid the readl() that normally takes place.
8838 * It locks machines, causes machine checks, and other
8839 * fun things. So, temporarily disable the 5701
8840 * hardware workaround, while we do the reset.
8841 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008842 write_op = tp->write32;
8843 if (write_op == tg3_write_flush_reg32)
8844 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845
Michael Chand18edcb2007-03-24 20:57:11 -07008846 /* Prevent the irq handler from reading or writing PCI registers
8847 * during chip reset when the memory enable bit in the PCI command
8848 * register may be cleared. The chip does not generate interrupt
8849 * at this time, but the irq handler may still be called due to irq
8850 * sharing or irqpoll.
8851 */
Joe Perches63c3a662011-04-26 08:12:10 +00008852 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008853 for (i = 0; i < tp->irq_cnt; i++) {
8854 struct tg3_napi *tnapi = &tp->napi[i];
8855 if (tnapi->hw_status) {
8856 tnapi->hw_status->status = 0;
8857 tnapi->hw_status->status_tag = 0;
8858 }
8859 tnapi->last_tag = 0;
8860 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008861 }
Michael Chand18edcb2007-03-24 20:57:11 -07008862 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008863
8864 for (i = 0; i < tp->irq_cnt; i++)
8865 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008866
Joe Perches41535772013-02-16 11:20:04 +00008867 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008868 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8869 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8870 }
8871
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872 /* do the reset */
8873 val = GRC_MISC_CFG_CORECLK_RESET;
8874
Joe Perches63c3a662011-04-26 08:12:10 +00008875 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008876 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008877 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008878 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008879 tr32(TG3_PCIE_PHY_TSTCTL) ==
8880 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8881 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8882
Joe Perches41535772013-02-16 11:20:04 +00008883 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884 tw32(GRC_MISC_CFG, (1 << 29));
8885 val |= (1 << 29);
8886 }
8887 }
8888
Joe Perches41535772013-02-16 11:20:04 +00008889 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008890 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8891 tw32(GRC_VCPU_EXT_CTRL,
8892 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8893 }
8894
Matt Carlsonf37500d2010-08-02 11:25:59 +00008895 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008896 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008898
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899 tw32(GRC_MISC_CFG, val);
8900
Michael Chan1ee582d2005-08-09 20:16:46 -07008901 /* restore 5701 hardware bug workaround write method */
8902 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903
8904 /* Unfortunately, we have to delay before the PCI read back.
8905 * Some 575X chips even will not respond to a PCI cfg access
8906 * when the reset command is given to the chip.
8907 *
8908 * How do these hardware designers expect things to work
8909 * properly if the PCI write is posted for a long period
8910 * of time? It is always necessary to have some method by
8911 * which a register read back can occur to push the write
8912 * out which does the reset.
8913 *
8914 * For most tg3 variants the trick below was working.
8915 * Ho hum...
8916 */
8917 udelay(120);
8918
8919 /* Flush PCI posted writes. The normal MMIO registers
8920 * are inaccessible at this time so this is the only
8921 * way to make this reliably (actually, this is no longer
8922 * the case, see above). I tried to use indirect
8923 * register read/write but this upset some 5701 variants.
8924 */
8925 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8926
8927 udelay(120);
8928
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008929 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008930 u16 val16;
8931
Joe Perches41535772013-02-16 11:20:04 +00008932 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008933 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 u32 cfg_val;
8935
8936 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008937 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938 udelay(100);
8939
8940 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8941 pci_write_config_dword(tp->pdev, 0xc4,
8942 cfg_val | (1 << 15));
8943 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008944
Matt Carlsone7126992009-08-25 10:08:16 +00008945 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008946 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008947 /*
8948 * Older PCIe devices only support the 128 byte
8949 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008950 */
Joe Perches63c3a662011-04-26 08:12:10 +00008951 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008952 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8953 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008954
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008955 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008956 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008957 PCI_EXP_DEVSTA_CED |
8958 PCI_EXP_DEVSTA_NFED |
8959 PCI_EXP_DEVSTA_FED |
8960 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961 }
8962
Michael Chanee6a99b2007-07-18 21:49:10 -07008963 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964
Joe Perches63c3a662011-04-26 08:12:10 +00008965 tg3_flag_clear(tp, CHIP_RESETTING);
8966 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008967
Michael Chanee6a99b2007-07-18 21:49:10 -07008968 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008969 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008970 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008971 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972
Joe Perches41535772013-02-16 11:20:04 +00008973 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008974 tg3_stop_fw(tp);
8975 tw32(0x5000, 0x400);
8976 }
8977
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00008978 if (tg3_flag(tp, IS_SSB_CORE)) {
8979 /*
8980 * BCM4785: In order to avoid repercussions from using
8981 * potentially defective internal ROM, stop the Rx RISC CPU,
8982 * which is not required.
8983 */
8984 tg3_stop_fw(tp);
8985 tg3_halt_cpu(tp, RX_CPU_BASE);
8986 }
8987
Nithin Sujirfb03a432013-05-21 12:57:32 +00008988 err = tg3_poll_fw(tp);
8989 if (err)
8990 return err;
8991
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992 tw32(GRC_MODE, tp->grc_mode);
8993
Joe Perches41535772013-02-16 11:20:04 +00008994 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008995 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996
8997 tw32(0xc4, val | (1 << 15));
8998 }
8999
9000 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009001 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009002 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009003 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9005 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9006 }
9007
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009008 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009009 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009010 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009011 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009012 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009013 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009015 val = 0;
9016
9017 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009018 udelay(40);
9019
Matt Carlson77b483f2008-08-15 14:07:24 -07009020 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9021
Matt Carlson0a9140c2009-08-28 12:27:50 +00009022 tg3_mdio_start(tp);
9023
Joe Perches63c3a662011-04-26 08:12:10 +00009024 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009025 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9026 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009027 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009028 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009029
9030 tw32(0x7c00, val | (1 << 25));
9031 }
9032
Joe Perches41535772013-02-16 11:20:04 +00009033 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00009034 val = tr32(TG3_CPMU_CLCK_ORIDE);
9035 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9036 }
9037
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009039 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009040 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9041 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9042
Joe Perches63c3a662011-04-26 08:12:10 +00009043 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009044 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9045 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9046 u32 nic_cfg;
9047
9048 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9049 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009050 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009051 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009052 if (tg3_flag(tp, 5750_PLUS))
9053 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009054
9055 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9056 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9057 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9058 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9059 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060 }
9061 }
9062
9063 return 0;
9064}
9065
Matt Carlson65ec6982012-02-28 23:33:37 +00009066static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9067static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009068
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009070static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071{
9072 int err;
9073
9074 tg3_stop_fw(tp);
9075
Michael Chan944d9802005-05-29 14:57:48 -07009076 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009077
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009078 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009079 err = tg3_chip_reset(tp);
9080
Joe Perches953c96e2013-04-09 10:18:14 +00009081 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009082
Michael Chan944d9802005-05-29 14:57:48 -07009083 tg3_write_sig_legacy(tp, kind);
9084 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085
Matt Carlson92feeab2011-12-08 14:40:14 +00009086 if (tp->hw_stats) {
9087 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009088 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009089 tg3_get_estats(tp, &tp->estats_prev);
9090
9091 /* And make sure the next sample is new data */
9092 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9093 }
9094
Linus Torvalds1da177e2005-04-16 15:20:36 -07009095 if (err)
9096 return err;
9097
9098 return 0;
9099}
9100
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101static int tg3_set_mac_addr(struct net_device *dev, void *p)
9102{
9103 struct tg3 *tp = netdev_priv(dev);
9104 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009105 int err = 0;
9106 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107
Michael Chanf9804dd2005-09-27 12:13:10 -07009108 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009109 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009110
Linus Torvalds1da177e2005-04-16 15:20:36 -07009111 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9112
Michael Chane75f7c92006-03-20 21:33:26 -08009113 if (!netif_running(dev))
9114 return 0;
9115
Joe Perches63c3a662011-04-26 08:12:10 +00009116 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009117 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009118
Michael Chan986e0ae2007-05-05 12:10:20 -07009119 addr0_high = tr32(MAC_ADDR_0_HIGH);
9120 addr0_low = tr32(MAC_ADDR_0_LOW);
9121 addr1_high = tr32(MAC_ADDR_1_HIGH);
9122 addr1_low = tr32(MAC_ADDR_1_LOW);
9123
9124 /* Skip MAC addr 1 if ASF is using it. */
9125 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9126 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009127 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009128 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009129 spin_lock_bh(&tp->lock);
9130 __tg3_set_mac_addr(tp, skip_mac_1);
9131 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009132
Michael Chanb9ec6c12006-07-25 16:37:27 -07009133 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134}
9135
9136/* tp->lock is held. */
9137static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9138 dma_addr_t mapping, u32 maxlen_flags,
9139 u32 nic_addr)
9140{
9141 tg3_write_mem(tp,
9142 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9143 ((u64) mapping >> 32));
9144 tg3_write_mem(tp,
9145 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9146 ((u64) mapping & 0xffffffff));
9147 tg3_write_mem(tp,
9148 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9149 maxlen_flags);
9150
Joe Perches63c3a662011-04-26 08:12:10 +00009151 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152 tg3_write_mem(tp,
9153 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9154 nic_addr);
9155}
9156
Michael Chana489b6d2012-09-28 07:12:39 +00009157
9158static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009159{
Michael Chana489b6d2012-09-28 07:12:39 +00009160 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009161
Joe Perches63c3a662011-04-26 08:12:10 +00009162 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009163 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9164 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9165 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009166 } else {
9167 tw32(HOSTCC_TXCOL_TICKS, 0);
9168 tw32(HOSTCC_TXMAX_FRAMES, 0);
9169 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009170
9171 for (; i < tp->txq_cnt; i++) {
9172 u32 reg;
9173
9174 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9175 tw32(reg, ec->tx_coalesce_usecs);
9176 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9177 tw32(reg, ec->tx_max_coalesced_frames);
9178 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9179 tw32(reg, ec->tx_max_coalesced_frames_irq);
9180 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009181 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009182
Michael Chana489b6d2012-09-28 07:12:39 +00009183 for (; i < tp->irq_max - 1; i++) {
9184 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9185 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9186 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9187 }
9188}
9189
9190static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9191{
9192 int i = 0;
9193 u32 limit = tp->rxq_cnt;
9194
Joe Perches63c3a662011-04-26 08:12:10 +00009195 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009196 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9197 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9198 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009199 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009200 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009201 tw32(HOSTCC_RXCOL_TICKS, 0);
9202 tw32(HOSTCC_RXMAX_FRAMES, 0);
9203 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009204 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009205
Michael Chana489b6d2012-09-28 07:12:39 +00009206 for (; i < limit; i++) {
9207 u32 reg;
9208
9209 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9210 tw32(reg, ec->rx_coalesce_usecs);
9211 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9212 tw32(reg, ec->rx_max_coalesced_frames);
9213 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9214 tw32(reg, ec->rx_max_coalesced_frames_irq);
9215 }
9216
9217 for (; i < tp->irq_max - 1; i++) {
9218 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9219 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9220 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9221 }
9222}
9223
9224static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9225{
9226 tg3_coal_tx_init(tp, ec);
9227 tg3_coal_rx_init(tp, ec);
9228
Joe Perches63c3a662011-04-26 08:12:10 +00009229 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009230 u32 val = ec->stats_block_coalesce_usecs;
9231
Matt Carlsonb6080e12009-09-01 13:12:00 +00009232 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9233 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9234
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009235 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009236 val = 0;
9237
9238 tw32(HOSTCC_STAT_COAL_TICKS, val);
9239 }
9240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241
9242/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009243static void tg3_tx_rcbs_init(struct tg3 *tp)
9244{
9245 int i = 0;
9246 u32 txrcb = NIC_SRAM_SEND_RCB;
9247
9248 if (tg3_flag(tp, ENABLE_TSS))
9249 i++;
9250
9251 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9252 struct tg3_napi *tnapi = &tp->napi[i];
9253
9254 if (!tnapi->tx_ring)
9255 continue;
9256
9257 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9258 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9259 NIC_SRAM_TX_BUFFER_DESC);
9260 }
9261}
9262
9263/* tp->lock is held. */
9264static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9265{
9266 int i = 0;
9267 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9268
9269 if (tg3_flag(tp, ENABLE_RSS))
9270 i++;
9271
9272 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9273 struct tg3_napi *tnapi = &tp->napi[i];
9274
9275 if (!tnapi->rx_rcb)
9276 continue;
9277
9278 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9279 (tp->rx_ret_ring_mask + 1) <<
9280 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9281 }
9282}
9283
9284/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009285static void tg3_rings_reset(struct tg3 *tp)
9286{
9287 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009288 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009289 struct tg3_napi *tnapi = &tp->napi[0];
9290
9291 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00009292 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009293 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009294 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00009295 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00009296 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009297 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00009298 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009299 else
9300 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9301
9302 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9303 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9304 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9305 BDINFO_FLAGS_DISABLED);
9306
9307
9308 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00009309 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009310 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00009311 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009312 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches41535772013-02-16 11:20:04 +00009313 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9314 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009315 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009316 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9317 else
9318 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9319
9320 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9321 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9322 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9323 BDINFO_FLAGS_DISABLED);
9324
9325 /* Disable interrupts */
9326 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009327 tp->napi[0].chk_msi_cnt = 0;
9328 tp->napi[0].last_rx_cons = 0;
9329 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009330
9331 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009332 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009333 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009334 tp->napi[i].tx_prod = 0;
9335 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009336 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009337 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009338 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9339 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009340 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009341 tp->napi[i].last_rx_cons = 0;
9342 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009343 }
Joe Perches63c3a662011-04-26 08:12:10 +00009344 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009345 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009346 } else {
9347 tp->napi[0].tx_prod = 0;
9348 tp->napi[0].tx_cons = 0;
9349 tw32_mailbox(tp->napi[0].prodmbox, 0);
9350 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9351 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009352
9353 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009354 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009355 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9356 for (i = 0; i < 16; i++)
9357 tw32_tx_mbox(mbox + i * 8, 0);
9358 }
9359
Matt Carlson2d31eca2009-09-01 12:53:31 +00009360 /* Clear status block in ram. */
9361 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9362
9363 /* Set status block DMA address */
9364 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9365 ((u64) tnapi->status_mapping >> 32));
9366 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9367 ((u64) tnapi->status_mapping & 0xffffffff));
9368
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009369 stblk = HOSTCC_STATBLCK_RING1;
9370
9371 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9372 u64 mapping = (u64)tnapi->status_mapping;
9373 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9374 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009375 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009376
9377 /* Clear status block in ram. */
9378 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009379 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009380
9381 tg3_tx_rcbs_init(tp);
9382 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009383}
9384
Matt Carlsoneb07a942011-04-20 07:57:36 +00009385static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9386{
9387 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9388
Joe Perches63c3a662011-04-26 08:12:10 +00009389 if (!tg3_flag(tp, 5750_PLUS) ||
9390 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009391 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9392 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009393 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009394 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009395 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9396 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009397 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9398 else
9399 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9400
9401 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9402 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9403
9404 val = min(nic_rep_thresh, host_rep_thresh);
9405 tw32(RCVBDI_STD_THRESH, val);
9406
Joe Perches63c3a662011-04-26 08:12:10 +00009407 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009408 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9409
Joe Perches63c3a662011-04-26 08:12:10 +00009410 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009411 return;
9412
Matt Carlson513aa6e2011-11-21 15:01:18 +00009413 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009414
9415 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9416
9417 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9418 tw32(RCVBDI_JUMBO_THRESH, val);
9419
Joe Perches63c3a662011-04-26 08:12:10 +00009420 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009421 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9422}
9423
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009424static inline u32 calc_crc(unsigned char *buf, int len)
9425{
9426 u32 reg;
9427 u32 tmp;
9428 int j, k;
9429
9430 reg = 0xffffffff;
9431
9432 for (j = 0; j < len; j++) {
9433 reg ^= buf[j];
9434
9435 for (k = 0; k < 8; k++) {
9436 tmp = reg & 0x01;
9437
9438 reg >>= 1;
9439
9440 if (tmp)
9441 reg ^= 0xedb88320;
9442 }
9443 }
9444
9445 return ~reg;
9446}
9447
9448static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9449{
9450 /* accept or reject all multicast frames */
9451 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9452 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9453 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9454 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9455}
9456
9457static void __tg3_set_rx_mode(struct net_device *dev)
9458{
9459 struct tg3 *tp = netdev_priv(dev);
9460 u32 rx_mode;
9461
9462 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9463 RX_MODE_KEEP_VLAN_TAG);
9464
9465#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9466 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9467 * flag clear.
9468 */
9469 if (!tg3_flag(tp, ENABLE_ASF))
9470 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9471#endif
9472
9473 if (dev->flags & IFF_PROMISC) {
9474 /* Promiscuous mode. */
9475 rx_mode |= RX_MODE_PROMISC;
9476 } else if (dev->flags & IFF_ALLMULTI) {
9477 /* Accept all multicast. */
9478 tg3_set_multi(tp, 1);
9479 } else if (netdev_mc_empty(dev)) {
9480 /* Reject all multicast. */
9481 tg3_set_multi(tp, 0);
9482 } else {
9483 /* Accept one or more multicast(s). */
9484 struct netdev_hw_addr *ha;
9485 u32 mc_filter[4] = { 0, };
9486 u32 regidx;
9487 u32 bit;
9488 u32 crc;
9489
9490 netdev_for_each_mc_addr(ha, dev) {
9491 crc = calc_crc(ha->addr, ETH_ALEN);
9492 bit = ~crc & 0x7f;
9493 regidx = (bit & 0x60) >> 5;
9494 bit &= 0x1f;
9495 mc_filter[regidx] |= (1 << bit);
9496 }
9497
9498 tw32(MAC_HASH_REG_0, mc_filter[0]);
9499 tw32(MAC_HASH_REG_1, mc_filter[1]);
9500 tw32(MAC_HASH_REG_2, mc_filter[2]);
9501 tw32(MAC_HASH_REG_3, mc_filter[3]);
9502 }
9503
9504 if (rx_mode != tp->rx_mode) {
9505 tp->rx_mode = rx_mode;
9506 tw32_f(MAC_RX_MODE, rx_mode);
9507 udelay(10);
9508 }
9509}
9510
Michael Chan91024262012-09-28 07:12:38 +00009511static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009512{
9513 int i;
9514
9515 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009516 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009517}
9518
9519static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009520{
9521 int i;
9522
9523 if (!tg3_flag(tp, SUPPORT_MSIX))
9524 return;
9525
Michael Chan0b3ba052012-11-14 14:44:29 +00009526 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009527 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009528 return;
9529 }
9530
9531 /* Validate table against current IRQ count */
9532 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009533 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009534 break;
9535 }
9536
9537 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009538 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009539}
9540
Matt Carlson90415472011-12-16 13:33:23 +00009541static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009542{
9543 int i = 0;
9544 u32 reg = MAC_RSS_INDIR_TBL_0;
9545
9546 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9547 u32 val = tp->rss_ind_tbl[i];
9548 i++;
9549 for (; i % 8; i++) {
9550 val <<= 4;
9551 val |= tp->rss_ind_tbl[i];
9552 }
9553 tw32(reg, val);
9554 reg += 4;
9555 }
9556}
9557
Matt Carlson2d31eca2009-09-01 12:53:31 +00009558/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009559static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560{
9561 u32 val, rdmac_mode;
9562 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009563 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009564
9565 tg3_disable_ints(tp);
9566
9567 tg3_stop_fw(tp);
9568
9569 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9570
Joe Perches63c3a662011-04-26 08:12:10 +00009571 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009572 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009574 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9575 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9576 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009577 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009578 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9579 }
9580
Nithin Sujir400dfba2013-05-18 06:26:53 +00009581 /* Enable MAC control of LPI */
9582 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9583 tg3_setup_eee(tp);
9584
Matt Carlson603f1172010-02-12 14:47:10 +00009585 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009586 tg3_phy_reset(tp);
9587
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588 err = tg3_chip_reset(tp);
9589 if (err)
9590 return err;
9591
9592 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9593
Joe Perches41535772013-02-16 11:20:04 +00009594 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009595 val = tr32(TG3_CPMU_CTRL);
9596 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9597 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009598
9599 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9600 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9601 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9602 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9603
9604 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9605 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9606 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9607 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9608
9609 val = tr32(TG3_CPMU_HST_ACC);
9610 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9611 val |= CPMU_HST_ACC_MACCLK_6_25;
9612 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009613 }
9614
Joe Perches41535772013-02-16 11:20:04 +00009615 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d932009-04-20 06:57:41 +00009616 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9617 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9618 PCIE_PWR_MGMT_L1_THRESH_4MS;
9619 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009620
9621 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9622 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9623
9624 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00009625
Matt Carlsonf40386c2009-11-02 14:24:02 +00009626 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9627 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009628 }
9629
Joe Perches63c3a662011-04-26 08:12:10 +00009630 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009631 u32 grc_mode = tr32(GRC_MODE);
9632
9633 /* Access the lower 1K of PL PCIE block registers. */
9634 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9635 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9636
9637 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9638 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9639 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9640
9641 tw32(GRC_MODE, grc_mode);
9642 }
9643
Matt Carlson55086ad2011-12-14 11:09:59 +00009644 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009645 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009646 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009647
Matt Carlson5093eed2010-11-24 08:31:45 +00009648 /* Access the lower 1K of PL PCIE block registers. */
9649 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9650 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009651
Matt Carlson5093eed2010-11-24 08:31:45 +00009652 val = tr32(TG3_PCIE_TLDLPL_PORT +
9653 TG3_PCIE_PL_LO_PHYCTL5);
9654 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9655 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009656
Matt Carlson5093eed2010-11-24 08:31:45 +00009657 tw32(GRC_MODE, grc_mode);
9658 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009659
Joe Perches41535772013-02-16 11:20:04 +00009660 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009661 u32 grc_mode;
9662
9663 /* Fix transmit hangs */
9664 val = tr32(TG3_CPMU_PADRNG_CTL);
9665 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9666 tw32(TG3_CPMU_PADRNG_CTL, val);
9667
9668 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009669
9670 /* Access the lower 1K of DL PCIE block registers. */
9671 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9672 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9673
9674 val = tr32(TG3_PCIE_TLDLPL_PORT +
9675 TG3_PCIE_DL_LO_FTSMAX);
9676 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9677 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9678 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9679
9680 tw32(GRC_MODE, grc_mode);
9681 }
9682
Matt Carlsona977dbe2010-04-12 06:58:26 +00009683 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9684 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9685 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9686 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009687 }
9688
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689 /* This works around an issue with Athlon chipsets on
9690 * B3 tigon3 silicon. This bit has no effect on any
9691 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009692 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009693 */
Joe Perches63c3a662011-04-26 08:12:10 +00009694 if (!tg3_flag(tp, CPMU_PRESENT)) {
9695 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009696 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9697 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699
Joe Perches41535772013-02-16 11:20:04 +00009700 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009701 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702 val = tr32(TG3PCI_PCISTATE);
9703 val |= PCISTATE_RETRY_SAME_DMA;
9704 tw32(TG3PCI_PCISTATE, val);
9705 }
9706
Joe Perches63c3a662011-04-26 08:12:10 +00009707 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009708 /* Allow reads and writes to the
9709 * APE register and memory space.
9710 */
9711 val = tr32(TG3PCI_PCISTATE);
9712 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009713 PCISTATE_ALLOW_APE_SHMEM_WR |
9714 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009715 tw32(TG3PCI_PCISTATE, val);
9716 }
9717
Joe Perches41535772013-02-16 11:20:04 +00009718 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719 /* Enable some hw fixes. */
9720 val = tr32(TG3PCI_MSI_DATA);
9721 val |= (1 << 26) | (1 << 28) | (1 << 29);
9722 tw32(TG3PCI_MSI_DATA, val);
9723 }
9724
9725 /* Descriptor ring init may make accesses to the
9726 * NIC SRAM area to setup the TX descriptors, so we
9727 * can only do this after the hardware has been
9728 * successfully reset.
9729 */
Michael Chan32d8c572006-07-25 16:38:29 -07009730 err = tg3_init_rings(tp);
9731 if (err)
9732 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733
Joe Perches63c3a662011-04-26 08:12:10 +00009734 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009735 val = tr32(TG3PCI_DMA_RW_CTRL) &
9736 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009737 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009738 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009739 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009740 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9741 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009742 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009743 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009744 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9745 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009746 /* This value is determined during the probe time DMA
9747 * engine test, tg3_test_dma.
9748 */
9749 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751
9752 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9753 GRC_MODE_4X_NIC_SEND_RINGS |
9754 GRC_MODE_NO_TX_PHDR_CSUM |
9755 GRC_MODE_NO_RX_PHDR_CSUM);
9756 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009757
9758 /* Pseudo-header checksum is done by hardware logic and not
9759 * the offload processers, so make the chip do the pseudo-
9760 * header checksums on receive. For transmit it is more
9761 * convenient to do the pseudo-header checksum in software
9762 * as Linux does that on transmit for us in all cases.
9763 */
9764 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009765
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009766 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9767 if (tp->rxptpctl)
9768 tw32(TG3_RX_PTP_CTL,
9769 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9770
9771 if (tg3_flag(tp, PTP_CAPABLE))
9772 val |= GRC_MODE_TIME_SYNC_ENABLE;
9773
9774 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775
9776 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9777 val = tr32(GRC_MISC_CFG);
9778 val &= ~0xff;
9779 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9780 tw32(GRC_MISC_CFG, val);
9781
9782 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009783 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009785 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009787 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9789 else
9790 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9791 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9792 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009793 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794 int fw_len;
9795
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009796 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9798 tw32(BUFMGR_MB_POOL_ADDR,
9799 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9800 tw32(BUFMGR_MB_POOL_SIZE,
9801 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803
Michael Chan0f893dc2005-07-25 12:30:38 -07009804 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9806 tp->bufmgr_config.mbuf_read_dma_low_water);
9807 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9808 tp->bufmgr_config.mbuf_mac_rx_low_water);
9809 tw32(BUFMGR_MB_HIGH_WATER,
9810 tp->bufmgr_config.mbuf_high_water);
9811 } else {
9812 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9813 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9814 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9815 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9816 tw32(BUFMGR_MB_HIGH_WATER,
9817 tp->bufmgr_config.mbuf_high_water_jumbo);
9818 }
9819 tw32(BUFMGR_DMA_LOW_WATER,
9820 tp->bufmgr_config.dma_low_water);
9821 tw32(BUFMGR_DMA_HIGH_WATER,
9822 tp->bufmgr_config.dma_high_water);
9823
Matt Carlsond309a462010-09-30 10:34:31 +00009824 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009825 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009826 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009827 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9828 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9829 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009830 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009831 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 for (i = 0; i < 2000; i++) {
9833 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9834 break;
9835 udelay(10);
9836 }
9837 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009838 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 return -ENODEV;
9840 }
9841
Joe Perches41535772013-02-16 11:20:04 +00009842 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009843 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009844
Matt Carlsoneb07a942011-04-20 07:57:36 +00009845 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846
9847 /* Initialize TG3_BDINFO's at:
9848 * RCVDBDI_STD_BD: standard eth size rx ring
9849 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9850 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9851 *
9852 * like so:
9853 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9854 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9855 * ring attribute flags
9856 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9857 *
9858 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9859 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9860 *
9861 * The size of each ring is fixed in the firmware, but the location is
9862 * configurable.
9863 */
9864 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009865 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009866 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009867 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009868 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009869 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9870 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009872 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009873 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9875 BDINFO_FLAGS_DISABLED);
9876
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009877 /* Program the jumbo buffer descriptor ring control
9878 * blocks on those devices that have them.
9879 */
Joe Perches41535772013-02-16 11:20:04 +00009880 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009881 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009882
Joe Perches63c3a662011-04-26 08:12:10 +00009883 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009885 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009887 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009888 val = TG3_RX_JMB_RING_SIZE(tp) <<
9889 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009891 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009892 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009893 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009894 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009895 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9896 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897 } else {
9898 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9899 BDINFO_FLAGS_DISABLED);
9900 }
9901
Joe Perches63c3a662011-04-26 08:12:10 +00009902 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009903 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009904 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9905 val |= (TG3_RX_STD_DMA_SZ << 2);
9906 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009907 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009908 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009909 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009910
9911 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009912
Matt Carlson411da642009-11-13 13:03:46 +00009913 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00009914 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009915
Joe Perches63c3a662011-04-26 08:12:10 +00009916 tpr->rx_jmb_prod_idx =
9917 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00009918 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009919
Matt Carlson2d31eca2009-09-01 12:53:31 +00009920 tg3_rings_reset(tp);
9921
Linus Torvalds1da177e2005-04-16 15:20:36 -07009922 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +00009923 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924
9925 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009926 tw32(MAC_RX_MTU_SIZE,
9927 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009928
9929 /* The slot time is changed by tg3_setup_phy if we
9930 * run at gigabit with half duplex.
9931 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009932 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9933 (6 << TX_LENGTHS_IPG_SHIFT) |
9934 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9935
Joe Perches41535772013-02-16 11:20:04 +00009936 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9937 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009938 val |= tr32(MAC_TX_LENGTHS) &
9939 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9940 TX_LENGTHS_CNT_DWN_VAL_MSK);
9941
9942 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943
9944 /* Receive rules. */
9945 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9946 tw32(RCVLPC_CONFIG, 0x0181);
9947
9948 /* Calculate RDMAC_MODE setting early, we need it to determine
9949 * the RCVLPC_STATE_ENABLE mask.
9950 */
9951 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9952 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9953 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9954 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9955 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009956
Joe Perches41535772013-02-16 11:20:04 +00009957 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009958 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9959
Joe Perches41535772013-02-16 11:20:04 +00009960 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
9961 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9962 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009963 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9964 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9965 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9966
Joe Perches41535772013-02-16 11:20:04 +00009967 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9968 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009969 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009970 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9972 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009973 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9975 }
9976 }
9977
Joe Perches63c3a662011-04-26 08:12:10 +00009978 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009979 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9980
Joe Perches41535772013-02-16 11:20:04 +00009981 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009982 tp->dma_limit = 0;
9983 if (tp->dev->mtu <= ETH_DATA_LEN) {
9984 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
9985 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
9986 }
9987 }
9988
Joe Perches63c3a662011-04-26 08:12:10 +00009989 if (tg3_flag(tp, HW_TSO_1) ||
9990 tg3_flag(tp, HW_TSO_2) ||
9991 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009992 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9993
Matt Carlson108a6c12011-05-19 12:12:47 +00009994 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009995 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9996 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -08009997 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009998
Joe Perches41535772013-02-16 11:20:04 +00009999 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10000 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010001 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10002
Joe Perches41535772013-02-16 11:20:04 +000010003 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10004 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10005 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10006 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010007 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010008 u32 tgtreg;
10009
Joe Perches41535772013-02-16 11:20:04 +000010010 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010011 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10012 else
10013 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10014
10015 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010016 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10017 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010018 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10019 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10020 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10021 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10022 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10023 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010024 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010025 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010026 }
10027
Joe Perches41535772013-02-16 11:20:04 +000010028 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10029 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10030 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010031 u32 tgtreg;
10032
Joe Perches41535772013-02-16 11:20:04 +000010033 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010034 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10035 else
10036 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10037
10038 val = tr32(tgtreg);
10039 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010040 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10041 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10042 }
10043
Linus Torvalds1da177e2005-04-16 15:20:36 -070010044 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010045 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010046 val = tr32(RCVLPC_STATS_ENABLE);
10047 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10048 tw32(RCVLPC_STATS_ENABLE, val);
10049 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010050 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051 val = tr32(RCVLPC_STATS_ENABLE);
10052 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10053 tw32(RCVLPC_STATS_ENABLE, val);
10054 } else {
10055 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10056 }
10057 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10058 tw32(SNDDATAI_STATSENAB, 0xffffff);
10059 tw32(SNDDATAI_STATSCTRL,
10060 (SNDDATAI_SCTRL_ENABLE |
10061 SNDDATAI_SCTRL_FASTUPD));
10062
10063 /* Setup host coalescing engine. */
10064 tw32(HOSTCC_MODE, 0);
10065 for (i = 0; i < 2000; i++) {
10066 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10067 break;
10068 udelay(10);
10069 }
10070
Michael Chand244c892005-07-05 14:42:33 -070010071 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072
Joe Perches63c3a662011-04-26 08:12:10 +000010073 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074 /* Status/statistics block address. See tg3_timer,
10075 * the tg3_periodic_fetch_stats call there, and
10076 * tg3_get_stats to see how this works for 5705/5750 chips.
10077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010078 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10079 ((u64) tp->stats_mapping >> 32));
10080 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10081 ((u64) tp->stats_mapping & 0xffffffff));
10082 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010083
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010085
10086 /* Clear statistics and status block memory areas */
10087 for (i = NIC_SRAM_STATS_BLK;
10088 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10089 i += sizeof(u32)) {
10090 tg3_write_mem(tp, i, 0);
10091 udelay(40);
10092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093 }
10094
10095 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10096
10097 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10098 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010099 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10101
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010102 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10103 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010104 /* reset to prevent losing 1st rx packet intermittently */
10105 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10106 udelay(10);
10107 }
10108
Matt Carlson3bda1252008-08-15 14:08:22 -070010109 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010110 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10111 MAC_MODE_FHDE_ENABLE;
10112 if (tg3_flag(tp, ENABLE_APE))
10113 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010114 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010115 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010116 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010117 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10119 udelay(40);
10120
Michael Chan314fba32005-04-21 17:07:04 -070010121 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010122 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010123 * register to preserve the GPIO settings for LOMs. The GPIOs,
10124 * whether used as inputs or outputs, are set by boot code after
10125 * reset.
10126 */
Joe Perches63c3a662011-04-26 08:12:10 +000010127 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010128 u32 gpio_mask;
10129
Michael Chan9d26e212006-12-07 00:21:14 -080010130 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10131 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10132 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010133
Joe Perches41535772013-02-16 11:20:04 +000010134 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010135 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10136 GRC_LCLCTRL_GPIO_OUTPUT3;
10137
Joe Perches41535772013-02-16 11:20:04 +000010138 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010139 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10140
Gary Zambranoaaf84462007-05-05 11:51:45 -070010141 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010142 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10143
10144 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010145 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010146 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10147 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10150 udelay(100);
10151
Matt Carlsonc3b50032012-01-17 15:27:23 +000010152 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010153 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010154 val |= MSGINT_MODE_ENABLE;
10155 if (tp->irq_cnt > 1)
10156 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010157 if (!tg3_flag(tp, 1SHOT_MSI))
10158 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010159 tw32(MSGINT_MODE, val);
10160 }
10161
Joe Perches63c3a662011-04-26 08:12:10 +000010162 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010163 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10164 udelay(40);
10165 }
10166
10167 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10168 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10169 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10170 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10171 WDMAC_MODE_LNGREAD_ENAB);
10172
Joe Perches41535772013-02-16 11:20:04 +000010173 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10174 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010175 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010176 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10177 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178 /* nothing */
10179 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010180 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010181 val |= WDMAC_MODE_RX_ACCEL;
10182 }
10183 }
10184
Michael Chand9ab5ad12006-03-20 22:27:35 -080010185 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010186 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010187 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010188
Joe Perches41535772013-02-16 11:20:04 +000010189 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010190 val |= WDMAC_MODE_BURST_ALL_DATA;
10191
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192 tw32_f(WDMAC_MODE, val);
10193 udelay(40);
10194
Joe Perches63c3a662011-04-26 08:12:10 +000010195 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010196 u16 pcix_cmd;
10197
10198 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10199 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010200 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010201 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10202 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010203 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010204 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10205 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206 }
Matt Carlson9974a352007-10-07 23:27:28 -070010207 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10208 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209 }
10210
10211 tw32_f(RDMAC_MODE, rdmac_mode);
10212 udelay(40);
10213
Joe Perches41535772013-02-16 11:20:04 +000010214 if (tg3_asic_rev(tp) == ASIC_REV_5719) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010215 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10216 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10217 break;
10218 }
10219 if (i < TG3_NUM_RDMA_CHANNELS) {
10220 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10221 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
10222 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10223 tg3_flag_set(tp, 5719_RDMA_BUG);
10224 }
10225 }
10226
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010228 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010229 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010230
Joe Perches41535772013-02-16 11:20:04 +000010231 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010232 tw32(SNDDATAC_MODE,
10233 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10234 else
10235 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10236
Linus Torvalds1da177e2005-04-16 15:20:36 -070010237 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10238 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010239 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010240 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010241 val |= RCVDBDI_MODE_LRG_RING_SZ;
10242 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010244 if (tg3_flag(tp, HW_TSO_1) ||
10245 tg3_flag(tp, HW_TSO_2) ||
10246 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010248 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010249 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010250 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10251 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010252 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10253
Joe Perches41535772013-02-16 11:20:04 +000010254 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 err = tg3_load_5701_a0_firmware_fix(tp);
10256 if (err)
10257 return err;
10258 }
10259
Nithin Sujirc4dab502013-03-06 17:02:34 +000010260 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10261 /* Ignore any errors for the firmware download. If download
10262 * fails, the device will operate with EEE disabled
10263 */
10264 tg3_load_57766_firmware(tp);
10265 }
10266
Joe Perches63c3a662011-04-26 08:12:10 +000010267 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010268 err = tg3_load_tso_firmware(tp);
10269 if (err)
10270 return err;
10271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010272
10273 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010274
Joe Perches63c3a662011-04-26 08:12:10 +000010275 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010276 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010277 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010278
Joe Perches41535772013-02-16 11:20:04 +000010279 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10280 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010281 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10282 tp->tx_mode &= ~val;
10283 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10284 }
10285
Linus Torvalds1da177e2005-04-16 15:20:36 -070010286 tw32_f(MAC_TX_MODE, tp->tx_mode);
10287 udelay(100);
10288
Joe Perches63c3a662011-04-26 08:12:10 +000010289 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010290 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010291
10292 /* Setup the "secret" hash key. */
10293 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10294 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10295 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10296 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10297 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10298 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10299 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10300 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10301 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10302 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10303 }
10304
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010306 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010307 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10308
Joe Perches63c3a662011-04-26 08:12:10 +000010309 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010310 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10311 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10312 RX_MODE_RSS_IPV6_HASH_EN |
10313 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10314 RX_MODE_RSS_IPV4_HASH_EN |
10315 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10316
Linus Torvalds1da177e2005-04-16 15:20:36 -070010317 tw32_f(MAC_RX_MODE, tp->rx_mode);
10318 udelay(10);
10319
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320 tw32(MAC_LED_CTRL, tp->led_ctrl);
10321
10322 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010323 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10325 udelay(10);
10326 }
10327 tw32_f(MAC_RX_MODE, tp->rx_mode);
10328 udelay(10);
10329
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010330 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010331 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10332 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010333 /* Set drive transmission level to 1.2V */
10334 /* only if the signal pre-emphasis bit is not set */
10335 val = tr32(MAC_SERDES_CFG);
10336 val &= 0xfffff000;
10337 val |= 0x880;
10338 tw32(MAC_SERDES_CFG, val);
10339 }
Joe Perches41535772013-02-16 11:20:04 +000010340 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010341 tw32(MAC_SERDES_CFG, 0x616000);
10342 }
10343
10344 /* Prevent chip from dropping frames when flow control
10345 * is enabled.
10346 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010347 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010348 val = 1;
10349 else
10350 val = 2;
10351 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352
Joe Perches41535772013-02-16 11:20:04 +000010353 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010354 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010355 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010356 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010357 }
10358
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010359 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010360 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010361 u32 tmp;
10362
10363 tmp = tr32(SERDES_RX_CTRL);
10364 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10365 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10366 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10367 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10368 }
10369
Joe Perches63c3a662011-04-26 08:12:10 +000010370 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010371 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010372 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010373
Joe Perches953c96e2013-04-09 10:18:14 +000010374 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010375 if (err)
10376 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010377
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010378 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10379 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010380 u32 tmp;
10381
10382 /* Clear CRC stats. */
10383 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10384 tg3_writephy(tp, MII_TG3_TEST1,
10385 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010386 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010388 }
10389 }
10390
10391 __tg3_set_rx_mode(tp->dev);
10392
10393 /* Initialize receive rules. */
10394 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10395 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10396 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10397 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10398
Joe Perches63c3a662011-04-26 08:12:10 +000010399 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400 limit = 8;
10401 else
10402 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010403 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404 limit -= 4;
10405 switch (limit) {
10406 case 16:
10407 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10408 case 15:
10409 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10410 case 14:
10411 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10412 case 13:
10413 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10414 case 12:
10415 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10416 case 11:
10417 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10418 case 10:
10419 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10420 case 9:
10421 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10422 case 8:
10423 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10424 case 7:
10425 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10426 case 6:
10427 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10428 case 5:
10429 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10430 case 4:
10431 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10432 case 3:
10433 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10434 case 2:
10435 case 1:
10436
10437 default:
10438 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440
Joe Perches63c3a662011-04-26 08:12:10 +000010441 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010442 /* Write our heartbeat update interval to APE. */
10443 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10444 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010445
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10447
Linus Torvalds1da177e2005-04-16 15:20:36 -070010448 return 0;
10449}
10450
10451/* Called at device open time to get the chip ready for
10452 * packet processing. Invoked with tp->lock held.
10453 */
Joe Perches953c96e2013-04-09 10:18:14 +000010454static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010455{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010456 tg3_switch_clocks(tp);
10457
10458 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10459
Matt Carlson2f751b62008-08-04 23:17:34 -070010460 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461}
10462
Michael Chanaed93e02012-07-16 16:24:02 +000010463static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10464{
10465 int i;
10466
10467 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10468 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10469
10470 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10471 off += len;
10472
10473 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10474 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10475 memset(ocir, 0, TG3_OCIR_LEN);
10476 }
10477}
10478
10479/* sysfs attributes for hwmon */
10480static ssize_t tg3_show_temp(struct device *dev,
10481 struct device_attribute *devattr, char *buf)
10482{
10483 struct pci_dev *pdev = to_pci_dev(dev);
10484 struct net_device *netdev = pci_get_drvdata(pdev);
10485 struct tg3 *tp = netdev_priv(netdev);
10486 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10487 u32 temperature;
10488
10489 spin_lock_bh(&tp->lock);
10490 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10491 sizeof(temperature));
10492 spin_unlock_bh(&tp->lock);
10493 return sprintf(buf, "%u\n", temperature);
10494}
10495
10496
10497static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10498 TG3_TEMP_SENSOR_OFFSET);
10499static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10500 TG3_TEMP_CAUTION_OFFSET);
10501static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10502 TG3_TEMP_MAX_OFFSET);
10503
10504static struct attribute *tg3_attributes[] = {
10505 &sensor_dev_attr_temp1_input.dev_attr.attr,
10506 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10507 &sensor_dev_attr_temp1_max.dev_attr.attr,
10508 NULL
10509};
10510
10511static const struct attribute_group tg3_group = {
10512 .attrs = tg3_attributes,
10513};
10514
Michael Chanaed93e02012-07-16 16:24:02 +000010515static void tg3_hwmon_close(struct tg3 *tp)
10516{
Michael Chanaed93e02012-07-16 16:24:02 +000010517 if (tp->hwmon_dev) {
10518 hwmon_device_unregister(tp->hwmon_dev);
10519 tp->hwmon_dev = NULL;
10520 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10521 }
Michael Chanaed93e02012-07-16 16:24:02 +000010522}
10523
10524static void tg3_hwmon_open(struct tg3 *tp)
10525{
Michael Chanaed93e02012-07-16 16:24:02 +000010526 int i, err;
10527 u32 size = 0;
10528 struct pci_dev *pdev = tp->pdev;
10529 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10530
10531 tg3_sd_scan_scratchpad(tp, ocirs);
10532
10533 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10534 if (!ocirs[i].src_data_length)
10535 continue;
10536
10537 size += ocirs[i].src_hdr_length;
10538 size += ocirs[i].src_data_length;
10539 }
10540
10541 if (!size)
10542 return;
10543
10544 /* Register hwmon sysfs hooks */
10545 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10546 if (err) {
10547 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10548 return;
10549 }
10550
10551 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10552 if (IS_ERR(tp->hwmon_dev)) {
10553 tp->hwmon_dev = NULL;
10554 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10555 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10556 }
Michael Chanaed93e02012-07-16 16:24:02 +000010557}
10558
10559
Linus Torvalds1da177e2005-04-16 15:20:36 -070010560#define TG3_STAT_ADD32(PSTAT, REG) \
10561do { u32 __val = tr32(REG); \
10562 (PSTAT)->low += __val; \
10563 if ((PSTAT)->low < __val) \
10564 (PSTAT)->high += 1; \
10565} while (0)
10566
10567static void tg3_periodic_fetch_stats(struct tg3 *tp)
10568{
10569 struct tg3_hw_stats *sp = tp->hw_stats;
10570
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010571 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010572 return;
10573
10574 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10575 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10576 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10577 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10578 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10579 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10580 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10581 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10582 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10583 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10584 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10585 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10586 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010587 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10588 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10589 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10590 u32 val;
10591
10592 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10593 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10594 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10595 tg3_flag_clear(tp, 5719_RDMA_BUG);
10596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597
10598 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10599 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10600 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10601 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10602 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10603 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10604 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10605 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10606 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10607 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10608 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10609 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10610 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10611 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010612
10613 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010614 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10615 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10616 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010617 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10618 } else {
10619 u32 val = tr32(HOSTCC_FLOW_ATTN);
10620 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10621 if (val) {
10622 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10623 sp->rx_discards.low += val;
10624 if (sp->rx_discards.low < val)
10625 sp->rx_discards.high += 1;
10626 }
10627 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10628 }
Michael Chan463d3052006-05-22 16:36:27 -070010629 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010630}
10631
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010632static void tg3_chk_missed_msi(struct tg3 *tp)
10633{
10634 u32 i;
10635
10636 for (i = 0; i < tp->irq_cnt; i++) {
10637 struct tg3_napi *tnapi = &tp->napi[i];
10638
10639 if (tg3_has_work(tnapi)) {
10640 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10641 tnapi->last_tx_cons == tnapi->tx_cons) {
10642 if (tnapi->chk_msi_cnt < 1) {
10643 tnapi->chk_msi_cnt++;
10644 return;
10645 }
Matt Carlson7f230732011-08-31 11:44:48 +000010646 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010647 }
10648 }
10649 tnapi->chk_msi_cnt = 0;
10650 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10651 tnapi->last_tx_cons = tnapi->tx_cons;
10652 }
10653}
10654
Linus Torvalds1da177e2005-04-16 15:20:36 -070010655static void tg3_timer(unsigned long __opaque)
10656{
10657 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010658
Matt Carlson5b190622011-11-04 09:15:04 +000010659 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010660 goto restart_timer;
10661
David S. Millerf47c11e2005-06-24 20:18:35 -070010662 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010663
Joe Perches41535772013-02-16 11:20:04 +000010664 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010665 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010666 tg3_chk_missed_msi(tp);
10667
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010668 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10669 /* BCM4785: Flush posted writes from GbE to host memory. */
10670 tr32(HOSTCC_MODE);
10671 }
10672
Joe Perches63c3a662011-04-26 08:12:10 +000010673 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010674 /* All of this garbage is because when using non-tagged
10675 * IRQ status the mailbox/status_block protocol the chip
10676 * uses with the cpu is race prone.
10677 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010678 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010679 tw32(GRC_LOCAL_CTRL,
10680 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10681 } else {
10682 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010683 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010685
David S. Millerfac9b832005-05-18 22:46:34 -070010686 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010687 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010688 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010689 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010691 }
10692
Linus Torvalds1da177e2005-04-16 15:20:36 -070010693 /* This part only runs once per second. */
10694 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010695 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010696 tg3_periodic_fetch_stats(tp);
10697
Matt Carlsonb0c59432011-05-19 12:12:48 +000010698 if (tp->setlpicnt && !--tp->setlpicnt)
10699 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010700
Joe Perches63c3a662011-04-26 08:12:10 +000010701 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702 u32 mac_stat;
10703 int phy_event;
10704
10705 mac_stat = tr32(MAC_STATUS);
10706
10707 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010708 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010709 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10710 phy_event = 1;
10711 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10712 phy_event = 1;
10713
10714 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010715 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010716 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010717 u32 mac_stat = tr32(MAC_STATUS);
10718 int need_setup = 0;
10719
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010720 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010721 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10722 need_setup = 1;
10723 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010724 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010725 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10726 MAC_STATUS_SIGNAL_DET))) {
10727 need_setup = 1;
10728 }
10729 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010730 if (!tp->serdes_counter) {
10731 tw32_f(MAC_MODE,
10732 (tp->mac_mode &
10733 ~MAC_MODE_PORT_MODE_MASK));
10734 udelay(40);
10735 tw32_f(MAC_MODE, tp->mac_mode);
10736 udelay(40);
10737 }
Joe Perches953c96e2013-04-09 10:18:14 +000010738 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010740 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010741 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010742 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010744
10745 tp->timer_counter = tp->timer_multiplier;
10746 }
10747
Michael Chan130b8e42006-09-27 16:00:40 -070010748 /* Heartbeat is only sent once every 2 seconds.
10749 *
10750 * The heartbeat is to tell the ASF firmware that the host
10751 * driver is still alive. In the event that the OS crashes,
10752 * ASF needs to reset the hardware to free up the FIFO space
10753 * that may be filled with rx packets destined for the host.
10754 * If the FIFO is full, ASF will no longer function properly.
10755 *
10756 * Unintended resets have been reported on real time kernels
10757 * where the timer doesn't run on time. Netpoll will also have
10758 * same problem.
10759 *
10760 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10761 * to check the ring condition when the heartbeat is expiring
10762 * before doing the reset. This will prevent most unintended
10763 * resets.
10764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010765 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010766 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010767 tg3_wait_for_event_ack(tp);
10768
Michael Chanbbadf502006-04-06 21:46:34 -070010769 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010770 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010771 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010772 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10773 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010774
10775 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010776 }
10777 tp->asf_counter = tp->asf_multiplier;
10778 }
10779
David S. Millerf47c11e2005-06-24 20:18:35 -070010780 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010781
Michael Chanf475f162006-03-27 23:20:14 -080010782restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010783 tp->timer.expires = jiffies + tp->timer_offset;
10784 add_timer(&tp->timer);
10785}
10786
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010787static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010788{
10789 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010790 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010791 !tg3_flag(tp, 57765_CLASS))
10792 tp->timer_offset = HZ;
10793 else
10794 tp->timer_offset = HZ / 10;
10795
10796 BUG_ON(tp->timer_offset > HZ);
10797
10798 tp->timer_multiplier = (HZ / tp->timer_offset);
10799 tp->asf_multiplier = (HZ / tp->timer_offset) *
10800 TG3_FW_UPDATE_FREQ_SEC;
10801
10802 init_timer(&tp->timer);
10803 tp->timer.data = (unsigned long) tp;
10804 tp->timer.function = tg3_timer;
10805}
10806
10807static void tg3_timer_start(struct tg3 *tp)
10808{
10809 tp->asf_counter = tp->asf_multiplier;
10810 tp->timer_counter = tp->timer_multiplier;
10811
10812 tp->timer.expires = jiffies + tp->timer_offset;
10813 add_timer(&tp->timer);
10814}
10815
10816static void tg3_timer_stop(struct tg3 *tp)
10817{
10818 del_timer_sync(&tp->timer);
10819}
10820
10821/* Restart hardware after configuration changes, self-test, etc.
10822 * Invoked with tp->lock held.
10823 */
Joe Perches953c96e2013-04-09 10:18:14 +000010824static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000010825 __releases(tp->lock)
10826 __acquires(tp->lock)
10827{
10828 int err;
10829
10830 err = tg3_init_hw(tp, reset_phy);
10831 if (err) {
10832 netdev_err(tp->dev,
10833 "Failed to re-initialize device, aborting\n");
10834 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10835 tg3_full_unlock(tp);
10836 tg3_timer_stop(tp);
10837 tp->irq_sync = 0;
10838 tg3_napi_enable(tp);
10839 dev_close(tp->dev);
10840 tg3_full_lock(tp, 0);
10841 }
10842 return err;
10843}
10844
10845static void tg3_reset_task(struct work_struct *work)
10846{
10847 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10848 int err;
10849
10850 tg3_full_lock(tp, 0);
10851
10852 if (!netif_running(tp->dev)) {
10853 tg3_flag_clear(tp, RESET_TASK_PENDING);
10854 tg3_full_unlock(tp);
10855 return;
10856 }
10857
10858 tg3_full_unlock(tp);
10859
10860 tg3_phy_stop(tp);
10861
10862 tg3_netif_stop(tp);
10863
10864 tg3_full_lock(tp, 1);
10865
10866 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10867 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10868 tp->write32_rx_mbox = tg3_write_flush_reg32;
10869 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10870 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10871 }
10872
10873 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000010874 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000010875 if (err)
10876 goto out;
10877
10878 tg3_netif_start(tp);
10879
10880out:
10881 tg3_full_unlock(tp);
10882
10883 if (!err)
10884 tg3_phy_start(tp);
10885
10886 tg3_flag_clear(tp, RESET_TASK_PENDING);
10887}
10888
Matt Carlson4f125f42009-09-01 12:55:02 +000010889static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010890{
David Howells7d12e782006-10-05 14:55:46 +010010891 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010892 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010893 char *name;
10894 struct tg3_napi *tnapi = &tp->napi[irq_num];
10895
10896 if (tp->irq_cnt == 1)
10897 name = tp->dev->name;
10898 else {
10899 name = &tnapi->irq_lbl[0];
10900 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10901 name[IFNAMSIZ-1] = 0;
10902 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010903
Joe Perches63c3a662011-04-26 08:12:10 +000010904 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010905 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010906 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010907 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010908 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010909 } else {
10910 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010911 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010912 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010913 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010914 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010915
10916 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010917}
10918
Michael Chan79381092005-04-21 17:13:59 -070010919static int tg3_test_interrupt(struct tg3 *tp)
10920{
Matt Carlson09943a12009-08-28 14:01:57 +000010921 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010922 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010923 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010924 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010925
Michael Chand4bc3922005-05-29 14:59:20 -070010926 if (!netif_running(dev))
10927 return -ENODEV;
10928
Michael Chan79381092005-04-21 17:13:59 -070010929 tg3_disable_ints(tp);
10930
Matt Carlson4f125f42009-09-01 12:55:02 +000010931 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010932
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010933 /*
10934 * Turn off MSI one shot mode. Otherwise this test has no
10935 * observable way to know whether the interrupt was delivered.
10936 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010937 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010938 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10939 tw32(MSGINT_MODE, val);
10940 }
10941
Matt Carlson4f125f42009-09-01 12:55:02 +000010942 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010943 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010944 if (err)
10945 return err;
10946
Matt Carlson898a56f2009-08-28 14:02:40 +000010947 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010948 tg3_enable_ints(tp);
10949
10950 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010951 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010952
10953 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010954 u32 int_mbox, misc_host_ctrl;
10955
Matt Carlson898a56f2009-08-28 14:02:40 +000010956 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010957 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10958
10959 if ((int_mbox != 0) ||
10960 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10961 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010962 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010963 }
10964
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010965 if (tg3_flag(tp, 57765_PLUS) &&
10966 tnapi->hw_status->status_tag != tnapi->last_tag)
10967 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10968
Michael Chan79381092005-04-21 17:13:59 -070010969 msleep(10);
10970 }
10971
10972 tg3_disable_ints(tp);
10973
Matt Carlson4f125f42009-09-01 12:55:02 +000010974 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010975
Matt Carlson4f125f42009-09-01 12:55:02 +000010976 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010977
10978 if (err)
10979 return err;
10980
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010981 if (intr_ok) {
10982 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010983 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010984 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10985 tw32(MSGINT_MODE, val);
10986 }
Michael Chan79381092005-04-21 17:13:59 -070010987 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010988 }
Michael Chan79381092005-04-21 17:13:59 -070010989
10990 return -EIO;
10991}
10992
10993/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10994 * successfully restored
10995 */
10996static int tg3_test_msi(struct tg3 *tp)
10997{
Michael Chan79381092005-04-21 17:13:59 -070010998 int err;
10999 u16 pci_cmd;
11000
Joe Perches63c3a662011-04-26 08:12:10 +000011001 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011002 return 0;
11003
11004 /* Turn off SERR reporting in case MSI terminates with Master
11005 * Abort.
11006 */
11007 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11008 pci_write_config_word(tp->pdev, PCI_COMMAND,
11009 pci_cmd & ~PCI_COMMAND_SERR);
11010
11011 err = tg3_test_interrupt(tp);
11012
11013 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11014
11015 if (!err)
11016 return 0;
11017
11018 /* other failures */
11019 if (err != -EIO)
11020 return err;
11021
11022 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011023 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11024 "to INTx mode. Please report this failure to the PCI "
11025 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011026
Matt Carlson4f125f42009-09-01 12:55:02 +000011027 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011028
Michael Chan79381092005-04-21 17:13:59 -070011029 pci_disable_msi(tp->pdev);
11030
Joe Perches63c3a662011-04-26 08:12:10 +000011031 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011032 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011033
Matt Carlson4f125f42009-09-01 12:55:02 +000011034 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011035 if (err)
11036 return err;
11037
11038 /* Need to reset the chip because the MSI cycle may have terminated
11039 * with Master Abort.
11040 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011041 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011042
Michael Chan944d9802005-05-29 14:57:48 -070011043 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011044 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011045
David S. Millerf47c11e2005-06-24 20:18:35 -070011046 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011047
11048 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011049 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011050
11051 return err;
11052}
11053
Matt Carlson9e9fd122009-01-19 16:57:45 -080011054static int tg3_request_firmware(struct tg3 *tp)
11055{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011056 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011057
11058 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011059 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11060 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011061 return -ENOENT;
11062 }
11063
Nithin Sujir77997ea2013-03-06 17:02:32 +000011064 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011065
11066 /* Firmware blob starts with version numbers, followed by
11067 * start address and _full_ length including BSS sections
11068 * (which must be longer than the actual data, of course
11069 */
11070
Nithin Sujir77997ea2013-03-06 17:02:32 +000011071 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11072 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011073 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11074 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011075 release_firmware(tp->fw);
11076 tp->fw = NULL;
11077 return -EINVAL;
11078 }
11079
11080 /* We no longer need firmware; we have it. */
11081 tp->fw_needed = NULL;
11082 return 0;
11083}
11084
Michael Chan91024262012-09-28 07:12:38 +000011085static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011086{
Michael Chan91024262012-09-28 07:12:38 +000011087 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011088
Michael Chan91024262012-09-28 07:12:38 +000011089 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011090 /* We want as many rx rings enabled as there are cpus.
11091 * In multiqueue MSI-X mode, the first MSI-X vector
11092 * only deals with link interrupts, etc, so we add
11093 * one to the number of vectors we are requesting.
11094 */
Michael Chan91024262012-09-28 07:12:38 +000011095 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011096 }
Matt Carlson679563f2009-09-01 12:55:46 +000011097
Michael Chan91024262012-09-28 07:12:38 +000011098 return irq_cnt;
11099}
11100
11101static bool tg3_enable_msix(struct tg3 *tp)
11102{
11103 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011104 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011105
Michael Chan09681692012-09-28 07:12:42 +000011106 tp->txq_cnt = tp->txq_req;
11107 tp->rxq_cnt = tp->rxq_req;
11108 if (!tp->rxq_cnt)
11109 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011110 if (tp->rxq_cnt > tp->rxq_max)
11111 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011112
11113 /* Disable multiple TX rings by default. Simple round-robin hardware
11114 * scheduling of the TX rings can cause starvation of rings with
11115 * small packets when other rings have TSO or jumbo packets.
11116 */
11117 if (!tp->txq_req)
11118 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011119
11120 tp->irq_cnt = tg3_irq_count(tp);
11121
Matt Carlson679563f2009-09-01 12:55:46 +000011122 for (i = 0; i < tp->irq_max; i++) {
11123 msix_ent[i].entry = i;
11124 msix_ent[i].vector = 0;
11125 }
11126
11127 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011128 if (rc < 0) {
11129 return false;
11130 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011131 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11132 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011133 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11134 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011135 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011136 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011137 if (tp->txq_cnt)
11138 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011139 }
11140
11141 for (i = 0; i < tp->irq_max; i++)
11142 tp->napi[i].irq_vec = msix_ent[i].vector;
11143
Michael Chan49a359e2012-09-28 07:12:37 +000011144 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011145 pci_disable_msix(tp->pdev);
11146 return false;
11147 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011148
Michael Chan91024262012-09-28 07:12:38 +000011149 if (tp->irq_cnt == 1)
11150 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011151
Michael Chan91024262012-09-28 07:12:38 +000011152 tg3_flag_set(tp, ENABLE_RSS);
11153
11154 if (tp->txq_cnt > 1)
11155 tg3_flag_set(tp, ENABLE_TSS);
11156
11157 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011158
Matt Carlson679563f2009-09-01 12:55:46 +000011159 return true;
11160}
11161
Matt Carlson07b01732009-08-28 14:01:15 +000011162static void tg3_ints_init(struct tg3 *tp)
11163{
Joe Perches63c3a662011-04-26 08:12:10 +000011164 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11165 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011166 /* All MSI supporting chips should support tagged
11167 * status. Assert that this is the case.
11168 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011169 netdev_warn(tp->dev,
11170 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011171 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011172 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011173
Joe Perches63c3a662011-04-26 08:12:10 +000011174 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11175 tg3_flag_set(tp, USING_MSIX);
11176 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11177 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011178
Joe Perches63c3a662011-04-26 08:12:10 +000011179 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011180 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011181 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011182 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011183 if (!tg3_flag(tp, 1SHOT_MSI))
11184 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011185 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11186 }
11187defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011188 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011189 tp->irq_cnt = 1;
11190 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011191 }
11192
11193 if (tp->irq_cnt == 1) {
11194 tp->txq_cnt = 1;
11195 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011196 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011197 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011198 }
Matt Carlson07b01732009-08-28 14:01:15 +000011199}
11200
11201static void tg3_ints_fini(struct tg3 *tp)
11202{
Joe Perches63c3a662011-04-26 08:12:10 +000011203 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011204 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011205 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011206 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011207 tg3_flag_clear(tp, USING_MSI);
11208 tg3_flag_clear(tp, USING_MSIX);
11209 tg3_flag_clear(tp, ENABLE_RSS);
11210 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011211}
11212
Matt Carlsonbe947302012-12-03 19:36:57 +000011213static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11214 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011215{
Michael Chand8f4cd32012-09-28 07:12:40 +000011216 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011217 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011218
Matt Carlson679563f2009-09-01 12:55:46 +000011219 /*
11220 * Setup interrupts first so we know how
11221 * many NAPI resources to allocate
11222 */
11223 tg3_ints_init(tp);
11224
Matt Carlson90415472011-12-16 13:33:23 +000011225 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011226
Linus Torvalds1da177e2005-04-16 15:20:36 -070011227 /* The placement of this call is tied
11228 * to the setup and use of Host TX descriptors.
11229 */
11230 err = tg3_alloc_consistent(tp);
11231 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000011232 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011233
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011234 tg3_napi_init(tp);
11235
Matt Carlsonfed97812009-09-01 13:10:19 +000011236 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011237
Matt Carlson4f125f42009-09-01 12:55:02 +000011238 for (i = 0; i < tp->irq_cnt; i++) {
11239 struct tg3_napi *tnapi = &tp->napi[i];
11240 err = tg3_request_irq(tp, i);
11241 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011242 for (i--; i >= 0; i--) {
11243 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011244 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011245 }
11246 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000011247 }
11248 }
Matt Carlson07b01732009-08-28 14:01:15 +000011249
David S. Millerf47c11e2005-06-24 20:18:35 -070011250 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011251
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011252 if (init)
11253 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11254
Michael Chand8f4cd32012-09-28 07:12:40 +000011255 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011256 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011257 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011258 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011259 }
11260
David S. Millerf47c11e2005-06-24 20:18:35 -070011261 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011262
Matt Carlson07b01732009-08-28 14:01:15 +000011263 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000011264 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011265
Michael Chand8f4cd32012-09-28 07:12:40 +000011266 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011267 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011268
Michael Chan79381092005-04-21 17:13:59 -070011269 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011270 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011271 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011272 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011273 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011274
Matt Carlson679563f2009-09-01 12:55:46 +000011275 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070011276 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011277
Joe Perches63c3a662011-04-26 08:12:10 +000011278 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011279 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011280
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011281 tw32(PCIE_TRANSACTION_CFG,
11282 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011283 }
Michael Chan79381092005-04-21 17:13:59 -070011284 }
11285
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011286 tg3_phy_start(tp);
11287
Michael Chanaed93e02012-07-16 16:24:02 +000011288 tg3_hwmon_open(tp);
11289
David S. Millerf47c11e2005-06-24 20:18:35 -070011290 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011291
Matt Carlson21f76382012-02-22 12:35:21 +000011292 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011293 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011294 tg3_enable_ints(tp);
11295
Matt Carlsonbe947302012-12-03 19:36:57 +000011296 if (init)
11297 tg3_ptp_init(tp);
11298 else
11299 tg3_ptp_resume(tp);
11300
11301
David S. Millerf47c11e2005-06-24 20:18:35 -070011302 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011303
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011304 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011305
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011306 /*
11307 * Reset loopback feature if it was turned on while the device was down
11308 * make sure that it's installed properly now.
11309 */
11310 if (dev->features & NETIF_F_LOOPBACK)
11311 tg3_set_loopback(dev, dev->features);
11312
Linus Torvalds1da177e2005-04-16 15:20:36 -070011313 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011314
Matt Carlson679563f2009-09-01 12:55:46 +000011315err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000011316 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11317 struct tg3_napi *tnapi = &tp->napi[i];
11318 free_irq(tnapi->irq_vec, tnapi);
11319 }
Matt Carlson07b01732009-08-28 14:01:15 +000011320
Matt Carlson679563f2009-09-01 12:55:46 +000011321err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000011322 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011323 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011324 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011325
11326err_out1:
11327 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011328
Matt Carlson07b01732009-08-28 14:01:15 +000011329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011330}
11331
Michael Chan65138592012-09-28 07:12:41 +000011332static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011333{
Matt Carlson4f125f42009-09-01 12:55:02 +000011334 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011335
Matt Carlsondb219972011-11-04 09:15:03 +000011336 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011337 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011338
Matt Carlson21f76382012-02-22 12:35:21 +000011339 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011340
Michael Chanaed93e02012-07-16 16:24:02 +000011341 tg3_hwmon_close(tp);
11342
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011343 tg3_phy_stop(tp);
11344
David S. Millerf47c11e2005-06-24 20:18:35 -070011345 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011346
11347 tg3_disable_ints(tp);
11348
Michael Chan944d9802005-05-29 14:57:48 -070011349 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011350 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011351 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011352
David S. Millerf47c11e2005-06-24 20:18:35 -070011353 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011354
Matt Carlson4f125f42009-09-01 12:55:02 +000011355 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11356 struct tg3_napi *tnapi = &tp->napi[i];
11357 free_irq(tnapi->irq_vec, tnapi);
11358 }
Matt Carlson07b01732009-08-28 14:01:15 +000011359
11360 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011361
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011362 tg3_napi_fini(tp);
11363
Linus Torvalds1da177e2005-04-16 15:20:36 -070011364 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011365}
11366
Michael Chand8f4cd32012-09-28 07:12:40 +000011367static int tg3_open(struct net_device *dev)
11368{
11369 struct tg3 *tp = netdev_priv(dev);
11370 int err;
11371
11372 if (tp->fw_needed) {
11373 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011374 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11375 if (err) {
11376 netdev_warn(tp->dev, "EEE capability disabled\n");
11377 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11378 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11379 netdev_warn(tp->dev, "EEE capability restored\n");
11380 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11381 }
11382 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011383 if (err)
11384 return err;
11385 } else if (err) {
11386 netdev_warn(tp->dev, "TSO capability disabled\n");
11387 tg3_flag_clear(tp, TSO_CAPABLE);
11388 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11389 netdev_notice(tp->dev, "TSO capability restored\n");
11390 tg3_flag_set(tp, TSO_CAPABLE);
11391 }
11392 }
11393
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011394 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011395
11396 err = tg3_power_up(tp);
11397 if (err)
11398 return err;
11399
11400 tg3_full_lock(tp, 0);
11401
11402 tg3_disable_ints(tp);
11403 tg3_flag_clear(tp, INIT_COMPLETE);
11404
11405 tg3_full_unlock(tp);
11406
Nithin Sujir942d1af2013-04-09 08:48:07 +000011407 err = tg3_start(tp,
11408 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11409 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011410 if (err) {
11411 tg3_frob_aux_power(tp, false);
11412 pci_set_power_state(tp->pdev, PCI_D3hot);
11413 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011414
Matt Carlson7d41e492012-12-03 19:36:58 +000011415 if (tg3_flag(tp, PTP_CAPABLE)) {
11416 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11417 &tp->pdev->dev);
11418 if (IS_ERR(tp->ptp_clock))
11419 tp->ptp_clock = NULL;
11420 }
11421
Linus Torvalds1da177e2005-04-16 15:20:36 -070011422 return err;
11423}
11424
11425static int tg3_close(struct net_device *dev)
11426{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011427 struct tg3 *tp = netdev_priv(dev);
11428
Matt Carlsonbe947302012-12-03 19:36:57 +000011429 tg3_ptp_fini(tp);
11430
Michael Chan65138592012-09-28 07:12:41 +000011431 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011432
11433 /* Clear stats across close / open calls */
11434 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11435 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011436
11437 tg3_power_down(tp);
11438
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011439 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011440
11441 return 0;
11442}
11443
11444static inline u64 get_stat64(tg3_stat64_t *val)
11445{
11446 return ((u64)val->high << 32) | ((u64)val->low);
11447}
11448
11449static u64 tg3_calc_crc_errors(struct tg3 *tp)
11450{
11451 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11452
11453 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011454 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11455 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011456 u32 val;
11457
11458 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11459 tg3_writephy(tp, MII_TG3_TEST1,
11460 val | MII_TG3_TEST1_CRC_EN);
11461 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11462 } else
11463 val = 0;
11464
11465 tp->phy_crc_errors += val;
11466
11467 return tp->phy_crc_errors;
11468 }
11469
11470 return get_stat64(&hw_stats->rx_fcs_errors);
11471}
11472
11473#define ESTAT_ADD(member) \
11474 estats->member = old_estats->member + \
11475 get_stat64(&hw_stats->member)
11476
11477static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11478{
11479 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11480 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11481
11482 ESTAT_ADD(rx_octets);
11483 ESTAT_ADD(rx_fragments);
11484 ESTAT_ADD(rx_ucast_packets);
11485 ESTAT_ADD(rx_mcast_packets);
11486 ESTAT_ADD(rx_bcast_packets);
11487 ESTAT_ADD(rx_fcs_errors);
11488 ESTAT_ADD(rx_align_errors);
11489 ESTAT_ADD(rx_xon_pause_rcvd);
11490 ESTAT_ADD(rx_xoff_pause_rcvd);
11491 ESTAT_ADD(rx_mac_ctrl_rcvd);
11492 ESTAT_ADD(rx_xoff_entered);
11493 ESTAT_ADD(rx_frame_too_long_errors);
11494 ESTAT_ADD(rx_jabbers);
11495 ESTAT_ADD(rx_undersize_packets);
11496 ESTAT_ADD(rx_in_length_errors);
11497 ESTAT_ADD(rx_out_length_errors);
11498 ESTAT_ADD(rx_64_or_less_octet_packets);
11499 ESTAT_ADD(rx_65_to_127_octet_packets);
11500 ESTAT_ADD(rx_128_to_255_octet_packets);
11501 ESTAT_ADD(rx_256_to_511_octet_packets);
11502 ESTAT_ADD(rx_512_to_1023_octet_packets);
11503 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11504 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11505 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11506 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11507 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11508
11509 ESTAT_ADD(tx_octets);
11510 ESTAT_ADD(tx_collisions);
11511 ESTAT_ADD(tx_xon_sent);
11512 ESTAT_ADD(tx_xoff_sent);
11513 ESTAT_ADD(tx_flow_control);
11514 ESTAT_ADD(tx_mac_errors);
11515 ESTAT_ADD(tx_single_collisions);
11516 ESTAT_ADD(tx_mult_collisions);
11517 ESTAT_ADD(tx_deferred);
11518 ESTAT_ADD(tx_excessive_collisions);
11519 ESTAT_ADD(tx_late_collisions);
11520 ESTAT_ADD(tx_collide_2times);
11521 ESTAT_ADD(tx_collide_3times);
11522 ESTAT_ADD(tx_collide_4times);
11523 ESTAT_ADD(tx_collide_5times);
11524 ESTAT_ADD(tx_collide_6times);
11525 ESTAT_ADD(tx_collide_7times);
11526 ESTAT_ADD(tx_collide_8times);
11527 ESTAT_ADD(tx_collide_9times);
11528 ESTAT_ADD(tx_collide_10times);
11529 ESTAT_ADD(tx_collide_11times);
11530 ESTAT_ADD(tx_collide_12times);
11531 ESTAT_ADD(tx_collide_13times);
11532 ESTAT_ADD(tx_collide_14times);
11533 ESTAT_ADD(tx_collide_15times);
11534 ESTAT_ADD(tx_ucast_packets);
11535 ESTAT_ADD(tx_mcast_packets);
11536 ESTAT_ADD(tx_bcast_packets);
11537 ESTAT_ADD(tx_carrier_sense_errors);
11538 ESTAT_ADD(tx_discards);
11539 ESTAT_ADD(tx_errors);
11540
11541 ESTAT_ADD(dma_writeq_full);
11542 ESTAT_ADD(dma_write_prioq_full);
11543 ESTAT_ADD(rxbds_empty);
11544 ESTAT_ADD(rx_discards);
11545 ESTAT_ADD(rx_errors);
11546 ESTAT_ADD(rx_threshold_hit);
11547
11548 ESTAT_ADD(dma_readq_full);
11549 ESTAT_ADD(dma_read_prioq_full);
11550 ESTAT_ADD(tx_comp_queue_full);
11551
11552 ESTAT_ADD(ring_set_send_prod_index);
11553 ESTAT_ADD(ring_status_update);
11554 ESTAT_ADD(nic_irqs);
11555 ESTAT_ADD(nic_avoided_irqs);
11556 ESTAT_ADD(nic_tx_threshold_hit);
11557
Matt Carlson4452d092011-05-19 12:12:51 +000011558 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011559}
11560
Matt Carlson65ec6982012-02-28 23:33:37 +000011561static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011562{
Eric Dumazet511d2222010-07-07 20:44:24 +000011563 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011564 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11565
Linus Torvalds1da177e2005-04-16 15:20:36 -070011566 stats->rx_packets = old_stats->rx_packets +
11567 get_stat64(&hw_stats->rx_ucast_packets) +
11568 get_stat64(&hw_stats->rx_mcast_packets) +
11569 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011570
Linus Torvalds1da177e2005-04-16 15:20:36 -070011571 stats->tx_packets = old_stats->tx_packets +
11572 get_stat64(&hw_stats->tx_ucast_packets) +
11573 get_stat64(&hw_stats->tx_mcast_packets) +
11574 get_stat64(&hw_stats->tx_bcast_packets);
11575
11576 stats->rx_bytes = old_stats->rx_bytes +
11577 get_stat64(&hw_stats->rx_octets);
11578 stats->tx_bytes = old_stats->tx_bytes +
11579 get_stat64(&hw_stats->tx_octets);
11580
11581 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011582 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583 stats->tx_errors = old_stats->tx_errors +
11584 get_stat64(&hw_stats->tx_errors) +
11585 get_stat64(&hw_stats->tx_mac_errors) +
11586 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11587 get_stat64(&hw_stats->tx_discards);
11588
11589 stats->multicast = old_stats->multicast +
11590 get_stat64(&hw_stats->rx_mcast_packets);
11591 stats->collisions = old_stats->collisions +
11592 get_stat64(&hw_stats->tx_collisions);
11593
11594 stats->rx_length_errors = old_stats->rx_length_errors +
11595 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11596 get_stat64(&hw_stats->rx_undersize_packets);
11597
11598 stats->rx_over_errors = old_stats->rx_over_errors +
11599 get_stat64(&hw_stats->rxbds_empty);
11600 stats->rx_frame_errors = old_stats->rx_frame_errors +
11601 get_stat64(&hw_stats->rx_align_errors);
11602 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11603 get_stat64(&hw_stats->tx_discards);
11604 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11605 get_stat64(&hw_stats->tx_carrier_sense_errors);
11606
11607 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011608 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011609
John W. Linville4f63b872005-09-12 14:43:18 -070011610 stats->rx_missed_errors = old_stats->rx_missed_errors +
11611 get_stat64(&hw_stats->rx_discards);
11612
Eric Dumazetb0057c52010-10-10 19:55:52 +000011613 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011614 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011615}
11616
Linus Torvalds1da177e2005-04-16 15:20:36 -070011617static int tg3_get_regs_len(struct net_device *dev)
11618{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011619 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011620}
11621
11622static void tg3_get_regs(struct net_device *dev,
11623 struct ethtool_regs *regs, void *_p)
11624{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011625 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011626
11627 regs->version = 0;
11628
Matt Carlson97bd8e42011-04-13 11:05:04 +000011629 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011630
Matt Carlson80096062010-08-02 11:26:06 +000011631 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011632 return;
11633
David S. Millerf47c11e2005-06-24 20:18:35 -070011634 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011635
Matt Carlson97bd8e42011-04-13 11:05:04 +000011636 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011637
David S. Millerf47c11e2005-06-24 20:18:35 -070011638 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011639}
11640
11641static int tg3_get_eeprom_len(struct net_device *dev)
11642{
11643 struct tg3 *tp = netdev_priv(dev);
11644
11645 return tp->nvram_size;
11646}
11647
Linus Torvalds1da177e2005-04-16 15:20:36 -070011648static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11649{
11650 struct tg3 *tp = netdev_priv(dev);
11651 int ret;
11652 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011653 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011654 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011655
Joe Perches63c3a662011-04-26 08:12:10 +000011656 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011657 return -EINVAL;
11658
Matt Carlson80096062010-08-02 11:26:06 +000011659 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011660 return -EAGAIN;
11661
Linus Torvalds1da177e2005-04-16 15:20:36 -070011662 offset = eeprom->offset;
11663 len = eeprom->len;
11664 eeprom->len = 0;
11665
11666 eeprom->magic = TG3_EEPROM_MAGIC;
11667
11668 if (offset & 3) {
11669 /* adjustments to start on required 4 byte boundary */
11670 b_offset = offset & 3;
11671 b_count = 4 - b_offset;
11672 if (b_count > len) {
11673 /* i.e. offset=1 len=2 */
11674 b_count = len;
11675 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011676 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011677 if (ret)
11678 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011679 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011680 len -= b_count;
11681 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011682 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011683 }
11684
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011685 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011686 pd = &data[eeprom->len];
11687 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011688 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011689 if (ret) {
11690 eeprom->len += i;
11691 return ret;
11692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011693 memcpy(pd + i, &val, 4);
11694 }
11695 eeprom->len += i;
11696
11697 if (len & 3) {
11698 /* read last bytes not ending on 4 byte boundary */
11699 pd = &data[eeprom->len];
11700 b_count = len & 3;
11701 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011702 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011703 if (ret)
11704 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011705 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011706 eeprom->len += b_count;
11707 }
11708 return 0;
11709}
11710
Linus Torvalds1da177e2005-04-16 15:20:36 -070011711static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11712{
11713 struct tg3 *tp = netdev_priv(dev);
11714 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011715 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011716 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011717 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011718
Matt Carlson80096062010-08-02 11:26:06 +000011719 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011720 return -EAGAIN;
11721
Joe Perches63c3a662011-04-26 08:12:10 +000011722 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011723 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011724 return -EINVAL;
11725
11726 offset = eeprom->offset;
11727 len = eeprom->len;
11728
11729 if ((b_offset = (offset & 3))) {
11730 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011731 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011732 if (ret)
11733 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011734 len += b_offset;
11735 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070011736 if (len < 4)
11737 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011738 }
11739
11740 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070011741 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011742 /* adjustments to end on required 4 byte boundary */
11743 odd_len = 1;
11744 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011745 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011746 if (ret)
11747 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011748 }
11749
11750 buf = data;
11751 if (b_offset || odd_len) {
11752 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011753 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011754 return -ENOMEM;
11755 if (b_offset)
11756 memcpy(buf, &start, 4);
11757 if (odd_len)
11758 memcpy(buf+len-4, &end, 4);
11759 memcpy(buf + b_offset, data, eeprom->len);
11760 }
11761
11762 ret = tg3_nvram_write_block(tp, offset, len, buf);
11763
11764 if (buf != data)
11765 kfree(buf);
11766
11767 return ret;
11768}
11769
11770static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11771{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011772 struct tg3 *tp = netdev_priv(dev);
11773
Joe Perches63c3a662011-04-26 08:12:10 +000011774 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011775 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011776 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011777 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011778 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11779 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011780 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011781
Linus Torvalds1da177e2005-04-16 15:20:36 -070011782 cmd->supported = (SUPPORTED_Autoneg);
11783
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011784 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011785 cmd->supported |= (SUPPORTED_1000baseT_Half |
11786 SUPPORTED_1000baseT_Full);
11787
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011788 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011789 cmd->supported |= (SUPPORTED_100baseT_Half |
11790 SUPPORTED_100baseT_Full |
11791 SUPPORTED_10baseT_Half |
11792 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011793 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011794 cmd->port = PORT_TP;
11795 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011796 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011797 cmd->port = PORT_FIBRE;
11798 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011799
Linus Torvalds1da177e2005-04-16 15:20:36 -070011800 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011801 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11802 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11803 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11804 cmd->advertising |= ADVERTISED_Pause;
11805 } else {
11806 cmd->advertising |= ADVERTISED_Pause |
11807 ADVERTISED_Asym_Pause;
11808 }
11809 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11810 cmd->advertising |= ADVERTISED_Asym_Pause;
11811 }
11812 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011813 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011814 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011815 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011816 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011817 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11818 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11819 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11820 else
11821 cmd->eth_tp_mdix = ETH_TP_MDI;
11822 }
Matt Carlson64c22182010-10-14 10:37:44 +000011823 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011824 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11825 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011826 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011827 }
Matt Carlson882e9792009-09-01 13:21:36 +000011828 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011829 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011830 cmd->autoneg = tp->link_config.autoneg;
11831 cmd->maxtxpkt = 0;
11832 cmd->maxrxpkt = 0;
11833 return 0;
11834}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011835
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11837{
11838 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011839 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011840
Joe Perches63c3a662011-04-26 08:12:10 +000011841 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011842 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011843 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011844 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011845 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11846 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011847 }
11848
Matt Carlson7e5856b2009-02-25 14:23:01 +000011849 if (cmd->autoneg != AUTONEG_ENABLE &&
11850 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011851 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011852
11853 if (cmd->autoneg == AUTONEG_DISABLE &&
11854 cmd->duplex != DUPLEX_FULL &&
11855 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011856 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011857
Matt Carlson7e5856b2009-02-25 14:23:01 +000011858 if (cmd->autoneg == AUTONEG_ENABLE) {
11859 u32 mask = ADVERTISED_Autoneg |
11860 ADVERTISED_Pause |
11861 ADVERTISED_Asym_Pause;
11862
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011863 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011864 mask |= ADVERTISED_1000baseT_Half |
11865 ADVERTISED_1000baseT_Full;
11866
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011867 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011868 mask |= ADVERTISED_100baseT_Half |
11869 ADVERTISED_100baseT_Full |
11870 ADVERTISED_10baseT_Half |
11871 ADVERTISED_10baseT_Full |
11872 ADVERTISED_TP;
11873 else
11874 mask |= ADVERTISED_FIBRE;
11875
11876 if (cmd->advertising & ~mask)
11877 return -EINVAL;
11878
11879 mask &= (ADVERTISED_1000baseT_Half |
11880 ADVERTISED_1000baseT_Full |
11881 ADVERTISED_100baseT_Half |
11882 ADVERTISED_100baseT_Full |
11883 ADVERTISED_10baseT_Half |
11884 ADVERTISED_10baseT_Full);
11885
11886 cmd->advertising &= mask;
11887 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011888 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011889 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011890 return -EINVAL;
11891
11892 if (cmd->duplex != DUPLEX_FULL)
11893 return -EINVAL;
11894 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011895 if (speed != SPEED_100 &&
11896 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011897 return -EINVAL;
11898 }
11899 }
11900
David S. Millerf47c11e2005-06-24 20:18:35 -070011901 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011902
11903 tp->link_config.autoneg = cmd->autoneg;
11904 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011905 tp->link_config.advertising = (cmd->advertising |
11906 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011907 tp->link_config.speed = SPEED_UNKNOWN;
11908 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011909 } else {
11910 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011911 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011912 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011913 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011914
Nithin Sujirfdad8de2013-04-09 08:48:08 +000011915 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
11916
Nithin Sujirce20f162013-04-09 08:48:04 +000011917 tg3_warn_mgmt_link_flap(tp);
11918
Linus Torvalds1da177e2005-04-16 15:20:36 -070011919 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000011920 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011921
David S. Millerf47c11e2005-06-24 20:18:35 -070011922 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011923
Linus Torvalds1da177e2005-04-16 15:20:36 -070011924 return 0;
11925}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011926
Linus Torvalds1da177e2005-04-16 15:20:36 -070011927static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11928{
11929 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011930
Rick Jones68aad782011-11-07 13:29:27 +000011931 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11932 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11933 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11934 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011935}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011936
Linus Torvalds1da177e2005-04-16 15:20:36 -070011937static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11938{
11939 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011940
Joe Perches63c3a662011-04-26 08:12:10 +000011941 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011942 wol->supported = WAKE_MAGIC;
11943 else
11944 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011945 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011946 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011947 wol->wolopts = WAKE_MAGIC;
11948 memset(&wol->sopass, 0, sizeof(wol->sopass));
11949}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011950
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11952{
11953 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011954 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011955
Linus Torvalds1da177e2005-04-16 15:20:36 -070011956 if (wol->wolopts & ~WAKE_MAGIC)
11957 return -EINVAL;
11958 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011959 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011960 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011961
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011962 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11963
David S. Millerf47c11e2005-06-24 20:18:35 -070011964 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011965 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011966 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011967 else
Joe Perches63c3a662011-04-26 08:12:10 +000011968 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011969 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011970
Linus Torvalds1da177e2005-04-16 15:20:36 -070011971 return 0;
11972}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011973
Linus Torvalds1da177e2005-04-16 15:20:36 -070011974static u32 tg3_get_msglevel(struct net_device *dev)
11975{
11976 struct tg3 *tp = netdev_priv(dev);
11977 return tp->msg_enable;
11978}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011979
Linus Torvalds1da177e2005-04-16 15:20:36 -070011980static void tg3_set_msglevel(struct net_device *dev, u32 value)
11981{
11982 struct tg3 *tp = netdev_priv(dev);
11983 tp->msg_enable = value;
11984}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011985
Linus Torvalds1da177e2005-04-16 15:20:36 -070011986static int tg3_nway_reset(struct net_device *dev)
11987{
11988 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011989 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011990
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991 if (!netif_running(dev))
11992 return -EAGAIN;
11993
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011994 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011995 return -EINVAL;
11996
Nithin Sujirce20f162013-04-09 08:48:04 +000011997 tg3_warn_mgmt_link_flap(tp);
11998
Joe Perches63c3a662011-04-26 08:12:10 +000011999 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012000 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012001 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012002 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012003 } else {
12004 u32 bmcr;
12005
12006 spin_lock_bh(&tp->lock);
12007 r = -EINVAL;
12008 tg3_readphy(tp, MII_BMCR, &bmcr);
12009 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12010 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012011 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012012 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12013 BMCR_ANENABLE);
12014 r = 0;
12015 }
12016 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012017 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012018
Linus Torvalds1da177e2005-04-16 15:20:36 -070012019 return r;
12020}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012021
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12023{
12024 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012025
Matt Carlson2c49a442010-09-30 10:34:35 +000012026 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012027 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012028 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012029 else
12030 ering->rx_jumbo_max_pending = 0;
12031
12032 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012033
12034 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012035 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012036 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12037 else
12038 ering->rx_jumbo_pending = 0;
12039
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012040 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012041}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012042
Linus Torvalds1da177e2005-04-16 15:20:36 -070012043static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12044{
12045 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012046 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012047
Matt Carlson2c49a442010-09-30 10:34:35 +000012048 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12049 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012050 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12051 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012052 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012053 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012054 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012055
Michael Chanbbe832c2005-06-24 20:20:04 -070012056 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012057 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012059 irq_sync = 1;
12060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012061
Michael Chanbbe832c2005-06-24 20:20:04 -070012062 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012063
Linus Torvalds1da177e2005-04-16 15:20:36 -070012064 tp->rx_pending = ering->rx_pending;
12065
Joe Perches63c3a662011-04-26 08:12:10 +000012066 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012067 tp->rx_pending > 63)
12068 tp->rx_pending = 63;
12069 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012070
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012071 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012072 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012073
12074 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012075 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012076 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012077 if (!err)
12078 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012079 }
12080
David S. Millerf47c11e2005-06-24 20:18:35 -070012081 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012082
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012083 if (irq_sync && !err)
12084 tg3_phy_start(tp);
12085
Michael Chanb9ec6c12006-07-25 16:37:27 -070012086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012088
Linus Torvalds1da177e2005-04-16 15:20:36 -070012089static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12090{
12091 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012092
Joe Perches63c3a662011-04-26 08:12:10 +000012093 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012094
Matt Carlson4a2db502011-12-08 14:40:17 +000012095 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012096 epause->rx_pause = 1;
12097 else
12098 epause->rx_pause = 0;
12099
Matt Carlson4a2db502011-12-08 14:40:17 +000012100 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012101 epause->tx_pause = 1;
12102 else
12103 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012104}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012105
Linus Torvalds1da177e2005-04-16 15:20:36 -070012106static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12107{
12108 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012109 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012110
Nithin Sujirce20f162013-04-09 08:48:04 +000012111 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12112 tg3_warn_mgmt_link_flap(tp);
12113
Joe Perches63c3a662011-04-26 08:12:10 +000012114 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012115 u32 newadv;
12116 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012117
Matt Carlson27121682010-02-17 15:16:57 +000012118 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012119
Matt Carlson27121682010-02-17 15:16:57 +000012120 if (!(phydev->supported & SUPPORTED_Pause) ||
12121 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012122 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012123 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012124
Matt Carlson27121682010-02-17 15:16:57 +000012125 tp->link_config.flowctrl = 0;
12126 if (epause->rx_pause) {
12127 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012128
Matt Carlson27121682010-02-17 15:16:57 +000012129 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012130 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012131 newadv = ADVERTISED_Pause;
12132 } else
12133 newadv = ADVERTISED_Pause |
12134 ADVERTISED_Asym_Pause;
12135 } else if (epause->tx_pause) {
12136 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12137 newadv = ADVERTISED_Asym_Pause;
12138 } else
12139 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012140
Matt Carlson27121682010-02-17 15:16:57 +000012141 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012142 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012143 else
Joe Perches63c3a662011-04-26 08:12:10 +000012144 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012145
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012146 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012147 u32 oldadv = phydev->advertising &
12148 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12149 if (oldadv != newadv) {
12150 phydev->advertising &=
12151 ~(ADVERTISED_Pause |
12152 ADVERTISED_Asym_Pause);
12153 phydev->advertising |= newadv;
12154 if (phydev->autoneg) {
12155 /*
12156 * Always renegotiate the link to
12157 * inform our link partner of our
12158 * flow control settings, even if the
12159 * flow control is forced. Let
12160 * tg3_adjust_link() do the final
12161 * flow control setup.
12162 */
12163 return phy_start_aneg(phydev);
12164 }
12165 }
12166
12167 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012168 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012169 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012170 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012171 ~(ADVERTISED_Pause |
12172 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012173 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012174 }
12175 } else {
12176 int irq_sync = 0;
12177
12178 if (netif_running(dev)) {
12179 tg3_netif_stop(tp);
12180 irq_sync = 1;
12181 }
12182
12183 tg3_full_lock(tp, irq_sync);
12184
12185 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012186 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012187 else
Joe Perches63c3a662011-04-26 08:12:10 +000012188 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012189 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012190 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012191 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012192 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012193 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012194 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012195 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012196 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012197
12198 if (netif_running(dev)) {
12199 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012200 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012201 if (!err)
12202 tg3_netif_start(tp);
12203 }
12204
12205 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012207
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012208 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12209
Michael Chanb9ec6c12006-07-25 16:37:27 -070012210 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012211}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012212
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012213static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012214{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012215 switch (sset) {
12216 case ETH_SS_TEST:
12217 return TG3_NUM_TEST;
12218 case ETH_SS_STATS:
12219 return TG3_NUM_STATS;
12220 default:
12221 return -EOPNOTSUPP;
12222 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012223}
12224
Matt Carlson90415472011-12-16 13:33:23 +000012225static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12226 u32 *rules __always_unused)
12227{
12228 struct tg3 *tp = netdev_priv(dev);
12229
12230 if (!tg3_flag(tp, SUPPORT_MSIX))
12231 return -EOPNOTSUPP;
12232
12233 switch (info->cmd) {
12234 case ETHTOOL_GRXRINGS:
12235 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012236 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012237 else {
12238 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012239 if (info->data > TG3_RSS_MAX_NUM_QS)
12240 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012241 }
12242
12243 /* The first interrupt vector only
12244 * handles link interrupts.
12245 */
12246 info->data -= 1;
12247 return 0;
12248
12249 default:
12250 return -EOPNOTSUPP;
12251 }
12252}
12253
12254static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12255{
12256 u32 size = 0;
12257 struct tg3 *tp = netdev_priv(dev);
12258
12259 if (tg3_flag(tp, SUPPORT_MSIX))
12260 size = TG3_RSS_INDIR_TBL_SIZE;
12261
12262 return size;
12263}
12264
12265static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12266{
12267 struct tg3 *tp = netdev_priv(dev);
12268 int i;
12269
12270 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12271 indir[i] = tp->rss_ind_tbl[i];
12272
12273 return 0;
12274}
12275
12276static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12277{
12278 struct tg3 *tp = netdev_priv(dev);
12279 size_t i;
12280
12281 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12282 tp->rss_ind_tbl[i] = indir[i];
12283
12284 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12285 return 0;
12286
12287 /* It is legal to write the indirection
12288 * table while the device is running.
12289 */
12290 tg3_full_lock(tp, 0);
12291 tg3_rss_write_indir_tbl(tp);
12292 tg3_full_unlock(tp);
12293
12294 return 0;
12295}
12296
Michael Chan09681692012-09-28 07:12:42 +000012297static void tg3_get_channels(struct net_device *dev,
12298 struct ethtool_channels *channel)
12299{
12300 struct tg3 *tp = netdev_priv(dev);
12301 u32 deflt_qs = netif_get_num_default_rss_queues();
12302
12303 channel->max_rx = tp->rxq_max;
12304 channel->max_tx = tp->txq_max;
12305
12306 if (netif_running(dev)) {
12307 channel->rx_count = tp->rxq_cnt;
12308 channel->tx_count = tp->txq_cnt;
12309 } else {
12310 if (tp->rxq_req)
12311 channel->rx_count = tp->rxq_req;
12312 else
12313 channel->rx_count = min(deflt_qs, tp->rxq_max);
12314
12315 if (tp->txq_req)
12316 channel->tx_count = tp->txq_req;
12317 else
12318 channel->tx_count = min(deflt_qs, tp->txq_max);
12319 }
12320}
12321
12322static int tg3_set_channels(struct net_device *dev,
12323 struct ethtool_channels *channel)
12324{
12325 struct tg3 *tp = netdev_priv(dev);
12326
12327 if (!tg3_flag(tp, SUPPORT_MSIX))
12328 return -EOPNOTSUPP;
12329
12330 if (channel->rx_count > tp->rxq_max ||
12331 channel->tx_count > tp->txq_max)
12332 return -EINVAL;
12333
12334 tp->rxq_req = channel->rx_count;
12335 tp->txq_req = channel->tx_count;
12336
12337 if (!netif_running(dev))
12338 return 0;
12339
12340 tg3_stop(tp);
12341
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012342 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012343
Matt Carlsonbe947302012-12-03 19:36:57 +000012344 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012345
12346 return 0;
12347}
12348
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012349static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012350{
12351 switch (stringset) {
12352 case ETH_SS_STATS:
12353 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12354 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012355 case ETH_SS_TEST:
12356 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358 default:
12359 WARN_ON(1); /* we need a WARN() */
12360 break;
12361 }
12362}
12363
stephen hemminger81b87092011-04-04 08:43:50 +000012364static int tg3_set_phys_id(struct net_device *dev,
12365 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012366{
12367 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012368
12369 if (!netif_running(tp->dev))
12370 return -EAGAIN;
12371
stephen hemminger81b87092011-04-04 08:43:50 +000012372 switch (state) {
12373 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012374 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012375
stephen hemminger81b87092011-04-04 08:43:50 +000012376 case ETHTOOL_ID_ON:
12377 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12378 LED_CTRL_1000MBPS_ON |
12379 LED_CTRL_100MBPS_ON |
12380 LED_CTRL_10MBPS_ON |
12381 LED_CTRL_TRAFFIC_OVERRIDE |
12382 LED_CTRL_TRAFFIC_BLINK |
12383 LED_CTRL_TRAFFIC_LED);
12384 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012385
stephen hemminger81b87092011-04-04 08:43:50 +000012386 case ETHTOOL_ID_OFF:
12387 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12388 LED_CTRL_TRAFFIC_OVERRIDE);
12389 break;
Michael Chan4009a932005-09-05 17:52:54 -070012390
stephen hemminger81b87092011-04-04 08:43:50 +000012391 case ETHTOOL_ID_INACTIVE:
12392 tw32(MAC_LED_CTRL, tp->led_ctrl);
12393 break;
Michael Chan4009a932005-09-05 17:52:54 -070012394 }
stephen hemminger81b87092011-04-04 08:43:50 +000012395
Michael Chan4009a932005-09-05 17:52:54 -070012396 return 0;
12397}
12398
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012399static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012400 struct ethtool_stats *estats, u64 *tmp_stats)
12401{
12402 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012403
Matt Carlsonb546e462012-02-13 15:20:09 +000012404 if (tp->hw_stats)
12405 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12406 else
12407 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012408}
12409
Matt Carlson535a4902011-07-20 10:20:56 +000012410static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012411{
12412 int i;
12413 __be32 *buf;
12414 u32 offset = 0, len = 0;
12415 u32 magic, val;
12416
Joe Perches63c3a662011-04-26 08:12:10 +000012417 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012418 return NULL;
12419
12420 if (magic == TG3_EEPROM_MAGIC) {
12421 for (offset = TG3_NVM_DIR_START;
12422 offset < TG3_NVM_DIR_END;
12423 offset += TG3_NVM_DIRENT_SIZE) {
12424 if (tg3_nvram_read(tp, offset, &val))
12425 return NULL;
12426
12427 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12428 TG3_NVM_DIRTYPE_EXTVPD)
12429 break;
12430 }
12431
12432 if (offset != TG3_NVM_DIR_END) {
12433 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12434 if (tg3_nvram_read(tp, offset + 4, &offset))
12435 return NULL;
12436
12437 offset = tg3_nvram_logical_addr(tp, offset);
12438 }
12439 }
12440
12441 if (!offset || !len) {
12442 offset = TG3_NVM_VPD_OFF;
12443 len = TG3_NVM_VPD_LEN;
12444 }
12445
12446 buf = kmalloc(len, GFP_KERNEL);
12447 if (buf == NULL)
12448 return NULL;
12449
12450 if (magic == TG3_EEPROM_MAGIC) {
12451 for (i = 0; i < len; i += 4) {
12452 /* The data is in little-endian format in NVRAM.
12453 * Use the big-endian read routines to preserve
12454 * the byte order as it exists in NVRAM.
12455 */
12456 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12457 goto error;
12458 }
12459 } else {
12460 u8 *ptr;
12461 ssize_t cnt;
12462 unsigned int pos = 0;
12463
12464 ptr = (u8 *)&buf[0];
12465 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12466 cnt = pci_read_vpd(tp->pdev, pos,
12467 len - pos, ptr);
12468 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12469 cnt = 0;
12470 else if (cnt < 0)
12471 goto error;
12472 }
12473 if (pos != len)
12474 goto error;
12475 }
12476
Matt Carlson535a4902011-07-20 10:20:56 +000012477 *vpdlen = len;
12478
Matt Carlsonc3e94502011-04-13 11:05:08 +000012479 return buf;
12480
12481error:
12482 kfree(buf);
12483 return NULL;
12484}
12485
Michael Chan566f86a2005-05-29 14:56:58 -070012486#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012487#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12488#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12489#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012490#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12491#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012492#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012493#define NVRAM_SELFBOOT_HW_SIZE 0x20
12494#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012495
12496static int tg3_test_nvram(struct tg3 *tp)
12497{
Matt Carlson535a4902011-07-20 10:20:56 +000012498 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012499 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012500 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012501
Joe Perches63c3a662011-04-26 08:12:10 +000012502 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012503 return 0;
12504
Matt Carlsone4f34112009-02-25 14:25:00 +000012505 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012506 return -EIO;
12507
Michael Chan1b277772006-03-20 22:27:48 -080012508 if (magic == TG3_EEPROM_MAGIC)
12509 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012510 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012511 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12512 TG3_EEPROM_SB_FORMAT_1) {
12513 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12514 case TG3_EEPROM_SB_REVISION_0:
12515 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12516 break;
12517 case TG3_EEPROM_SB_REVISION_2:
12518 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12519 break;
12520 case TG3_EEPROM_SB_REVISION_3:
12521 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12522 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012523 case TG3_EEPROM_SB_REVISION_4:
12524 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12525 break;
12526 case TG3_EEPROM_SB_REVISION_5:
12527 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12528 break;
12529 case TG3_EEPROM_SB_REVISION_6:
12530 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12531 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012532 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012533 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012534 }
12535 } else
Michael Chan1b277772006-03-20 22:27:48 -080012536 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012537 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12538 size = NVRAM_SELFBOOT_HW_SIZE;
12539 else
Michael Chan1b277772006-03-20 22:27:48 -080012540 return -EIO;
12541
12542 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012543 if (buf == NULL)
12544 return -ENOMEM;
12545
Michael Chan1b277772006-03-20 22:27:48 -080012546 err = -EIO;
12547 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012548 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12549 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012550 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012551 }
Michael Chan1b277772006-03-20 22:27:48 -080012552 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012553 goto out;
12554
Michael Chan1b277772006-03-20 22:27:48 -080012555 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012556 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012557 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012558 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012559 u8 *buf8 = (u8 *) buf, csum8 = 0;
12560
Al Virob9fc7dc2007-12-17 22:59:57 -080012561 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012562 TG3_EEPROM_SB_REVISION_2) {
12563 /* For rev 2, the csum doesn't include the MBA. */
12564 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12565 csum8 += buf8[i];
12566 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12567 csum8 += buf8[i];
12568 } else {
12569 for (i = 0; i < size; i++)
12570 csum8 += buf8[i];
12571 }
Michael Chan1b277772006-03-20 22:27:48 -080012572
Adrian Bunkad96b482006-04-05 22:21:04 -070012573 if (csum8 == 0) {
12574 err = 0;
12575 goto out;
12576 }
12577
12578 err = -EIO;
12579 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012580 }
Michael Chan566f86a2005-05-29 14:56:58 -070012581
Al Virob9fc7dc2007-12-17 22:59:57 -080012582 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012583 TG3_EEPROM_MAGIC_HW) {
12584 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012585 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012586 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012587
12588 /* Separate the parity bits and the data bytes. */
12589 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12590 if ((i == 0) || (i == 8)) {
12591 int l;
12592 u8 msk;
12593
12594 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12595 parity[k++] = buf8[i] & msk;
12596 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012597 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012598 int l;
12599 u8 msk;
12600
12601 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12602 parity[k++] = buf8[i] & msk;
12603 i++;
12604
12605 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12606 parity[k++] = buf8[i] & msk;
12607 i++;
12608 }
12609 data[j++] = buf8[i];
12610 }
12611
12612 err = -EIO;
12613 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12614 u8 hw8 = hweight8(data[i]);
12615
12616 if ((hw8 & 0x1) && parity[i])
12617 goto out;
12618 else if (!(hw8 & 0x1) && !parity[i])
12619 goto out;
12620 }
12621 err = 0;
12622 goto out;
12623 }
12624
Matt Carlson01c3a392011-03-09 16:58:20 +000012625 err = -EIO;
12626
Michael Chan566f86a2005-05-29 14:56:58 -070012627 /* Bootstrap checksum at offset 0x10 */
12628 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012629 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012630 goto out;
12631
12632 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12633 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012634 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012635 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012636
Matt Carlsonc3e94502011-04-13 11:05:08 +000012637 kfree(buf);
12638
Matt Carlson535a4902011-07-20 10:20:56 +000012639 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012640 if (!buf)
12641 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012642
Matt Carlson535a4902011-07-20 10:20:56 +000012643 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012644 if (i > 0) {
12645 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12646 if (j < 0)
12647 goto out;
12648
Matt Carlson535a4902011-07-20 10:20:56 +000012649 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012650 goto out;
12651
12652 i += PCI_VPD_LRDT_TAG_SIZE;
12653 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12654 PCI_VPD_RO_KEYWORD_CHKSUM);
12655 if (j > 0) {
12656 u8 csum8 = 0;
12657
12658 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12659
12660 for (i = 0; i <= j; i++)
12661 csum8 += ((u8 *)buf)[i];
12662
12663 if (csum8)
12664 goto out;
12665 }
12666 }
12667
Michael Chan566f86a2005-05-29 14:56:58 -070012668 err = 0;
12669
12670out:
12671 kfree(buf);
12672 return err;
12673}
12674
Michael Chanca430072005-05-29 14:57:23 -070012675#define TG3_SERDES_TIMEOUT_SEC 2
12676#define TG3_COPPER_TIMEOUT_SEC 6
12677
12678static int tg3_test_link(struct tg3 *tp)
12679{
12680 int i, max;
12681
12682 if (!netif_running(tp->dev))
12683 return -ENODEV;
12684
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012685 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012686 max = TG3_SERDES_TIMEOUT_SEC;
12687 else
12688 max = TG3_COPPER_TIMEOUT_SEC;
12689
12690 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012691 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012692 return 0;
12693
12694 if (msleep_interruptible(1000))
12695 break;
12696 }
12697
12698 return -EIO;
12699}
12700
Michael Chana71116d2005-05-29 14:58:11 -070012701/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012702static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012703{
Michael Chanb16250e2006-09-27 16:10:14 -070012704 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012705 u32 offset, read_mask, write_mask, val, save_val, read_val;
12706 static struct {
12707 u16 offset;
12708 u16 flags;
12709#define TG3_FL_5705 0x1
12710#define TG3_FL_NOT_5705 0x2
12711#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012712#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012713 u32 read_mask;
12714 u32 write_mask;
12715 } reg_tbl[] = {
12716 /* MAC Control Registers */
12717 { MAC_MODE, TG3_FL_NOT_5705,
12718 0x00000000, 0x00ef6f8c },
12719 { MAC_MODE, TG3_FL_5705,
12720 0x00000000, 0x01ef6b8c },
12721 { MAC_STATUS, TG3_FL_NOT_5705,
12722 0x03800107, 0x00000000 },
12723 { MAC_STATUS, TG3_FL_5705,
12724 0x03800100, 0x00000000 },
12725 { MAC_ADDR_0_HIGH, 0x0000,
12726 0x00000000, 0x0000ffff },
12727 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012728 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012729 { MAC_RX_MTU_SIZE, 0x0000,
12730 0x00000000, 0x0000ffff },
12731 { MAC_TX_MODE, 0x0000,
12732 0x00000000, 0x00000070 },
12733 { MAC_TX_LENGTHS, 0x0000,
12734 0x00000000, 0x00003fff },
12735 { MAC_RX_MODE, TG3_FL_NOT_5705,
12736 0x00000000, 0x000007fc },
12737 { MAC_RX_MODE, TG3_FL_5705,
12738 0x00000000, 0x000007dc },
12739 { MAC_HASH_REG_0, 0x0000,
12740 0x00000000, 0xffffffff },
12741 { MAC_HASH_REG_1, 0x0000,
12742 0x00000000, 0xffffffff },
12743 { MAC_HASH_REG_2, 0x0000,
12744 0x00000000, 0xffffffff },
12745 { MAC_HASH_REG_3, 0x0000,
12746 0x00000000, 0xffffffff },
12747
12748 /* Receive Data and Receive BD Initiator Control Registers. */
12749 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12750 0x00000000, 0xffffffff },
12751 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12752 0x00000000, 0xffffffff },
12753 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12754 0x00000000, 0x00000003 },
12755 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12756 0x00000000, 0xffffffff },
12757 { RCVDBDI_STD_BD+0, 0x0000,
12758 0x00000000, 0xffffffff },
12759 { RCVDBDI_STD_BD+4, 0x0000,
12760 0x00000000, 0xffffffff },
12761 { RCVDBDI_STD_BD+8, 0x0000,
12762 0x00000000, 0xffff0002 },
12763 { RCVDBDI_STD_BD+0xc, 0x0000,
12764 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012765
Michael Chana71116d2005-05-29 14:58:11 -070012766 /* Receive BD Initiator Control Registers. */
12767 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12768 0x00000000, 0xffffffff },
12769 { RCVBDI_STD_THRESH, TG3_FL_5705,
12770 0x00000000, 0x000003ff },
12771 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12772 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012773
Michael Chana71116d2005-05-29 14:58:11 -070012774 /* Host Coalescing Control Registers. */
12775 { HOSTCC_MODE, TG3_FL_NOT_5705,
12776 0x00000000, 0x00000004 },
12777 { HOSTCC_MODE, TG3_FL_5705,
12778 0x00000000, 0x000000f6 },
12779 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12780 0x00000000, 0xffffffff },
12781 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12782 0x00000000, 0x000003ff },
12783 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12784 0x00000000, 0xffffffff },
12785 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12786 0x00000000, 0x000003ff },
12787 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12788 0x00000000, 0xffffffff },
12789 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12790 0x00000000, 0x000000ff },
12791 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12792 0x00000000, 0xffffffff },
12793 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12794 0x00000000, 0x000000ff },
12795 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12796 0x00000000, 0xffffffff },
12797 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12798 0x00000000, 0xffffffff },
12799 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12800 0x00000000, 0xffffffff },
12801 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12802 0x00000000, 0x000000ff },
12803 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12804 0x00000000, 0xffffffff },
12805 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12806 0x00000000, 0x000000ff },
12807 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12808 0x00000000, 0xffffffff },
12809 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12810 0x00000000, 0xffffffff },
12811 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12812 0x00000000, 0xffffffff },
12813 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12814 0x00000000, 0xffffffff },
12815 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12816 0x00000000, 0xffffffff },
12817 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12818 0xffffffff, 0x00000000 },
12819 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12820 0xffffffff, 0x00000000 },
12821
12822 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012823 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012824 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012825 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012826 0x00000000, 0x007fffff },
12827 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12828 0x00000000, 0x0000003f },
12829 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12830 0x00000000, 0x000001ff },
12831 { BUFMGR_MB_HIGH_WATER, 0x0000,
12832 0x00000000, 0x000001ff },
12833 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12834 0xffffffff, 0x00000000 },
12835 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12836 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012837
Michael Chana71116d2005-05-29 14:58:11 -070012838 /* Mailbox Registers */
12839 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12840 0x00000000, 0x000001ff },
12841 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12842 0x00000000, 0x000001ff },
12843 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12844 0x00000000, 0x000007ff },
12845 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12846 0x00000000, 0x000001ff },
12847
12848 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12849 };
12850
Michael Chanb16250e2006-09-27 16:10:14 -070012851 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012852 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012853 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012854 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012855 is_5750 = 1;
12856 }
Michael Chana71116d2005-05-29 14:58:11 -070012857
12858 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12859 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12860 continue;
12861
12862 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12863 continue;
12864
Joe Perches63c3a662011-04-26 08:12:10 +000012865 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012866 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12867 continue;
12868
Michael Chanb16250e2006-09-27 16:10:14 -070012869 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12870 continue;
12871
Michael Chana71116d2005-05-29 14:58:11 -070012872 offset = (u32) reg_tbl[i].offset;
12873 read_mask = reg_tbl[i].read_mask;
12874 write_mask = reg_tbl[i].write_mask;
12875
12876 /* Save the original register content */
12877 save_val = tr32(offset);
12878
12879 /* Determine the read-only value. */
12880 read_val = save_val & read_mask;
12881
12882 /* Write zero to the register, then make sure the read-only bits
12883 * are not changed and the read/write bits are all zeros.
12884 */
12885 tw32(offset, 0);
12886
12887 val = tr32(offset);
12888
12889 /* Test the read-only and read/write bits. */
12890 if (((val & read_mask) != read_val) || (val & write_mask))
12891 goto out;
12892
12893 /* Write ones to all the bits defined by RdMask and WrMask, then
12894 * make sure the read-only bits are not changed and the
12895 * read/write bits are all ones.
12896 */
12897 tw32(offset, read_mask | write_mask);
12898
12899 val = tr32(offset);
12900
12901 /* Test the read-only bits. */
12902 if ((val & read_mask) != read_val)
12903 goto out;
12904
12905 /* Test the read/write bits. */
12906 if ((val & write_mask) != write_mask)
12907 goto out;
12908
12909 tw32(offset, save_val);
12910 }
12911
12912 return 0;
12913
12914out:
Michael Chan9f88f292006-12-07 00:22:54 -080012915 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012916 netdev_err(tp->dev,
12917 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012918 tw32(offset, save_val);
12919 return -EIO;
12920}
12921
Michael Chan7942e1d2005-05-29 14:58:36 -070012922static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12923{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012924 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012925 int i;
12926 u32 j;
12927
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012928 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012929 for (j = 0; j < len; j += 4) {
12930 u32 val;
12931
12932 tg3_write_mem(tp, offset + j, test_pattern[i]);
12933 tg3_read_mem(tp, offset + j, &val);
12934 if (val != test_pattern[i])
12935 return -EIO;
12936 }
12937 }
12938 return 0;
12939}
12940
12941static int tg3_test_memory(struct tg3 *tp)
12942{
12943 static struct mem_entry {
12944 u32 offset;
12945 u32 len;
12946 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012947 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012948 { 0x00002000, 0x1c000},
12949 { 0xffffffff, 0x00000}
12950 }, mem_tbl_5705[] = {
12951 { 0x00000100, 0x0000c},
12952 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012953 { 0x00004000, 0x00800},
12954 { 0x00006000, 0x01000},
12955 { 0x00008000, 0x02000},
12956 { 0x00010000, 0x0e000},
12957 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012958 }, mem_tbl_5755[] = {
12959 { 0x00000200, 0x00008},
12960 { 0x00004000, 0x00800},
12961 { 0x00006000, 0x00800},
12962 { 0x00008000, 0x02000},
12963 { 0x00010000, 0x0c000},
12964 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012965 }, mem_tbl_5906[] = {
12966 { 0x00000200, 0x00008},
12967 { 0x00004000, 0x00400},
12968 { 0x00006000, 0x00400},
12969 { 0x00008000, 0x01000},
12970 { 0x00010000, 0x01000},
12971 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012972 }, mem_tbl_5717[] = {
12973 { 0x00000200, 0x00008},
12974 { 0x00010000, 0x0a000},
12975 { 0x00020000, 0x13c00},
12976 { 0xffffffff, 0x00000}
12977 }, mem_tbl_57765[] = {
12978 { 0x00000200, 0x00008},
12979 { 0x00004000, 0x00800},
12980 { 0x00006000, 0x09800},
12981 { 0x00010000, 0x0a000},
12982 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012983 };
12984 struct mem_entry *mem_tbl;
12985 int err = 0;
12986 int i;
12987
Joe Perches63c3a662011-04-26 08:12:10 +000012988 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012989 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012990 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000012991 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012992 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012993 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012994 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000012995 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080012996 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012997 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012998 mem_tbl = mem_tbl_5705;
12999 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013000 mem_tbl = mem_tbl_570x;
13001
13002 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013003 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13004 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013005 break;
13006 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013007
Michael Chan7942e1d2005-05-29 14:58:36 -070013008 return err;
13009}
13010
Matt Carlsonbb158d62011-04-25 12:42:47 +000013011#define TG3_TSO_MSS 500
13012
13013#define TG3_TSO_IP_HDR_LEN 20
13014#define TG3_TSO_TCP_HDR_LEN 20
13015#define TG3_TSO_TCP_OPT_LEN 12
13016
13017static const u8 tg3_tso_header[] = {
130180x08, 0x00,
130190x45, 0x00, 0x00, 0x00,
130200x00, 0x00, 0x40, 0x00,
130210x40, 0x06, 0x00, 0x00,
130220x0a, 0x00, 0x00, 0x01,
130230x0a, 0x00, 0x00, 0x02,
130240x0d, 0x00, 0xe0, 0x00,
130250x00, 0x00, 0x01, 0x00,
130260x00, 0x00, 0x02, 0x00,
130270x80, 0x10, 0x10, 0x00,
130280x14, 0x09, 0x00, 0x00,
130290x01, 0x01, 0x08, 0x0a,
130300x11, 0x11, 0x11, 0x11,
130310x11, 0x11, 0x11, 0x11,
13032};
Michael Chan9f40dea2005-09-05 17:53:06 -070013033
Matt Carlson28a45952011-08-19 13:58:22 +000013034static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013035{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013036 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013037 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013038 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013039 struct sk_buff *skb;
13040 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013041 dma_addr_t map;
13042 int num_pkts, tx_len, rx_len, i, err;
13043 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013044 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013045 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013046
Matt Carlsonc8873402010-02-12 14:47:11 +000013047 tnapi = &tp->napi[0];
13048 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013049 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013050 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013051 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013052 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013053 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013054 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013055 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013056
Michael Chanc76949a2005-05-29 14:58:59 -070013057 err = -EIO;
13058
Matt Carlson4852a862011-04-13 11:05:07 +000013059 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013060 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013061 if (!skb)
13062 return -ENOMEM;
13063
Michael Chanc76949a2005-05-29 14:58:59 -070013064 tx_data = skb_put(skb, tx_len);
13065 memcpy(tx_data, tp->dev->dev_addr, 6);
13066 memset(tx_data + 6, 0x0, 8);
13067
Matt Carlson4852a862011-04-13 11:05:07 +000013068 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013069
Matt Carlson28a45952011-08-19 13:58:22 +000013070 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013071 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13072
13073 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13074 TG3_TSO_TCP_OPT_LEN;
13075
13076 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13077 sizeof(tg3_tso_header));
13078 mss = TG3_TSO_MSS;
13079
13080 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13081 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13082
13083 /* Set the total length field in the IP header */
13084 iph->tot_len = htons((u16)(mss + hdr_len));
13085
13086 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13087 TXD_FLAG_CPU_POST_DMA);
13088
Joe Perches63c3a662011-04-26 08:12:10 +000013089 if (tg3_flag(tp, HW_TSO_1) ||
13090 tg3_flag(tp, HW_TSO_2) ||
13091 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013092 struct tcphdr *th;
13093 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13094 th = (struct tcphdr *)&tx_data[val];
13095 th->check = 0;
13096 } else
13097 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13098
Joe Perches63c3a662011-04-26 08:12:10 +000013099 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013100 mss |= (hdr_len & 0xc) << 12;
13101 if (hdr_len & 0x10)
13102 base_flags |= 0x00000010;
13103 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013104 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013105 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013106 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013107 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013108 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13109 } else {
13110 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13111 }
13112
13113 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13114 } else {
13115 num_pkts = 1;
13116 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013117
13118 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13119 tx_len > VLAN_ETH_FRAME_LEN)
13120 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013121 }
13122
13123 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013124 tx_data[i] = (u8) (i & 0xff);
13125
Alexander Duyckf4188d82009-12-02 16:48:38 +000013126 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13127 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013128 dev_kfree_skb(skb);
13129 return -EIO;
13130 }
Michael Chanc76949a2005-05-29 14:58:59 -070013131
Matt Carlson0d681b22011-07-27 14:20:49 +000013132 val = tnapi->tx_prod;
13133 tnapi->tx_buffers[val].skb = skb;
13134 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13135
Michael Chanc76949a2005-05-29 14:58:59 -070013136 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013137 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013138
13139 udelay(10);
13140
Matt Carlson898a56f2009-08-28 14:02:40 +000013141 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013142
Matt Carlson84b67b22011-07-27 14:20:52 +000013143 budget = tg3_tx_avail(tnapi);
13144 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013145 base_flags | TXD_FLAG_END, mss, 0)) {
13146 tnapi->tx_buffers[val].skb = NULL;
13147 dev_kfree_skb(skb);
13148 return -EIO;
13149 }
Michael Chanc76949a2005-05-29 14:58:59 -070013150
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013151 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013152
Michael Chan6541b802012-03-04 14:48:14 +000013153 /* Sync BD data before updating mailbox */
13154 wmb();
13155
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013156 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13157 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013158
13159 udelay(10);
13160
Matt Carlson303fc922009-11-02 14:27:34 +000013161 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13162 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013163 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013164 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013165
13166 udelay(10);
13167
Matt Carlson898a56f2009-08-28 14:02:40 +000013168 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13169 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013170 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013171 (rx_idx == (rx_start_idx + num_pkts)))
13172 break;
13173 }
13174
Matt Carlsonba1142e2011-11-04 09:15:00 +000013175 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013176 dev_kfree_skb(skb);
13177
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013178 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013179 goto out;
13180
13181 if (rx_idx != rx_start_idx + num_pkts)
13182 goto out;
13183
Matt Carlsonbb158d62011-04-25 12:42:47 +000013184 val = data_off;
13185 while (rx_idx != rx_start_idx) {
13186 desc = &rnapi->rx_rcb[rx_start_idx++];
13187 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13188 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013189
Matt Carlsonbb158d62011-04-25 12:42:47 +000013190 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13191 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013192 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013193
Matt Carlsonbb158d62011-04-25 12:42:47 +000013194 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13195 - ETH_FCS_LEN;
13196
Matt Carlson28a45952011-08-19 13:58:22 +000013197 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013198 if (rx_len != tx_len)
13199 goto out;
13200
13201 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13202 if (opaque_key != RXD_OPAQUE_RING_STD)
13203 goto out;
13204 } else {
13205 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13206 goto out;
13207 }
13208 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13209 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013210 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013211 goto out;
13212 }
13213
13214 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013215 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013216 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13217 mapping);
13218 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013219 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013220 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13221 mapping);
13222 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013223 goto out;
13224
Matt Carlsonbb158d62011-04-25 12:42:47 +000013225 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13226 PCI_DMA_FROMDEVICE);
13227
Eric Dumazet9205fd92011-11-18 06:47:01 +000013228 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013229 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013230 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013231 goto out;
13232 }
Matt Carlson4852a862011-04-13 11:05:07 +000013233 }
13234
Michael Chanc76949a2005-05-29 14:58:59 -070013235 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013236
Eric Dumazet9205fd92011-11-18 06:47:01 +000013237 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013238out:
13239 return err;
13240}
13241
Matt Carlson00c266b2011-04-25 12:42:46 +000013242#define TG3_STD_LOOPBACK_FAILED 1
13243#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013244#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013245#define TG3_LOOPBACK_FAILED \
13246 (TG3_STD_LOOPBACK_FAILED | \
13247 TG3_JMB_LOOPBACK_FAILED | \
13248 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013249
Matt Carlson941ec902011-08-19 13:58:23 +000013250static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013251{
Matt Carlson28a45952011-08-19 13:58:22 +000013252 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013253 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013254 u32 jmb_pkt_sz = 9000;
13255
13256 if (tp->dma_limit)
13257 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013258
Matt Carlsonab789042011-01-25 15:58:54 +000013259 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13260 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13261
Matt Carlson28a45952011-08-19 13:58:22 +000013262 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013263 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13264 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013265 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013266 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013267 goto done;
13268 }
13269
Joe Perches953c96e2013-04-09 10:18:14 +000013270 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013271 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013272 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13273 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013274 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013275 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013276 goto done;
13277 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013278
Joe Perches63c3a662011-04-26 08:12:10 +000013279 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013280 int i;
13281
13282 /* Reroute all rx packets to the 1st queue */
13283 for (i = MAC_RSS_INDIR_TBL_0;
13284 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13285 tw32(i, 0x0);
13286 }
13287
Matt Carlson6e01b202011-08-19 13:58:20 +000013288 /* HW errata - mac loopback fails in some cases on 5780.
13289 * Normal traffic and PHY loopback are not affected by
13290 * errata. Also, the MAC loopback test is deprecated for
13291 * all newer ASIC revisions.
13292 */
Joe Perches41535772013-02-16 11:20:04 +000013293 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013294 !tg3_flag(tp, CPMU_PRESENT)) {
13295 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013296
Matt Carlson28a45952011-08-19 13:58:22 +000013297 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013298 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013299
13300 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013301 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013302 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013303
13304 tg3_mac_loopback(tp, false);
13305 }
Matt Carlson4852a862011-04-13 11:05:07 +000013306
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013307 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013308 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013309 int i;
13310
Matt Carlson941ec902011-08-19 13:58:23 +000013311 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013312
13313 /* Wait for link */
13314 for (i = 0; i < 100; i++) {
13315 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13316 break;
13317 mdelay(1);
13318 }
13319
Matt Carlson28a45952011-08-19 13:58:22 +000013320 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013321 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013322 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013323 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013324 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013325 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013326 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013327 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013328
Matt Carlson941ec902011-08-19 13:58:23 +000013329 if (do_extlpbk) {
13330 tg3_phy_lpbk_set(tp, 0, true);
13331
13332 /* All link indications report up, but the hardware
13333 * isn't really ready for about 20 msec. Double it
13334 * to be sure.
13335 */
13336 mdelay(40);
13337
13338 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013339 data[TG3_EXT_LOOPB_TEST] |=
13340 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013341 if (tg3_flag(tp, TSO_CAPABLE) &&
13342 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013343 data[TG3_EXT_LOOPB_TEST] |=
13344 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013345 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013346 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013347 data[TG3_EXT_LOOPB_TEST] |=
13348 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013349 }
13350
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013351 /* Re-enable gphy autopowerdown. */
13352 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13353 tg3_phy_toggle_apd(tp, true);
13354 }
Matt Carlson6833c042008-11-21 17:18:59 -080013355
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013356 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13357 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013358
Matt Carlsonab789042011-01-25 15:58:54 +000013359done:
13360 tp->phy_flags |= eee_cap;
13361
Michael Chan9f40dea2005-09-05 17:53:06 -070013362 return err;
13363}
13364
Michael Chan4cafd3f2005-05-29 14:56:34 -070013365static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13366 u64 *data)
13367{
Michael Chan566f86a2005-05-29 14:56:58 -070013368 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013369 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013370
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013371 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13372 if (tg3_power_up(tp)) {
13373 etest->flags |= ETH_TEST_FL_FAILED;
13374 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13375 return;
13376 }
13377 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013378 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013379
Michael Chan566f86a2005-05-29 14:56:58 -070013380 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13381
13382 if (tg3_test_nvram(tp) != 0) {
13383 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013384 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013385 }
Matt Carlson941ec902011-08-19 13:58:23 +000013386 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013387 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013388 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013389 }
Michael Chana71116d2005-05-29 14:58:11 -070013390 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013391 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013392
Michael Chanbbe832c2005-06-24 20:20:04 -070013393 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013394 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013395 tg3_netif_stop(tp);
13396 irq_sync = 1;
13397 }
13398
13399 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013400 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013401 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013402 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013403 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013404 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013405 if (!err)
13406 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013407
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013408 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013409 tg3_phy_reset(tp);
13410
Michael Chana71116d2005-05-29 14:58:11 -070013411 if (tg3_test_registers(tp) != 0) {
13412 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013413 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013414 }
Matt Carlson28a45952011-08-19 13:58:22 +000013415
Michael Chan7942e1d2005-05-29 14:58:36 -070013416 if (tg3_test_memory(tp) != 0) {
13417 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013418 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013419 }
Matt Carlson28a45952011-08-19 13:58:22 +000013420
Matt Carlson941ec902011-08-19 13:58:23 +000013421 if (doextlpbk)
13422 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13423
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013424 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013425 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013426
David S. Millerf47c11e2005-06-24 20:18:35 -070013427 tg3_full_unlock(tp);
13428
Michael Chand4bc3922005-05-29 14:59:20 -070013429 if (tg3_test_interrupt(tp) != 0) {
13430 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013431 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013432 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013433
13434 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013435
Michael Chana71116d2005-05-29 14:58:11 -070013436 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13437 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013438 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013439 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013440 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013441 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013442 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013443
13444 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013445
13446 if (irq_sync && !err2)
13447 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013448 }
Matt Carlson80096062010-08-02 11:26:06 +000013449 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013450 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013451
Michael Chan4cafd3f2005-05-29 14:56:34 -070013452}
13453
Matt Carlson0a633ac2012-12-03 19:36:59 +000013454static int tg3_hwtstamp_ioctl(struct net_device *dev,
13455 struct ifreq *ifr, int cmd)
13456{
13457 struct tg3 *tp = netdev_priv(dev);
13458 struct hwtstamp_config stmpconf;
13459
13460 if (!tg3_flag(tp, PTP_CAPABLE))
13461 return -EINVAL;
13462
13463 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13464 return -EFAULT;
13465
13466 if (stmpconf.flags)
13467 return -EINVAL;
13468
13469 switch (stmpconf.tx_type) {
13470 case HWTSTAMP_TX_ON:
13471 tg3_flag_set(tp, TX_TSTAMP_EN);
13472 break;
13473 case HWTSTAMP_TX_OFF:
13474 tg3_flag_clear(tp, TX_TSTAMP_EN);
13475 break;
13476 default:
13477 return -ERANGE;
13478 }
13479
13480 switch (stmpconf.rx_filter) {
13481 case HWTSTAMP_FILTER_NONE:
13482 tp->rxptpctl = 0;
13483 break;
13484 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13485 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13486 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13487 break;
13488 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13489 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13490 TG3_RX_PTP_CTL_SYNC_EVNT;
13491 break;
13492 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13493 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13494 TG3_RX_PTP_CTL_DELAY_REQ;
13495 break;
13496 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13497 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13498 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13499 break;
13500 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13501 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13502 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13503 break;
13504 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13505 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13506 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13507 break;
13508 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13509 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13510 TG3_RX_PTP_CTL_SYNC_EVNT;
13511 break;
13512 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13513 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13514 TG3_RX_PTP_CTL_SYNC_EVNT;
13515 break;
13516 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13517 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13518 TG3_RX_PTP_CTL_SYNC_EVNT;
13519 break;
13520 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13521 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13522 TG3_RX_PTP_CTL_DELAY_REQ;
13523 break;
13524 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13525 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13526 TG3_RX_PTP_CTL_DELAY_REQ;
13527 break;
13528 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13529 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13530 TG3_RX_PTP_CTL_DELAY_REQ;
13531 break;
13532 default:
13533 return -ERANGE;
13534 }
13535
13536 if (netif_running(dev) && tp->rxptpctl)
13537 tw32(TG3_RX_PTP_CTL,
13538 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13539
13540 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13541 -EFAULT : 0;
13542}
13543
Linus Torvalds1da177e2005-04-16 15:20:36 -070013544static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13545{
13546 struct mii_ioctl_data *data = if_mii(ifr);
13547 struct tg3 *tp = netdev_priv(dev);
13548 int err;
13549
Joe Perches63c3a662011-04-26 08:12:10 +000013550 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013551 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013552 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013553 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013554 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013555 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013556 }
13557
Matt Carlson33f401a2010-04-05 10:19:27 +000013558 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013559 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013560 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013561
13562 /* fallthru */
13563 case SIOCGMIIREG: {
13564 u32 mii_regval;
13565
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013566 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013567 break; /* We have no PHY */
13568
Matt Carlson34eea5a2011-04-20 07:57:38 +000013569 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013570 return -EAGAIN;
13571
David S. Millerf47c11e2005-06-24 20:18:35 -070013572 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013573 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13574 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013575 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013576
13577 data->val_out = mii_regval;
13578
13579 return err;
13580 }
13581
13582 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013583 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013584 break; /* We have no PHY */
13585
Matt Carlson34eea5a2011-04-20 07:57:38 +000013586 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013587 return -EAGAIN;
13588
David S. Millerf47c11e2005-06-24 20:18:35 -070013589 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013590 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13591 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013592 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013593
13594 return err;
13595
Matt Carlson0a633ac2012-12-03 19:36:59 +000013596 case SIOCSHWTSTAMP:
13597 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13598
Linus Torvalds1da177e2005-04-16 15:20:36 -070013599 default:
13600 /* do nothing */
13601 break;
13602 }
13603 return -EOPNOTSUPP;
13604}
13605
David S. Miller15f98502005-05-18 22:49:26 -070013606static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13607{
13608 struct tg3 *tp = netdev_priv(dev);
13609
13610 memcpy(ec, &tp->coal, sizeof(*ec));
13611 return 0;
13612}
13613
Michael Chand244c892005-07-05 14:42:33 -070013614static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13615{
13616 struct tg3 *tp = netdev_priv(dev);
13617 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13618 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13619
Joe Perches63c3a662011-04-26 08:12:10 +000013620 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013621 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13622 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13623 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13624 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13625 }
13626
13627 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13628 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13629 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13630 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13631 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13632 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13633 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13634 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13635 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13636 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13637 return -EINVAL;
13638
13639 /* No rx interrupts will be generated if both are zero */
13640 if ((ec->rx_coalesce_usecs == 0) &&
13641 (ec->rx_max_coalesced_frames == 0))
13642 return -EINVAL;
13643
13644 /* No tx interrupts will be generated if both are zero */
13645 if ((ec->tx_coalesce_usecs == 0) &&
13646 (ec->tx_max_coalesced_frames == 0))
13647 return -EINVAL;
13648
13649 /* Only copy relevant parameters, ignore all others. */
13650 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13651 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13652 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13653 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13654 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13655 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13656 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13657 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13658 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13659
13660 if (netif_running(dev)) {
13661 tg3_full_lock(tp, 0);
13662 __tg3_set_coalesce(tp, &tp->coal);
13663 tg3_full_unlock(tp);
13664 }
13665 return 0;
13666}
13667
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013668static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
13669{
13670 struct tg3 *tp = netdev_priv(dev);
13671
13672 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13673 netdev_warn(tp->dev, "Board does not support EEE!\n");
13674 return -EOPNOTSUPP;
13675 }
13676
13677 if (edata->advertised != tp->eee.advertised) {
13678 netdev_warn(tp->dev,
13679 "Direct manipulation of EEE advertisement is not supported\n");
13680 return -EINVAL;
13681 }
13682
13683 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
13684 netdev_warn(tp->dev,
13685 "Maximal Tx Lpi timer supported is %#x(u)\n",
13686 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
13687 return -EINVAL;
13688 }
13689
13690 tp->eee = *edata;
13691
13692 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
13693 tg3_warn_mgmt_link_flap(tp);
13694
13695 if (netif_running(tp->dev)) {
13696 tg3_full_lock(tp, 0);
13697 tg3_setup_eee(tp);
13698 tg3_phy_reset(tp);
13699 tg3_full_unlock(tp);
13700 }
13701
13702 return 0;
13703}
13704
13705static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
13706{
13707 struct tg3 *tp = netdev_priv(dev);
13708
13709 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13710 netdev_warn(tp->dev,
13711 "Board does not support EEE!\n");
13712 return -EOPNOTSUPP;
13713 }
13714
13715 *edata = tp->eee;
13716 return 0;
13717}
13718
Jeff Garzik7282d492006-09-13 14:30:00 -040013719static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013720 .get_settings = tg3_get_settings,
13721 .set_settings = tg3_set_settings,
13722 .get_drvinfo = tg3_get_drvinfo,
13723 .get_regs_len = tg3_get_regs_len,
13724 .get_regs = tg3_get_regs,
13725 .get_wol = tg3_get_wol,
13726 .set_wol = tg3_set_wol,
13727 .get_msglevel = tg3_get_msglevel,
13728 .set_msglevel = tg3_set_msglevel,
13729 .nway_reset = tg3_nway_reset,
13730 .get_link = ethtool_op_get_link,
13731 .get_eeprom_len = tg3_get_eeprom_len,
13732 .get_eeprom = tg3_get_eeprom,
13733 .set_eeprom = tg3_set_eeprom,
13734 .get_ringparam = tg3_get_ringparam,
13735 .set_ringparam = tg3_set_ringparam,
13736 .get_pauseparam = tg3_get_pauseparam,
13737 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013738 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013739 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013740 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013741 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013742 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013743 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013744 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013745 .get_rxnfc = tg3_get_rxnfc,
13746 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13747 .get_rxfh_indir = tg3_get_rxfh_indir,
13748 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013749 .get_channels = tg3_get_channels,
13750 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013751 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013752 .get_eee = tg3_get_eee,
13753 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013754};
13755
David S. Millerb4017c52012-03-01 17:57:40 -050013756static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13757 struct rtnl_link_stats64 *stats)
13758{
13759 struct tg3 *tp = netdev_priv(dev);
13760
David S. Millerb4017c52012-03-01 17:57:40 -050013761 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013762 if (!tp->hw_stats) {
13763 spin_unlock_bh(&tp->lock);
13764 return &tp->net_stats_prev;
13765 }
13766
David S. Millerb4017c52012-03-01 17:57:40 -050013767 tg3_get_nstats(tp, stats);
13768 spin_unlock_bh(&tp->lock);
13769
13770 return stats;
13771}
13772
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013773static void tg3_set_rx_mode(struct net_device *dev)
13774{
13775 struct tg3 *tp = netdev_priv(dev);
13776
13777 if (!netif_running(dev))
13778 return;
13779
13780 tg3_full_lock(tp, 0);
13781 __tg3_set_rx_mode(dev);
13782 tg3_full_unlock(tp);
13783}
13784
Matt Carlsonfaf16272012-02-13 10:20:07 +000013785static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13786 int new_mtu)
13787{
13788 dev->mtu = new_mtu;
13789
13790 if (new_mtu > ETH_DATA_LEN) {
13791 if (tg3_flag(tp, 5780_CLASS)) {
13792 netdev_update_features(dev);
13793 tg3_flag_clear(tp, TSO_CAPABLE);
13794 } else {
13795 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13796 }
13797 } else {
13798 if (tg3_flag(tp, 5780_CLASS)) {
13799 tg3_flag_set(tp, TSO_CAPABLE);
13800 netdev_update_features(dev);
13801 }
13802 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13803 }
13804}
13805
13806static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13807{
13808 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000013809 int err;
13810 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013811
13812 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13813 return -EINVAL;
13814
13815 if (!netif_running(dev)) {
13816 /* We'll just catch it later when the
13817 * device is up'd.
13818 */
13819 tg3_set_mtu(dev, tp, new_mtu);
13820 return 0;
13821 }
13822
13823 tg3_phy_stop(tp);
13824
13825 tg3_netif_stop(tp);
13826
13827 tg3_full_lock(tp, 1);
13828
13829 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13830
13831 tg3_set_mtu(dev, tp, new_mtu);
13832
Michael Chan2fae5e32012-03-04 14:48:15 +000013833 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13834 * breaks all requests to 256 bytes.
13835 */
Joe Perches41535772013-02-16 11:20:04 +000013836 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000013837 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000013838
13839 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013840
13841 if (!err)
13842 tg3_netif_start(tp);
13843
13844 tg3_full_unlock(tp);
13845
13846 if (!err)
13847 tg3_phy_start(tp);
13848
13849 return err;
13850}
13851
13852static const struct net_device_ops tg3_netdev_ops = {
13853 .ndo_open = tg3_open,
13854 .ndo_stop = tg3_close,
13855 .ndo_start_xmit = tg3_start_xmit,
13856 .ndo_get_stats64 = tg3_get_stats64,
13857 .ndo_validate_addr = eth_validate_addr,
13858 .ndo_set_rx_mode = tg3_set_rx_mode,
13859 .ndo_set_mac_address = tg3_set_mac_addr,
13860 .ndo_do_ioctl = tg3_ioctl,
13861 .ndo_tx_timeout = tg3_tx_timeout,
13862 .ndo_change_mtu = tg3_change_mtu,
13863 .ndo_fix_features = tg3_fix_features,
13864 .ndo_set_features = tg3_set_features,
13865#ifdef CONFIG_NET_POLL_CONTROLLER
13866 .ndo_poll_controller = tg3_poll_controller,
13867#endif
13868};
13869
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013870static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013871{
Michael Chan1b277772006-03-20 22:27:48 -080013872 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013873
13874 tp->nvram_size = EEPROM_CHIP_SIZE;
13875
Matt Carlsone4f34112009-02-25 14:25:00 +000013876 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013877 return;
13878
Michael Chanb16250e2006-09-27 16:10:14 -070013879 if ((magic != TG3_EEPROM_MAGIC) &&
13880 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13881 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013882 return;
13883
13884 /*
13885 * Size the chip by reading offsets at increasing powers of two.
13886 * When we encounter our validation signature, we know the addressing
13887 * has wrapped around, and thus have our chip size.
13888 */
Michael Chan1b277772006-03-20 22:27:48 -080013889 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013890
13891 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013892 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013893 return;
13894
Michael Chan18201802006-03-20 22:29:15 -080013895 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013896 break;
13897
13898 cursize <<= 1;
13899 }
13900
13901 tp->nvram_size = cursize;
13902}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013903
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013904static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013905{
13906 u32 val;
13907
Joe Perches63c3a662011-04-26 08:12:10 +000013908 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013909 return;
13910
13911 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013912 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013913 tg3_get_eeprom_size(tp);
13914 return;
13915 }
13916
Matt Carlson6d348f22009-02-25 14:25:52 +000013917 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013918 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013919 /* This is confusing. We want to operate on the
13920 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13921 * call will read from NVRAM and byteswap the data
13922 * according to the byteswapping settings for all
13923 * other register accesses. This ensures the data we
13924 * want will always reside in the lower 16-bits.
13925 * However, the data in NVRAM is in LE format, which
13926 * means the data from the NVRAM read will always be
13927 * opposite the endianness of the CPU. The 16-bit
13928 * byteswap then brings the data to CPU endianness.
13929 */
13930 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013931 return;
13932 }
13933 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013934 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013935}
13936
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013937static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013938{
13939 u32 nvcfg1;
13940
13941 nvcfg1 = tr32(NVRAM_CFG1);
13942 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013943 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013944 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013945 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13946 tw32(NVRAM_CFG1, nvcfg1);
13947 }
13948
Joe Perches41535772013-02-16 11:20:04 +000013949 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013950 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013951 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013952 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13953 tp->nvram_jedecnum = JEDEC_ATMEL;
13954 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013955 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013956 break;
13957 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13958 tp->nvram_jedecnum = JEDEC_ATMEL;
13959 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13960 break;
13961 case FLASH_VENDOR_ATMEL_EEPROM:
13962 tp->nvram_jedecnum = JEDEC_ATMEL;
13963 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013964 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013965 break;
13966 case FLASH_VENDOR_ST:
13967 tp->nvram_jedecnum = JEDEC_ST;
13968 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013969 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013970 break;
13971 case FLASH_VENDOR_SAIFUN:
13972 tp->nvram_jedecnum = JEDEC_SAIFUN;
13973 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13974 break;
13975 case FLASH_VENDOR_SST_SMALL:
13976 case FLASH_VENDOR_SST_LARGE:
13977 tp->nvram_jedecnum = JEDEC_SST;
13978 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980 }
Matt Carlson8590a602009-08-28 12:29:16 +000013981 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982 tp->nvram_jedecnum = JEDEC_ATMEL;
13983 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013984 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013985 }
13986}
13987
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013988static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013989{
13990 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13991 case FLASH_5752PAGE_SIZE_256:
13992 tp->nvram_pagesize = 256;
13993 break;
13994 case FLASH_5752PAGE_SIZE_512:
13995 tp->nvram_pagesize = 512;
13996 break;
13997 case FLASH_5752PAGE_SIZE_1K:
13998 tp->nvram_pagesize = 1024;
13999 break;
14000 case FLASH_5752PAGE_SIZE_2K:
14001 tp->nvram_pagesize = 2048;
14002 break;
14003 case FLASH_5752PAGE_SIZE_4K:
14004 tp->nvram_pagesize = 4096;
14005 break;
14006 case FLASH_5752PAGE_SIZE_264:
14007 tp->nvram_pagesize = 264;
14008 break;
14009 case FLASH_5752PAGE_SIZE_528:
14010 tp->nvram_pagesize = 528;
14011 break;
14012 }
14013}
14014
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014015static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014016{
14017 u32 nvcfg1;
14018
14019 nvcfg1 = tr32(NVRAM_CFG1);
14020
Michael Chane6af3012005-04-21 17:12:05 -070014021 /* NVRAM protection for TPM */
14022 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014023 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014024
Michael Chan361b4ac2005-04-21 17:11:21 -070014025 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014026 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14027 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14028 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014029 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014030 break;
14031 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14032 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014033 tg3_flag_set(tp, NVRAM_BUFFERED);
14034 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014035 break;
14036 case FLASH_5752VENDOR_ST_M45PE10:
14037 case FLASH_5752VENDOR_ST_M45PE20:
14038 case FLASH_5752VENDOR_ST_M45PE40:
14039 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014040 tg3_flag_set(tp, NVRAM_BUFFERED);
14041 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014042 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014043 }
14044
Joe Perches63c3a662011-04-26 08:12:10 +000014045 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014046 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014047 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014048 /* For eeprom, set pagesize to maximum eeprom size */
14049 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14050
14051 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14052 tw32(NVRAM_CFG1, nvcfg1);
14053 }
14054}
14055
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014056static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014057{
Matt Carlson989a9d22007-05-05 11:51:05 -070014058 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014059
14060 nvcfg1 = tr32(NVRAM_CFG1);
14061
14062 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014063 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014064 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014065 protect = 1;
14066 }
Michael Chand3c7b882006-03-23 01:28:25 -080014067
Matt Carlson989a9d22007-05-05 11:51:05 -070014068 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14069 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014070 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14071 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14072 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14073 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14074 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014075 tg3_flag_set(tp, NVRAM_BUFFERED);
14076 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014077 tp->nvram_pagesize = 264;
14078 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14079 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14080 tp->nvram_size = (protect ? 0x3e200 :
14081 TG3_NVRAM_SIZE_512KB);
14082 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14083 tp->nvram_size = (protect ? 0x1f200 :
14084 TG3_NVRAM_SIZE_256KB);
14085 else
14086 tp->nvram_size = (protect ? 0x1f200 :
14087 TG3_NVRAM_SIZE_128KB);
14088 break;
14089 case FLASH_5752VENDOR_ST_M45PE10:
14090 case FLASH_5752VENDOR_ST_M45PE20:
14091 case FLASH_5752VENDOR_ST_M45PE40:
14092 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014093 tg3_flag_set(tp, NVRAM_BUFFERED);
14094 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014095 tp->nvram_pagesize = 256;
14096 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14097 tp->nvram_size = (protect ?
14098 TG3_NVRAM_SIZE_64KB :
14099 TG3_NVRAM_SIZE_128KB);
14100 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14101 tp->nvram_size = (protect ?
14102 TG3_NVRAM_SIZE_64KB :
14103 TG3_NVRAM_SIZE_256KB);
14104 else
14105 tp->nvram_size = (protect ?
14106 TG3_NVRAM_SIZE_128KB :
14107 TG3_NVRAM_SIZE_512KB);
14108 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014109 }
14110}
14111
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014112static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014113{
14114 u32 nvcfg1;
14115
14116 nvcfg1 = tr32(NVRAM_CFG1);
14117
14118 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014119 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14120 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14121 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14122 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14123 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014124 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014125 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014126
Matt Carlson8590a602009-08-28 12:29:16 +000014127 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14128 tw32(NVRAM_CFG1, nvcfg1);
14129 break;
14130 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14131 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14132 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14133 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14134 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014135 tg3_flag_set(tp, NVRAM_BUFFERED);
14136 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014137 tp->nvram_pagesize = 264;
14138 break;
14139 case FLASH_5752VENDOR_ST_M45PE10:
14140 case FLASH_5752VENDOR_ST_M45PE20:
14141 case FLASH_5752VENDOR_ST_M45PE40:
14142 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014143 tg3_flag_set(tp, NVRAM_BUFFERED);
14144 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014145 tp->nvram_pagesize = 256;
14146 break;
Michael Chan1b277772006-03-20 22:27:48 -080014147 }
14148}
14149
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014150static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014151{
14152 u32 nvcfg1, protect = 0;
14153
14154 nvcfg1 = tr32(NVRAM_CFG1);
14155
14156 /* NVRAM protection for TPM */
14157 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014158 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014159 protect = 1;
14160 }
14161
14162 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14163 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014164 case FLASH_5761VENDOR_ATMEL_ADB021D:
14165 case FLASH_5761VENDOR_ATMEL_ADB041D:
14166 case FLASH_5761VENDOR_ATMEL_ADB081D:
14167 case FLASH_5761VENDOR_ATMEL_ADB161D:
14168 case FLASH_5761VENDOR_ATMEL_MDB021D:
14169 case FLASH_5761VENDOR_ATMEL_MDB041D:
14170 case FLASH_5761VENDOR_ATMEL_MDB081D:
14171 case FLASH_5761VENDOR_ATMEL_MDB161D:
14172 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014173 tg3_flag_set(tp, NVRAM_BUFFERED);
14174 tg3_flag_set(tp, FLASH);
14175 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014176 tp->nvram_pagesize = 256;
14177 break;
14178 case FLASH_5761VENDOR_ST_A_M45PE20:
14179 case FLASH_5761VENDOR_ST_A_M45PE40:
14180 case FLASH_5761VENDOR_ST_A_M45PE80:
14181 case FLASH_5761VENDOR_ST_A_M45PE16:
14182 case FLASH_5761VENDOR_ST_M_M45PE20:
14183 case FLASH_5761VENDOR_ST_M_M45PE40:
14184 case FLASH_5761VENDOR_ST_M_M45PE80:
14185 case FLASH_5761VENDOR_ST_M_M45PE16:
14186 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014187 tg3_flag_set(tp, NVRAM_BUFFERED);
14188 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014189 tp->nvram_pagesize = 256;
14190 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014191 }
14192
14193 if (protect) {
14194 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14195 } else {
14196 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014197 case FLASH_5761VENDOR_ATMEL_ADB161D:
14198 case FLASH_5761VENDOR_ATMEL_MDB161D:
14199 case FLASH_5761VENDOR_ST_A_M45PE16:
14200 case FLASH_5761VENDOR_ST_M_M45PE16:
14201 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14202 break;
14203 case FLASH_5761VENDOR_ATMEL_ADB081D:
14204 case FLASH_5761VENDOR_ATMEL_MDB081D:
14205 case FLASH_5761VENDOR_ST_A_M45PE80:
14206 case FLASH_5761VENDOR_ST_M_M45PE80:
14207 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14208 break;
14209 case FLASH_5761VENDOR_ATMEL_ADB041D:
14210 case FLASH_5761VENDOR_ATMEL_MDB041D:
14211 case FLASH_5761VENDOR_ST_A_M45PE40:
14212 case FLASH_5761VENDOR_ST_M_M45PE40:
14213 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14214 break;
14215 case FLASH_5761VENDOR_ATMEL_ADB021D:
14216 case FLASH_5761VENDOR_ATMEL_MDB021D:
14217 case FLASH_5761VENDOR_ST_A_M45PE20:
14218 case FLASH_5761VENDOR_ST_M_M45PE20:
14219 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14220 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014221 }
14222 }
14223}
14224
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014225static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014226{
14227 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014228 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014229 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14230}
14231
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014232static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014233{
14234 u32 nvcfg1;
14235
14236 nvcfg1 = tr32(NVRAM_CFG1);
14237
14238 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14239 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14240 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14241 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014242 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014243 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14244
14245 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14246 tw32(NVRAM_CFG1, nvcfg1);
14247 return;
14248 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14249 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14250 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14251 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14252 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14253 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14254 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14255 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014256 tg3_flag_set(tp, NVRAM_BUFFERED);
14257 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014258
14259 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14260 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14261 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14262 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14263 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14264 break;
14265 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14266 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14267 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14268 break;
14269 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14270 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14271 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14272 break;
14273 }
14274 break;
14275 case FLASH_5752VENDOR_ST_M45PE10:
14276 case FLASH_5752VENDOR_ST_M45PE20:
14277 case FLASH_5752VENDOR_ST_M45PE40:
14278 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014279 tg3_flag_set(tp, NVRAM_BUFFERED);
14280 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014281
14282 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14283 case FLASH_5752VENDOR_ST_M45PE10:
14284 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14285 break;
14286 case FLASH_5752VENDOR_ST_M45PE20:
14287 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14288 break;
14289 case FLASH_5752VENDOR_ST_M45PE40:
14290 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14291 break;
14292 }
14293 break;
14294 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014295 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014296 return;
14297 }
14298
Matt Carlsona1b950d2009-09-01 13:20:17 +000014299 tg3_nvram_get_pagesize(tp, nvcfg1);
14300 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014301 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014302}
14303
14304
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014305static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014306{
14307 u32 nvcfg1;
14308
14309 nvcfg1 = tr32(NVRAM_CFG1);
14310
14311 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14312 case FLASH_5717VENDOR_ATMEL_EEPROM:
14313 case FLASH_5717VENDOR_MICRO_EEPROM:
14314 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014315 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014316 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14317
14318 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14319 tw32(NVRAM_CFG1, nvcfg1);
14320 return;
14321 case FLASH_5717VENDOR_ATMEL_MDB011D:
14322 case FLASH_5717VENDOR_ATMEL_ADB011B:
14323 case FLASH_5717VENDOR_ATMEL_ADB011D:
14324 case FLASH_5717VENDOR_ATMEL_MDB021D:
14325 case FLASH_5717VENDOR_ATMEL_ADB021B:
14326 case FLASH_5717VENDOR_ATMEL_ADB021D:
14327 case FLASH_5717VENDOR_ATMEL_45USPT:
14328 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014329 tg3_flag_set(tp, NVRAM_BUFFERED);
14330 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014331
14332 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14333 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014334 /* Detect size with tg3_nvram_get_size() */
14335 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014336 case FLASH_5717VENDOR_ATMEL_ADB021B:
14337 case FLASH_5717VENDOR_ATMEL_ADB021D:
14338 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14339 break;
14340 default:
14341 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14342 break;
14343 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014344 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014345 case FLASH_5717VENDOR_ST_M_M25PE10:
14346 case FLASH_5717VENDOR_ST_A_M25PE10:
14347 case FLASH_5717VENDOR_ST_M_M45PE10:
14348 case FLASH_5717VENDOR_ST_A_M45PE10:
14349 case FLASH_5717VENDOR_ST_M_M25PE20:
14350 case FLASH_5717VENDOR_ST_A_M25PE20:
14351 case FLASH_5717VENDOR_ST_M_M45PE20:
14352 case FLASH_5717VENDOR_ST_A_M45PE20:
14353 case FLASH_5717VENDOR_ST_25USPT:
14354 case FLASH_5717VENDOR_ST_45USPT:
14355 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014356 tg3_flag_set(tp, NVRAM_BUFFERED);
14357 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014358
14359 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14360 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014361 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014362 /* Detect size with tg3_nvram_get_size() */
14363 break;
14364 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014365 case FLASH_5717VENDOR_ST_A_M45PE20:
14366 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14367 break;
14368 default:
14369 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14370 break;
14371 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014372 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014373 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014374 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014375 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014376 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014377
14378 tg3_nvram_get_pagesize(tp, nvcfg1);
14379 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014380 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014381}
14382
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014383static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014384{
14385 u32 nvcfg1, nvmpinstrp;
14386
14387 nvcfg1 = tr32(NVRAM_CFG1);
14388 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14389
Joe Perches41535772013-02-16 11:20:04 +000014390 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014391 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14392 tg3_flag_set(tp, NO_NVRAM);
14393 return;
14394 }
14395
14396 switch (nvmpinstrp) {
14397 case FLASH_5762_EEPROM_HD:
14398 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014399 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014400 case FLASH_5762_EEPROM_LD:
14401 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014402 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014403 case FLASH_5720VENDOR_M_ST_M45PE20:
14404 /* This pinstrap supports multiple sizes, so force it
14405 * to read the actual size from location 0xf0.
14406 */
14407 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14408 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014409 }
14410 }
14411
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014412 switch (nvmpinstrp) {
14413 case FLASH_5720_EEPROM_HD:
14414 case FLASH_5720_EEPROM_LD:
14415 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014416 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014417
14418 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14419 tw32(NVRAM_CFG1, nvcfg1);
14420 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14421 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14422 else
14423 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14424 return;
14425 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14426 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14427 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14428 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14429 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14430 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14431 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14432 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14433 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14434 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14435 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14436 case FLASH_5720VENDOR_ATMEL_45USPT:
14437 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014438 tg3_flag_set(tp, NVRAM_BUFFERED);
14439 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014440
14441 switch (nvmpinstrp) {
14442 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14443 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14444 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14445 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14446 break;
14447 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14448 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14449 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14450 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14451 break;
14452 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14453 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14454 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14455 break;
14456 default:
Joe Perches41535772013-02-16 11:20:04 +000014457 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014458 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014459 break;
14460 }
14461 break;
14462 case FLASH_5720VENDOR_M_ST_M25PE10:
14463 case FLASH_5720VENDOR_M_ST_M45PE10:
14464 case FLASH_5720VENDOR_A_ST_M25PE10:
14465 case FLASH_5720VENDOR_A_ST_M45PE10:
14466 case FLASH_5720VENDOR_M_ST_M25PE20:
14467 case FLASH_5720VENDOR_M_ST_M45PE20:
14468 case FLASH_5720VENDOR_A_ST_M25PE20:
14469 case FLASH_5720VENDOR_A_ST_M45PE20:
14470 case FLASH_5720VENDOR_M_ST_M25PE40:
14471 case FLASH_5720VENDOR_M_ST_M45PE40:
14472 case FLASH_5720VENDOR_A_ST_M25PE40:
14473 case FLASH_5720VENDOR_A_ST_M45PE40:
14474 case FLASH_5720VENDOR_M_ST_M25PE80:
14475 case FLASH_5720VENDOR_M_ST_M45PE80:
14476 case FLASH_5720VENDOR_A_ST_M25PE80:
14477 case FLASH_5720VENDOR_A_ST_M45PE80:
14478 case FLASH_5720VENDOR_ST_25USPT:
14479 case FLASH_5720VENDOR_ST_45USPT:
14480 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014481 tg3_flag_set(tp, NVRAM_BUFFERED);
14482 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014483
14484 switch (nvmpinstrp) {
14485 case FLASH_5720VENDOR_M_ST_M25PE20:
14486 case FLASH_5720VENDOR_M_ST_M45PE20:
14487 case FLASH_5720VENDOR_A_ST_M25PE20:
14488 case FLASH_5720VENDOR_A_ST_M45PE20:
14489 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14490 break;
14491 case FLASH_5720VENDOR_M_ST_M25PE40:
14492 case FLASH_5720VENDOR_M_ST_M45PE40:
14493 case FLASH_5720VENDOR_A_ST_M25PE40:
14494 case FLASH_5720VENDOR_A_ST_M45PE40:
14495 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14496 break;
14497 case FLASH_5720VENDOR_M_ST_M25PE80:
14498 case FLASH_5720VENDOR_M_ST_M45PE80:
14499 case FLASH_5720VENDOR_A_ST_M25PE80:
14500 case FLASH_5720VENDOR_A_ST_M45PE80:
14501 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14502 break;
14503 default:
Joe Perches41535772013-02-16 11:20:04 +000014504 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014505 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014506 break;
14507 }
14508 break;
14509 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014510 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014511 return;
14512 }
14513
14514 tg3_nvram_get_pagesize(tp, nvcfg1);
14515 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014516 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014517
Joe Perches41535772013-02-16 11:20:04 +000014518 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014519 u32 val;
14520
14521 if (tg3_nvram_read(tp, 0, &val))
14522 return;
14523
14524 if (val != TG3_EEPROM_MAGIC &&
14525 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14526 tg3_flag_set(tp, NO_NVRAM);
14527 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014528}
14529
Linus Torvalds1da177e2005-04-16 15:20:36 -070014530/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014531static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014532{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014533 if (tg3_flag(tp, IS_SSB_CORE)) {
14534 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14535 tg3_flag_clear(tp, NVRAM);
14536 tg3_flag_clear(tp, NVRAM_BUFFERED);
14537 tg3_flag_set(tp, NO_NVRAM);
14538 return;
14539 }
14540
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541 tw32_f(GRC_EEPROM_ADDR,
14542 (EEPROM_ADDR_FSM_RESET |
14543 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14544 EEPROM_ADDR_CLKPERD_SHIFT)));
14545
Michael Chan9d57f012006-12-07 00:23:25 -080014546 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014547
14548 /* Enable seeprom accesses. */
14549 tw32_f(GRC_LOCAL_CTRL,
14550 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14551 udelay(100);
14552
Joe Perches41535772013-02-16 11:20:04 +000014553 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14554 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014555 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014556
Michael Chanec41c7d2006-01-17 02:40:55 -080014557 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014558 netdev_warn(tp->dev,
14559 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014560 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014561 return;
14562 }
Michael Chane6af3012005-04-21 17:12:05 -070014563 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564
Matt Carlson989a9d22007-05-05 11:51:05 -070014565 tp->nvram_size = 0;
14566
Joe Perches41535772013-02-16 11:20:04 +000014567 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014568 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014569 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014570 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014571 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14572 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14573 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014574 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014575 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014576 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014577 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014578 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014579 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014580 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014581 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014582 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14583 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014584 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014585 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14586 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014587 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014588 else
14589 tg3_get_nvram_info(tp);
14590
Matt Carlson989a9d22007-05-05 11:51:05 -070014591 if (tp->nvram_size == 0)
14592 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014593
Michael Chane6af3012005-04-21 17:12:05 -070014594 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014595 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014596
14597 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014598 tg3_flag_clear(tp, NVRAM);
14599 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014600
14601 tg3_get_eeprom_size(tp);
14602 }
14603}
14604
Linus Torvalds1da177e2005-04-16 15:20:36 -070014605struct subsys_tbl_ent {
14606 u16 subsys_vendor, subsys_devid;
14607 u32 phy_id;
14608};
14609
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014610static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014612 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014613 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014614 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014615 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014616 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014617 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014618 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14619 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14620 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014621 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014622 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014623 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014624 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14625 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14626 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014627 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014628 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014629 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014630 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014631 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014632 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014633 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634
14635 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014636 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014637 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014638 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014639 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014640 { TG3PCI_SUBVENDOR_ID_3COM,
14641 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14642 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014643 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014644 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014645 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646
14647 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014648 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014649 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014650 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014651 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014652 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014653 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014654 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014655 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014656
14657 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014658 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014659 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014660 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014661 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014662 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14663 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14664 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014665 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014666 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014667 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668
14669 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014670 { TG3PCI_SUBVENDOR_ID_IBM,
14671 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014672};
14673
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014674static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675{
14676 int i;
14677
14678 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14679 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14680 tp->pdev->subsystem_vendor) &&
14681 (subsys_id_to_phy_id[i].subsys_devid ==
14682 tp->pdev->subsystem_device))
14683 return &subsys_id_to_phy_id[i];
14684 }
14685 return NULL;
14686}
14687
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014688static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014689{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014690 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014691
Matt Carlson79eb6902010-02-17 15:17:03 +000014692 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014693 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14694
Gary Zambranoa85feb82007-05-05 11:52:19 -070014695 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014696 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14697 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014698
Joe Perches41535772013-02-16 11:20:04 +000014699 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014700 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014701 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14702 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014703 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014704 val = tr32(VCPU_CFGSHDW);
14705 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014706 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014707 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014708 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014709 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014710 device_set_wakeup_enable(&tp->pdev->dev, true);
14711 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014712 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014713 }
14714
Linus Torvalds1da177e2005-04-16 15:20:36 -070014715 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14716 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14717 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014718 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014719 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014720
14721 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14722 tp->nic_sram_data_cfg = nic_cfg;
14723
14724 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14725 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014726 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14727 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14728 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014729 (ver > 0) && (ver < 0x100))
14730 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14731
Joe Perches41535772013-02-16 11:20:04 +000014732 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014733 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14734
Linus Torvalds1da177e2005-04-16 15:20:36 -070014735 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14736 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14737 eeprom_phy_serdes = 1;
14738
14739 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14740 if (nic_phy_id != 0) {
14741 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14742 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14743
14744 eeprom_phy_id = (id1 >> 16) << 10;
14745 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14746 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14747 } else
14748 eeprom_phy_id = 0;
14749
Michael Chan7d0c41e2005-04-21 17:06:20 -070014750 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014751 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014752 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014753 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014754 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014755 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014756 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014757
Joe Perches63c3a662011-04-26 08:12:10 +000014758 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014759 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14760 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014761 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014762 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14763
14764 switch (led_cfg) {
14765 default:
14766 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14767 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14768 break;
14769
14770 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14771 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14772 break;
14773
14774 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14775 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014776
14777 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14778 * read on some older 5700/5701 bootcode.
14779 */
Joe Perches41535772013-02-16 11:20:04 +000014780 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14781 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014782 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14783
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784 break;
14785
14786 case SHASTA_EXT_LED_SHARED:
14787 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014788 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14789 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014790 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14791 LED_CTRL_MODE_PHY_2);
14792 break;
14793
14794 case SHASTA_EXT_LED_MAC:
14795 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14796 break;
14797
14798 case SHASTA_EXT_LED_COMBO:
14799 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014800 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14802 LED_CTRL_MODE_PHY_2);
14803 break;
14804
Stephen Hemminger855e1112008-04-16 16:37:28 -070014805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014806
Joe Perches41535772013-02-16 11:20:04 +000014807 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14808 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014809 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14810 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14811
Joe Perches41535772013-02-16 11:20:04 +000014812 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014813 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014814
Michael Chan9d26e212006-12-07 00:21:14 -080014815 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014816 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014817 if ((tp->pdev->subsystem_vendor ==
14818 PCI_VENDOR_ID_ARIMA) &&
14819 (tp->pdev->subsystem_device == 0x205a ||
14820 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014821 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014822 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014823 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14824 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014826
14827 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014828 tg3_flag_set(tp, ENABLE_ASF);
14829 if (tg3_flag(tp, 5750_PLUS))
14830 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014831 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014832
14833 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014834 tg3_flag(tp, 5750_PLUS))
14835 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014836
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014837 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014838 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014839 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014840
Joe Perches63c3a662011-04-26 08:12:10 +000014841 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014842 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014843 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014844 device_set_wakeup_enable(&tp->pdev->dev, true);
14845 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014846
Linus Torvalds1da177e2005-04-16 15:20:36 -070014847 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014848 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014849
14850 /* serdes signal pre-emphasis in register 0x590 set by */
14851 /* bootcode if bit 18 is set */
14852 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014853 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014854
Joe Perches63c3a662011-04-26 08:12:10 +000014855 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014856 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14857 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014858 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014859 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014860
Nithin Sujir942d1af2013-04-09 08:48:07 +000014861 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014862 u32 cfg3;
14863
14864 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014865 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
14866 !tg3_flag(tp, 57765_PLUS) &&
14867 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000014868 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014869 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
14870 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
14871 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
14872 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014873 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014874
Matt Carlson14417062010-02-17 15:16:59 +000014875 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014876 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014877 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014878 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014879 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014880 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014881 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014882done:
Joe Perches63c3a662011-04-26 08:12:10 +000014883 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014884 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014885 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014886 else
14887 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014888}
14889
Michael Chanc86a8562013-01-06 12:51:08 +000014890static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14891{
14892 int i, err;
14893 u32 val2, off = offset * 8;
14894
14895 err = tg3_nvram_lock(tp);
14896 if (err)
14897 return err;
14898
14899 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14900 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14901 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14902 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14903 udelay(10);
14904
14905 for (i = 0; i < 100; i++) {
14906 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14907 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14908 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14909 break;
14910 }
14911 udelay(10);
14912 }
14913
14914 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14915
14916 tg3_nvram_unlock(tp);
14917 if (val2 & APE_OTP_STATUS_CMD_DONE)
14918 return 0;
14919
14920 return -EBUSY;
14921}
14922
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014923static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014924{
14925 int i;
14926 u32 val;
14927
14928 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14929 tw32(OTP_CTRL, cmd);
14930
14931 /* Wait for up to 1 ms for command to execute. */
14932 for (i = 0; i < 100; i++) {
14933 val = tr32(OTP_STATUS);
14934 if (val & OTP_STATUS_CMD_DONE)
14935 break;
14936 udelay(10);
14937 }
14938
14939 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14940}
14941
14942/* Read the gphy configuration from the OTP region of the chip. The gphy
14943 * configuration is a 32-bit value that straddles the alignment boundary.
14944 * We do two 32-bit reads and then shift and merge the results.
14945 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014946static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014947{
14948 u32 bhalf_otp, thalf_otp;
14949
14950 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14951
14952 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14953 return 0;
14954
14955 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14956
14957 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14958 return 0;
14959
14960 thalf_otp = tr32(OTP_READ_DATA);
14961
14962 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14963
14964 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14965 return 0;
14966
14967 bhalf_otp = tr32(OTP_READ_DATA);
14968
14969 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14970}
14971
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014972static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014973{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014974 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014975
14976 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14977 adv |= ADVERTISED_1000baseT_Half |
14978 ADVERTISED_1000baseT_Full;
14979
14980 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14981 adv |= ADVERTISED_100baseT_Half |
14982 ADVERTISED_100baseT_Full |
14983 ADVERTISED_10baseT_Half |
14984 ADVERTISED_10baseT_Full |
14985 ADVERTISED_TP;
14986 else
14987 adv |= ADVERTISED_FIBRE;
14988
14989 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014990 tp->link_config.speed = SPEED_UNKNOWN;
14991 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014992 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014993 tp->link_config.active_speed = SPEED_UNKNOWN;
14994 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014995
14996 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014997}
14998
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014999static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015000{
15001 u32 hw_phy_id_1, hw_phy_id_2;
15002 u32 hw_phy_id, hw_phy_id_masked;
15003 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015004
Matt Carlsone256f8a2011-03-09 16:58:24 +000015005 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015006 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015007 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15008
Michael Chan8151ad52012-07-29 19:15:41 +000015009 if (tg3_flag(tp, ENABLE_APE)) {
15010 switch (tp->pci_fn) {
15011 case 0:
15012 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15013 break;
15014 case 1:
15015 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15016 break;
15017 case 2:
15018 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15019 break;
15020 case 3:
15021 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15022 break;
15023 }
15024 }
15025
Nithin Sujir942d1af2013-04-09 08:48:07 +000015026 if (!tg3_flag(tp, ENABLE_ASF) &&
15027 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15028 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15029 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15030 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15031
Joe Perches63c3a662011-04-26 08:12:10 +000015032 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015033 return tg3_phy_init(tp);
15034
Linus Torvalds1da177e2005-04-16 15:20:36 -070015035 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015036 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015037 */
15038 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015039 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015040 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015041 } else {
15042 /* Now read the physical PHY_ID from the chip and verify
15043 * that it is sane. If it doesn't look good, we fall back
15044 * to either the hard-coded table based PHY_ID and failing
15045 * that the value found in the eeprom area.
15046 */
15047 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15048 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15049
15050 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15051 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15052 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15053
Matt Carlson79eb6902010-02-17 15:17:03 +000015054 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015055 }
15056
Matt Carlson79eb6902010-02-17 15:17:03 +000015057 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015059 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015060 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015061 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015062 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015063 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015064 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015065 /* Do nothing, phy ID already set up in
15066 * tg3_get_eeprom_hw_cfg().
15067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015068 } else {
15069 struct subsys_tbl_ent *p;
15070
15071 /* No eeprom signature? Try the hardcoded
15072 * subsys device table.
15073 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015074 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015075 if (p) {
15076 tp->phy_id = p->phy_id;
15077 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15078 /* For now we saw the IDs 0xbc050cd0,
15079 * 0xbc050f80 and 0xbc050c30 on devices
15080 * connected to an BCM4785 and there are
15081 * probably more. Just assume that the phy is
15082 * supported when it is connected to a SSB core
15083 * for now.
15084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015087
Linus Torvalds1da177e2005-04-16 15:20:36 -070015088 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015089 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015090 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015091 }
15092 }
15093
Matt Carlsona6b68da2010-12-06 08:28:52 +000015094 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015095 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15096 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015097 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015098 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15099 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15100 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15101 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015102 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015103 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15104
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015105 tp->eee.supported = SUPPORTED_100baseT_Full |
15106 SUPPORTED_1000baseT_Full;
15107 tp->eee.advertised = ADVERTISED_100baseT_Full |
15108 ADVERTISED_1000baseT_Full;
15109 tp->eee.eee_enabled = 1;
15110 tp->eee.tx_lpi_enabled = 1;
15111 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15112 }
15113
Matt Carlsone256f8a2011-03-09 16:58:24 +000015114 tg3_phy_init_link_config(tp);
15115
Nithin Sujir942d1af2013-04-09 08:48:07 +000015116 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15117 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015118 !tg3_flag(tp, ENABLE_APE) &&
15119 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015120 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015121
15122 tg3_readphy(tp, MII_BMSR, &bmsr);
15123 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15124 (bmsr & BMSR_LSTATUS))
15125 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015126
Linus Torvalds1da177e2005-04-16 15:20:36 -070015127 err = tg3_phy_reset(tp);
15128 if (err)
15129 return err;
15130
Matt Carlson42b64a42011-05-19 12:12:49 +000015131 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015132
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015133 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015134 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15135 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015136
15137 tg3_writephy(tp, MII_BMCR,
15138 BMCR_ANENABLE | BMCR_ANRESTART);
15139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015140 }
15141
15142skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015143 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015144 err = tg3_init_5401phy_dsp(tp);
15145 if (err)
15146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015147
Linus Torvalds1da177e2005-04-16 15:20:36 -070015148 err = tg3_init_5401phy_dsp(tp);
15149 }
15150
Linus Torvalds1da177e2005-04-16 15:20:36 -070015151 return err;
15152}
15153
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015154static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015155{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015156 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015157 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015158 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015159 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015160
Matt Carlson535a4902011-07-20 10:20:56 +000015161 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015162 if (!vpd_data)
15163 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015164
Matt Carlson535a4902011-07-20 10:20:56 +000015165 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015166 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015167 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015168
15169 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15170 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15171 i += PCI_VPD_LRDT_TAG_SIZE;
15172
Matt Carlson535a4902011-07-20 10:20:56 +000015173 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015174 goto out_not_found;
15175
Matt Carlson184b8902010-04-05 10:19:25 +000015176 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15177 PCI_VPD_RO_KEYWORD_MFR_ID);
15178 if (j > 0) {
15179 len = pci_vpd_info_field_size(&vpd_data[j]);
15180
15181 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15182 if (j + len > block_end || len != 4 ||
15183 memcmp(&vpd_data[j], "1028", 4))
15184 goto partno;
15185
15186 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15187 PCI_VPD_RO_KEYWORD_VENDOR0);
15188 if (j < 0)
15189 goto partno;
15190
15191 len = pci_vpd_info_field_size(&vpd_data[j]);
15192
15193 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15194 if (j + len > block_end)
15195 goto partno;
15196
Kees Cook715230a2013-03-27 06:40:50 +000015197 if (len >= sizeof(tp->fw_ver))
15198 len = sizeof(tp->fw_ver) - 1;
15199 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15200 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15201 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015202 }
15203
15204partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015205 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15206 PCI_VPD_RO_KEYWORD_PARTNO);
15207 if (i < 0)
15208 goto out_not_found;
15209
15210 len = pci_vpd_info_field_size(&vpd_data[i]);
15211
15212 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15213 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015214 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015215 goto out_not_found;
15216
15217 memcpy(tp->board_part_number, &vpd_data[i], len);
15218
Linus Torvalds1da177e2005-04-16 15:20:36 -070015219out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015220 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015221 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015222 return;
15223
15224out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015225 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015226 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15227 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015228 strcpy(tp->board_part_number, "BCM5717");
15229 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15230 strcpy(tp->board_part_number, "BCM5718");
15231 else
15232 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015233 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015234 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15235 strcpy(tp->board_part_number, "BCM57780");
15236 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15237 strcpy(tp->board_part_number, "BCM57760");
15238 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15239 strcpy(tp->board_part_number, "BCM57790");
15240 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15241 strcpy(tp->board_part_number, "BCM57788");
15242 else
15243 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015244 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015245 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15246 strcpy(tp->board_part_number, "BCM57761");
15247 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15248 strcpy(tp->board_part_number, "BCM57765");
15249 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15250 strcpy(tp->board_part_number, "BCM57781");
15251 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15252 strcpy(tp->board_part_number, "BCM57785");
15253 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15254 strcpy(tp->board_part_number, "BCM57791");
15255 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15256 strcpy(tp->board_part_number, "BCM57795");
15257 else
15258 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015259 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015260 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15261 strcpy(tp->board_part_number, "BCM57762");
15262 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15263 strcpy(tp->board_part_number, "BCM57766");
15264 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15265 strcpy(tp->board_part_number, "BCM57782");
15266 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15267 strcpy(tp->board_part_number, "BCM57786");
15268 else
15269 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015270 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015271 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015272 } else {
15273nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015274 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015276}
15277
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015278static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015279{
15280 u32 val;
15281
Matt Carlsone4f34112009-02-25 14:25:00 +000015282 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015283 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015284 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015285 val != 0)
15286 return 0;
15287
15288 return 1;
15289}
15290
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015291static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015292{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015293 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015294 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015295 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015296
15297 if (tg3_nvram_read(tp, 0xc, &offset) ||
15298 tg3_nvram_read(tp, 0x4, &start))
15299 return;
15300
15301 offset = tg3_nvram_logical_addr(tp, offset);
15302
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015303 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015304 return;
15305
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015306 if ((val & 0xfc000000) == 0x0c000000) {
15307 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015308 return;
15309
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015310 if (val == 0)
15311 newver = true;
15312 }
15313
Matt Carlson75f99362010-04-05 10:19:24 +000015314 dst_off = strlen(tp->fw_ver);
15315
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015316 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015317 if (TG3_VER_SIZE - dst_off < 16 ||
15318 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015319 return;
15320
15321 offset = offset + ver_offset - start;
15322 for (i = 0; i < 16; i += 4) {
15323 __be32 v;
15324 if (tg3_nvram_read_be32(tp, offset + i, &v))
15325 return;
15326
Matt Carlson75f99362010-04-05 10:19:24 +000015327 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015328 }
15329 } else {
15330 u32 major, minor;
15331
15332 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15333 return;
15334
15335 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15336 TG3_NVM_BCVER_MAJSFT;
15337 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015338 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15339 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015340 }
15341}
15342
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015343static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015344{
15345 u32 val, major, minor;
15346
15347 /* Use native endian representation */
15348 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15349 return;
15350
15351 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15352 TG3_NVM_HWSB_CFG1_MAJSFT;
15353 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15354 TG3_NVM_HWSB_CFG1_MINSFT;
15355
15356 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15357}
15358
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015359static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015360{
15361 u32 offset, major, minor, build;
15362
Matt Carlson75f99362010-04-05 10:19:24 +000015363 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015364
15365 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15366 return;
15367
15368 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15369 case TG3_EEPROM_SB_REVISION_0:
15370 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15371 break;
15372 case TG3_EEPROM_SB_REVISION_2:
15373 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15374 break;
15375 case TG3_EEPROM_SB_REVISION_3:
15376 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15377 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015378 case TG3_EEPROM_SB_REVISION_4:
15379 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15380 break;
15381 case TG3_EEPROM_SB_REVISION_5:
15382 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15383 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015384 case TG3_EEPROM_SB_REVISION_6:
15385 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15386 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015387 default:
15388 return;
15389 }
15390
Matt Carlsone4f34112009-02-25 14:25:00 +000015391 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015392 return;
15393
15394 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15395 TG3_EEPROM_SB_EDH_BLD_SHFT;
15396 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15397 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15398 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15399
15400 if (minor > 99 || build > 26)
15401 return;
15402
Matt Carlson75f99362010-04-05 10:19:24 +000015403 offset = strlen(tp->fw_ver);
15404 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15405 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015406
15407 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015408 offset = strlen(tp->fw_ver);
15409 if (offset < TG3_VER_SIZE - 1)
15410 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015411 }
15412}
15413
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015414static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015415{
15416 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015417 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015418
15419 for (offset = TG3_NVM_DIR_START;
15420 offset < TG3_NVM_DIR_END;
15421 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015422 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015423 return;
15424
15425 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15426 break;
15427 }
15428
15429 if (offset == TG3_NVM_DIR_END)
15430 return;
15431
Joe Perches63c3a662011-04-26 08:12:10 +000015432 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015433 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015434 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015435 return;
15436
Matt Carlsone4f34112009-02-25 14:25:00 +000015437 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015438 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015439 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015440 return;
15441
15442 offset += val - start;
15443
Matt Carlsonacd9c112009-02-25 14:26:33 +000015444 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015445
Matt Carlsonacd9c112009-02-25 14:26:33 +000015446 tp->fw_ver[vlen++] = ',';
15447 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015448
15449 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015450 __be32 v;
15451 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015452 return;
15453
Al Virob9fc7dc2007-12-17 22:59:57 -080015454 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015455
Matt Carlsonacd9c112009-02-25 14:26:33 +000015456 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15457 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015458 break;
15459 }
15460
Matt Carlsonacd9c112009-02-25 14:26:33 +000015461 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15462 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015463 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015464}
15465
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015466static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015467{
Matt Carlson7fd76442009-02-25 14:27:20 +000015468 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015469
15470 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15471 if (apedata != APE_SEG_SIG_MAGIC)
15472 return;
15473
15474 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15475 if (!(apedata & APE_FW_STATUS_READY))
15476 return;
15477
Michael Chan165f4d12012-07-16 16:23:59 +000015478 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15479 tg3_flag_set(tp, APE_HAS_NCSI);
15480}
15481
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015482static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015483{
15484 int vlen;
15485 u32 apedata;
15486 char *fwtype;
15487
Matt Carlson7fd76442009-02-25 14:27:20 +000015488 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15489
Michael Chan165f4d12012-07-16 16:23:59 +000015490 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015491 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015492 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15493 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015494 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015495 fwtype = "DASH";
15496
Matt Carlson7fd76442009-02-25 14:27:20 +000015497 vlen = strlen(tp->fw_ver);
15498
Matt Carlsonecc79642010-08-02 11:26:01 +000015499 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15500 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015501 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15502 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15503 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15504 (apedata & APE_FW_VERSION_BLDMSK));
15505}
15506
Michael Chanc86a8562013-01-06 12:51:08 +000015507static void tg3_read_otp_ver(struct tg3 *tp)
15508{
15509 u32 val, val2;
15510
Joe Perches41535772013-02-16 11:20:04 +000015511 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015512 return;
15513
15514 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15515 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15516 TG3_OTP_MAGIC0_VALID(val)) {
15517 u64 val64 = (u64) val << 32 | val2;
15518 u32 ver = 0;
15519 int i, vlen;
15520
15521 for (i = 0; i < 7; i++) {
15522 if ((val64 & 0xff) == 0)
15523 break;
15524 ver = val64 & 0xff;
15525 val64 >>= 8;
15526 }
15527 vlen = strlen(tp->fw_ver);
15528 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15529 }
15530}
15531
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015532static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015533{
15534 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015535 bool vpd_vers = false;
15536
15537 if (tp->fw_ver[0] != 0)
15538 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015539
Joe Perches63c3a662011-04-26 08:12:10 +000015540 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015541 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015542 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015543 return;
15544 }
15545
Matt Carlsonacd9c112009-02-25 14:26:33 +000015546 if (tg3_nvram_read(tp, 0, &val))
15547 return;
15548
15549 if (val == TG3_EEPROM_MAGIC)
15550 tg3_read_bc_ver(tp);
15551 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15552 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015553 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15554 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015555
Michael Chan165f4d12012-07-16 16:23:59 +000015556 if (tg3_flag(tp, ENABLE_ASF)) {
15557 if (tg3_flag(tp, ENABLE_APE)) {
15558 tg3_probe_ncsi(tp);
15559 if (!vpd_vers)
15560 tg3_read_dash_ver(tp);
15561 } else if (!vpd_vers) {
15562 tg3_read_mgmtfw_ver(tp);
15563 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015564 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015565
15566 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015567}
15568
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015569static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15570{
Joe Perches63c3a662011-04-26 08:12:10 +000015571 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015572 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015573 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015574 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015575 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015576 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015577}
15578
Matt Carlson41434702011-03-09 16:58:22 +000015579static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015580 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15581 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15582 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15583 { },
15584};
15585
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015586static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015587{
15588 struct pci_dev *peer;
15589 unsigned int func, devnr = tp->pdev->devfn & ~7;
15590
15591 for (func = 0; func < 8; func++) {
15592 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15593 if (peer && peer != tp->pdev)
15594 break;
15595 pci_dev_put(peer);
15596 }
15597 /* 5704 can be configured in single-port mode, set peer to
15598 * tp->pdev in that case.
15599 */
15600 if (!peer) {
15601 peer = tp->pdev;
15602 return peer;
15603 }
15604
15605 /*
15606 * We don't need to keep the refcount elevated; there's no way
15607 * to remove one half of this device without removing the other
15608 */
15609 pci_dev_put(peer);
15610
15611 return peer;
15612}
15613
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015614static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015615{
15616 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015617 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015618 u32 reg;
15619
15620 /* All devices that use the alternate
15621 * ASIC REV location have a CPMU.
15622 */
15623 tg3_flag_set(tp, CPMU_PRESENT);
15624
15625 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015626 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015627 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15628 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015629 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15630 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15631 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15632 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015633 reg = TG3PCI_GEN2_PRODID_ASICREV;
15634 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15635 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15636 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15637 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15638 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15639 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15640 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15641 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15642 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15643 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15644 reg = TG3PCI_GEN15_PRODID_ASICREV;
15645 else
15646 reg = TG3PCI_PRODID_ASICREV;
15647
15648 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15649 }
15650
15651 /* Wrong chip ID in 5752 A0. This code can be removed later
15652 * as A0 is not in production.
15653 */
Joe Perches41535772013-02-16 11:20:04 +000015654 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015655 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15656
Joe Perches41535772013-02-16 11:20:04 +000015657 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015658 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15659
Joe Perches41535772013-02-16 11:20:04 +000015660 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15661 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15662 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015663 tg3_flag_set(tp, 5717_PLUS);
15664
Joe Perches41535772013-02-16 11:20:04 +000015665 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15666 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015667 tg3_flag_set(tp, 57765_CLASS);
15668
Michael Chanc65a17f2013-01-06 12:51:07 +000015669 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015670 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015671 tg3_flag_set(tp, 57765_PLUS);
15672
15673 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015674 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15675 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15676 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15677 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15678 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15679 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015680 tg3_flag(tp, 57765_PLUS))
15681 tg3_flag_set(tp, 5755_PLUS);
15682
Joe Perches41535772013-02-16 11:20:04 +000015683 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15684 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015685 tg3_flag_set(tp, 5780_CLASS);
15686
Joe Perches41535772013-02-16 11:20:04 +000015687 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15688 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15689 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015690 tg3_flag(tp, 5755_PLUS) ||
15691 tg3_flag(tp, 5780_CLASS))
15692 tg3_flag_set(tp, 5750_PLUS);
15693
Joe Perches41535772013-02-16 11:20:04 +000015694 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015695 tg3_flag(tp, 5750_PLUS))
15696 tg3_flag_set(tp, 5705_PLUS);
15697}
15698
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015699static bool tg3_10_100_only_device(struct tg3 *tp,
15700 const struct pci_device_id *ent)
15701{
15702 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15703
Joe Perches41535772013-02-16 11:20:04 +000015704 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15705 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015706 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15707 return true;
15708
15709 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015710 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015711 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15712 return true;
15713 } else {
15714 return true;
15715 }
15716 }
15717
15718 return false;
15719}
15720
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015721static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015722{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015723 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015724 u32 pci_state_reg, grc_misc_cfg;
15725 u32 val;
15726 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015727 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015728
Linus Torvalds1da177e2005-04-16 15:20:36 -070015729 /* Force memory write invalidate off. If we leave it on,
15730 * then on 5700_BX chips we have to enable a workaround.
15731 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15732 * to match the cacheline size. The Broadcom driver have this
15733 * workaround but turns MWI off all the times so never uses
15734 * it. This seems to suggest that the workaround is insufficient.
15735 */
15736 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15737 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15738 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15739
Matt Carlson16821282011-07-13 09:27:28 +000015740 /* Important! -- Make sure register accesses are byteswapped
15741 * correctly. Also, for those chips that require it, make
15742 * sure that indirect register accesses are enabled before
15743 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015744 */
15745 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15746 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015747 tp->misc_host_ctrl |= (misc_ctrl_reg &
15748 MISC_HOST_CTRL_CHIPREV);
15749 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15750 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015751
Matt Carlson42b123b2012-02-13 15:20:13 +000015752 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015753
Michael Chan68929142005-08-09 20:17:14 -070015754 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15755 * we need to disable memory and use config. cycles
15756 * only to access all registers. The 5702/03 chips
15757 * can mistakenly decode the special cycles from the
15758 * ICH chipsets as memory write cycles, causing corruption
15759 * of register and memory space. Only certain ICH bridges
15760 * will drive special cycles with non-zero data during the
15761 * address phase which can fall within the 5703's address
15762 * range. This is not an ICH bug as the PCI spec allows
15763 * non-zero address during special cycles. However, only
15764 * these ICH bridges are known to drive non-zero addresses
15765 * during special cycles.
15766 *
15767 * Since special cycles do not cross PCI bridges, we only
15768 * enable this workaround if the 5703 is on the secondary
15769 * bus of these ICH bridges.
15770 */
Joe Perches41535772013-02-16 11:20:04 +000015771 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15772 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015773 static struct tg3_dev_id {
15774 u32 vendor;
15775 u32 device;
15776 u32 rev;
15777 } ich_chipsets[] = {
15778 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15779 PCI_ANY_ID },
15780 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15781 PCI_ANY_ID },
15782 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15783 0xa },
15784 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15785 PCI_ANY_ID },
15786 { },
15787 };
15788 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15789 struct pci_dev *bridge = NULL;
15790
15791 while (pci_id->vendor != 0) {
15792 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15793 bridge);
15794 if (!bridge) {
15795 pci_id++;
15796 continue;
15797 }
15798 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015799 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015800 continue;
15801 }
15802 if (bridge->subordinate &&
15803 (bridge->subordinate->number ==
15804 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015805 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015806 pci_dev_put(bridge);
15807 break;
15808 }
15809 }
15810 }
15811
Joe Perches41535772013-02-16 11:20:04 +000015812 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015813 static struct tg3_dev_id {
15814 u32 vendor;
15815 u32 device;
15816 } bridge_chipsets[] = {
15817 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15818 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15819 { },
15820 };
15821 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15822 struct pci_dev *bridge = NULL;
15823
15824 while (pci_id->vendor != 0) {
15825 bridge = pci_get_device(pci_id->vendor,
15826 pci_id->device,
15827 bridge);
15828 if (!bridge) {
15829 pci_id++;
15830 continue;
15831 }
15832 if (bridge->subordinate &&
15833 (bridge->subordinate->number <=
15834 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015835 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015836 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015837 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015838 pci_dev_put(bridge);
15839 break;
15840 }
15841 }
15842 }
15843
Michael Chan4a29cc22006-03-19 13:21:12 -080015844 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15845 * DMA addresses > 40-bit. This bridge may have other additional
15846 * 57xx devices behind it in some 4-port NIC designs for example.
15847 * Any tg3 device found behind the bridge will also need the 40-bit
15848 * DMA workaround.
15849 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015850 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015851 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015852 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015853 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015854 struct pci_dev *bridge = NULL;
15855
15856 do {
15857 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15858 PCI_DEVICE_ID_SERVERWORKS_EPB,
15859 bridge);
15860 if (bridge && bridge->subordinate &&
15861 (bridge->subordinate->number <=
15862 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015863 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015864 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015865 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015866 pci_dev_put(bridge);
15867 break;
15868 }
15869 } while (bridge);
15870 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015871
Joe Perches41535772013-02-16 11:20:04 +000015872 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15873 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015874 tp->pdev_peer = tg3_find_peer(tp);
15875
Matt Carlson507399f2009-11-13 13:03:37 +000015876 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015877 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015878 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015879 else if (tg3_flag(tp, 57765_PLUS))
15880 tg3_flag_set(tp, HW_TSO_3);
15881 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015882 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015883 tg3_flag_set(tp, HW_TSO_2);
15884 else if (tg3_flag(tp, 5750_PLUS)) {
15885 tg3_flag_set(tp, HW_TSO_1);
15886 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015887 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15888 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015889 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015890 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15891 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15892 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000015893 tg3_flag_set(tp, FW_TSO);
15894 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015895 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015896 tp->fw_needed = FIRMWARE_TG3TSO5;
15897 else
15898 tp->fw_needed = FIRMWARE_TG3TSO;
15899 }
15900
Matt Carlsondabc5c62011-05-19 12:12:52 +000015901 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015902 if (tg3_flag(tp, HW_TSO_1) ||
15903 tg3_flag(tp, HW_TSO_2) ||
15904 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000015905 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015906 /* For firmware TSO, assume ASF is disabled.
15907 * We'll disable TSO later if we discover ASF
15908 * is enabled in tg3_get_eeprom_hw_cfg().
15909 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015910 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015911 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015912 tg3_flag_clear(tp, TSO_CAPABLE);
15913 tg3_flag_clear(tp, TSO_BUG);
15914 tp->fw_needed = NULL;
15915 }
15916
Joe Perches41535772013-02-16 11:20:04 +000015917 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015918 tp->fw_needed = FIRMWARE_TG3;
15919
Nithin Sujirc4dab502013-03-06 17:02:34 +000015920 if (tg3_asic_rev(tp) == ASIC_REV_57766)
15921 tp->fw_needed = FIRMWARE_TG357766;
15922
Matt Carlson507399f2009-11-13 13:03:37 +000015923 tp->irq_max = 1;
15924
Joe Perches63c3a662011-04-26 08:12:10 +000015925 if (tg3_flag(tp, 5750_PLUS)) {
15926 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015927 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15928 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15929 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15930 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015931 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015932 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015933
Joe Perches63c3a662011-04-26 08:12:10 +000015934 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015935 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015936 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015938
Joe Perches63c3a662011-04-26 08:12:10 +000015939 if (tg3_flag(tp, 57765_PLUS)) {
15940 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015941 tp->irq_max = TG3_IRQ_MAX_VECS;
15942 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015943 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015944
Michael Chan91024262012-09-28 07:12:38 +000015945 tp->txq_max = 1;
15946 tp->rxq_max = 1;
15947 if (tp->irq_max > 1) {
15948 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15949 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15950
Joe Perches41535772013-02-16 11:20:04 +000015951 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15952 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000015953 tp->txq_max = tp->irq_max - 1;
15954 }
15955
Matt Carlsonb7abee62012-06-07 12:56:54 +000015956 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015957 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015958 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015959
Joe Perches41535772013-02-16 11:20:04 +000015960 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015961 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015962
Joe Perches41535772013-02-16 11:20:04 +000015963 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15964 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15965 tg3_asic_rev(tp) == ASIC_REV_5720 ||
15966 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015967 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015968
Joe Perches63c3a662011-04-26 08:12:10 +000015969 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000015970 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015971 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015972
Joe Perches63c3a662011-04-26 08:12:10 +000015973 if (!tg3_flag(tp, 5705_PLUS) ||
15974 tg3_flag(tp, 5780_CLASS) ||
15975 tg3_flag(tp, USE_JUMBO_BDFLAG))
15976 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015977
Matt Carlson52f44902008-11-21 17:17:04 -080015978 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15979 &pci_state_reg);
15980
Jon Mason708ebb3a2011-06-27 12:56:50 +000015981 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015982 u16 lnkctl;
15983
Joe Perches63c3a662011-04-26 08:12:10 +000015984 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015985
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015986 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015987 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000015988 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015989 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015990 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015991 }
Joe Perches41535772013-02-16 11:20:04 +000015992 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
15993 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15994 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
15995 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015996 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015997 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015998 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015999 }
Joe Perches41535772013-02-16 11:20:04 +000016000 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016001 /* BCM5785 devices are effectively PCIe devices, and should
16002 * follow PCIe codepaths, but do not have a PCIe capabilities
16003 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016004 */
Joe Perches63c3a662011-04-26 08:12:10 +000016005 tg3_flag_set(tp, PCI_EXPRESS);
16006 } else if (!tg3_flag(tp, 5705_PLUS) ||
16007 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016008 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16009 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016010 dev_err(&tp->pdev->dev,
16011 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016012 return -EIO;
16013 }
16014
16015 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016016 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016018
Michael Chan399de502005-10-03 14:02:39 -070016019 /* If we have an AMD 762 or VIA K8T800 chipset, write
16020 * reordering to the mailbox registers done by the host
16021 * controller can cause major troubles. We read back from
16022 * every mailbox register write to force the writes to be
16023 * posted to the chip in order.
16024 */
Matt Carlson41434702011-03-09 16:58:22 +000016025 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016026 !tg3_flag(tp, PCI_EXPRESS))
16027 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016028
Matt Carlson69fc4052008-12-21 20:19:57 -080016029 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16030 &tp->pci_cacheline_sz);
16031 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16032 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016033 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016034 tp->pci_lat_timer < 64) {
16035 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016036 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16037 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016038 }
16039
Matt Carlson16821282011-07-13 09:27:28 +000016040 /* Important! -- It is critical that the PCI-X hw workaround
16041 * situation is decided before the first MMIO register access.
16042 */
Joe Perches41535772013-02-16 11:20:04 +000016043 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016044 /* 5700 BX chips need to have their TX producer index
16045 * mailboxes written twice to workaround a bug.
16046 */
Joe Perches63c3a662011-04-26 08:12:10 +000016047 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016048
Matt Carlson52f44902008-11-21 17:17:04 -080016049 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016050 *
16051 * The workaround is to use indirect register accesses
16052 * for all chip writes not to mailbox registers.
16053 */
Joe Perches63c3a662011-04-26 08:12:10 +000016054 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016055 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016056
Joe Perches63c3a662011-04-26 08:12:10 +000016057 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016058
16059 /* The chip can have it's power management PCI config
16060 * space registers clobbered due to this bug.
16061 * So explicitly force the chip into D0 here.
16062 */
Matt Carlson9974a352007-10-07 23:27:28 -070016063 pci_read_config_dword(tp->pdev,
16064 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016065 &pm_reg);
16066 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16067 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016068 pci_write_config_dword(tp->pdev,
16069 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016070 pm_reg);
16071
16072 /* Also, force SERR#/PERR# in PCI command. */
16073 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16074 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16075 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16076 }
16077 }
16078
Linus Torvalds1da177e2005-04-16 15:20:36 -070016079 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016080 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016081 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016082 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016083
16084 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016085 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016086 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16087 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16088 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16089 }
16090
Michael Chan1ee582d2005-08-09 20:16:46 -070016091 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016092 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016093 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016094 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016095 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016096 tp->write32_tx_mbox = tg3_write32;
16097 tp->write32_rx_mbox = tg3_write32;
16098
16099 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016100 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016101 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016102 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016103 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016104 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016105 /*
16106 * Back to back register writes can cause problems on these
16107 * chips, the workaround is to read back all reg writes
16108 * except those to mailbox regs.
16109 *
16110 * See tg3_write_indirect_reg32().
16111 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016112 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016113 }
16114
Joe Perches63c3a662011-04-26 08:12:10 +000016115 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016116 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016117 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016118 tp->write32_rx_mbox = tg3_write_flush_reg32;
16119 }
Michael Chan20094932005-08-09 20:16:32 -070016120
Joe Perches63c3a662011-04-26 08:12:10 +000016121 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016122 tp->read32 = tg3_read_indirect_reg32;
16123 tp->write32 = tg3_write_indirect_reg32;
16124 tp->read32_mbox = tg3_read_indirect_mbox;
16125 tp->write32_mbox = tg3_write_indirect_mbox;
16126 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16127 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16128
16129 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016130 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016131
16132 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16133 pci_cmd &= ~PCI_COMMAND_MEMORY;
16134 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16135 }
Joe Perches41535772013-02-16 11:20:04 +000016136 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016137 tp->read32_mbox = tg3_read32_mbox_5906;
16138 tp->write32_mbox = tg3_write32_mbox_5906;
16139 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16140 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16141 }
Michael Chan68929142005-08-09 20:17:14 -070016142
Michael Chanbbadf502006-04-06 21:46:34 -070016143 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016144 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016145 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16146 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016147 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016148
Matt Carlson16821282011-07-13 09:27:28 +000016149 /* The memory arbiter has to be enabled in order for SRAM accesses
16150 * to succeed. Normally on powerup the tg3 chip firmware will make
16151 * sure it is enabled, but other entities such as system netboot
16152 * code might disable it.
16153 */
16154 val = tr32(MEMARB_MODE);
16155 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16156
Matt Carlson9dc5e342011-11-04 09:15:02 +000016157 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016158 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016159 tg3_flag(tp, 5780_CLASS)) {
16160 if (tg3_flag(tp, PCIX_MODE)) {
16161 pci_read_config_dword(tp->pdev,
16162 tp->pcix_cap + PCI_X_STATUS,
16163 &val);
16164 tp->pci_fn = val & 0x7;
16165 }
Joe Perches41535772013-02-16 11:20:04 +000016166 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16167 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16168 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016169 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016170 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16171 val = tr32(TG3_CPMU_STATUS);
16172
Joe Perches41535772013-02-16 11:20:04 +000016173 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016174 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16175 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016176 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16177 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016178 }
16179
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016180 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16181 tp->write32_tx_mbox = tg3_write_flush_reg32;
16182 tp->write32_rx_mbox = tg3_write_flush_reg32;
16183 }
16184
Michael Chan7d0c41e2005-04-21 17:06:20 -070016185 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016186 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016187 * determined before calling tg3_set_power_state() so that
16188 * we know whether or not to switch out of Vaux power.
16189 * When the flag is set, it means that GPIO1 is used for eeprom
16190 * write protect and also implies that it is a LOM where GPIOs
16191 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016192 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016193 tg3_get_eeprom_hw_cfg(tp);
16194
Matt Carlson1caf13e2013-03-06 17:02:29 +000016195 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016196 tg3_flag_clear(tp, TSO_CAPABLE);
16197 tg3_flag_clear(tp, TSO_BUG);
16198 tp->fw_needed = NULL;
16199 }
16200
Joe Perches63c3a662011-04-26 08:12:10 +000016201 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016202 /* Allow reads and writes to the
16203 * APE register and memory space.
16204 */
16205 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016206 PCISTATE_ALLOW_APE_SHMEM_WR |
16207 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016208 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16209 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016210
16211 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016212 }
16213
Matt Carlson16821282011-07-13 09:27:28 +000016214 /* Set up tp->grc_local_ctrl before calling
16215 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16216 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016217 * It is also used as eeprom write protect on LOMs.
16218 */
16219 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016220 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016221 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016222 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16223 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016224 /* Unused GPIO3 must be driven as output on 5752 because there
16225 * are no pull-up resistors on unused GPIO pins.
16226 */
Joe Perches41535772013-02-16 11:20:04 +000016227 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016228 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016229
Joe Perches41535772013-02-16 11:20:04 +000016230 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16231 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016232 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016233 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16234
Matt Carlson8d519ab2009-04-20 06:58:01 +000016235 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16236 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016237 /* Turn off the debug UART. */
16238 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016239 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016240 /* Keep VMain power. */
16241 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16242 GRC_LCLCTRL_GPIO_OUTPUT0;
16243 }
16244
Joe Perches41535772013-02-16 11:20:04 +000016245 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016246 tp->grc_local_ctrl |=
16247 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16248
Matt Carlson16821282011-07-13 09:27:28 +000016249 /* Switch out of Vaux if it is a NIC */
16250 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016251
Linus Torvalds1da177e2005-04-16 15:20:36 -070016252 /* Derive initial jumbo mode from MTU assigned in
16253 * ether_setup() via the alloc_etherdev() call
16254 */
Joe Perches63c3a662011-04-26 08:12:10 +000016255 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16256 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016257
16258 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016259 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16260 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16261 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16262 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016263 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016264 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016265 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016266 }
16267
Joe Perches41535772013-02-16 11:20:04 +000016268 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016269 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016270
Linus Torvalds1da177e2005-04-16 15:20:36 -070016271 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016272 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16273 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16274 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16275 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016276 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16277 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16278 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016279
Joe Perches41535772013-02-16 11:20:04 +000016280 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16281 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016282 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016283 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016284 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016285
Joe Perches63c3a662011-04-26 08:12:10 +000016286 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016287 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016288 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16289 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016290 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016291 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16292 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16293 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16294 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016295 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16296 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016297 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016298 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016299 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016300 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016301 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016303
Joe Perches41535772013-02-16 11:20:04 +000016304 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16305 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016306 tp->phy_otp = tg3_read_otp_phycfg(tp);
16307 if (tp->phy_otp == 0)
16308 tp->phy_otp = TG3_OTP_DEFAULT;
16309 }
16310
Joe Perches63c3a662011-04-26 08:12:10 +000016311 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016312 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16313 else
16314 tp->mi_mode = MAC_MI_MODE_BASE;
16315
Linus Torvalds1da177e2005-04-16 15:20:36 -070016316 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016317 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16318 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016319 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16320
Matt Carlson4d958472011-04-20 07:57:35 +000016321 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016322 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16323 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16324 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016325 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16326 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16327 }
16328
Joe Perches41535772013-02-16 11:20:04 +000016329 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16330 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016331 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016332
Matt Carlson158d7ab2008-05-29 01:37:54 -070016333 err = tg3_mdio_init(tp);
16334 if (err)
16335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016336
16337 /* Initialize data/descriptor byte/word swapping. */
16338 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016339 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16340 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016341 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16342 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16343 GRC_MODE_B2HRX_ENABLE |
16344 GRC_MODE_HTX2B_ENABLE |
16345 GRC_MODE_HOST_STACKUP);
16346 else
16347 val &= GRC_MODE_HOST_STACKUP;
16348
Linus Torvalds1da177e2005-04-16 15:20:36 -070016349 tw32(GRC_MODE, val | tp->grc_mode);
16350
16351 tg3_switch_clocks(tp);
16352
16353 /* Clear this out for sanity. */
16354 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16355
16356 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16357 &pci_state_reg);
16358 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016359 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016360 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16361 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16362 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16363 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016364 void __iomem *sram_base;
16365
16366 /* Write some dummy words into the SRAM status block
16367 * area, see if it reads back correctly. If the return
16368 * value is bad, force enable the PCIX workaround.
16369 */
16370 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16371
16372 writel(0x00000000, sram_base);
16373 writel(0x00000000, sram_base + 4);
16374 writel(0xffffffff, sram_base + 4);
16375 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016376 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016377 }
16378 }
16379
16380 udelay(50);
16381 tg3_nvram_init(tp);
16382
Nithin Sujirc4dab502013-03-06 17:02:34 +000016383 /* If the device has an NVRAM, no need to load patch firmware */
16384 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16385 !tg3_flag(tp, NO_NVRAM))
16386 tp->fw_needed = NULL;
16387
Linus Torvalds1da177e2005-04-16 15:20:36 -070016388 grc_misc_cfg = tr32(GRC_MISC_CFG);
16389 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16390
Joe Perches41535772013-02-16 11:20:04 +000016391 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016392 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16393 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016394 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016395
Joe Perches63c3a662011-04-26 08:12:10 +000016396 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016397 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016398 tg3_flag_set(tp, TAGGED_STATUS);
16399 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016400 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16401 HOSTCC_MODE_CLRTICK_TXBD);
16402
16403 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16404 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16405 tp->misc_host_ctrl);
16406 }
16407
Matt Carlson3bda1252008-08-15 14:08:22 -070016408 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016409 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016410 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016411 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016412 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016413
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016414 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016415 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016416
16417 err = tg3_phy_probe(tp);
16418 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016419 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016420 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016421 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016422 }
16423
Matt Carlson184b8902010-04-05 10:19:25 +000016424 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016425 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016426
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016427 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16428 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016429 } else {
Joe Perches41535772013-02-16 11:20:04 +000016430 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016431 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016432 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016433 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016434 }
16435
16436 /* 5700 {AX,BX} chips have a broken status block link
16437 * change bit implementation, so we must use the
16438 * status register in those cases.
16439 */
Joe Perches41535772013-02-16 11:20:04 +000016440 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016441 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016442 else
Joe Perches63c3a662011-04-26 08:12:10 +000016443 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016444
16445 /* The led_ctrl is set during tg3_phy_probe, here we might
16446 * have to force the link status polling mechanism based
16447 * upon subsystem IDs.
16448 */
16449 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016450 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016451 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16452 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016453 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016454 }
16455
16456 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016457 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016458 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016459 else
Joe Perches63c3a662011-04-26 08:12:10 +000016460 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016461
Eric Dumazet9205fd92011-11-18 06:47:01 +000016462 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016463 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016464 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016465 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016466 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016467#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016468 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016469#endif
16470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016471
Matt Carlson2c49a442010-09-30 10:34:35 +000016472 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16473 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016474 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16475
Matt Carlson2c49a442010-09-30 10:34:35 +000016476 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016477
16478 /* Increment the rx prod index on the rx std ring by at most
16479 * 8 for these chips to workaround hw errata.
16480 */
Joe Perches41535772013-02-16 11:20:04 +000016481 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16482 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16483 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016484 tp->rx_std_max_post = 8;
16485
Joe Perches63c3a662011-04-26 08:12:10 +000016486 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016487 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16488 PCIE_PWR_MGMT_L1_THRESH_MSK;
16489
Linus Torvalds1da177e2005-04-16 15:20:36 -070016490 return err;
16491}
16492
David S. Miller49b6e95f2007-03-29 01:38:42 -070016493#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016494static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016495{
16496 struct net_device *dev = tp->dev;
16497 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016498 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016499 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016500 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016501
David S. Miller49b6e95f2007-03-29 01:38:42 -070016502 addr = of_get_property(dp, "local-mac-address", &len);
16503 if (addr && len == 6) {
16504 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016506 }
16507 return -ENODEV;
16508}
16509
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016510static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016511{
16512 struct net_device *dev = tp->dev;
16513
16514 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
16515 return 0;
16516}
16517#endif
16518
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016519static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016520{
16521 struct net_device *dev = tp->dev;
16522 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016523 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016525
David S. Miller49b6e95f2007-03-29 01:38:42 -070016526#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016527 if (!tg3_get_macaddr_sparc(tp))
16528 return 0;
16529#endif
16530
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016531 if (tg3_flag(tp, IS_SSB_CORE)) {
16532 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16533 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16534 return 0;
16535 }
16536
Linus Torvalds1da177e2005-04-16 15:20:36 -070016537 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016538 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016539 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016540 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16541 mac_offset = 0xcc;
16542 if (tg3_nvram_lock(tp))
16543 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16544 else
16545 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016546 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016547 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016548 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016549 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016550 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016551 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016552 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016553
16554 /* First try to get it from MAC address mailbox. */
16555 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16556 if ((hi >> 16) == 0x484b) {
16557 dev->dev_addr[0] = (hi >> 8) & 0xff;
16558 dev->dev_addr[1] = (hi >> 0) & 0xff;
16559
16560 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16561 dev->dev_addr[2] = (lo >> 24) & 0xff;
16562 dev->dev_addr[3] = (lo >> 16) & 0xff;
16563 dev->dev_addr[4] = (lo >> 8) & 0xff;
16564 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016565
Michael Chan008652b2006-03-27 23:14:53 -080016566 /* Some old bootcode may report a 0 MAC address in SRAM */
16567 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16568 }
16569 if (!addr_ok) {
16570 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016571 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016572 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016573 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016574 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16575 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016576 }
16577 /* Finally just fetch it out of the MAC control regs. */
16578 else {
16579 hi = tr32(MAC_ADDR_0_HIGH);
16580 lo = tr32(MAC_ADDR_0_LOW);
16581
16582 dev->dev_addr[5] = lo & 0xff;
16583 dev->dev_addr[4] = (lo >> 8) & 0xff;
16584 dev->dev_addr[3] = (lo >> 16) & 0xff;
16585 dev->dev_addr[2] = (lo >> 24) & 0xff;
16586 dev->dev_addr[1] = hi & 0xff;
16587 dev->dev_addr[0] = (hi >> 8) & 0xff;
16588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016589 }
16590
16591 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016592#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016593 if (!tg3_get_default_macaddr_sparc(tp))
16594 return 0;
16595#endif
16596 return -EINVAL;
16597 }
16598 return 0;
16599}
16600
David S. Miller59e6b432005-05-18 22:50:10 -070016601#define BOUNDARY_SINGLE_CACHELINE 1
16602#define BOUNDARY_MULTI_CACHELINE 2
16603
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016604static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016605{
16606 int cacheline_size;
16607 u8 byte;
16608 int goal;
16609
16610 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16611 if (byte == 0)
16612 cacheline_size = 1024;
16613 else
16614 cacheline_size = (int) byte * 4;
16615
16616 /* On 5703 and later chips, the boundary bits have no
16617 * effect.
16618 */
Joe Perches41535772013-02-16 11:20:04 +000016619 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16620 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016621 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016622 goto out;
16623
16624#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16625 goal = BOUNDARY_MULTI_CACHELINE;
16626#else
16627#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16628 goal = BOUNDARY_SINGLE_CACHELINE;
16629#else
16630 goal = 0;
16631#endif
16632#endif
16633
Joe Perches63c3a662011-04-26 08:12:10 +000016634 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016635 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16636 goto out;
16637 }
16638
David S. Miller59e6b432005-05-18 22:50:10 -070016639 if (!goal)
16640 goto out;
16641
16642 /* PCI controllers on most RISC systems tend to disconnect
16643 * when a device tries to burst across a cache-line boundary.
16644 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16645 *
16646 * Unfortunately, for PCI-E there are only limited
16647 * write-side controls for this, and thus for reads
16648 * we will still get the disconnects. We'll also waste
16649 * these PCI cycles for both read and write for chips
16650 * other than 5700 and 5701 which do not implement the
16651 * boundary bits.
16652 */
Joe Perches63c3a662011-04-26 08:12:10 +000016653 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016654 switch (cacheline_size) {
16655 case 16:
16656 case 32:
16657 case 64:
16658 case 128:
16659 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16660 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16661 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16662 } else {
16663 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16664 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16665 }
16666 break;
16667
16668 case 256:
16669 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16670 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16671 break;
16672
16673 default:
16674 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16675 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16676 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016677 }
Joe Perches63c3a662011-04-26 08:12:10 +000016678 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016679 switch (cacheline_size) {
16680 case 16:
16681 case 32:
16682 case 64:
16683 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16684 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16685 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16686 break;
16687 }
16688 /* fallthrough */
16689 case 128:
16690 default:
16691 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16692 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16693 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016694 }
David S. Miller59e6b432005-05-18 22:50:10 -070016695 } else {
16696 switch (cacheline_size) {
16697 case 16:
16698 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16699 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16700 DMA_RWCTRL_WRITE_BNDRY_16);
16701 break;
16702 }
16703 /* fallthrough */
16704 case 32:
16705 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16706 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16707 DMA_RWCTRL_WRITE_BNDRY_32);
16708 break;
16709 }
16710 /* fallthrough */
16711 case 64:
16712 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16713 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16714 DMA_RWCTRL_WRITE_BNDRY_64);
16715 break;
16716 }
16717 /* fallthrough */
16718 case 128:
16719 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16720 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16721 DMA_RWCTRL_WRITE_BNDRY_128);
16722 break;
16723 }
16724 /* fallthrough */
16725 case 256:
16726 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16727 DMA_RWCTRL_WRITE_BNDRY_256);
16728 break;
16729 case 512:
16730 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16731 DMA_RWCTRL_WRITE_BNDRY_512);
16732 break;
16733 case 1024:
16734 default:
16735 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16736 DMA_RWCTRL_WRITE_BNDRY_1024);
16737 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016738 }
David S. Miller59e6b432005-05-18 22:50:10 -070016739 }
16740
16741out:
16742 return val;
16743}
16744
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016745static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000016746 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016747{
16748 struct tg3_internal_buffer_desc test_desc;
16749 u32 sram_dma_descs;
16750 int i, ret;
16751
16752 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16753
16754 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16755 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16756 tw32(RDMAC_STATUS, 0);
16757 tw32(WDMAC_STATUS, 0);
16758
16759 tw32(BUFMGR_MODE, 0);
16760 tw32(FTQ_RESET, 0);
16761
16762 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16763 test_desc.addr_lo = buf_dma & 0xffffffff;
16764 test_desc.nic_mbuf = 0x00002100;
16765 test_desc.len = size;
16766
16767 /*
16768 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16769 * the *second* time the tg3 driver was getting loaded after an
16770 * initial scan.
16771 *
16772 * Broadcom tells me:
16773 * ...the DMA engine is connected to the GRC block and a DMA
16774 * reset may affect the GRC block in some unpredictable way...
16775 * The behavior of resets to individual blocks has not been tested.
16776 *
16777 * Broadcom noted the GRC reset will also reset all sub-components.
16778 */
16779 if (to_device) {
16780 test_desc.cqid_sqid = (13 << 8) | 2;
16781
16782 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16783 udelay(40);
16784 } else {
16785 test_desc.cqid_sqid = (16 << 8) | 7;
16786
16787 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16788 udelay(40);
16789 }
16790 test_desc.flags = 0x00000005;
16791
16792 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16793 u32 val;
16794
16795 val = *(((u32 *)&test_desc) + i);
16796 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16797 sram_dma_descs + (i * sizeof(u32)));
16798 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16799 }
16800 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16801
Matt Carlson859a588792010-04-05 10:19:28 +000016802 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016803 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016804 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016805 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016806
16807 ret = -ENODEV;
16808 for (i = 0; i < 40; i++) {
16809 u32 val;
16810
16811 if (to_device)
16812 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16813 else
16814 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16815 if ((val & 0xffff) == sram_dma_descs) {
16816 ret = 0;
16817 break;
16818 }
16819
16820 udelay(100);
16821 }
16822
16823 return ret;
16824}
16825
David S. Millerded73402005-05-23 13:59:47 -070016826#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016827
Matt Carlson41434702011-03-09 16:58:22 +000016828static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016829 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16830 { },
16831};
16832
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016833static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016834{
16835 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016836 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016837 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016838
Matt Carlson4bae65c2010-11-24 08:31:52 +000016839 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16840 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016841 if (!buf) {
16842 ret = -ENOMEM;
16843 goto out_nofree;
16844 }
16845
16846 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16847 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16848
David S. Miller59e6b432005-05-18 22:50:10 -070016849 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016850
Joe Perches63c3a662011-04-26 08:12:10 +000016851 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016852 goto out;
16853
Joe Perches63c3a662011-04-26 08:12:10 +000016854 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016855 /* DMA read watermark not used on PCIE */
16856 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016857 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016858 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16859 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016860 tp->dma_rwctrl |= 0x003f0000;
16861 else
16862 tp->dma_rwctrl |= 0x003f000f;
16863 } else {
Joe Perches41535772013-02-16 11:20:04 +000016864 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16865 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016866 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016867 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016868
Michael Chan4a29cc22006-03-19 13:21:12 -080016869 /* If the 5704 is behind the EPB bridge, we can
16870 * do the less restrictive ONE_DMA workaround for
16871 * better performance.
16872 */
Joe Perches63c3a662011-04-26 08:12:10 +000016873 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016874 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016875 tp->dma_rwctrl |= 0x8000;
16876 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016877 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16878
Joe Perches41535772013-02-16 11:20:04 +000016879 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016880 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016881 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016882 tp->dma_rwctrl |=
16883 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16884 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16885 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016886 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016887 /* 5780 always in PCIX mode */
16888 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016889 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016890 /* 5714 always in PCIX mode */
16891 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016892 } else {
16893 tp->dma_rwctrl |= 0x001b000f;
16894 }
16895 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016896 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16897 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016898
Joe Perches41535772013-02-16 11:20:04 +000016899 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16900 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016901 tp->dma_rwctrl &= 0xfffffff0;
16902
Joe Perches41535772013-02-16 11:20:04 +000016903 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16904 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016905 /* Remove this if it causes problems for some boards. */
16906 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16907
16908 /* On 5700/5701 chips, we need to set this bit.
16909 * Otherwise the chip will issue cacheline transactions
16910 * to streamable DMA memory with not all the byte
16911 * enables turned on. This is an error on several
16912 * RISC PCI controllers, in particular sparc64.
16913 *
16914 * On 5703/5704 chips, this bit has been reassigned
16915 * a different meaning. In particular, it is used
16916 * on those chips to enable a PCI-X workaround.
16917 */
16918 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16919 }
16920
16921 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16922
16923#if 0
16924 /* Unneeded, already done by tg3_get_invariants. */
16925 tg3_switch_clocks(tp);
16926#endif
16927
Joe Perches41535772013-02-16 11:20:04 +000016928 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16929 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016930 goto out;
16931
David S. Miller59e6b432005-05-18 22:50:10 -070016932 /* It is best to perform DMA test with maximum write burst size
16933 * to expose the 5700/5701 write DMA bug.
16934 */
16935 saved_dma_rwctrl = tp->dma_rwctrl;
16936 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16937 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16938
Linus Torvalds1da177e2005-04-16 15:20:36 -070016939 while (1) {
16940 u32 *p = buf, i;
16941
16942 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16943 p[i] = i;
16944
16945 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000016946 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016947 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016948 dev_err(&tp->pdev->dev,
16949 "%s: Buffer write failed. err = %d\n",
16950 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016951 break;
16952 }
16953
16954#if 0
16955 /* validate data reached card RAM correctly. */
16956 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16957 u32 val;
16958 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16959 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016960 dev_err(&tp->pdev->dev,
16961 "%s: Buffer corrupted on device! "
16962 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016963 /* ret = -ENODEV here? */
16964 }
16965 p[i] = 0;
16966 }
16967#endif
16968 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000016969 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016970 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016971 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16972 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016973 break;
16974 }
16975
16976 /* Verify it. */
16977 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16978 if (p[i] == i)
16979 continue;
16980
David S. Miller59e6b432005-05-18 22:50:10 -070016981 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16982 DMA_RWCTRL_WRITE_BNDRY_16) {
16983 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016984 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16985 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16986 break;
16987 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016988 dev_err(&tp->pdev->dev,
16989 "%s: Buffer corrupted on read back! "
16990 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016991 ret = -ENODEV;
16992 goto out;
16993 }
16994 }
16995
16996 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16997 /* Success. */
16998 ret = 0;
16999 break;
17000 }
17001 }
David S. Miller59e6b432005-05-18 22:50:10 -070017002 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17003 DMA_RWCTRL_WRITE_BNDRY_16) {
17004 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017005 * now look for chipsets that are known to expose the
17006 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017007 */
Matt Carlson41434702011-03-09 16:58:22 +000017008 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017009 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17010 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017011 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017012 /* Safe to use the calculated DMA boundary. */
17013 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017014 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017015
David S. Miller59e6b432005-05-18 22:50:10 -070017016 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017018
17019out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017020 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017021out_nofree:
17022 return ret;
17023}
17024
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017025static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017026{
Joe Perches63c3a662011-04-26 08:12:10 +000017027 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017028 tp->bufmgr_config.mbuf_read_dma_low_water =
17029 DEFAULT_MB_RDMA_LOW_WATER_5705;
17030 tp->bufmgr_config.mbuf_mac_rx_low_water =
17031 DEFAULT_MB_MACRX_LOW_WATER_57765;
17032 tp->bufmgr_config.mbuf_high_water =
17033 DEFAULT_MB_HIGH_WATER_57765;
17034
17035 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17036 DEFAULT_MB_RDMA_LOW_WATER_5705;
17037 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17038 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17039 tp->bufmgr_config.mbuf_high_water_jumbo =
17040 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017041 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017042 tp->bufmgr_config.mbuf_read_dma_low_water =
17043 DEFAULT_MB_RDMA_LOW_WATER_5705;
17044 tp->bufmgr_config.mbuf_mac_rx_low_water =
17045 DEFAULT_MB_MACRX_LOW_WATER_5705;
17046 tp->bufmgr_config.mbuf_high_water =
17047 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017048 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017049 tp->bufmgr_config.mbuf_mac_rx_low_water =
17050 DEFAULT_MB_MACRX_LOW_WATER_5906;
17051 tp->bufmgr_config.mbuf_high_water =
17052 DEFAULT_MB_HIGH_WATER_5906;
17053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017054
Michael Chanfdfec1722005-07-25 12:31:48 -070017055 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17056 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17057 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17058 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17059 tp->bufmgr_config.mbuf_high_water_jumbo =
17060 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17061 } else {
17062 tp->bufmgr_config.mbuf_read_dma_low_water =
17063 DEFAULT_MB_RDMA_LOW_WATER;
17064 tp->bufmgr_config.mbuf_mac_rx_low_water =
17065 DEFAULT_MB_MACRX_LOW_WATER;
17066 tp->bufmgr_config.mbuf_high_water =
17067 DEFAULT_MB_HIGH_WATER;
17068
17069 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17070 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17071 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17072 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17073 tp->bufmgr_config.mbuf_high_water_jumbo =
17074 DEFAULT_MB_HIGH_WATER_JUMBO;
17075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017076
17077 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17078 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17079}
17080
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017081static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017082{
Matt Carlson79eb6902010-02-17 15:17:03 +000017083 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17084 case TG3_PHY_ID_BCM5400: return "5400";
17085 case TG3_PHY_ID_BCM5401: return "5401";
17086 case TG3_PHY_ID_BCM5411: return "5411";
17087 case TG3_PHY_ID_BCM5701: return "5701";
17088 case TG3_PHY_ID_BCM5703: return "5703";
17089 case TG3_PHY_ID_BCM5704: return "5704";
17090 case TG3_PHY_ID_BCM5705: return "5705";
17091 case TG3_PHY_ID_BCM5750: return "5750";
17092 case TG3_PHY_ID_BCM5752: return "5752";
17093 case TG3_PHY_ID_BCM5714: return "5714";
17094 case TG3_PHY_ID_BCM5780: return "5780";
17095 case TG3_PHY_ID_BCM5755: return "5755";
17096 case TG3_PHY_ID_BCM5787: return "5787";
17097 case TG3_PHY_ID_BCM5784: return "5784";
17098 case TG3_PHY_ID_BCM5756: return "5722/5756";
17099 case TG3_PHY_ID_BCM5906: return "5906";
17100 case TG3_PHY_ID_BCM5761: return "5761";
17101 case TG3_PHY_ID_BCM5718C: return "5718C";
17102 case TG3_PHY_ID_BCM5718S: return "5718S";
17103 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017104 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017105 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017106 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017107 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017108 case 0: return "serdes";
17109 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017111}
17112
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017113static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017114{
Joe Perches63c3a662011-04-26 08:12:10 +000017115 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017116 strcpy(str, "PCI Express");
17117 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017118 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017119 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17120
17121 strcpy(str, "PCIX:");
17122
17123 if ((clock_ctrl == 7) ||
17124 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17125 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17126 strcat(str, "133MHz");
17127 else if (clock_ctrl == 0)
17128 strcat(str, "33MHz");
17129 else if (clock_ctrl == 2)
17130 strcat(str, "50MHz");
17131 else if (clock_ctrl == 4)
17132 strcat(str, "66MHz");
17133 else if (clock_ctrl == 6)
17134 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017135 } else {
17136 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017137 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017138 strcat(str, "66MHz");
17139 else
17140 strcat(str, "33MHz");
17141 }
Joe Perches63c3a662011-04-26 08:12:10 +000017142 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017143 strcat(str, ":32-bit");
17144 else
17145 strcat(str, ":64-bit");
17146 return str;
17147}
17148
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017149static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017150{
17151 struct ethtool_coalesce *ec = &tp->coal;
17152
17153 memset(ec, 0, sizeof(*ec));
17154 ec->cmd = ETHTOOL_GCOALESCE;
17155 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17156 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17157 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17158 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17159 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17160 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17161 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17162 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17163 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17164
17165 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17166 HOSTCC_MODE_CLRTICK_TXBD)) {
17167 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17168 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17169 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17170 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17171 }
Michael Chand244c892005-07-05 14:42:33 -070017172
Joe Perches63c3a662011-04-26 08:12:10 +000017173 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017174 ec->rx_coalesce_usecs_irq = 0;
17175 ec->tx_coalesce_usecs_irq = 0;
17176 ec->stats_block_coalesce_usecs = 0;
17177 }
David S. Miller15f98502005-05-18 22:49:26 -070017178}
17179
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017180static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017181 const struct pci_device_id *ent)
17182{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017183 struct net_device *dev;
17184 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017185 int i, err, pm_cap;
17186 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017187 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017188 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017189 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017190
Joe Perches05dbe002010-02-17 19:44:19 +000017191 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017192
17193 err = pci_enable_device(pdev);
17194 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017195 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017196 return err;
17197 }
17198
Linus Torvalds1da177e2005-04-16 15:20:36 -070017199 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17200 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017201 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017202 goto err_out_disable_pdev;
17203 }
17204
17205 pci_set_master(pdev);
17206
17207 /* Find power-management capability. */
17208 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
17209 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000017210 dev_err(&pdev->dev,
17211 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017212 err = -EIO;
17213 goto err_out_free_res;
17214 }
17215
Matt Carlson16821282011-07-13 09:27:28 +000017216 err = pci_set_power_state(pdev, PCI_D0);
17217 if (err) {
17218 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
17219 goto err_out_free_res;
17220 }
17221
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017222 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017223 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017224 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000017225 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017226 }
17227
Linus Torvalds1da177e2005-04-16 15:20:36 -070017228 SET_NETDEV_DEV(dev, &pdev->dev);
17229
Linus Torvalds1da177e2005-04-16 15:20:36 -070017230 tp = netdev_priv(dev);
17231 tp->pdev = pdev;
17232 tp->dev = dev;
17233 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017234 tp->rx_mode = TG3_DEF_RX_MODE;
17235 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017236 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017237
Linus Torvalds1da177e2005-04-16 15:20:36 -070017238 if (tg3_debug > 0)
17239 tp->msg_enable = tg3_debug;
17240 else
17241 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17242
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017243 if (pdev_is_ssb_gige_core(pdev)) {
17244 tg3_flag_set(tp, IS_SSB_CORE);
17245 if (ssb_gige_must_flush_posted_writes(pdev))
17246 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17247 if (ssb_gige_one_dma_at_once(pdev))
17248 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
17249 if (ssb_gige_have_roboswitch(pdev))
17250 tg3_flag_set(tp, ROBOSWITCH);
17251 if (ssb_gige_is_rgmii(pdev))
17252 tg3_flag_set(tp, RGMII_MODE);
17253 }
17254
Linus Torvalds1da177e2005-04-16 15:20:36 -070017255 /* The word/byte swap controls here control register access byte
17256 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17257 * setting below.
17258 */
17259 tp->misc_host_ctrl =
17260 MISC_HOST_CTRL_MASK_PCI_INT |
17261 MISC_HOST_CTRL_WORD_SWAP |
17262 MISC_HOST_CTRL_INDIR_ACCESS |
17263 MISC_HOST_CTRL_PCISTATE_RW;
17264
17265 /* The NONFRM (non-frame) byte/word swap controls take effect
17266 * on descriptor entries, anything which isn't packet data.
17267 *
17268 * The StrongARM chips on the board (one for tx, one for rx)
17269 * are running in big-endian mode.
17270 */
17271 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17272 GRC_MODE_WSWAP_NONFRM_DATA);
17273#ifdef __BIG_ENDIAN
17274 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17275#endif
17276 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017277 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017278 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017279
Matt Carlsond5fe4882008-11-21 17:20:32 -080017280 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017281 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017282 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017283 err = -ENOMEM;
17284 goto err_out_free_dev;
17285 }
17286
Matt Carlsonc9cab242011-07-13 09:27:27 +000017287 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17288 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17289 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17290 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17291 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017292 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017293 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17294 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017295 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
17296 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17297 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
17298 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017299 tg3_flag_set(tp, ENABLE_APE);
17300 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17301 if (!tp->aperegs) {
17302 dev_err(&pdev->dev,
17303 "Cannot map APE registers, aborting\n");
17304 err = -ENOMEM;
17305 goto err_out_iounmap;
17306 }
17307 }
17308
Linus Torvalds1da177e2005-04-16 15:20:36 -070017309 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17310 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017311
Linus Torvalds1da177e2005-04-16 15:20:36 -070017312 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017313 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017314 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017315 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017316
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017317 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017318 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017319 dev_err(&pdev->dev,
17320 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017321 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017322 }
17323
Michael Chan4a29cc22006-03-19 13:21:12 -080017324 /* The EPB bridge inside 5714, 5715, and 5780 and any
17325 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017326 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17327 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17328 * do DMA address check in tg3_start_xmit().
17329 */
Joe Perches63c3a662011-04-26 08:12:10 +000017330 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017331 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017332 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017333 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017334#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017335 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017336#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017337 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017338 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017339
17340 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017341 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017342 err = pci_set_dma_mask(pdev, dma_mask);
17343 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017344 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017345 err = pci_set_consistent_dma_mask(pdev,
17346 persist_dma_mask);
17347 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017348 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17349 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017350 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017351 }
17352 }
17353 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017354 if (err || dma_mask == DMA_BIT_MASK(32)) {
17355 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017356 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017357 dev_err(&pdev->dev,
17358 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017359 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017360 }
17361 }
17362
Michael Chanfdfec1722005-07-25 12:31:48 -070017363 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017364
Patrick McHardyf6469682013-04-19 02:04:27 +000017365 features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlson0da06062011-05-19 12:12:53 +000017366
17367 /* 5700 B0 chips do not support checksumming correctly due
17368 * to hardware bugs.
17369 */
Joe Perches41535772013-02-16 11:20:04 +000017370 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017371 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17372
17373 if (tg3_flag(tp, 5755_PLUS))
17374 features |= NETIF_F_IPV6_CSUM;
17375 }
17376
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017377 /* TSO is on by default on chips that support hardware TSO.
17378 * Firmware TSO on older chips gives lower performance, so it
17379 * is off by default, but can be enabled using ethtool.
17380 */
Joe Perches63c3a662011-04-26 08:12:10 +000017381 if ((tg3_flag(tp, HW_TSO_1) ||
17382 tg3_flag(tp, HW_TSO_2) ||
17383 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017384 (features & NETIF_F_IP_CSUM))
17385 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017386 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017387 if (features & NETIF_F_IPV6_CSUM)
17388 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017389 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017390 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17391 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17392 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17393 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17394 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017395 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017397
Matt Carlsond542fe22011-05-19 16:02:43 +000017398 dev->features |= features;
17399 dev->vlan_features |= features;
17400
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017401 /*
17402 * Add loopback capability only for a subset of devices that support
17403 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17404 * loopback for the remaining devices.
17405 */
Joe Perches41535772013-02-16 11:20:04 +000017406 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017407 !tg3_flag(tp, CPMU_PRESENT))
17408 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017409 features |= NETIF_F_LOOPBACK;
17410
Matt Carlson0da06062011-05-19 12:12:53 +000017411 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017412
Joe Perches41535772013-02-16 11:20:04 +000017413 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017414 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017415 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017416 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017417 tp->rx_pending = 63;
17418 }
17419
Linus Torvalds1da177e2005-04-16 15:20:36 -070017420 err = tg3_get_device_address(tp);
17421 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017422 dev_err(&pdev->dev,
17423 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017424 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017425 }
17426
Matt Carlsonc88864d2007-11-12 21:07:01 -080017427 /*
17428 * Reset chip in case UNDI or EFI driver did not shutdown
17429 * DMA self test will enable WDMAC and we'll see (spurious)
17430 * pending DMA on the PCI bus at that point.
17431 */
17432 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17433 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17434 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17435 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17436 }
17437
17438 err = tg3_test_dma(tp);
17439 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017440 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017441 goto err_out_apeunmap;
17442 }
17443
Matt Carlson78f90dc2009-11-13 13:03:42 +000017444 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17445 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17446 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017447 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017448 struct tg3_napi *tnapi = &tp->napi[i];
17449
17450 tnapi->tp = tp;
17451 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17452
17453 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017454 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017455 intmbx += 0x8;
17456 else
17457 intmbx += 0x4;
17458
17459 tnapi->consmbox = rcvmbx;
17460 tnapi->prodmbox = sndmbx;
17461
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017462 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017463 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017464 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017465 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017466
Joe Perches63c3a662011-04-26 08:12:10 +000017467 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017468 break;
17469
17470 /*
17471 * If we support MSIX, we'll be using RSS. If we're using
17472 * RSS, the first vector only handles link interrupts and the
17473 * remaining vectors handle rx and tx interrupts. Reuse the
17474 * mailbox values for the next iteration. The values we setup
17475 * above are still useful for the single vectored mode.
17476 */
17477 if (!i)
17478 continue;
17479
17480 rcvmbx += 0x8;
17481
17482 if (sndmbx & 0x4)
17483 sndmbx -= 0x4;
17484 else
17485 sndmbx += 0xc;
17486 }
17487
Matt Carlsonc88864d2007-11-12 21:07:01 -080017488 tg3_init_coal(tp);
17489
Michael Chanc49a1562006-12-17 17:07:29 -080017490 pci_set_drvdata(pdev, dev);
17491
Joe Perches41535772013-02-16 11:20:04 +000017492 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17493 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17494 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017495 tg3_flag_set(tp, PTP_CAPABLE);
17496
Matt Carlsoncd0d7222011-07-13 09:27:33 +000017497 if (tg3_flag(tp, 5717_PLUS)) {
17498 /* Resume a low-power mode */
17499 tg3_frob_aux_power(tp, false);
17500 }
17501
Matt Carlson21f76382012-02-22 12:35:21 +000017502 tg3_timer_init(tp);
17503
Michael Chan402e1392013-02-14 12:13:41 +000017504 tg3_carrier_off(tp);
17505
Linus Torvalds1da177e2005-04-16 15:20:36 -070017506 err = register_netdev(dev);
17507 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017508 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017509 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017510 }
17511
Joe Perches05dbe002010-02-17 19:44:19 +000017512 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17513 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017514 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017515 tg3_bus_string(tp, str),
17516 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017517
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017518 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017519 struct phy_device *phydev;
17520 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017521 netdev_info(dev,
17522 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017523 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017524 } else {
17525 char *ethtype;
17526
17527 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17528 ethtype = "10/100Base-TX";
17529 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17530 ethtype = "1000Base-SX";
17531 else
17532 ethtype = "10/100/1000Base-T";
17533
Matt Carlson5129c3a2010-04-05 10:19:23 +000017534 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017535 "(WireSpeed[%d], EEE[%d])\n",
17536 tg3_phy_string(tp), ethtype,
17537 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17538 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017539 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017540
Joe Perches05dbe002010-02-17 19:44:19 +000017541 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017542 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017543 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017544 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017545 tg3_flag(tp, ENABLE_ASF) != 0,
17546 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017547 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17548 tp->dma_rwctrl,
17549 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17550 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017551
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017552 pci_save_state(pdev);
17553
Linus Torvalds1da177e2005-04-16 15:20:36 -070017554 return 0;
17555
Matt Carlson0d3031d2007-10-10 18:02:43 -070017556err_out_apeunmap:
17557 if (tp->aperegs) {
17558 iounmap(tp->aperegs);
17559 tp->aperegs = NULL;
17560 }
17561
Linus Torvalds1da177e2005-04-16 15:20:36 -070017562err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017563 if (tp->regs) {
17564 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017565 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017567
17568err_out_free_dev:
17569 free_netdev(dev);
17570
Matt Carlson16821282011-07-13 09:27:28 +000017571err_out_power_down:
17572 pci_set_power_state(pdev, PCI_D3hot);
17573
Linus Torvalds1da177e2005-04-16 15:20:36 -070017574err_out_free_res:
17575 pci_release_regions(pdev);
17576
17577err_out_disable_pdev:
17578 pci_disable_device(pdev);
17579 pci_set_drvdata(pdev, NULL);
17580 return err;
17581}
17582
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017583static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017584{
17585 struct net_device *dev = pci_get_drvdata(pdev);
17586
17587 if (dev) {
17588 struct tg3 *tp = netdev_priv(dev);
17589
Jesper Juhle3c55302012-04-09 22:50:15 +020017590 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017591
Matt Carlsondb219972011-11-04 09:15:03 +000017592 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017593
David S. Miller1805b2f2011-10-24 18:18:09 -040017594 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017595 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017596 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017597 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017598
Linus Torvalds1da177e2005-04-16 15:20:36 -070017599 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017600 if (tp->aperegs) {
17601 iounmap(tp->aperegs);
17602 tp->aperegs = NULL;
17603 }
Michael Chan68929142005-08-09 20:17:14 -070017604 if (tp->regs) {
17605 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017606 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017608 free_netdev(dev);
17609 pci_release_regions(pdev);
17610 pci_disable_device(pdev);
17611 pci_set_drvdata(pdev, NULL);
17612 }
17613}
17614
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017615#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017616static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017617{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017618 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017619 struct net_device *dev = pci_get_drvdata(pdev);
17620 struct tg3 *tp = netdev_priv(dev);
17621 int err;
17622
17623 if (!netif_running(dev))
17624 return 0;
17625
Matt Carlsondb219972011-11-04 09:15:03 +000017626 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017627 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017628 tg3_netif_stop(tp);
17629
Matt Carlson21f76382012-02-22 12:35:21 +000017630 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017631
David S. Millerf47c11e2005-06-24 20:18:35 -070017632 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017633 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017634 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017635
17636 netif_device_detach(dev);
17637
David S. Millerf47c11e2005-06-24 20:18:35 -070017638 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017639 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017640 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017641 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017642
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017643 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017644 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017645 int err2;
17646
David S. Millerf47c11e2005-06-24 20:18:35 -070017647 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017648
Joe Perches63c3a662011-04-26 08:12:10 +000017649 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017650 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017651 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017652 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017653
Matt Carlson21f76382012-02-22 12:35:21 +000017654 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017655
17656 netif_device_attach(dev);
17657 tg3_netif_start(tp);
17658
Michael Chanb9ec6c12006-07-25 16:37:27 -070017659out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017660 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017661
17662 if (!err2)
17663 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017664 }
17665
17666 return err;
17667}
17668
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017669static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017670{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017671 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017672 struct net_device *dev = pci_get_drvdata(pdev);
17673 struct tg3 *tp = netdev_priv(dev);
17674 int err;
17675
17676 if (!netif_running(dev))
17677 return 0;
17678
Linus Torvalds1da177e2005-04-16 15:20:36 -070017679 netif_device_attach(dev);
17680
David S. Millerf47c11e2005-06-24 20:18:35 -070017681 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017682
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017683 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
17684
Joe Perches63c3a662011-04-26 08:12:10 +000017685 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017686 err = tg3_restart_hw(tp,
17687 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017688 if (err)
17689 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017690
Matt Carlson21f76382012-02-22 12:35:21 +000017691 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017692
Linus Torvalds1da177e2005-04-16 15:20:36 -070017693 tg3_netif_start(tp);
17694
Michael Chanb9ec6c12006-07-25 16:37:27 -070017695out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017696 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017697
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017698 if (!err)
17699 tg3_phy_start(tp);
17700
Michael Chanb9ec6c12006-07-25 16:37:27 -070017701 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017702}
Fabio Estevam42df36a2013-04-16 09:28:29 +000017703#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017704
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017705static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
17706
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017707/**
17708 * tg3_io_error_detected - called when PCI error is detected
17709 * @pdev: Pointer to PCI device
17710 * @state: The current pci connection state
17711 *
17712 * This function is called after a PCI bus error affecting
17713 * this device has been detected.
17714 */
17715static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17716 pci_channel_state_t state)
17717{
17718 struct net_device *netdev = pci_get_drvdata(pdev);
17719 struct tg3 *tp = netdev_priv(netdev);
17720 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17721
17722 netdev_info(netdev, "PCI I/O error detected\n");
17723
17724 rtnl_lock();
17725
17726 if (!netif_running(netdev))
17727 goto done;
17728
17729 tg3_phy_stop(tp);
17730
17731 tg3_netif_stop(tp);
17732
Matt Carlson21f76382012-02-22 12:35:21 +000017733 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017734
17735 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017736 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017737
17738 netif_device_detach(netdev);
17739
17740 /* Clean up software state, even if MMIO is blocked */
17741 tg3_full_lock(tp, 0);
17742 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17743 tg3_full_unlock(tp);
17744
17745done:
17746 if (state == pci_channel_io_perm_failure)
17747 err = PCI_ERS_RESULT_DISCONNECT;
17748 else
17749 pci_disable_device(pdev);
17750
17751 rtnl_unlock();
17752
17753 return err;
17754}
17755
17756/**
17757 * tg3_io_slot_reset - called after the pci bus has been reset.
17758 * @pdev: Pointer to PCI device
17759 *
17760 * Restart the card from scratch, as if from a cold-boot.
17761 * At this point, the card has exprienced a hard reset,
17762 * followed by fixups by BIOS, and has its config space
17763 * set up identically to what it was at cold boot.
17764 */
17765static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17766{
17767 struct net_device *netdev = pci_get_drvdata(pdev);
17768 struct tg3 *tp = netdev_priv(netdev);
17769 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17770 int err;
17771
17772 rtnl_lock();
17773
17774 if (pci_enable_device(pdev)) {
17775 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17776 goto done;
17777 }
17778
17779 pci_set_master(pdev);
17780 pci_restore_state(pdev);
17781 pci_save_state(pdev);
17782
17783 if (!netif_running(netdev)) {
17784 rc = PCI_ERS_RESULT_RECOVERED;
17785 goto done;
17786 }
17787
17788 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017789 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017790 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017791
17792 rc = PCI_ERS_RESULT_RECOVERED;
17793
17794done:
17795 rtnl_unlock();
17796
17797 return rc;
17798}
17799
17800/**
17801 * tg3_io_resume - called when traffic can start flowing again.
17802 * @pdev: Pointer to PCI device
17803 *
17804 * This callback is called when the error recovery driver tells
17805 * us that its OK to resume normal operation.
17806 */
17807static void tg3_io_resume(struct pci_dev *pdev)
17808{
17809 struct net_device *netdev = pci_get_drvdata(pdev);
17810 struct tg3 *tp = netdev_priv(netdev);
17811 int err;
17812
17813 rtnl_lock();
17814
17815 if (!netif_running(netdev))
17816 goto done;
17817
17818 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017819 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000017820 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017821 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017822 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017823 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017824 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17825 goto done;
17826 }
17827
17828 netif_device_attach(netdev);
17829
Matt Carlson21f76382012-02-22 12:35:21 +000017830 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017831
17832 tg3_netif_start(tp);
17833
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017834 tg3_full_unlock(tp);
17835
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017836 tg3_phy_start(tp);
17837
17838done:
17839 rtnl_unlock();
17840}
17841
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017842static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017843 .error_detected = tg3_io_error_detected,
17844 .slot_reset = tg3_io_slot_reset,
17845 .resume = tg3_io_resume
17846};
17847
Linus Torvalds1da177e2005-04-16 15:20:36 -070017848static struct pci_driver tg3_driver = {
17849 .name = DRV_MODULE_NAME,
17850 .id_table = tg3_pci_tbl,
17851 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017852 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017853 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000017854 .driver.pm = &tg3_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017855};
17856
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000017857module_pci_driver(tg3_driver);