blob: 00431bcefc41b2b046057d9aca4b1519713a7a63 [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 Sujircd77b2e2013-07-29 13:58:40 -070097#define TG3_MIN_NUM 133
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Sujircd77b2e2013-07-29 13:58:40 -0700100#define DRV_MODULE_RELDATE "Jul 29, 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)},
Nithin Sujir68273712013-09-20 16:46:56 -0700340 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
341 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
342 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
343 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
344 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700345 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
346 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
347 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
348 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
349 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
350 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
351 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000352 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700353 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
356MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
357
Andreas Mohr50da8592006-08-14 23:54:30 -0700358static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000360} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 { "rx_octets" },
362 { "rx_fragments" },
363 { "rx_ucast_packets" },
364 { "rx_mcast_packets" },
365 { "rx_bcast_packets" },
366 { "rx_fcs_errors" },
367 { "rx_align_errors" },
368 { "rx_xon_pause_rcvd" },
369 { "rx_xoff_pause_rcvd" },
370 { "rx_mac_ctrl_rcvd" },
371 { "rx_xoff_entered" },
372 { "rx_frame_too_long_errors" },
373 { "rx_jabbers" },
374 { "rx_undersize_packets" },
375 { "rx_in_length_errors" },
376 { "rx_out_length_errors" },
377 { "rx_64_or_less_octet_packets" },
378 { "rx_65_to_127_octet_packets" },
379 { "rx_128_to_255_octet_packets" },
380 { "rx_256_to_511_octet_packets" },
381 { "rx_512_to_1023_octet_packets" },
382 { "rx_1024_to_1522_octet_packets" },
383 { "rx_1523_to_2047_octet_packets" },
384 { "rx_2048_to_4095_octet_packets" },
385 { "rx_4096_to_8191_octet_packets" },
386 { "rx_8192_to_9022_octet_packets" },
387
388 { "tx_octets" },
389 { "tx_collisions" },
390
391 { "tx_xon_sent" },
392 { "tx_xoff_sent" },
393 { "tx_flow_control" },
394 { "tx_mac_errors" },
395 { "tx_single_collisions" },
396 { "tx_mult_collisions" },
397 { "tx_deferred" },
398 { "tx_excessive_collisions" },
399 { "tx_late_collisions" },
400 { "tx_collide_2times" },
401 { "tx_collide_3times" },
402 { "tx_collide_4times" },
403 { "tx_collide_5times" },
404 { "tx_collide_6times" },
405 { "tx_collide_7times" },
406 { "tx_collide_8times" },
407 { "tx_collide_9times" },
408 { "tx_collide_10times" },
409 { "tx_collide_11times" },
410 { "tx_collide_12times" },
411 { "tx_collide_13times" },
412 { "tx_collide_14times" },
413 { "tx_collide_15times" },
414 { "tx_ucast_packets" },
415 { "tx_mcast_packets" },
416 { "tx_bcast_packets" },
417 { "tx_carrier_sense_errors" },
418 { "tx_discards" },
419 { "tx_errors" },
420
421 { "dma_writeq_full" },
422 { "dma_write_prioq_full" },
423 { "rxbds_empty" },
424 { "rx_discards" },
425 { "rx_errors" },
426 { "rx_threshold_hit" },
427
428 { "dma_readq_full" },
429 { "dma_read_prioq_full" },
430 { "tx_comp_queue_full" },
431
432 { "ring_set_send_prod_index" },
433 { "ring_status_update" },
434 { "nic_irqs" },
435 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000436 { "nic_tx_threshold_hit" },
437
438 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
440
Matt Carlson48fa55a2011-04-13 11:05:06 +0000441#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000442#define TG3_NVRAM_TEST 0
443#define TG3_LINK_TEST 1
444#define TG3_REGISTER_TEST 2
445#define TG3_MEMORY_TEST 3
446#define TG3_MAC_LOOPB_TEST 4
447#define TG3_PHY_LOOPB_TEST 5
448#define TG3_EXT_LOOPB_TEST 6
449#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000450
451
Andreas Mohr50da8592006-08-14 23:54:30 -0700452static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700453 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000454} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000455 [TG3_NVRAM_TEST] = { "nvram test (online) " },
456 [TG3_LINK_TEST] = { "link test (online) " },
457 [TG3_REGISTER_TEST] = { "register test (offline)" },
458 [TG3_MEMORY_TEST] = { "memory test (offline)" },
459 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
460 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
461 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
462 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700463};
464
Matt Carlson48fa55a2011-04-13 11:05:06 +0000465#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
466
467
Michael Chanb401e9e2005-12-19 16:27:04 -0800468static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
469{
470 writel(val, tp->regs + off);
471}
472
473static u32 tg3_read32(struct tg3 *tp, u32 off)
474{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000475 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800476}
477
Matt Carlson0d3031d2007-10-10 18:02:43 -0700478static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
479{
480 writel(val, tp->aperegs + off);
481}
482
483static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
484{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000485 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
489{
Michael Chan68929142005-08-09 20:17:14 -0700490 unsigned long flags;
491
492 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700493 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
494 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700495 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700496}
497
498static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
499{
500 writel(val, tp->regs + off);
501 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Michael Chan68929142005-08-09 20:17:14 -0700504static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
505{
506 unsigned long flags;
507 u32 val;
508
509 spin_lock_irqsave(&tp->indirect_lock, flags);
510 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
511 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
512 spin_unlock_irqrestore(&tp->indirect_lock, flags);
513 return val;
514}
515
516static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
517{
518 unsigned long flags;
519
520 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
521 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
522 TG3_64BIT_REG_LOW, val);
523 return;
524 }
Matt Carlson66711e662009-11-13 13:03:49 +0000525 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700526 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
527 TG3_64BIT_REG_LOW, val);
528 return;
529 }
530
531 spin_lock_irqsave(&tp->indirect_lock, flags);
532 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
533 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
534 spin_unlock_irqrestore(&tp->indirect_lock, flags);
535
536 /* In indirect mode when disabling interrupts, we also need
537 * to clear the interrupt bit in the GRC local ctrl register.
538 */
539 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
540 (val == 0x1)) {
541 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
542 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
543 }
544}
545
546static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
547{
548 unsigned long flags;
549 u32 val;
550
551 spin_lock_irqsave(&tp->indirect_lock, flags);
552 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
553 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
554 spin_unlock_irqrestore(&tp->indirect_lock, flags);
555 return val;
556}
557
Michael Chanb401e9e2005-12-19 16:27:04 -0800558/* usec_wait specifies the wait time in usec when writing to certain registers
559 * where it is unsafe to read back the register without some delay.
560 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
561 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
562 */
563static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Joe Perches63c3a662011-04-26 08:12:10 +0000565 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800566 /* Non-posted methods */
567 tp->write32(tp, off, val);
568 else {
569 /* Posted method */
570 tg3_write32(tp, off, val);
571 if (usec_wait)
572 udelay(usec_wait);
573 tp->read32(tp, off);
574 }
575 /* Wait again after the read for the posted method to guarantee that
576 * the wait time is met.
577 */
578 if (usec_wait)
579 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Michael Chan09ee9292005-08-09 20:17:00 -0700582static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
583{
584 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000585 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
586 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
587 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700588 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700589}
590
Michael Chan20094932005-08-09 20:16:32 -0700591static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 void __iomem *mbox = tp->regs + off;
594 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000595 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000597 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
598 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 readl(mbox);
600}
601
Michael Chanb5d37722006-09-27 16:06:21 -0700602static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
603{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000604 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700605}
606
607static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
608{
609 writel(val, tp->regs + off + GRCMBOX_BASE);
610}
611
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000612#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700613#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000614#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
615#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
616#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700617
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000618#define tw32(reg, val) tp->write32(tp, reg, val)
619#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
620#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
621#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
624{
Michael Chan68929142005-08-09 20:17:14 -0700625 unsigned long flags;
626
Joe Perches41535772013-02-16 11:20:04 +0000627 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700628 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
629 return;
630
Michael Chan68929142005-08-09 20:17:14 -0700631 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000632 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700633 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
634 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Michael Chanbbadf502006-04-06 21:46:34 -0700636 /* Always leave this as zero. */
637 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
638 } else {
639 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
640 tw32_f(TG3PCI_MEM_WIN_DATA, val);
641
642 /* Always leave this as zero. */
643 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
644 }
Michael Chan68929142005-08-09 20:17:14 -0700645 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
649{
Michael Chan68929142005-08-09 20:17:14 -0700650 unsigned long flags;
651
Joe Perches41535772013-02-16 11:20:04 +0000652 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700653 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
654 *val = 0;
655 return;
656 }
657
Michael Chan68929142005-08-09 20:17:14 -0700658 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000659 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700660 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
661 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Michael Chanbbadf502006-04-06 21:46:34 -0700663 /* Always leave this as zero. */
664 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
665 } else {
666 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
667 *val = tr32(TG3PCI_MEM_WIN_DATA);
668
669 /* Always leave this as zero. */
670 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
671 }
Michael Chan68929142005-08-09 20:17:14 -0700672 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Matt Carlson0d3031d2007-10-10 18:02:43 -0700675static void tg3_ape_lock_init(struct tg3 *tp)
676{
677 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000678 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000679
Joe Perches41535772013-02-16 11:20:04 +0000680 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000681 regbase = TG3_APE_LOCK_GRANT;
682 else
683 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700684
685 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000686 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
687 switch (i) {
688 case TG3_APE_LOCK_PHY0:
689 case TG3_APE_LOCK_PHY1:
690 case TG3_APE_LOCK_PHY2:
691 case TG3_APE_LOCK_PHY3:
692 bit = APE_LOCK_GRANT_DRIVER;
693 break;
694 default:
695 if (!tp->pci_fn)
696 bit = APE_LOCK_GRANT_DRIVER;
697 else
698 bit = 1 << tp->pci_fn;
699 }
700 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000701 }
702
Matt Carlson0d3031d2007-10-10 18:02:43 -0700703}
704
705static int tg3_ape_lock(struct tg3 *tp, int locknum)
706{
707 int i, off;
708 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000709 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700710
Joe Perches63c3a662011-04-26 08:12:10 +0000711 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700712 return 0;
713
714 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000715 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000716 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000717 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000718 case TG3_APE_LOCK_GRC:
719 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000720 if (!tp->pci_fn)
721 bit = APE_LOCK_REQ_DRIVER;
722 else
723 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000724 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000725 case TG3_APE_LOCK_PHY0:
726 case TG3_APE_LOCK_PHY1:
727 case TG3_APE_LOCK_PHY2:
728 case TG3_APE_LOCK_PHY3:
729 bit = APE_LOCK_REQ_DRIVER;
730 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000731 default:
732 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700733 }
734
Joe Perches41535772013-02-16 11:20:04 +0000735 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000736 req = TG3_APE_LOCK_REQ;
737 gnt = TG3_APE_LOCK_GRANT;
738 } else {
739 req = TG3_APE_PER_LOCK_REQ;
740 gnt = TG3_APE_PER_LOCK_GRANT;
741 }
742
Matt Carlson0d3031d2007-10-10 18:02:43 -0700743 off = 4 * locknum;
744
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000745 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746
747 /* Wait for up to 1 millisecond to acquire lock. */
748 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000749 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000750 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700751 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800752 if (pci_channel_offline(tp->pdev))
753 break;
754
Matt Carlson0d3031d2007-10-10 18:02:43 -0700755 udelay(10);
756 }
757
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000758 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700759 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000760 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700761 ret = -EBUSY;
762 }
763
764 return ret;
765}
766
767static void tg3_ape_unlock(struct tg3 *tp, int locknum)
768{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000769 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700770
Joe Perches63c3a662011-04-26 08:12:10 +0000771 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700772 return;
773
774 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000775 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000776 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000777 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000778 case TG3_APE_LOCK_GRC:
779 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000780 if (!tp->pci_fn)
781 bit = APE_LOCK_GRANT_DRIVER;
782 else
783 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000784 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000785 case TG3_APE_LOCK_PHY0:
786 case TG3_APE_LOCK_PHY1:
787 case TG3_APE_LOCK_PHY2:
788 case TG3_APE_LOCK_PHY3:
789 bit = APE_LOCK_GRANT_DRIVER;
790 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000791 default:
792 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700793 }
794
Joe Perches41535772013-02-16 11:20:04 +0000795 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000796 gnt = TG3_APE_LOCK_GRANT;
797 else
798 gnt = TG3_APE_PER_LOCK_GRANT;
799
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000800 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700801}
802
Matt Carlsonb65a3722012-07-16 16:24:00 +0000803static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000804{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000805 u32 apedata;
806
Matt Carlsonb65a3722012-07-16 16:24:00 +0000807 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000808 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000809 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000810
811 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000812 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
813 break;
814
Matt Carlsonb65a3722012-07-16 16:24:00 +0000815 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
816
817 udelay(10);
818 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000819 }
820
Matt Carlsonb65a3722012-07-16 16:24:00 +0000821 return timeout_us ? 0 : -EBUSY;
822}
823
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000824static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
825{
826 u32 i, apedata;
827
828 for (i = 0; i < timeout_us / 10; i++) {
829 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
830
831 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
832 break;
833
834 udelay(10);
835 }
836
837 return i == timeout_us / 10;
838}
839
Michael Chan86449942012-10-02 20:31:14 -0700840static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
841 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000842{
843 int err;
844 u32 i, bufoff, msgoff, maxlen, apedata;
845
846 if (!tg3_flag(tp, APE_HAS_NCSI))
847 return 0;
848
849 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
850 if (apedata != APE_SEG_SIG_MAGIC)
851 return -ENODEV;
852
853 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
854 if (!(apedata & APE_FW_STATUS_READY))
855 return -EAGAIN;
856
857 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
858 TG3_APE_SHMEM_BASE;
859 msgoff = bufoff + 2 * sizeof(u32);
860 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
861
862 while (len) {
863 u32 length;
864
865 /* Cap xfer sizes to scratchpad limits. */
866 length = (len > maxlen) ? maxlen : len;
867 len -= length;
868
869 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
870 if (!(apedata & APE_FW_STATUS_READY))
871 return -EAGAIN;
872
873 /* Wait for up to 1 msec for APE to service previous event. */
874 err = tg3_ape_event_lock(tp, 1000);
875 if (err)
876 return err;
877
878 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
879 APE_EVENT_STATUS_SCRTCHPD_READ |
880 APE_EVENT_STATUS_EVENT_PENDING;
881 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
882
883 tg3_ape_write32(tp, bufoff, base_off);
884 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
885
886 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
887 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
888
889 base_off += length;
890
891 if (tg3_ape_wait_for_event(tp, 30000))
892 return -EAGAIN;
893
894 for (i = 0; length; i += 4, length -= 4) {
895 u32 val = tg3_ape_read32(tp, msgoff + i);
896 memcpy(data, &val, sizeof(u32));
897 data++;
898 }
899 }
900
901 return 0;
902}
903
Matt Carlsonb65a3722012-07-16 16:24:00 +0000904static int tg3_ape_send_event(struct tg3 *tp, u32 event)
905{
906 int err;
907 u32 apedata;
908
909 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
910 if (apedata != APE_SEG_SIG_MAGIC)
911 return -EAGAIN;
912
913 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
914 if (!(apedata & APE_FW_STATUS_READY))
915 return -EAGAIN;
916
917 /* Wait for up to 1 millisecond for APE to service previous event. */
918 err = tg3_ape_event_lock(tp, 1000);
919 if (err)
920 return err;
921
922 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
923 event | APE_EVENT_STATUS_EVENT_PENDING);
924
925 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
926 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
927
928 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000929}
930
931static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
932{
933 u32 event;
934 u32 apedata;
935
936 if (!tg3_flag(tp, ENABLE_APE))
937 return;
938
939 switch (kind) {
940 case RESET_KIND_INIT:
941 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
942 APE_HOST_SEG_SIG_MAGIC);
943 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
944 APE_HOST_SEG_LEN_MAGIC);
945 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
946 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
947 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
948 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
949 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
950 APE_HOST_BEHAV_NO_PHYLOCK);
951 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
952 TG3_APE_HOST_DRVR_STATE_START);
953
954 event = APE_EVENT_STATUS_STATE_START;
955 break;
956 case RESET_KIND_SHUTDOWN:
957 /* With the interface we are currently using,
958 * APE does not track driver state. Wiping
959 * out the HOST SEGMENT SIGNATURE forces
960 * the APE to assume OS absent status.
961 */
962 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
963
964 if (device_may_wakeup(&tp->pdev->dev) &&
965 tg3_flag(tp, WOL_ENABLE)) {
966 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
967 TG3_APE_HOST_WOL_SPEED_AUTO);
968 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
969 } else
970 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
971
972 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
973
974 event = APE_EVENT_STATUS_STATE_UNLOAD;
975 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000976 default:
977 return;
978 }
979
980 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
981
982 tg3_ape_send_event(tp, event);
983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985static void tg3_disable_ints(struct tg3 *tp)
986{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000987 int i;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 tw32(TG3PCI_MISC_HOST_CTRL,
990 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000991 for (i = 0; i < tp->irq_max; i++)
992 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static void tg3_enable_ints(struct tg3 *tp)
996{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000997 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000998
Michael Chanbbe832c2005-06-24 20:20:04 -0700999 tp->irq_sync = 0;
1000 wmb();
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 tw32(TG3PCI_MISC_HOST_CTRL,
1003 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001004
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001005 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001006 for (i = 0; i < tp->irq_cnt; i++) {
1007 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001008
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001010 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001011 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1012
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001013 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001014 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001015
1016 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001017 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001018 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1019 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1020 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001021 tw32(HOSTCC_MODE, tp->coal_now);
1022
1023 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Matt Carlson17375d22009-08-28 14:02:18 +00001026static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001027{
Matt Carlson17375d22009-08-28 14:02:18 +00001028 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001029 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001030 unsigned int work_exists = 0;
1031
1032 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001033 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001034 if (sblk->status & SD_STATUS_LINK_CHG)
1035 work_exists = 1;
1036 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001037
1038 /* check for TX work to do */
1039 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1040 work_exists = 1;
1041
1042 /* check for RX work to do */
1043 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001044 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001045 work_exists = 1;
1046
1047 return work_exists;
1048}
1049
Matt Carlson17375d22009-08-28 14:02:18 +00001050/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001051 * similar to tg3_enable_ints, but it accurately determines whether there
1052 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001053 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Matt Carlson17375d22009-08-28 14:02:18 +00001055static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Matt Carlson17375d22009-08-28 14:02:18 +00001057 struct tg3 *tp = tnapi->tp;
1058
Matt Carlson898a56f2009-08-28 14:02:40 +00001059 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 mmiowb();
1061
David S. Millerfac9b832005-05-18 22:46:34 -07001062 /* When doing tagged status, this work check is unnecessary.
1063 * The last_tag we write above tells the chip which piece of
1064 * work we've completed.
1065 */
Joe Perches63c3a662011-04-26 08:12:10 +00001066 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001067 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001068 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static void tg3_switch_clocks(struct tg3 *tp)
1072{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001073 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 u32 orig_clock_ctrl;
1075
Joe Perches63c3a662011-04-26 08:12:10 +00001076 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001077 return;
1078
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001079 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 orig_clock_ctrl = clock_ctrl;
1082 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1083 CLOCK_CTRL_CLKRUN_OENABLE |
1084 0x1f);
1085 tp->pci_clock_ctrl = clock_ctrl;
1086
Joe Perches63c3a662011-04-26 08:12:10 +00001087 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001089 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1090 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001093 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1094 clock_ctrl |
1095 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1096 40);
1097 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1098 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1099 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001101 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104#define PHY_BUSY_LOOPS 5000
1105
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001106static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1107 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 u32 frame_val;
1110 unsigned int loops;
1111 int ret;
1112
1113 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1114 tw32_f(MAC_MI_MODE,
1115 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1116 udelay(80);
1117 }
1118
Michael Chan8151ad52012-07-29 19:15:41 +00001119 tg3_ape_lock(tp, tp->phy_ape_lock);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 *val = 0x0;
1122
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001123 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 MI_COM_PHY_ADDR_MASK);
1125 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1126 MI_COM_REG_ADDR_MASK);
1127 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 tw32_f(MAC_MI_COM, frame_val);
1130
1131 loops = PHY_BUSY_LOOPS;
1132 while (loops != 0) {
1133 udelay(10);
1134 frame_val = tr32(MAC_MI_COM);
1135
1136 if ((frame_val & MI_COM_BUSY) == 0) {
1137 udelay(5);
1138 frame_val = tr32(MAC_MI_COM);
1139 break;
1140 }
1141 loops -= 1;
1142 }
1143
1144 ret = -EBUSY;
1145 if (loops != 0) {
1146 *val = frame_val & MI_COM_DATA_MASK;
1147 ret = 0;
1148 }
1149
1150 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1151 tw32_f(MAC_MI_MODE, tp->mi_mode);
1152 udelay(80);
1153 }
1154
Michael Chan8151ad52012-07-29 19:15:41 +00001155 tg3_ape_unlock(tp, tp->phy_ape_lock);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return ret;
1158}
1159
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001160static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1161{
1162 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1163}
1164
1165static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1166 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 u32 frame_val;
1169 unsigned int loops;
1170 int ret;
1171
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001172 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001173 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001174 return 0;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1177 tw32_f(MAC_MI_MODE,
1178 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1179 udelay(80);
1180 }
1181
Michael Chan8151ad52012-07-29 19:15:41 +00001182 tg3_ape_lock(tp, tp->phy_ape_lock);
1183
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001184 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 MI_COM_PHY_ADDR_MASK);
1186 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1187 MI_COM_REG_ADDR_MASK);
1188 frame_val |= (val & MI_COM_DATA_MASK);
1189 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 tw32_f(MAC_MI_COM, frame_val);
1192
1193 loops = PHY_BUSY_LOOPS;
1194 while (loops != 0) {
1195 udelay(10);
1196 frame_val = tr32(MAC_MI_COM);
1197 if ((frame_val & MI_COM_BUSY) == 0) {
1198 udelay(5);
1199 frame_val = tr32(MAC_MI_COM);
1200 break;
1201 }
1202 loops -= 1;
1203 }
1204
1205 ret = -EBUSY;
1206 if (loops != 0)
1207 ret = 0;
1208
1209 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1210 tw32_f(MAC_MI_MODE, tp->mi_mode);
1211 udelay(80);
1212 }
1213
Michael Chan8151ad52012-07-29 19:15:41 +00001214 tg3_ape_unlock(tp, tp->phy_ape_lock);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return ret;
1217}
1218
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001219static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1220{
1221 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1222}
1223
Matt Carlsonb0988c12011-04-20 07:57:39 +00001224static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1225{
1226 int err;
1227
1228 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1229 if (err)
1230 goto done;
1231
1232 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1233 if (err)
1234 goto done;
1235
1236 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1237 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1238 if (err)
1239 goto done;
1240
1241 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1242
1243done:
1244 return err;
1245}
1246
1247static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1248{
1249 int err;
1250
1251 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1252 if (err)
1253 goto done;
1254
1255 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1256 if (err)
1257 goto done;
1258
1259 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1260 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1261 if (err)
1262 goto done;
1263
1264 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1265
1266done:
1267 return err;
1268}
1269
1270static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1271{
1272 int err;
1273
1274 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1275 if (!err)
1276 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1277
1278 return err;
1279}
1280
1281static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1282{
1283 int err;
1284
1285 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1286 if (!err)
1287 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1288
1289 return err;
1290}
1291
Matt Carlson15ee95c2011-04-20 07:57:40 +00001292static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1293{
1294 int err;
1295
1296 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1297 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1298 MII_TG3_AUXCTL_SHDWSEL_MISC);
1299 if (!err)
1300 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1301
1302 return err;
1303}
1304
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001305static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1306{
1307 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1308 set |= MII_TG3_AUXCTL_MISC_WREN;
1309
1310 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1311}
1312
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001313static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1314{
1315 u32 val;
1316 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001317
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001318 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1319
1320 if (err)
1321 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001322
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001323 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001324 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1325 else
1326 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1327
1328 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1329 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1330
1331 return err;
1332}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001333
Nithin Sujir3ab71072013-09-20 16:46:55 -07001334static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1335{
1336 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1337 reg | val | MII_TG3_MISC_SHDW_WREN);
1338}
1339
Matt Carlson95e28692008-05-25 23:44:14 -07001340static int tg3_bmcr_reset(struct tg3 *tp)
1341{
1342 u32 phy_control;
1343 int limit, err;
1344
1345 /* OK, reset it, and poll the BMCR_RESET bit until it
1346 * clears or we time out.
1347 */
1348 phy_control = BMCR_RESET;
1349 err = tg3_writephy(tp, MII_BMCR, phy_control);
1350 if (err != 0)
1351 return -EBUSY;
1352
1353 limit = 5000;
1354 while (limit--) {
1355 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1356 if (err != 0)
1357 return -EBUSY;
1358
1359 if ((phy_control & BMCR_RESET) == 0) {
1360 udelay(40);
1361 break;
1362 }
1363 udelay(10);
1364 }
Roel Kluind4675b52009-02-12 16:33:27 -08001365 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001366 return -EBUSY;
1367
1368 return 0;
1369}
1370
Matt Carlson158d7ab2008-05-29 01:37:54 -07001371static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1372{
Francois Romieu3d165432009-01-19 16:56:50 -08001373 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001374 u32 val;
1375
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001376 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001377
1378 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001379 val = -EIO;
1380
1381 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001382
1383 return val;
1384}
1385
1386static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1387{
Francois Romieu3d165432009-01-19 16:56:50 -08001388 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001389 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001390
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001391 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001392
1393 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001394 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001395
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001396 spin_unlock_bh(&tp->lock);
1397
1398 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001399}
1400
1401static int tg3_mdio_reset(struct mii_bus *bp)
1402{
1403 return 0;
1404}
1405
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001406static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001407{
1408 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001409 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001410
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001411 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001412 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001413 case PHY_ID_BCM50610:
1414 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001415 val = MAC_PHYCFG2_50610_LED_MODES;
1416 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001417 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001418 val = MAC_PHYCFG2_AC131_LED_MODES;
1419 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001420 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001421 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1422 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001423 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001424 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1425 break;
1426 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001427 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001428 }
1429
1430 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1431 tw32(MAC_PHYCFG2, val);
1432
1433 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001434 val &= ~(MAC_PHYCFG1_RGMII_INT |
1435 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1436 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001437 tw32(MAC_PHYCFG1, val);
1438
1439 return;
1440 }
1441
Joe Perches63c3a662011-04-26 08:12:10 +00001442 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001443 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1444 MAC_PHYCFG2_FMODE_MASK_MASK |
1445 MAC_PHYCFG2_GMODE_MASK_MASK |
1446 MAC_PHYCFG2_ACT_MASK_MASK |
1447 MAC_PHYCFG2_QUAL_MASK_MASK |
1448 MAC_PHYCFG2_INBAND_ENABLE;
1449
1450 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001451
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001452 val = tr32(MAC_PHYCFG1);
1453 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1454 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001455 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1456 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001457 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001458 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001459 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1460 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001461 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1462 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1463 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001464
Matt Carlsona9daf362008-05-25 23:49:44 -07001465 val = tr32(MAC_EXT_RGMII_MODE);
1466 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1467 MAC_RGMII_MODE_RX_QUALITY |
1468 MAC_RGMII_MODE_RX_ACTIVITY |
1469 MAC_RGMII_MODE_RX_ENG_DET |
1470 MAC_RGMII_MODE_TX_ENABLE |
1471 MAC_RGMII_MODE_TX_LOWPWR |
1472 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001473 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1474 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001475 val |= MAC_RGMII_MODE_RX_INT_B |
1476 MAC_RGMII_MODE_RX_QUALITY |
1477 MAC_RGMII_MODE_RX_ACTIVITY |
1478 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001479 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001480 val |= MAC_RGMII_MODE_TX_ENABLE |
1481 MAC_RGMII_MODE_TX_LOWPWR |
1482 MAC_RGMII_MODE_TX_RESET;
1483 }
1484 tw32(MAC_EXT_RGMII_MODE, val);
1485}
1486
Matt Carlson158d7ab2008-05-29 01:37:54 -07001487static void tg3_mdio_start(struct tg3 *tp)
1488{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001489 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1490 tw32_f(MAC_MI_MODE, tp->mi_mode);
1491 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001492
Joe Perches63c3a662011-04-26 08:12:10 +00001493 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001494 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001495 tg3_mdio_config_5785(tp);
1496}
1497
1498static int tg3_mdio_init(struct tg3 *tp)
1499{
1500 int i;
1501 u32 reg;
1502 struct phy_device *phydev;
1503
Joe Perches63c3a662011-04-26 08:12:10 +00001504 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001505 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001506
Matt Carlson69f11c92011-07-13 09:27:30 +00001507 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001508
Joe Perches41535772013-02-16 11:20:04 +00001509 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001510 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1511 else
1512 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1513 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001514 if (is_serdes)
1515 tp->phy_addr += 7;
1516 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001517 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001518
Matt Carlson158d7ab2008-05-29 01:37:54 -07001519 tg3_mdio_start(tp);
1520
Joe Perches63c3a662011-04-26 08:12:10 +00001521 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001522 return 0;
1523
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001524 tp->mdio_bus = mdiobus_alloc();
1525 if (tp->mdio_bus == NULL)
1526 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001527
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001528 tp->mdio_bus->name = "tg3 mdio bus";
1529 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001530 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001531 tp->mdio_bus->priv = tp;
1532 tp->mdio_bus->parent = &tp->pdev->dev;
1533 tp->mdio_bus->read = &tg3_mdio_read;
1534 tp->mdio_bus->write = &tg3_mdio_write;
1535 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001536 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001537 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001538
1539 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001540 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001541
1542 /* The bus registration will look for all the PHYs on the mdio bus.
1543 * Unfortunately, it does not ensure the PHY is powered up before
1544 * accessing the PHY ID registers. A chip reset is the
1545 * quickest way to bring the device back to an operational state..
1546 */
1547 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1548 tg3_bmcr_reset(tp);
1549
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001550 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001551 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001552 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001553 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001554 return i;
1555 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001556
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001557 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001558
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001559 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001560 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001561 mdiobus_unregister(tp->mdio_bus);
1562 mdiobus_free(tp->mdio_bus);
1563 return -ENODEV;
1564 }
1565
1566 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001567 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001568 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001569 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001570 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001571 case PHY_ID_BCM50610:
1572 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001573 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001574 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001575 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001576 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001577 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001578 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001579 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001580 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001581 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001582 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001583 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001584 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001585 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001586 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001587 case PHY_ID_RTL8201E:
1588 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001589 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001590 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001591 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001592 break;
1593 }
1594
Joe Perches63c3a662011-04-26 08:12:10 +00001595 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001596
Joe Perches41535772013-02-16 11:20:04 +00001597 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001598 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001599
1600 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001601}
1602
1603static void tg3_mdio_fini(struct tg3 *tp)
1604{
Joe Perches63c3a662011-04-26 08:12:10 +00001605 if (tg3_flag(tp, MDIOBUS_INITED)) {
1606 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001607 mdiobus_unregister(tp->mdio_bus);
1608 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001609 }
1610}
1611
Matt Carlson95e28692008-05-25 23:44:14 -07001612/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001613static inline void tg3_generate_fw_event(struct tg3 *tp)
1614{
1615 u32 val;
1616
1617 val = tr32(GRC_RX_CPU_EVENT);
1618 val |= GRC_RX_CPU_DRIVER_EVENT;
1619 tw32_f(GRC_RX_CPU_EVENT, val);
1620
1621 tp->last_event_jiffies = jiffies;
1622}
1623
1624#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1625
1626/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001627static void tg3_wait_for_event_ack(struct tg3 *tp)
1628{
1629 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001630 unsigned int delay_cnt;
1631 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001632
Matt Carlson4ba526c2008-08-15 14:10:04 -07001633 /* If enough time has passed, no wait is necessary. */
1634 time_remain = (long)(tp->last_event_jiffies + 1 +
1635 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1636 (long)jiffies;
1637 if (time_remain < 0)
1638 return;
1639
1640 /* Check if we can shorten the wait time. */
1641 delay_cnt = jiffies_to_usecs(time_remain);
1642 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1643 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1644 delay_cnt = (delay_cnt >> 3) + 1;
1645
1646 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001647 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1648 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001649 if (pci_channel_offline(tp->pdev))
1650 break;
1651
Matt Carlson4ba526c2008-08-15 14:10:04 -07001652 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001653 }
1654}
1655
1656/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001657static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001658{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001659 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001660
1661 val = 0;
1662 if (!tg3_readphy(tp, MII_BMCR, &reg))
1663 val = reg << 16;
1664 if (!tg3_readphy(tp, MII_BMSR, &reg))
1665 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001666 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001667
1668 val = 0;
1669 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1670 val = reg << 16;
1671 if (!tg3_readphy(tp, MII_LPA, &reg))
1672 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001673 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001674
1675 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001676 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001677 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1678 val = reg << 16;
1679 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1680 val |= (reg & 0xffff);
1681 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001682 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001683
1684 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1685 val = reg << 16;
1686 else
1687 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001688 *data++ = val;
1689}
1690
1691/* tp->lock is held. */
1692static void tg3_ump_link_report(struct tg3 *tp)
1693{
1694 u32 data[4];
1695
1696 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1697 return;
1698
1699 tg3_phy_gather_ump_data(tp, data);
1700
1701 tg3_wait_for_event_ack(tp);
1702
1703 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1704 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1705 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1706 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1707 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1708 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001709
Matt Carlson4ba526c2008-08-15 14:10:04 -07001710 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001711}
1712
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001713/* tp->lock is held. */
1714static void tg3_stop_fw(struct tg3 *tp)
1715{
1716 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1717 /* Wait for RX cpu to ACK the previous event. */
1718 tg3_wait_for_event_ack(tp);
1719
1720 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1721
1722 tg3_generate_fw_event(tp);
1723
1724 /* Wait for RX cpu to ACK this event. */
1725 tg3_wait_for_event_ack(tp);
1726 }
1727}
1728
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001729/* tp->lock is held. */
1730static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1731{
1732 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1733 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1734
1735 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1736 switch (kind) {
1737 case RESET_KIND_INIT:
1738 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1739 DRV_STATE_START);
1740 break;
1741
1742 case RESET_KIND_SHUTDOWN:
1743 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1744 DRV_STATE_UNLOAD);
1745 break;
1746
1747 case RESET_KIND_SUSPEND:
1748 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1749 DRV_STATE_SUSPEND);
1750 break;
1751
1752 default:
1753 break;
1754 }
1755 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001756}
1757
1758/* tp->lock is held. */
1759static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1760{
1761 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1762 switch (kind) {
1763 case RESET_KIND_INIT:
1764 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1765 DRV_STATE_START_DONE);
1766 break;
1767
1768 case RESET_KIND_SHUTDOWN:
1769 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1770 DRV_STATE_UNLOAD_DONE);
1771 break;
1772
1773 default:
1774 break;
1775 }
1776 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001777}
1778
1779/* tp->lock is held. */
1780static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1781{
1782 if (tg3_flag(tp, ENABLE_ASF)) {
1783 switch (kind) {
1784 case RESET_KIND_INIT:
1785 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1786 DRV_STATE_START);
1787 break;
1788
1789 case RESET_KIND_SHUTDOWN:
1790 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1791 DRV_STATE_UNLOAD);
1792 break;
1793
1794 case RESET_KIND_SUSPEND:
1795 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1796 DRV_STATE_SUSPEND);
1797 break;
1798
1799 default:
1800 break;
1801 }
1802 }
1803}
1804
1805static int tg3_poll_fw(struct tg3 *tp)
1806{
1807 int i;
1808 u32 val;
1809
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001810 if (tg3_flag(tp, NO_FWARE_REPORTED))
1811 return 0;
1812
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001813 if (tg3_flag(tp, IS_SSB_CORE)) {
1814 /* We don't use firmware. */
1815 return 0;
1816 }
1817
Joe Perches41535772013-02-16 11:20:04 +00001818 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001819 /* Wait up to 20ms for init done. */
1820 for (i = 0; i < 200; i++) {
1821 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1822 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001823 if (pci_channel_offline(tp->pdev))
1824 return -ENODEV;
1825
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001826 udelay(100);
1827 }
1828 return -ENODEV;
1829 }
1830
1831 /* Wait for firmware initialization to complete. */
1832 for (i = 0; i < 100000; i++) {
1833 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1834 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1835 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001836 if (pci_channel_offline(tp->pdev)) {
1837 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1838 tg3_flag_set(tp, NO_FWARE_REPORTED);
1839 netdev_info(tp->dev, "No firmware running\n");
1840 }
1841
1842 break;
1843 }
1844
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001845 udelay(10);
1846 }
1847
1848 /* Chip might not be fitted with firmware. Some Sun onboard
1849 * parts are configured like that. So don't signal the timeout
1850 * of the above loop as an error, but do report the lack of
1851 * running firmware once.
1852 */
1853 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1854 tg3_flag_set(tp, NO_FWARE_REPORTED);
1855
1856 netdev_info(tp->dev, "No firmware running\n");
1857 }
1858
Joe Perches41535772013-02-16 11:20:04 +00001859 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001860 /* The 57765 A0 needs a little more
1861 * time to do some important work.
1862 */
1863 mdelay(10);
1864 }
1865
1866 return 0;
1867}
1868
Matt Carlson95e28692008-05-25 23:44:14 -07001869static void tg3_link_report(struct tg3 *tp)
1870{
1871 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001872 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001873 tg3_ump_link_report(tp);
1874 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001875 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1876 (tp->link_config.active_speed == SPEED_1000 ?
1877 1000 :
1878 (tp->link_config.active_speed == SPEED_100 ?
1879 100 : 10)),
1880 (tp->link_config.active_duplex == DUPLEX_FULL ?
1881 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001882
Joe Perches05dbe002010-02-17 19:44:19 +00001883 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1884 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1885 "on" : "off",
1886 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1887 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001888
1889 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1890 netdev_info(tp->dev, "EEE is %s\n",
1891 tp->setlpicnt ? "enabled" : "disabled");
1892
Matt Carlson95e28692008-05-25 23:44:14 -07001893 tg3_ump_link_report(tp);
1894 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001895
1896 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001897}
1898
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001899static u32 tg3_decode_flowctrl_1000T(u32 adv)
1900{
1901 u32 flowctrl = 0;
1902
1903 if (adv & ADVERTISE_PAUSE_CAP) {
1904 flowctrl |= FLOW_CTRL_RX;
1905 if (!(adv & ADVERTISE_PAUSE_ASYM))
1906 flowctrl |= FLOW_CTRL_TX;
1907 } else if (adv & ADVERTISE_PAUSE_ASYM)
1908 flowctrl |= FLOW_CTRL_TX;
1909
1910 return flowctrl;
1911}
1912
Matt Carlson95e28692008-05-25 23:44:14 -07001913static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1914{
1915 u16 miireg;
1916
Steve Glendinninge18ce342008-12-16 02:00:00 -08001917 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001918 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001919 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001920 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001921 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001922 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1923 else
1924 miireg = 0;
1925
1926 return miireg;
1927}
1928
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001929static u32 tg3_decode_flowctrl_1000X(u32 adv)
1930{
1931 u32 flowctrl = 0;
1932
1933 if (adv & ADVERTISE_1000XPAUSE) {
1934 flowctrl |= FLOW_CTRL_RX;
1935 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1936 flowctrl |= FLOW_CTRL_TX;
1937 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1938 flowctrl |= FLOW_CTRL_TX;
1939
1940 return flowctrl;
1941}
1942
Matt Carlson95e28692008-05-25 23:44:14 -07001943static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1944{
1945 u8 cap = 0;
1946
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001947 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1948 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1949 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1950 if (lcladv & ADVERTISE_1000XPAUSE)
1951 cap = FLOW_CTRL_RX;
1952 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001953 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001954 }
1955
1956 return cap;
1957}
1958
Matt Carlsonf51f3562008-05-25 23:45:08 -07001959static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001960{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001961 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001962 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001963 u32 old_rx_mode = tp->rx_mode;
1964 u32 old_tx_mode = tp->tx_mode;
1965
Joe Perches63c3a662011-04-26 08:12:10 +00001966 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001967 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001968 else
1969 autoneg = tp->link_config.autoneg;
1970
Joe Perches63c3a662011-04-26 08:12:10 +00001971 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001972 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001973 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001974 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001975 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001976 } else
1977 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001978
Matt Carlsonf51f3562008-05-25 23:45:08 -07001979 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001980
Steve Glendinninge18ce342008-12-16 02:00:00 -08001981 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001982 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1983 else
1984 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1985
Matt Carlsonf51f3562008-05-25 23:45:08 -07001986 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001987 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001988
Steve Glendinninge18ce342008-12-16 02:00:00 -08001989 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001990 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1991 else
1992 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1993
Matt Carlsonf51f3562008-05-25 23:45:08 -07001994 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001995 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001996}
1997
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001998static void tg3_adjust_link(struct net_device *dev)
1999{
2000 u8 oldflowctrl, linkmesg = 0;
2001 u32 mac_mode, lcl_adv, rmt_adv;
2002 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002003 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002004
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002005 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002006
2007 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2008 MAC_MODE_HALF_DUPLEX);
2009
2010 oldflowctrl = tp->link_config.active_flowctrl;
2011
2012 if (phydev->link) {
2013 lcl_adv = 0;
2014 rmt_adv = 0;
2015
2016 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2017 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002018 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002019 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002020 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002021 else
2022 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002023
2024 if (phydev->duplex == DUPLEX_HALF)
2025 mac_mode |= MAC_MODE_HALF_DUPLEX;
2026 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002027 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002028 tp->link_config.flowctrl);
2029
2030 if (phydev->pause)
2031 rmt_adv = LPA_PAUSE_CAP;
2032 if (phydev->asym_pause)
2033 rmt_adv |= LPA_PAUSE_ASYM;
2034 }
2035
2036 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2037 } else
2038 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2039
2040 if (mac_mode != tp->mac_mode) {
2041 tp->mac_mode = mac_mode;
2042 tw32_f(MAC_MODE, tp->mac_mode);
2043 udelay(40);
2044 }
2045
Joe Perches41535772013-02-16 11:20:04 +00002046 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002047 if (phydev->speed == SPEED_10)
2048 tw32(MAC_MI_STAT,
2049 MAC_MI_STAT_10MBPS_MODE |
2050 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2051 else
2052 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2053 }
2054
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002055 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2056 tw32(MAC_TX_LENGTHS,
2057 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2058 (6 << TX_LENGTHS_IPG_SHIFT) |
2059 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2060 else
2061 tw32(MAC_TX_LENGTHS,
2062 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2063 (6 << TX_LENGTHS_IPG_SHIFT) |
2064 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2065
Matt Carlson34655ad2012-02-22 12:35:18 +00002066 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002067 phydev->speed != tp->link_config.active_speed ||
2068 phydev->duplex != tp->link_config.active_duplex ||
2069 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002070 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002071
Matt Carlson34655ad2012-02-22 12:35:18 +00002072 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002073 tp->link_config.active_speed = phydev->speed;
2074 tp->link_config.active_duplex = phydev->duplex;
2075
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002076 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002077
2078 if (linkmesg)
2079 tg3_link_report(tp);
2080}
2081
2082static int tg3_phy_init(struct tg3 *tp)
2083{
2084 struct phy_device *phydev;
2085
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002086 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002087 return 0;
2088
2089 /* Bring the PHY back to a known state. */
2090 tg3_bmcr_reset(tp);
2091
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002092 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002093
2094 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002095 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2096 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002097 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002098 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002099 return PTR_ERR(phydev);
2100 }
2101
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002102 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002103 switch (phydev->interface) {
2104 case PHY_INTERFACE_MODE_GMII:
2105 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002106 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002107 phydev->supported &= (PHY_GBIT_FEATURES |
2108 SUPPORTED_Pause |
2109 SUPPORTED_Asym_Pause);
2110 break;
2111 }
2112 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002113 case PHY_INTERFACE_MODE_MII:
2114 phydev->supported &= (PHY_BASIC_FEATURES |
2115 SUPPORTED_Pause |
2116 SUPPORTED_Asym_Pause);
2117 break;
2118 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002119 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002120 return -EINVAL;
2121 }
2122
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002123 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002124
2125 phydev->advertising = phydev->supported;
2126
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002127 return 0;
2128}
2129
2130static void tg3_phy_start(struct tg3 *tp)
2131{
2132 struct phy_device *phydev;
2133
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002134 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002135 return;
2136
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002137 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002138
Matt Carlson80096062010-08-02 11:26:06 +00002139 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2140 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002141 phydev->speed = tp->link_config.speed;
2142 phydev->duplex = tp->link_config.duplex;
2143 phydev->autoneg = tp->link_config.autoneg;
2144 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002145 }
2146
2147 phy_start(phydev);
2148
2149 phy_start_aneg(phydev);
2150}
2151
2152static void tg3_phy_stop(struct tg3 *tp)
2153{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002154 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002155 return;
2156
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002157 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002158}
2159
2160static void tg3_phy_fini(struct tg3 *tp)
2161{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002162 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002163 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002164 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002165 }
2166}
2167
Matt Carlson941ec902011-08-19 13:58:23 +00002168static int tg3_phy_set_extloopbk(struct tg3 *tp)
2169{
2170 int err;
2171 u32 val;
2172
2173 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2174 return 0;
2175
2176 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2177 /* Cannot do read-modify-write on 5401 */
2178 err = tg3_phy_auxctl_write(tp,
2179 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2180 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2181 0x4c20);
2182 goto done;
2183 }
2184
2185 err = tg3_phy_auxctl_read(tp,
2186 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2187 if (err)
2188 return err;
2189
2190 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2191 err = tg3_phy_auxctl_write(tp,
2192 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2193
2194done:
2195 return err;
2196}
2197
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002198static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2199{
2200 u32 phytest;
2201
2202 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2203 u32 phy;
2204
2205 tg3_writephy(tp, MII_TG3_FET_TEST,
2206 phytest | MII_TG3_FET_SHADOW_EN);
2207 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2208 if (enable)
2209 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2210 else
2211 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2212 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2213 }
2214 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2215 }
2216}
2217
Matt Carlson6833c042008-11-21 17:18:59 -08002218static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2219{
2220 u32 reg;
2221
Joe Perches63c3a662011-04-26 08:12:10 +00002222 if (!tg3_flag(tp, 5705_PLUS) ||
2223 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002224 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002225 return;
2226
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002227 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002228 tg3_phy_fet_toggle_apd(tp, enable);
2229 return;
2230 }
2231
Nithin Sujir3ab71072013-09-20 16:46:55 -07002232 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002233 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2234 MII_TG3_MISC_SHDW_SCR5_SDTL |
2235 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002236 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002237 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2238
Nithin Sujir3ab71072013-09-20 16:46:55 -07002239 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002240
2241
Nithin Sujir3ab71072013-09-20 16:46:55 -07002242 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002243 if (enable)
2244 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2245
Nithin Sujir3ab71072013-09-20 16:46:55 -07002246 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002247}
2248
Joe Perches953c96e2013-04-09 10:18:14 +00002249static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002250{
2251 u32 phy;
2252
Joe Perches63c3a662011-04-26 08:12:10 +00002253 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002254 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002255 return;
2256
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002257 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002258 u32 ephy;
2259
Matt Carlson535ef6e2009-08-25 10:09:36 +00002260 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2261 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2262
2263 tg3_writephy(tp, MII_TG3_FET_TEST,
2264 ephy | MII_TG3_FET_SHADOW_EN);
2265 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002266 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002267 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002268 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002269 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2270 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002271 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002272 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002273 }
2274 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002275 int ret;
2276
2277 ret = tg3_phy_auxctl_read(tp,
2278 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2279 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002280 if (enable)
2281 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2282 else
2283 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002284 tg3_phy_auxctl_write(tp,
2285 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002286 }
2287 }
2288}
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290static void tg3_phy_set_wirespeed(struct tg3 *tp)
2291{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002292 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 u32 val;
2294
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002295 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return;
2297
Matt Carlson15ee95c2011-04-20 07:57:40 +00002298 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2299 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002300 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2301 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002304static void tg3_phy_apply_otp(struct tg3 *tp)
2305{
2306 u32 otp, phy;
2307
2308 if (!tp->phy_otp)
2309 return;
2310
2311 otp = tp->phy_otp;
2312
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002313 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002314 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002315
2316 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2317 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2318 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2319
2320 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2321 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2322 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2323
2324 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2325 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2326 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2327
2328 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2329 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2330
2331 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2332 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2333
2334 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2335 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2336 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2337
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002338 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002339}
2340
Nithin Sujir400dfba2013-05-18 06:26:53 +00002341static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2342{
2343 u32 val;
2344 struct ethtool_eee *dest = &tp->eee;
2345
2346 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2347 return;
2348
2349 if (eee)
2350 dest = eee;
2351
2352 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2353 return;
2354
2355 /* Pull eee_active */
2356 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2357 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2358 dest->eee_active = 1;
2359 } else
2360 dest->eee_active = 0;
2361
2362 /* Pull lp advertised settings */
2363 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2364 return;
2365 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2366
2367 /* Pull advertised and eee_enabled settings */
2368 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2369 return;
2370 dest->eee_enabled = !!val;
2371 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2372
2373 /* Pull tx_lpi_enabled */
2374 val = tr32(TG3_CPMU_EEE_MODE);
2375 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2376
2377 /* Pull lpi timer value */
2378 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2379}
2380
Joe Perches953c96e2013-04-09 10:18:14 +00002381static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002382{
2383 u32 val;
2384
2385 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2386 return;
2387
2388 tp->setlpicnt = 0;
2389
2390 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002391 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002392 tp->link_config.active_duplex == DUPLEX_FULL &&
2393 (tp->link_config.active_speed == SPEED_100 ||
2394 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002395 u32 eeectl;
2396
2397 if (tp->link_config.active_speed == SPEED_1000)
2398 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2399 else
2400 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2401
2402 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2403
Nithin Sujir400dfba2013-05-18 06:26:53 +00002404 tg3_eee_pull_config(tp, NULL);
2405 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002406 tp->setlpicnt = 2;
2407 }
2408
2409 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002410 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002411 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002412 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002413 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002414 }
2415
Matt Carlson52b02d02010-10-14 10:37:41 +00002416 val = tr32(TG3_CPMU_EEE_MODE);
2417 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2418 }
2419}
2420
Matt Carlsonb0c59432011-05-19 12:12:48 +00002421static void tg3_phy_eee_enable(struct tg3 *tp)
2422{
2423 u32 val;
2424
2425 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002426 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2427 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002428 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002429 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002430 val = MII_TG3_DSP_TAP26_ALNOKO |
2431 MII_TG3_DSP_TAP26_RMRXSTO;
2432 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002433 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002434 }
2435
2436 val = tr32(TG3_CPMU_EEE_MODE);
2437 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2438}
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static int tg3_wait_macro_done(struct tg3 *tp)
2441{
2442 int limit = 100;
2443
2444 while (limit--) {
2445 u32 tmp32;
2446
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002447 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if ((tmp32 & 0x1000) == 0)
2449 break;
2450 }
2451 }
Roel Kluind4675b52009-02-12 16:33:27 -08002452 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return -EBUSY;
2454
2455 return 0;
2456}
2457
2458static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2459{
2460 static const u32 test_pat[4][6] = {
2461 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2462 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2463 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2464 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2465 };
2466 int chan;
2467
2468 for (chan = 0; chan < 4; chan++) {
2469 int i;
2470
2471 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2472 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002473 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 for (i = 0; i < 6; i++)
2476 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2477 test_pat[chan][i]);
2478
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002479 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (tg3_wait_macro_done(tp)) {
2481 *resetp = 1;
2482 return -EBUSY;
2483 }
2484
2485 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2486 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002487 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (tg3_wait_macro_done(tp)) {
2489 *resetp = 1;
2490 return -EBUSY;
2491 }
2492
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002493 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (tg3_wait_macro_done(tp)) {
2495 *resetp = 1;
2496 return -EBUSY;
2497 }
2498
2499 for (i = 0; i < 6; i += 2) {
2500 u32 low, high;
2501
2502 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2503 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2504 tg3_wait_macro_done(tp)) {
2505 *resetp = 1;
2506 return -EBUSY;
2507 }
2508 low &= 0x7fff;
2509 high &= 0x000f;
2510 if (low != test_pat[chan][i] ||
2511 high != test_pat[chan][i+1]) {
2512 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2513 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2514 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2515
2516 return -EBUSY;
2517 }
2518 }
2519 }
2520
2521 return 0;
2522}
2523
2524static int tg3_phy_reset_chanpat(struct tg3 *tp)
2525{
2526 int chan;
2527
2528 for (chan = 0; chan < 4; chan++) {
2529 int i;
2530
2531 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2532 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002533 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 for (i = 0; i < 6; i++)
2535 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002536 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (tg3_wait_macro_done(tp))
2538 return -EBUSY;
2539 }
2540
2541 return 0;
2542}
2543
2544static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2545{
2546 u32 reg32, phy9_orig;
2547 int retries, do_phy_reset, err;
2548
2549 retries = 10;
2550 do_phy_reset = 1;
2551 do {
2552 if (do_phy_reset) {
2553 err = tg3_bmcr_reset(tp);
2554 if (err)
2555 return err;
2556 do_phy_reset = 0;
2557 }
2558
2559 /* Disable transmitter and interrupt. */
2560 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2561 continue;
2562
2563 reg32 |= 0x3000;
2564 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2565
2566 /* Set full-duplex, 1000 mbps. */
2567 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002568 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002571 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 continue;
2573
Matt Carlson221c5632011-06-13 13:39:01 +00002574 tg3_writephy(tp, MII_CTRL1000,
2575 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002577 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002578 if (err)
2579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002582 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2585 if (!err)
2586 break;
2587 } while (--retries);
2588
2589 err = tg3_phy_reset_chanpat(tp);
2590 if (err)
2591 return err;
2592
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002593 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002596 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002598 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Matt Carlson221c5632011-06-13 13:39:01 +00002600 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2603 reg32 &= ~0x3000;
2604 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2605 } else if (!err)
2606 err = -EBUSY;
2607
2608 return err;
2609}
2610
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002611static void tg3_carrier_off(struct tg3 *tp)
2612{
2613 netif_carrier_off(tp->dev);
2614 tp->link_up = false;
2615}
2616
Nithin Sujirce20f162013-04-09 08:48:04 +00002617static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2618{
2619 if (tg3_flag(tp, ENABLE_ASF))
2620 netdev_warn(tp->dev,
2621 "Management side-band traffic will be interrupted during phy settings change\n");
2622}
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624/* This will reset the tigon3 PHY if there is no valid
2625 * link unless the FORCE argument is non-zero.
2626 */
2627static int tg3_phy_reset(struct tg3 *tp)
2628{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002629 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 int err;
2631
Joe Perches41535772013-02-16 11:20:04 +00002632 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002633 val = tr32(GRC_MISC_CFG);
2634 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2635 udelay(40);
2636 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002637 err = tg3_readphy(tp, MII_BMSR, &val);
2638 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (err != 0)
2640 return -EBUSY;
2641
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002642 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002643 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002644 tg3_link_report(tp);
2645 }
2646
Joe Perches41535772013-02-16 11:20:04 +00002647 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2648 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2649 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 err = tg3_phy_reset_5703_4_5(tp);
2651 if (err)
2652 return err;
2653 goto out;
2654 }
2655
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002656 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002657 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2658 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002659 cpmuctrl = tr32(TG3_CPMU_CTRL);
2660 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2661 tw32(TG3_CPMU_CTRL,
2662 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2663 }
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 err = tg3_bmcr_reset(tp);
2666 if (err)
2667 return err;
2668
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002669 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002670 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2671 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002672
2673 tw32(TG3_CPMU_CTRL, cpmuctrl);
2674 }
2675
Joe Perches41535772013-02-16 11:20:04 +00002676 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2677 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002678 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2679 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2680 CPMU_LSPD_1000MB_MACCLK_12_5) {
2681 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2682 udelay(40);
2683 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2684 }
2685 }
2686
Joe Perches63c3a662011-04-26 08:12:10 +00002687 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002688 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002689 return 0;
2690
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002691 tg3_phy_apply_otp(tp);
2692
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002693 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002694 tg3_phy_toggle_apd(tp, true);
2695 else
2696 tg3_phy_toggle_apd(tp, false);
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002699 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002700 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002701 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2702 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002703 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002705
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002706 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002707 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2708 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002710
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002711 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002712 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002713 tg3_phydsp_write(tp, 0x000a, 0x310b);
2714 tg3_phydsp_write(tp, 0x201f, 0x9506);
2715 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002716 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002717 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002718 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002719 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002720 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2721 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2722 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2723 tg3_writephy(tp, MII_TG3_TEST1,
2724 MII_TG3_TEST1_TRIM_EN | 0x4);
2725 } else
2726 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2727
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002728 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002729 }
Michael Chanc424cb22006-04-29 18:56:34 -07002730 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 /* Set Extended packet length bit (bit 14) on all chips that */
2733 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002734 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002736 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002737 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002739 err = tg3_phy_auxctl_read(tp,
2740 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2741 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002742 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2743 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745
2746 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2747 * jumbo frames transmission.
2748 */
Joe Perches63c3a662011-04-26 08:12:10 +00002749 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002750 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002751 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002752 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
2754
Joe Perches41535772013-02-16 11:20:04 +00002755 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002756 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002757 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002758 }
2759
Joe Perches41535772013-02-16 11:20:04 +00002760 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002761 tg3_phydsp_write(tp, 0xffb, 0x4000);
2762
Joe Perches953c96e2013-04-09 10:18:14 +00002763 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 tg3_phy_set_wirespeed(tp);
2765 return 0;
2766}
2767
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002768#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2769#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2770#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2771 TG3_GPIO_MSG_NEED_VAUX)
2772#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2773 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2774 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2775 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2776 (TG3_GPIO_MSG_DRVR_PRES << 12))
2777
2778#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2779 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2780 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2781 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2782 (TG3_GPIO_MSG_NEED_VAUX << 12))
2783
2784static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2785{
2786 u32 status, shift;
2787
Joe Perches41535772013-02-16 11:20:04 +00002788 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2789 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002790 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2791 else
2792 status = tr32(TG3_CPMU_DRV_STATUS);
2793
2794 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2795 status &= ~(TG3_GPIO_MSG_MASK << shift);
2796 status |= (newstat << shift);
2797
Joe Perches41535772013-02-16 11:20:04 +00002798 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2799 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002800 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2801 else
2802 tw32(TG3_CPMU_DRV_STATUS, status);
2803
2804 return status >> TG3_APE_GPIO_MSG_SHIFT;
2805}
2806
Matt Carlson520b2752011-06-13 13:39:02 +00002807static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2808{
2809 if (!tg3_flag(tp, IS_NIC))
2810 return 0;
2811
Joe Perches41535772013-02-16 11:20:04 +00002812 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2813 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2814 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002815 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2816 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002817
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002818 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2819
2820 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2821 TG3_GRC_LCLCTL_PWRSW_DELAY);
2822
2823 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2824 } else {
2825 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2826 TG3_GRC_LCLCTL_PWRSW_DELAY);
2827 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002828
Matt Carlson520b2752011-06-13 13:39:02 +00002829 return 0;
2830}
2831
2832static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2833{
2834 u32 grc_local_ctrl;
2835
2836 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002837 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2838 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002839 return;
2840
2841 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2842
2843 tw32_wait_f(GRC_LOCAL_CTRL,
2844 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2845 TG3_GRC_LCLCTL_PWRSW_DELAY);
2846
2847 tw32_wait_f(GRC_LOCAL_CTRL,
2848 grc_local_ctrl,
2849 TG3_GRC_LCLCTL_PWRSW_DELAY);
2850
2851 tw32_wait_f(GRC_LOCAL_CTRL,
2852 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2853 TG3_GRC_LCLCTL_PWRSW_DELAY);
2854}
2855
2856static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2857{
2858 if (!tg3_flag(tp, IS_NIC))
2859 return;
2860
Joe Perches41535772013-02-16 11:20:04 +00002861 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2862 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002863 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2864 (GRC_LCLCTRL_GPIO_OE0 |
2865 GRC_LCLCTRL_GPIO_OE1 |
2866 GRC_LCLCTRL_GPIO_OE2 |
2867 GRC_LCLCTRL_GPIO_OUTPUT0 |
2868 GRC_LCLCTRL_GPIO_OUTPUT1),
2869 TG3_GRC_LCLCTL_PWRSW_DELAY);
2870 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2871 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2872 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2873 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2874 GRC_LCLCTRL_GPIO_OE1 |
2875 GRC_LCLCTRL_GPIO_OE2 |
2876 GRC_LCLCTRL_GPIO_OUTPUT0 |
2877 GRC_LCLCTRL_GPIO_OUTPUT1 |
2878 tp->grc_local_ctrl;
2879 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2880 TG3_GRC_LCLCTL_PWRSW_DELAY);
2881
2882 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2883 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2884 TG3_GRC_LCLCTL_PWRSW_DELAY);
2885
2886 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2887 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2888 TG3_GRC_LCLCTL_PWRSW_DELAY);
2889 } else {
2890 u32 no_gpio2;
2891 u32 grc_local_ctrl = 0;
2892
2893 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002894 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002895 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2896 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2897 grc_local_ctrl,
2898 TG3_GRC_LCLCTL_PWRSW_DELAY);
2899 }
2900
2901 /* On 5753 and variants, GPIO2 cannot be used. */
2902 no_gpio2 = tp->nic_sram_data_cfg &
2903 NIC_SRAM_DATA_CFG_NO_GPIO2;
2904
2905 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2906 GRC_LCLCTRL_GPIO_OE1 |
2907 GRC_LCLCTRL_GPIO_OE2 |
2908 GRC_LCLCTRL_GPIO_OUTPUT1 |
2909 GRC_LCLCTRL_GPIO_OUTPUT2;
2910 if (no_gpio2) {
2911 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2912 GRC_LCLCTRL_GPIO_OUTPUT2);
2913 }
2914 tw32_wait_f(GRC_LOCAL_CTRL,
2915 tp->grc_local_ctrl | grc_local_ctrl,
2916 TG3_GRC_LCLCTL_PWRSW_DELAY);
2917
2918 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2919
2920 tw32_wait_f(GRC_LOCAL_CTRL,
2921 tp->grc_local_ctrl | grc_local_ctrl,
2922 TG3_GRC_LCLCTL_PWRSW_DELAY);
2923
2924 if (!no_gpio2) {
2925 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2926 tw32_wait_f(GRC_LOCAL_CTRL,
2927 tp->grc_local_ctrl | grc_local_ctrl,
2928 TG3_GRC_LCLCTL_PWRSW_DELAY);
2929 }
2930 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002931}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002932
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002933static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002934{
2935 u32 msg = 0;
2936
2937 /* Serialize power state transitions */
2938 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2939 return;
2940
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002941 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002942 msg = TG3_GPIO_MSG_NEED_VAUX;
2943
2944 msg = tg3_set_function_status(tp, msg);
2945
2946 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2947 goto done;
2948
2949 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2950 tg3_pwrsrc_switch_to_vaux(tp);
2951 else
2952 tg3_pwrsrc_die_with_vmain(tp);
2953
2954done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002955 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002956}
2957
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002958static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Matt Carlson683644b2011-03-09 16:58:23 +00002960 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Matt Carlson334355a2010-01-20 16:58:10 +00002962 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002963 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return;
2965
Joe Perches41535772013-02-16 11:20:04 +00002966 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2967 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2968 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002969 tg3_frob_aux_power_5717(tp, include_wol ?
2970 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002971 return;
2972 }
2973
2974 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002975 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002977 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002978
Michael Chanbc1c7562006-03-20 17:48:03 -08002979 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002980 if (dev_peer) {
2981 struct tg3 *tp_peer = netdev_priv(dev_peer);
2982
Joe Perches63c3a662011-04-26 08:12:10 +00002983 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002984 return;
2985
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002986 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002987 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002988 need_vaux = true;
2989 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002992 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2993 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002994 need_vaux = true;
2995
Matt Carlson520b2752011-06-13 13:39:02 +00002996 if (need_vaux)
2997 tg3_pwrsrc_switch_to_vaux(tp);
2998 else
2999 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000}
3001
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003002static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3003{
3004 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3005 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003006 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003007 if (speed != SPEED_10)
3008 return 1;
3009 } else if (speed == SPEED_10)
3010 return 1;
3011
3012 return 0;
3013}
3014
Nithin Sujir44f3b502013-05-13 11:04:15 +00003015static bool tg3_phy_power_bug(struct tg3 *tp)
3016{
3017 switch (tg3_asic_rev(tp)) {
3018 case ASIC_REV_5700:
3019 case ASIC_REV_5704:
3020 return true;
3021 case ASIC_REV_5780:
3022 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3023 return true;
3024 return false;
3025 case ASIC_REV_5717:
3026 if (!tp->pci_fn)
3027 return true;
3028 return false;
3029 case ASIC_REV_5719:
3030 case ASIC_REV_5720:
3031 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3032 !tp->pci_fn)
3033 return true;
3034 return false;
3035 }
3036
3037 return false;
3038}
3039
Nithin Sujir989038e2013-08-30 17:01:36 -07003040static bool tg3_phy_led_bug(struct tg3 *tp)
3041{
3042 switch (tg3_asic_rev(tp)) {
3043 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003044 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003045 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3046 !tp->pci_fn)
3047 return true;
3048 return false;
3049 }
3050
3051 return false;
3052}
3053
Matt Carlson0a459aa2008-11-03 16:54:15 -08003054static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003055{
Matt Carlsonce057f02007-11-12 21:08:03 -08003056 u32 val;
3057
Nithin Sujir942d1af2013-04-09 08:48:07 +00003058 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3059 return;
3060
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003061 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003062 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003063 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3064 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3065
3066 sg_dig_ctrl |=
3067 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3068 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3069 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3070 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003071 return;
Michael Chan51297242007-02-13 12:17:57 -08003072 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003073
Joe Perches41535772013-02-16 11:20:04 +00003074 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003075 tg3_bmcr_reset(tp);
3076 val = tr32(GRC_MISC_CFG);
3077 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3078 udelay(40);
3079 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003080 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003081 u32 phytest;
3082 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3083 u32 phy;
3084
3085 tg3_writephy(tp, MII_ADVERTISE, 0);
3086 tg3_writephy(tp, MII_BMCR,
3087 BMCR_ANENABLE | BMCR_ANRESTART);
3088
3089 tg3_writephy(tp, MII_TG3_FET_TEST,
3090 phytest | MII_TG3_FET_SHADOW_EN);
3091 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3092 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3093 tg3_writephy(tp,
3094 MII_TG3_FET_SHDW_AUXMODE4,
3095 phy);
3096 }
3097 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3098 }
3099 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003100 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003101 if (!tg3_phy_led_bug(tp))
3102 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3103 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003104
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003105 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3106 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3107 MII_TG3_AUXCTL_PCTL_VREG_11V;
3108 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003109 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003110
Michael Chan15c3b692006-03-22 01:06:52 -08003111 /* The PHY should not be powered down on some chips because
3112 * of bugs.
3113 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003114 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003115 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003116
Joe Perches41535772013-02-16 11:20:04 +00003117 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3118 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003119 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3120 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3121 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3122 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3123 }
3124
Michael Chan15c3b692006-03-22 01:06:52 -08003125 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3126}
3127
Matt Carlson3f007892008-11-03 16:51:36 -08003128/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003129static int tg3_nvram_lock(struct tg3 *tp)
3130{
Joe Perches63c3a662011-04-26 08:12:10 +00003131 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003132 int i;
3133
3134 if (tp->nvram_lock_cnt == 0) {
3135 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3136 for (i = 0; i < 8000; i++) {
3137 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3138 break;
3139 udelay(20);
3140 }
3141 if (i == 8000) {
3142 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3143 return -ENODEV;
3144 }
3145 }
3146 tp->nvram_lock_cnt++;
3147 }
3148 return 0;
3149}
3150
3151/* tp->lock is held. */
3152static void tg3_nvram_unlock(struct tg3 *tp)
3153{
Joe Perches63c3a662011-04-26 08:12:10 +00003154 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003155 if (tp->nvram_lock_cnt > 0)
3156 tp->nvram_lock_cnt--;
3157 if (tp->nvram_lock_cnt == 0)
3158 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3159 }
3160}
3161
3162/* tp->lock is held. */
3163static void tg3_enable_nvram_access(struct tg3 *tp)
3164{
Joe Perches63c3a662011-04-26 08:12:10 +00003165 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003166 u32 nvaccess = tr32(NVRAM_ACCESS);
3167
3168 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3169 }
3170}
3171
3172/* tp->lock is held. */
3173static void tg3_disable_nvram_access(struct tg3 *tp)
3174{
Joe Perches63c3a662011-04-26 08:12:10 +00003175 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003176 u32 nvaccess = tr32(NVRAM_ACCESS);
3177
3178 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3179 }
3180}
3181
3182static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3183 u32 offset, u32 *val)
3184{
3185 u32 tmp;
3186 int i;
3187
3188 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3189 return -EINVAL;
3190
3191 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3192 EEPROM_ADDR_DEVID_MASK |
3193 EEPROM_ADDR_READ);
3194 tw32(GRC_EEPROM_ADDR,
3195 tmp |
3196 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3197 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3198 EEPROM_ADDR_ADDR_MASK) |
3199 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3200
3201 for (i = 0; i < 1000; i++) {
3202 tmp = tr32(GRC_EEPROM_ADDR);
3203
3204 if (tmp & EEPROM_ADDR_COMPLETE)
3205 break;
3206 msleep(1);
3207 }
3208 if (!(tmp & EEPROM_ADDR_COMPLETE))
3209 return -EBUSY;
3210
Matt Carlson62cedd12009-04-20 14:52:29 -07003211 tmp = tr32(GRC_EEPROM_DATA);
3212
3213 /*
3214 * The data will always be opposite the native endian
3215 * format. Perform a blind byteswap to compensate.
3216 */
3217 *val = swab32(tmp);
3218
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003219 return 0;
3220}
3221
3222#define NVRAM_CMD_TIMEOUT 10000
3223
3224static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3225{
3226 int i;
3227
3228 tw32(NVRAM_CMD, nvram_cmd);
3229 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3230 udelay(10);
3231 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3232 udelay(10);
3233 break;
3234 }
3235 }
3236
3237 if (i == NVRAM_CMD_TIMEOUT)
3238 return -EBUSY;
3239
3240 return 0;
3241}
3242
3243static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3244{
Joe Perches63c3a662011-04-26 08:12:10 +00003245 if (tg3_flag(tp, NVRAM) &&
3246 tg3_flag(tp, NVRAM_BUFFERED) &&
3247 tg3_flag(tp, FLASH) &&
3248 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003249 (tp->nvram_jedecnum == JEDEC_ATMEL))
3250
3251 addr = ((addr / tp->nvram_pagesize) <<
3252 ATMEL_AT45DB0X1B_PAGE_POS) +
3253 (addr % tp->nvram_pagesize);
3254
3255 return addr;
3256}
3257
3258static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3259{
Joe Perches63c3a662011-04-26 08:12:10 +00003260 if (tg3_flag(tp, NVRAM) &&
3261 tg3_flag(tp, NVRAM_BUFFERED) &&
3262 tg3_flag(tp, FLASH) &&
3263 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003264 (tp->nvram_jedecnum == JEDEC_ATMEL))
3265
3266 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3267 tp->nvram_pagesize) +
3268 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3269
3270 return addr;
3271}
3272
Matt Carlsone4f34112009-02-25 14:25:00 +00003273/* NOTE: Data read in from NVRAM is byteswapped according to
3274 * the byteswapping settings for all other register accesses.
3275 * tg3 devices are BE devices, so on a BE machine, the data
3276 * returned will be exactly as it is seen in NVRAM. On a LE
3277 * machine, the 32-bit value will be byteswapped.
3278 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003279static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3280{
3281 int ret;
3282
Joe Perches63c3a662011-04-26 08:12:10 +00003283 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003284 return tg3_nvram_read_using_eeprom(tp, offset, val);
3285
3286 offset = tg3_nvram_phys_addr(tp, offset);
3287
3288 if (offset > NVRAM_ADDR_MSK)
3289 return -EINVAL;
3290
3291 ret = tg3_nvram_lock(tp);
3292 if (ret)
3293 return ret;
3294
3295 tg3_enable_nvram_access(tp);
3296
3297 tw32(NVRAM_ADDR, offset);
3298 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3299 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3300
3301 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003302 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003303
3304 tg3_disable_nvram_access(tp);
3305
3306 tg3_nvram_unlock(tp);
3307
3308 return ret;
3309}
3310
Matt Carlsona9dc5292009-02-25 14:25:30 +00003311/* Ensures NVRAM data is in bytestream format. */
3312static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003313{
3314 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003315 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003316 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003317 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003318 return res;
3319}
3320
Matt Carlsondbe9b922012-02-13 10:20:09 +00003321static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3322 u32 offset, u32 len, u8 *buf)
3323{
3324 int i, j, rc = 0;
3325 u32 val;
3326
3327 for (i = 0; i < len; i += 4) {
3328 u32 addr;
3329 __be32 data;
3330
3331 addr = offset + i;
3332
3333 memcpy(&data, buf + i, 4);
3334
3335 /*
3336 * The SEEPROM interface expects the data to always be opposite
3337 * the native endian format. We accomplish this by reversing
3338 * all the operations that would have been performed on the
3339 * data from a call to tg3_nvram_read_be32().
3340 */
3341 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3342
3343 val = tr32(GRC_EEPROM_ADDR);
3344 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3345
3346 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3347 EEPROM_ADDR_READ);
3348 tw32(GRC_EEPROM_ADDR, val |
3349 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3350 (addr & EEPROM_ADDR_ADDR_MASK) |
3351 EEPROM_ADDR_START |
3352 EEPROM_ADDR_WRITE);
3353
3354 for (j = 0; j < 1000; j++) {
3355 val = tr32(GRC_EEPROM_ADDR);
3356
3357 if (val & EEPROM_ADDR_COMPLETE)
3358 break;
3359 msleep(1);
3360 }
3361 if (!(val & EEPROM_ADDR_COMPLETE)) {
3362 rc = -EBUSY;
3363 break;
3364 }
3365 }
3366
3367 return rc;
3368}
3369
3370/* offset and length are dword aligned */
3371static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3372 u8 *buf)
3373{
3374 int ret = 0;
3375 u32 pagesize = tp->nvram_pagesize;
3376 u32 pagemask = pagesize - 1;
3377 u32 nvram_cmd;
3378 u8 *tmp;
3379
3380 tmp = kmalloc(pagesize, GFP_KERNEL);
3381 if (tmp == NULL)
3382 return -ENOMEM;
3383
3384 while (len) {
3385 int j;
3386 u32 phy_addr, page_off, size;
3387
3388 phy_addr = offset & ~pagemask;
3389
3390 for (j = 0; j < pagesize; j += 4) {
3391 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3392 (__be32 *) (tmp + j));
3393 if (ret)
3394 break;
3395 }
3396 if (ret)
3397 break;
3398
3399 page_off = offset & pagemask;
3400 size = pagesize;
3401 if (len < size)
3402 size = len;
3403
3404 len -= size;
3405
3406 memcpy(tmp + page_off, buf, size);
3407
3408 offset = offset + (pagesize - page_off);
3409
3410 tg3_enable_nvram_access(tp);
3411
3412 /*
3413 * Before we can erase the flash page, we need
3414 * to issue a special "write enable" command.
3415 */
3416 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3417
3418 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3419 break;
3420
3421 /* Erase the target page */
3422 tw32(NVRAM_ADDR, phy_addr);
3423
3424 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3425 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3426
3427 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3428 break;
3429
3430 /* Issue another write enable to start the write. */
3431 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3432
3433 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3434 break;
3435
3436 for (j = 0; j < pagesize; j += 4) {
3437 __be32 data;
3438
3439 data = *((__be32 *) (tmp + j));
3440
3441 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3442
3443 tw32(NVRAM_ADDR, phy_addr + j);
3444
3445 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3446 NVRAM_CMD_WR;
3447
3448 if (j == 0)
3449 nvram_cmd |= NVRAM_CMD_FIRST;
3450 else if (j == (pagesize - 4))
3451 nvram_cmd |= NVRAM_CMD_LAST;
3452
3453 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3454 if (ret)
3455 break;
3456 }
3457 if (ret)
3458 break;
3459 }
3460
3461 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3462 tg3_nvram_exec_cmd(tp, nvram_cmd);
3463
3464 kfree(tmp);
3465
3466 return ret;
3467}
3468
3469/* offset and length are dword aligned */
3470static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3471 u8 *buf)
3472{
3473 int i, ret = 0;
3474
3475 for (i = 0; i < len; i += 4, offset += 4) {
3476 u32 page_off, phy_addr, nvram_cmd;
3477 __be32 data;
3478
3479 memcpy(&data, buf + i, 4);
3480 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3481
3482 page_off = offset % tp->nvram_pagesize;
3483
3484 phy_addr = tg3_nvram_phys_addr(tp, offset);
3485
Matt Carlsondbe9b922012-02-13 10:20:09 +00003486 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3487
3488 if (page_off == 0 || i == 0)
3489 nvram_cmd |= NVRAM_CMD_FIRST;
3490 if (page_off == (tp->nvram_pagesize - 4))
3491 nvram_cmd |= NVRAM_CMD_LAST;
3492
3493 if (i == (len - 4))
3494 nvram_cmd |= NVRAM_CMD_LAST;
3495
Matt Carlson42278222012-02-13 15:20:11 +00003496 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3497 !tg3_flag(tp, FLASH) ||
3498 !tg3_flag(tp, 57765_PLUS))
3499 tw32(NVRAM_ADDR, phy_addr);
3500
Joe Perches41535772013-02-16 11:20:04 +00003501 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003502 !tg3_flag(tp, 5755_PLUS) &&
3503 (tp->nvram_jedecnum == JEDEC_ST) &&
3504 (nvram_cmd & NVRAM_CMD_FIRST)) {
3505 u32 cmd;
3506
3507 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3508 ret = tg3_nvram_exec_cmd(tp, cmd);
3509 if (ret)
3510 break;
3511 }
3512 if (!tg3_flag(tp, FLASH)) {
3513 /* We always do complete word writes to eeprom. */
3514 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3515 }
3516
3517 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3518 if (ret)
3519 break;
3520 }
3521 return ret;
3522}
3523
3524/* offset and length are dword aligned */
3525static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3526{
3527 int ret;
3528
3529 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3530 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3531 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3532 udelay(40);
3533 }
3534
3535 if (!tg3_flag(tp, NVRAM)) {
3536 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3537 } else {
3538 u32 grc_mode;
3539
3540 ret = tg3_nvram_lock(tp);
3541 if (ret)
3542 return ret;
3543
3544 tg3_enable_nvram_access(tp);
3545 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3546 tw32(NVRAM_WRITE1, 0x406);
3547
3548 grc_mode = tr32(GRC_MODE);
3549 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3550
3551 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3552 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3553 buf);
3554 } else {
3555 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3556 buf);
3557 }
3558
3559 grc_mode = tr32(GRC_MODE);
3560 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3561
3562 tg3_disable_nvram_access(tp);
3563 tg3_nvram_unlock(tp);
3564 }
3565
3566 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3567 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3568 udelay(40);
3569 }
3570
3571 return ret;
3572}
3573
Matt Carlson997b4f12011-08-31 11:44:53 +00003574#define RX_CPU_SCRATCH_BASE 0x30000
3575#define RX_CPU_SCRATCH_SIZE 0x04000
3576#define TX_CPU_SCRATCH_BASE 0x34000
3577#define TX_CPU_SCRATCH_SIZE 0x04000
3578
3579/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003580static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003581{
3582 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003583 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003584
Nithin Sujir837c45b2013-03-06 17:02:30 +00003585 for (i = 0; i < iters; i++) {
3586 tw32(cpu_base + CPU_STATE, 0xffffffff);
3587 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3588 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3589 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003590 if (pci_channel_offline(tp->pdev))
3591 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003592 }
3593
3594 return (i == iters) ? -EBUSY : 0;
3595}
3596
3597/* tp->lock is held. */
3598static int tg3_rxcpu_pause(struct tg3 *tp)
3599{
3600 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3601
3602 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3603 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3604 udelay(10);
3605
3606 return rc;
3607}
3608
3609/* tp->lock is held. */
3610static int tg3_txcpu_pause(struct tg3 *tp)
3611{
3612 return tg3_pause_cpu(tp, TX_CPU_BASE);
3613}
3614
3615/* tp->lock is held. */
3616static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3617{
3618 tw32(cpu_base + CPU_STATE, 0xffffffff);
3619 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3620}
3621
3622/* tp->lock is held. */
3623static void tg3_rxcpu_resume(struct tg3 *tp)
3624{
3625 tg3_resume_cpu(tp, RX_CPU_BASE);
3626}
3627
3628/* tp->lock is held. */
3629static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3630{
3631 int rc;
3632
3633 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003634
Joe Perches41535772013-02-16 11:20:04 +00003635 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003636 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3637
3638 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3639 return 0;
3640 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003641 if (cpu_base == RX_CPU_BASE) {
3642 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003643 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003644 /*
3645 * There is only an Rx CPU for the 5750 derivative in the
3646 * BCM4785.
3647 */
3648 if (tg3_flag(tp, IS_SSB_CORE))
3649 return 0;
3650
Nithin Sujir837c45b2013-03-06 17:02:30 +00003651 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003652 }
3653
Nithin Sujir837c45b2013-03-06 17:02:30 +00003654 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003655 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003656 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003657 return -ENODEV;
3658 }
3659
3660 /* Clear firmware's nvram arbitration. */
3661 if (tg3_flag(tp, NVRAM))
3662 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3663 return 0;
3664}
3665
Nithin Sujir31f11a92013-03-06 17:02:33 +00003666static int tg3_fw_data_len(struct tg3 *tp,
3667 const struct tg3_firmware_hdr *fw_hdr)
3668{
3669 int fw_len;
3670
3671 /* Non fragmented firmware have one firmware header followed by a
3672 * contiguous chunk of data to be written. The length field in that
3673 * header is not the length of data to be written but the complete
3674 * length of the bss. The data length is determined based on
3675 * tp->fw->size minus headers.
3676 *
3677 * Fragmented firmware have a main header followed by multiple
3678 * fragments. Each fragment is identical to non fragmented firmware
3679 * with a firmware header followed by a contiguous chunk of data. In
3680 * the main header, the length field is unused and set to 0xffffffff.
3681 * In each fragment header the length is the entire size of that
3682 * fragment i.e. fragment data + header length. Data length is
3683 * therefore length field in the header minus TG3_FW_HDR_LEN.
3684 */
3685 if (tp->fw_len == 0xffffffff)
3686 fw_len = be32_to_cpu(fw_hdr->len);
3687 else
3688 fw_len = tp->fw->size;
3689
3690 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3691}
3692
Matt Carlson997b4f12011-08-31 11:44:53 +00003693/* tp->lock is held. */
3694static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3695 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003696 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003697{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003698 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003699 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003700 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003701
3702 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3703 netdev_err(tp->dev,
3704 "%s: Trying to load TX cpu firmware which is 5705\n",
3705 __func__);
3706 return -EINVAL;
3707 }
3708
Nithin Sujirc4dab502013-03-06 17:02:34 +00003709 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003710 write_op = tg3_write_mem;
3711 else
3712 write_op = tg3_write_indirect_reg32;
3713
Nithin Sujirc4dab502013-03-06 17:02:34 +00003714 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3715 /* It is possible that bootcode is still loading at this point.
3716 * Get the nvram lock first before halting the cpu.
3717 */
3718 int lock_err = tg3_nvram_lock(tp);
3719 err = tg3_halt_cpu(tp, cpu_base);
3720 if (!lock_err)
3721 tg3_nvram_unlock(tp);
3722 if (err)
3723 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003724
Nithin Sujirc4dab502013-03-06 17:02:34 +00003725 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3726 write_op(tp, cpu_scratch_base + i, 0);
3727 tw32(cpu_base + CPU_STATE, 0xffffffff);
3728 tw32(cpu_base + CPU_MODE,
3729 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3730 } else {
3731 /* Subtract additional main header for fragmented firmware and
3732 * advance to the first fragment
3733 */
3734 total_len -= TG3_FW_HDR_LEN;
3735 fw_hdr++;
3736 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003737
Nithin Sujir31f11a92013-03-06 17:02:33 +00003738 do {
3739 u32 *fw_data = (u32 *)(fw_hdr + 1);
3740 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3741 write_op(tp, cpu_scratch_base +
3742 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3743 (i * sizeof(u32)),
3744 be32_to_cpu(fw_data[i]));
3745
3746 total_len -= be32_to_cpu(fw_hdr->len);
3747
3748 /* Advance to next fragment */
3749 fw_hdr = (struct tg3_firmware_hdr *)
3750 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3751 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003752
3753 err = 0;
3754
3755out:
3756 return err;
3757}
3758
3759/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003760static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3761{
3762 int i;
3763 const int iters = 5;
3764
3765 tw32(cpu_base + CPU_STATE, 0xffffffff);
3766 tw32_f(cpu_base + CPU_PC, pc);
3767
3768 for (i = 0; i < iters; i++) {
3769 if (tr32(cpu_base + CPU_PC) == pc)
3770 break;
3771 tw32(cpu_base + CPU_STATE, 0xffffffff);
3772 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3773 tw32_f(cpu_base + CPU_PC, pc);
3774 udelay(1000);
3775 }
3776
3777 return (i == iters) ? -EBUSY : 0;
3778}
3779
3780/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003781static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3782{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003783 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003784 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003785
Nithin Sujir77997ea2013-03-06 17:02:32 +00003786 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003787
3788 /* Firmware blob starts with version numbers, followed by
3789 start address and length. We are setting complete length.
3790 length = end_address_of_bss - start_address_of_text.
3791 Remainder is the blob to be loaded contiguously
3792 from start address. */
3793
Matt Carlson997b4f12011-08-31 11:44:53 +00003794 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3795 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003796 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003797 if (err)
3798 return err;
3799
3800 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3801 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003802 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003803 if (err)
3804 return err;
3805
3806 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003807 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3808 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003809 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003810 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3811 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003812 tr32(RX_CPU_BASE + CPU_PC),
3813 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003814 return -ENODEV;
3815 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003816
3817 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003818
3819 return 0;
3820}
3821
Nithin Sujirc4dab502013-03-06 17:02:34 +00003822static int tg3_validate_rxcpu_state(struct tg3 *tp)
3823{
3824 const int iters = 1000;
3825 int i;
3826 u32 val;
3827
3828 /* Wait for boot code to complete initialization and enter service
3829 * loop. It is then safe to download service patches
3830 */
3831 for (i = 0; i < iters; i++) {
3832 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3833 break;
3834
3835 udelay(10);
3836 }
3837
3838 if (i == iters) {
3839 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3840 return -EBUSY;
3841 }
3842
3843 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3844 if (val & 0xff) {
3845 netdev_warn(tp->dev,
3846 "Other patches exist. Not downloading EEE patch\n");
3847 return -EEXIST;
3848 }
3849
3850 return 0;
3851}
3852
3853/* tp->lock is held. */
3854static void tg3_load_57766_firmware(struct tg3 *tp)
3855{
3856 struct tg3_firmware_hdr *fw_hdr;
3857
3858 if (!tg3_flag(tp, NO_NVRAM))
3859 return;
3860
3861 if (tg3_validate_rxcpu_state(tp))
3862 return;
3863
3864 if (!tp->fw)
3865 return;
3866
3867 /* This firmware blob has a different format than older firmware
3868 * releases as given below. The main difference is we have fragmented
3869 * data to be written to non-contiguous locations.
3870 *
3871 * In the beginning we have a firmware header identical to other
3872 * firmware which consists of version, base addr and length. The length
3873 * here is unused and set to 0xffffffff.
3874 *
3875 * This is followed by a series of firmware fragments which are
3876 * individually identical to previous firmware. i.e. they have the
3877 * firmware header and followed by data for that fragment. The version
3878 * field of the individual fragment header is unused.
3879 */
3880
3881 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3882 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3883 return;
3884
3885 if (tg3_rxcpu_pause(tp))
3886 return;
3887
3888 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3889 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3890
3891 tg3_rxcpu_resume(tp);
3892}
3893
Matt Carlson997b4f12011-08-31 11:44:53 +00003894/* tp->lock is held. */
3895static int tg3_load_tso_firmware(struct tg3 *tp)
3896{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003897 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003898 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003899 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003900
Matt Carlson1caf13e2013-03-06 17:02:29 +00003901 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003902 return 0;
3903
Nithin Sujir77997ea2013-03-06 17:02:32 +00003904 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003905
3906 /* Firmware blob starts with version numbers, followed by
3907 start address and length. We are setting complete length.
3908 length = end_address_of_bss - start_address_of_text.
3909 Remainder is the blob to be loaded contiguously
3910 from start address. */
3911
Matt Carlson997b4f12011-08-31 11:44:53 +00003912 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003913
Joe Perches41535772013-02-16 11:20:04 +00003914 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003915 cpu_base = RX_CPU_BASE;
3916 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3917 } else {
3918 cpu_base = TX_CPU_BASE;
3919 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3920 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3921 }
3922
3923 err = tg3_load_firmware_cpu(tp, cpu_base,
3924 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003925 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003926 if (err)
3927 return err;
3928
3929 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003930 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3931 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003932 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003933 netdev_err(tp->dev,
3934 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003935 __func__, tr32(cpu_base + CPU_PC),
3936 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003937 return -ENODEV;
3938 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003939
3940 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003941 return 0;
3942}
3943
3944
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003945/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003946static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003947{
3948 u32 addr_high, addr_low;
3949 int i;
3950
3951 addr_high = ((tp->dev->dev_addr[0] << 8) |
3952 tp->dev->dev_addr[1]);
3953 addr_low = ((tp->dev->dev_addr[2] << 24) |
3954 (tp->dev->dev_addr[3] << 16) |
3955 (tp->dev->dev_addr[4] << 8) |
3956 (tp->dev->dev_addr[5] << 0));
3957 for (i = 0; i < 4; i++) {
3958 if (i == 1 && skip_mac_1)
3959 continue;
3960 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3961 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3962 }
3963
Joe Perches41535772013-02-16 11:20:04 +00003964 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3965 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003966 for (i = 0; i < 12; i++) {
3967 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3968 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3969 }
3970 }
3971
3972 addr_high = (tp->dev->dev_addr[0] +
3973 tp->dev->dev_addr[1] +
3974 tp->dev->dev_addr[2] +
3975 tp->dev->dev_addr[3] +
3976 tp->dev->dev_addr[4] +
3977 tp->dev->dev_addr[5]) &
3978 TX_BACKOFF_SEED_MASK;
3979 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3980}
3981
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003982static void tg3_enable_register_access(struct tg3 *tp)
3983{
3984 /*
3985 * Make sure register accesses (indirect or otherwise) will function
3986 * correctly.
3987 */
3988 pci_write_config_dword(tp->pdev,
3989 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3990}
3991
3992static int tg3_power_up(struct tg3 *tp)
3993{
Matt Carlsonbed98292011-07-13 09:27:29 +00003994 int err;
3995
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003996 tg3_enable_register_access(tp);
3997
Matt Carlsonbed98292011-07-13 09:27:29 +00003998 err = pci_set_power_state(tp->pdev, PCI_D0);
3999 if (!err) {
4000 /* Switch out of Vaux if it is a NIC */
4001 tg3_pwrsrc_switch_to_vmain(tp);
4002 } else {
4003 netdev_err(tp->dev, "Transition to D0 failed\n");
4004 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004005
Matt Carlsonbed98292011-07-13 09:27:29 +00004006 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004007}
4008
Joe Perches953c96e2013-04-09 10:18:14 +00004009static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004010
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004011static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
4013 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004014 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004016 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004017
4018 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004019 if (tg3_flag(tp, CLKREQ_BUG))
4020 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4021 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4024 tw32(TG3PCI_MISC_HOST_CTRL,
4025 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4026
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004027 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004028 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004029
Joe Perches63c3a662011-04-26 08:12:10 +00004030 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004031 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004032 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00004033 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004034 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004035 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004036
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00004037 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004038
Matt Carlson80096062010-08-02 11:26:06 +00004039 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004040
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004041 tp->link_config.speed = phydev->speed;
4042 tp->link_config.duplex = phydev->duplex;
4043 tp->link_config.autoneg = phydev->autoneg;
4044 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004045
4046 advertising = ADVERTISED_TP |
4047 ADVERTISED_Pause |
4048 ADVERTISED_Autoneg |
4049 ADVERTISED_10baseT_Half;
4050
Joe Perches63c3a662011-04-26 08:12:10 +00004051 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4052 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004053 advertising |=
4054 ADVERTISED_100baseT_Half |
4055 ADVERTISED_100baseT_Full |
4056 ADVERTISED_10baseT_Full;
4057 else
4058 advertising |= ADVERTISED_10baseT_Full;
4059 }
4060
4061 phydev->advertising = advertising;
4062
4063 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004064
4065 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004066 if (phyid != PHY_ID_BCMAC131) {
4067 phyid &= PHY_BCM_OUI_MASK;
4068 if (phyid == PHY_BCM_OUI_1 ||
4069 phyid == PHY_BCM_OUI_2 ||
4070 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004071 do_low_power = true;
4072 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004073 }
Matt Carlsondd477002008-05-25 23:45:58 -07004074 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004075 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004076
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004077 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004078 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Matt Carlson2855b9f2012-02-13 15:20:14 +00004080 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004081 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 }
4083
Joe Perches41535772013-02-16 11:20:04 +00004084 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004085 u32 val;
4086
4087 val = tr32(GRC_VCPU_EXT_CTRL);
4088 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004089 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004090 int i;
4091 u32 val;
4092
4093 for (i = 0; i < 200; i++) {
4094 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4095 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4096 break;
4097 msleep(1);
4098 }
4099 }
Joe Perches63c3a662011-04-26 08:12:10 +00004100 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004101 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4102 WOL_DRV_STATE_SHUTDOWN |
4103 WOL_DRV_WOL |
4104 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004105
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004106 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 u32 mac_mode;
4108
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004109 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004110 if (do_low_power &&
4111 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4112 tg3_phy_auxctl_write(tp,
4113 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4114 MII_TG3_AUXCTL_PCTL_WOL_EN |
4115 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4116 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004117 udelay(40);
4118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004120 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004121 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004122 else if (tp->phy_flags &
4123 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4124 if (tp->link_config.active_speed == SPEED_1000)
4125 mac_mode = MAC_MODE_PORT_MODE_GMII;
4126 else
4127 mac_mode = MAC_MODE_PORT_MODE_MII;
4128 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004129 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004131 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004132 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004133 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004134 SPEED_100 : SPEED_10;
4135 if (tg3_5700_link_polarity(tp, speed))
4136 mac_mode |= MAC_MODE_LINK_POLARITY;
4137 else
4138 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 } else {
4141 mac_mode = MAC_MODE_PORT_MODE_TBI;
4142 }
4143
Joe Perches63c3a662011-04-26 08:12:10 +00004144 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 tw32(MAC_LED_CTRL, tp->led_ctrl);
4146
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004147 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004148 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4149 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004150 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Joe Perches63c3a662011-04-26 08:12:10 +00004152 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004153 mac_mode |= MAC_MODE_APE_TX_EN |
4154 MAC_MODE_APE_RX_EN |
4155 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 tw32_f(MAC_MODE, mac_mode);
4158 udelay(100);
4159
4160 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4161 udelay(10);
4162 }
4163
Joe Perches63c3a662011-04-26 08:12:10 +00004164 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004165 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4166 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 u32 base_val;
4168
4169 base_val = tp->pci_clock_ctrl;
4170 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4171 CLOCK_CTRL_TXCLK_DISABLE);
4172
Michael Chanb401e9e2005-12-19 16:27:04 -08004173 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4174 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004175 } else if (tg3_flag(tp, 5780_CLASS) ||
4176 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004177 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004178 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004179 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 u32 newbits1, newbits2;
4181
Joe Perches41535772013-02-16 11:20:04 +00004182 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4183 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4185 CLOCK_CTRL_TXCLK_DISABLE |
4186 CLOCK_CTRL_ALTCLK);
4187 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004188 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 newbits1 = CLOCK_CTRL_625_CORE;
4190 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4191 } else {
4192 newbits1 = CLOCK_CTRL_ALTCLK;
4193 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4194 }
4195
Michael Chanb401e9e2005-12-19 16:27:04 -08004196 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4197 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Michael Chanb401e9e2005-12-19 16:27:04 -08004199 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4200 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Joe Perches63c3a662011-04-26 08:12:10 +00004202 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 u32 newbits3;
4204
Joe Perches41535772013-02-16 11:20:04 +00004205 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4206 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4208 CLOCK_CTRL_TXCLK_DISABLE |
4209 CLOCK_CTRL_44MHZ_CORE);
4210 } else {
4211 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4212 }
4213
Michael Chanb401e9e2005-12-19 16:27:04 -08004214 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4215 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
4217 }
4218
Joe Perches63c3a662011-04-26 08:12:10 +00004219 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004220 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004221
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004222 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004225 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004226 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4227 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 u32 val = tr32(0x7d00);
4229
4230 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4231 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004232 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004233 int err;
4234
4235 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004237 if (!err)
4238 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 }
4241
Michael Chanbbadf502006-04-06 21:46:34 -07004242 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4243
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004244 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 return 0;
4247}
4248
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004249static void tg3_power_down(struct tg3 *tp)
4250{
Joe Perches63c3a662011-04-26 08:12:10 +00004251 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004252 pci_set_power_state(tp->pdev, PCI_D3hot);
4253}
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4256{
4257 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4258 case MII_TG3_AUX_STAT_10HALF:
4259 *speed = SPEED_10;
4260 *duplex = DUPLEX_HALF;
4261 break;
4262
4263 case MII_TG3_AUX_STAT_10FULL:
4264 *speed = SPEED_10;
4265 *duplex = DUPLEX_FULL;
4266 break;
4267
4268 case MII_TG3_AUX_STAT_100HALF:
4269 *speed = SPEED_100;
4270 *duplex = DUPLEX_HALF;
4271 break;
4272
4273 case MII_TG3_AUX_STAT_100FULL:
4274 *speed = SPEED_100;
4275 *duplex = DUPLEX_FULL;
4276 break;
4277
4278 case MII_TG3_AUX_STAT_1000HALF:
4279 *speed = SPEED_1000;
4280 *duplex = DUPLEX_HALF;
4281 break;
4282
4283 case MII_TG3_AUX_STAT_1000FULL:
4284 *speed = SPEED_1000;
4285 *duplex = DUPLEX_FULL;
4286 break;
4287
4288 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004289 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004290 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4291 SPEED_10;
4292 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4293 DUPLEX_HALF;
4294 break;
4295 }
Matt Carlsone7405222012-02-13 15:20:16 +00004296 *speed = SPEED_UNKNOWN;
4297 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300}
4301
Matt Carlson42b64a42011-05-19 12:12:49 +00004302static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
Matt Carlson42b64a42011-05-19 12:12:49 +00004304 int err = 0;
4305 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Matt Carlson42b64a42011-05-19 12:12:49 +00004307 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004308 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004309 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Matt Carlson42b64a42011-05-19 12:12:49 +00004311 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4312 if (err)
4313 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Matt Carlson4f272092011-12-14 11:09:57 +00004315 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4316 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004317
Joe Perches41535772013-02-16 11:20:04 +00004318 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4319 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004320 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004321
Matt Carlson4f272092011-12-14 11:09:57 +00004322 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4323 if (err)
4324 goto done;
4325 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004326
Matt Carlson42b64a42011-05-19 12:12:49 +00004327 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4328 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Matt Carlson42b64a42011-05-19 12:12:49 +00004330 tw32(TG3_CPMU_EEE_MODE,
4331 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004332
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004333 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004334 if (!err) {
4335 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004336
Matt Carlsona6b68da2010-12-06 08:28:52 +00004337 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004338 /* Advertise 100-BaseTX EEE ability */
4339 if (advertise & ADVERTISED_100baseT_Full)
4340 val |= MDIO_AN_EEE_ADV_100TX;
4341 /* Advertise 1000-BaseT EEE ability */
4342 if (advertise & ADVERTISED_1000baseT_Full)
4343 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004344
4345 if (!tp->eee.eee_enabled) {
4346 val = 0;
4347 tp->eee.advertised = 0;
4348 } else {
4349 tp->eee.advertised = advertise &
4350 (ADVERTISED_100baseT_Full |
4351 ADVERTISED_1000baseT_Full);
4352 }
4353
Matt Carlson42b64a42011-05-19 12:12:49 +00004354 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004355 if (err)
4356 val = 0;
4357
Joe Perches41535772013-02-16 11:20:04 +00004358 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004359 case ASIC_REV_5717:
4360 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004361 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004362 case ASIC_REV_5719:
4363 /* If we advertised any eee advertisements above... */
4364 if (val)
4365 val = MII_TG3_DSP_TAP26_ALNOKO |
4366 MII_TG3_DSP_TAP26_RMRXSTO |
4367 MII_TG3_DSP_TAP26_OPCSINPT;
4368 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4369 /* Fall through */
4370 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004371 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004372 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4373 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4374 MII_TG3_DSP_CH34TP2_HIBW01);
4375 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004376
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004377 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004378 if (!err)
4379 err = err2;
4380 }
4381
4382done:
4383 return err;
4384}
4385
4386static void tg3_phy_copper_begin(struct tg3 *tp)
4387{
Matt Carlsond13ba512012-02-22 12:35:19 +00004388 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4389 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4390 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004391
Nithin Sujir942d1af2013-04-09 08:48:07 +00004392 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4393 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004394 adv = ADVERTISED_10baseT_Half |
4395 ADVERTISED_10baseT_Full;
4396 if (tg3_flag(tp, WOL_SPEED_100MB))
4397 adv |= ADVERTISED_100baseT_Half |
4398 ADVERTISED_100baseT_Full;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004399 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK)
4400 adv |= ADVERTISED_1000baseT_Half |
4401 ADVERTISED_1000baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004402
Matt Carlsond13ba512012-02-22 12:35:19 +00004403 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004404 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004405 adv = tp->link_config.advertising;
4406 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4407 adv &= ~(ADVERTISED_1000baseT_Half |
4408 ADVERTISED_1000baseT_Full);
4409
4410 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004411 }
4412
Matt Carlsond13ba512012-02-22 12:35:19 +00004413 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004414
Nithin Sujir942d1af2013-04-09 08:48:07 +00004415 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4416 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4417 /* Normally during power down we want to autonegotiate
4418 * the lowest possible speed for WOL. However, to avoid
4419 * link flap, we leave it untouched.
4420 */
4421 return;
4422 }
4423
Matt Carlsond13ba512012-02-22 12:35:19 +00004424 tg3_writephy(tp, MII_BMCR,
4425 BMCR_ANENABLE | BMCR_ANRESTART);
4426 } else {
4427 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 u32 bmcr, orig_bmcr;
4429
4430 tp->link_config.active_speed = tp->link_config.speed;
4431 tp->link_config.active_duplex = tp->link_config.duplex;
4432
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004433 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4434 /* With autoneg disabled, 5715 only links up when the
4435 * advertisement register has the configured speed
4436 * enabled.
4437 */
4438 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4439 }
4440
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 bmcr = 0;
4442 switch (tp->link_config.speed) {
4443 default:
4444 case SPEED_10:
4445 break;
4446
4447 case SPEED_100:
4448 bmcr |= BMCR_SPEED100;
4449 break;
4450
4451 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004452 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
4456 if (tp->link_config.duplex == DUPLEX_FULL)
4457 bmcr |= BMCR_FULLDPLX;
4458
4459 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4460 (bmcr != orig_bmcr)) {
4461 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4462 for (i = 0; i < 1500; i++) {
4463 u32 tmp;
4464
4465 udelay(10);
4466 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4467 tg3_readphy(tp, MII_BMSR, &tmp))
4468 continue;
4469 if (!(tmp & BMSR_LSTATUS)) {
4470 udelay(40);
4471 break;
4472 }
4473 }
4474 tg3_writephy(tp, MII_BMCR, bmcr);
4475 udelay(40);
4476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 }
4478}
4479
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004480static int tg3_phy_pull_config(struct tg3 *tp)
4481{
4482 int err;
4483 u32 val;
4484
4485 err = tg3_readphy(tp, MII_BMCR, &val);
4486 if (err)
4487 goto done;
4488
4489 if (!(val & BMCR_ANENABLE)) {
4490 tp->link_config.autoneg = AUTONEG_DISABLE;
4491 tp->link_config.advertising = 0;
4492 tg3_flag_clear(tp, PAUSE_AUTONEG);
4493
4494 err = -EIO;
4495
4496 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4497 case 0:
4498 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4499 goto done;
4500
4501 tp->link_config.speed = SPEED_10;
4502 break;
4503 case BMCR_SPEED100:
4504 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4505 goto done;
4506
4507 tp->link_config.speed = SPEED_100;
4508 break;
4509 case BMCR_SPEED1000:
4510 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4511 tp->link_config.speed = SPEED_1000;
4512 break;
4513 }
4514 /* Fall through */
4515 default:
4516 goto done;
4517 }
4518
4519 if (val & BMCR_FULLDPLX)
4520 tp->link_config.duplex = DUPLEX_FULL;
4521 else
4522 tp->link_config.duplex = DUPLEX_HALF;
4523
4524 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4525
4526 err = 0;
4527 goto done;
4528 }
4529
4530 tp->link_config.autoneg = AUTONEG_ENABLE;
4531 tp->link_config.advertising = ADVERTISED_Autoneg;
4532 tg3_flag_set(tp, PAUSE_AUTONEG);
4533
4534 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4535 u32 adv;
4536
4537 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4538 if (err)
4539 goto done;
4540
4541 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4542 tp->link_config.advertising |= adv | ADVERTISED_TP;
4543
4544 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4545 } else {
4546 tp->link_config.advertising |= ADVERTISED_FIBRE;
4547 }
4548
4549 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4550 u32 adv;
4551
4552 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4553 err = tg3_readphy(tp, MII_CTRL1000, &val);
4554 if (err)
4555 goto done;
4556
4557 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4558 } else {
4559 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4560 if (err)
4561 goto done;
4562
4563 adv = tg3_decode_flowctrl_1000X(val);
4564 tp->link_config.flowctrl = adv;
4565
4566 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4567 adv = mii_adv_to_ethtool_adv_x(val);
4568 }
4569
4570 tp->link_config.advertising |= adv;
4571 }
4572
4573done:
4574 return err;
4575}
4576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577static int tg3_init_5401phy_dsp(struct tg3 *tp)
4578{
4579 int err;
4580
4581 /* Turn off tap power management. */
4582 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004583 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004585 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4586 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4587 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4588 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4589 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591 udelay(40);
4592
4593 return err;
4594}
4595
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004596static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4597{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004598 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004599
4600 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4601 return true;
4602
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004603 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004604
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004605 if (tp->eee.eee_enabled) {
4606 if (tp->eee.advertised != eee.advertised ||
4607 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4608 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4609 return false;
4610 } else {
4611 /* EEE is disabled but we're advertising */
4612 if (eee.advertised)
4613 return false;
4614 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004615
4616 return true;
4617}
4618
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004619static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004621 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004622
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004623 advertising = tp->link_config.advertising;
4624 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004626 advmsk = ADVERTISE_ALL;
4627 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004628 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004629 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004632 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4633 return false;
4634
4635 if ((*lcladv & advmsk) != tgtadv)
4636 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004637
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004638 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 u32 tg3_ctrl;
4640
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004641 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004642
Matt Carlson221c5632011-06-13 13:39:01 +00004643 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004644 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Matt Carlson3198e072012-02-13 15:20:10 +00004646 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004647 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4648 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004649 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4650 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4651 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4652 } else {
4653 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4654 }
4655
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004656 if (tg3_ctrl != tgtadv)
4657 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004659
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004660 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004661}
4662
Matt Carlson859edb22011-12-08 14:40:16 +00004663static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4664{
4665 u32 lpeth = 0;
4666
4667 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4668 u32 val;
4669
4670 if (tg3_readphy(tp, MII_STAT1000, &val))
4671 return false;
4672
4673 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4674 }
4675
4676 if (tg3_readphy(tp, MII_LPA, rmtadv))
4677 return false;
4678
4679 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4680 tp->link_config.rmt_adv = lpeth;
4681
4682 return true;
4683}
4684
Joe Perches953c96e2013-04-09 10:18:14 +00004685static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004686{
4687 if (curr_link_up != tp->link_up) {
4688 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004689 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004690 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004691 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004692 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4693 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4694 }
4695
4696 tg3_link_report(tp);
4697 return true;
4698 }
4699
4700 return false;
4701}
4702
Michael Chan3310e242013-04-09 08:48:05 +00004703static void tg3_clear_mac_status(struct tg3 *tp)
4704{
4705 tw32(MAC_EVENT, 0);
4706
4707 tw32_f(MAC_STATUS,
4708 MAC_STATUS_SYNC_CHANGED |
4709 MAC_STATUS_CFG_CHANGED |
4710 MAC_STATUS_MI_COMPLETION |
4711 MAC_STATUS_LNKSTATE_CHANGED);
4712 udelay(40);
4713}
4714
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004715static void tg3_setup_eee(struct tg3 *tp)
4716{
4717 u32 val;
4718
4719 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4720 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4721 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4722 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4723
4724 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4725
4726 tw32_f(TG3_CPMU_EEE_CTRL,
4727 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4728
4729 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4730 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4731 TG3_CPMU_EEEMD_LPI_IN_RX |
4732 TG3_CPMU_EEEMD_EEE_ENABLE;
4733
4734 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4735 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4736
4737 if (tg3_flag(tp, ENABLE_APE))
4738 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4739
4740 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4741
4742 tw32_f(TG3_CPMU_EEE_DBTMR1,
4743 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4744 (tp->eee.tx_lpi_timer & 0xffff));
4745
4746 tw32_f(TG3_CPMU_EEE_DBTMR2,
4747 TG3_CPMU_DBTMR2_APE_TX_2047US |
4748 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4749}
4750
Joe Perches953c96e2013-04-09 10:18:14 +00004751static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
Joe Perches953c96e2013-04-09 10:18:14 +00004753 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004754 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004755 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 u16 current_speed;
4757 u8 current_duplex;
4758 int i, err;
4759
Michael Chan3310e242013-04-09 08:48:05 +00004760 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Matt Carlson8ef21422008-05-02 16:47:53 -07004762 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4763 tw32_f(MAC_MI_MODE,
4764 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4765 udelay(80);
4766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004768 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
4770 /* Some third-party PHYs need to be reset on link going
4771 * down.
4772 */
Joe Perches41535772013-02-16 11:20:04 +00004773 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4774 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4775 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004776 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 tg3_readphy(tp, MII_BMSR, &bmsr);
4778 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4779 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004780 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
4782 if (force_reset)
4783 tg3_phy_reset(tp);
4784
Matt Carlson79eb6902010-02-17 15:17:03 +00004785 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 tg3_readphy(tp, MII_BMSR, &bmsr);
4787 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004788 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 bmsr = 0;
4790
4791 if (!(bmsr & BMSR_LSTATUS)) {
4792 err = tg3_init_5401phy_dsp(tp);
4793 if (err)
4794 return err;
4795
4796 tg3_readphy(tp, MII_BMSR, &bmsr);
4797 for (i = 0; i < 1000; i++) {
4798 udelay(10);
4799 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4800 (bmsr & BMSR_LSTATUS)) {
4801 udelay(40);
4802 break;
4803 }
4804 }
4805
Matt Carlson79eb6902010-02-17 15:17:03 +00004806 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4807 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 !(bmsr & BMSR_LSTATUS) &&
4809 tp->link_config.active_speed == SPEED_1000) {
4810 err = tg3_phy_reset(tp);
4811 if (!err)
4812 err = tg3_init_5401phy_dsp(tp);
4813 if (err)
4814 return err;
4815 }
4816 }
Joe Perches41535772013-02-16 11:20:04 +00004817 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4818 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 /* 5701 {A0,B0} CRC bug workaround */
4820 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004821 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4822 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4823 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 }
4825
4826 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004827 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4828 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004830 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004832 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4834
Joe Perches41535772013-02-16 11:20:04 +00004835 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4836 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4838 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4839 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4840 else
4841 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4842 }
4843
Joe Perches953c96e2013-04-09 10:18:14 +00004844 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004845 current_speed = SPEED_UNKNOWN;
4846 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004847 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004848 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004850 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004851 err = tg3_phy_auxctl_read(tp,
4852 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4853 &val);
4854 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004855 tg3_phy_auxctl_write(tp,
4856 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4857 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 goto relink;
4859 }
4860 }
4861
4862 bmsr = 0;
4863 for (i = 0; i < 100; i++) {
4864 tg3_readphy(tp, MII_BMSR, &bmsr);
4865 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4866 (bmsr & BMSR_LSTATUS))
4867 break;
4868 udelay(40);
4869 }
4870
4871 if (bmsr & BMSR_LSTATUS) {
4872 u32 aux_stat, bmcr;
4873
4874 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4875 for (i = 0; i < 2000; i++) {
4876 udelay(10);
4877 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4878 aux_stat)
4879 break;
4880 }
4881
4882 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4883 &current_speed,
4884 &current_duplex);
4885
4886 bmcr = 0;
4887 for (i = 0; i < 200; i++) {
4888 tg3_readphy(tp, MII_BMCR, &bmcr);
4889 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4890 continue;
4891 if (bmcr && bmcr != 0x7fff)
4892 break;
4893 udelay(10);
4894 }
4895
Matt Carlsonef167e22007-12-20 20:10:01 -08004896 lcl_adv = 0;
4897 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
Matt Carlsonef167e22007-12-20 20:10:01 -08004899 tp->link_config.active_speed = current_speed;
4900 tp->link_config.active_duplex = current_duplex;
4901
4902 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004903 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4904
Matt Carlsonef167e22007-12-20 20:10:01 -08004905 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004906 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004907 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004908 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004909 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004910
4911 /* EEE settings changes take effect only after a phy
4912 * reset. If we have skipped a reset due to Link Flap
4913 * Avoidance being enabled, do it now.
4914 */
4915 if (!eee_config_ok &&
4916 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004917 !force_reset) {
4918 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004919 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 } else {
4922 if (!(bmcr & BMCR_ANENABLE) &&
4923 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004924 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004925 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 }
4927 }
4928
Joe Perches953c96e2013-04-09 10:18:14 +00004929 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004930 tp->link_config.active_duplex == DUPLEX_FULL) {
4931 u32 reg, bit;
4932
4933 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4934 reg = MII_TG3_FET_GEN_STAT;
4935 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4936 } else {
4937 reg = MII_TG3_EXT_STAT;
4938 bit = MII_TG3_EXT_STAT_MDIX;
4939 }
4940
4941 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4942 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4943
Matt Carlsonef167e22007-12-20 20:10:01 -08004944 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004949 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 tg3_phy_copper_begin(tp);
4951
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004952 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004953 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004954 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4955 current_speed = SPEED_1000;
4956 current_duplex = DUPLEX_FULL;
4957 tp->link_config.active_speed = current_speed;
4958 tp->link_config.active_duplex = current_duplex;
4959 }
4960
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004961 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004962 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4963 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004964 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 }
4966
4967 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004968 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 if (tp->link_config.active_speed == SPEED_100 ||
4970 tp->link_config.active_speed == SPEED_10)
4971 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4972 else
4973 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004974 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004975 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4976 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4978
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004979 /* In order for the 5750 core in BCM4785 chip to work properly
4980 * in RGMII mode, the Led Control Register must be set up.
4981 */
4982 if (tg3_flag(tp, RGMII_MODE)) {
4983 u32 led_ctrl = tr32(MAC_LED_CTRL);
4984 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4985
4986 if (tp->link_config.active_speed == SPEED_10)
4987 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4988 else if (tp->link_config.active_speed == SPEED_100)
4989 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4990 LED_CTRL_100MBPS_ON);
4991 else if (tp->link_config.active_speed == SPEED_1000)
4992 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4993 LED_CTRL_1000MBPS_ON);
4994
4995 tw32(MAC_LED_CTRL, led_ctrl);
4996 udelay(40);
4997 }
4998
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5000 if (tp->link_config.active_duplex == DUPLEX_HALF)
5001 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5002
Joe Perches41535772013-02-16 11:20:04 +00005003 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005004 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005005 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005007 else
5008 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 }
5010
5011 /* ??? Without this setting Netgear GA302T PHY does not
5012 * ??? send/receive packets...
5013 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005014 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005015 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5017 tw32_f(MAC_MI_MODE, tp->mi_mode);
5018 udelay(80);
5019 }
5020
5021 tw32_f(MAC_MODE, tp->mac_mode);
5022 udelay(40);
5023
Matt Carlson52b02d02010-10-14 10:37:41 +00005024 tg3_phy_eee_adjust(tp, current_link_up);
5025
Joe Perches63c3a662011-04-26 08:12:10 +00005026 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 /* Polled via timer. */
5028 tw32_f(MAC_EVENT, 0);
5029 } else {
5030 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5031 }
5032 udelay(40);
5033
Joe Perches41535772013-02-16 11:20:04 +00005034 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005035 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005037 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 udelay(120);
5039 tw32_f(MAC_STATUS,
5040 (MAC_STATUS_SYNC_CHANGED |
5041 MAC_STATUS_CFG_CHANGED));
5042 udelay(40);
5043 tg3_write_mem(tp,
5044 NIC_SRAM_FIRMWARE_MBOX,
5045 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5046 }
5047
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005048 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005049 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005050 if (tp->link_config.active_speed == SPEED_100 ||
5051 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005052 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5053 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005054 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005055 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5056 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005057 }
5058
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005059 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
5061 return 0;
5062}
5063
5064struct tg3_fiber_aneginfo {
5065 int state;
5066#define ANEG_STATE_UNKNOWN 0
5067#define ANEG_STATE_AN_ENABLE 1
5068#define ANEG_STATE_RESTART_INIT 2
5069#define ANEG_STATE_RESTART 3
5070#define ANEG_STATE_DISABLE_LINK_OK 4
5071#define ANEG_STATE_ABILITY_DETECT_INIT 5
5072#define ANEG_STATE_ABILITY_DETECT 6
5073#define ANEG_STATE_ACK_DETECT_INIT 7
5074#define ANEG_STATE_ACK_DETECT 8
5075#define ANEG_STATE_COMPLETE_ACK_INIT 9
5076#define ANEG_STATE_COMPLETE_ACK 10
5077#define ANEG_STATE_IDLE_DETECT_INIT 11
5078#define ANEG_STATE_IDLE_DETECT 12
5079#define ANEG_STATE_LINK_OK 13
5080#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5081#define ANEG_STATE_NEXT_PAGE_WAIT 15
5082
5083 u32 flags;
5084#define MR_AN_ENABLE 0x00000001
5085#define MR_RESTART_AN 0x00000002
5086#define MR_AN_COMPLETE 0x00000004
5087#define MR_PAGE_RX 0x00000008
5088#define MR_NP_LOADED 0x00000010
5089#define MR_TOGGLE_TX 0x00000020
5090#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5091#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5092#define MR_LP_ADV_SYM_PAUSE 0x00000100
5093#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5094#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5095#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5096#define MR_LP_ADV_NEXT_PAGE 0x00001000
5097#define MR_TOGGLE_RX 0x00002000
5098#define MR_NP_RX 0x00004000
5099
5100#define MR_LINK_OK 0x80000000
5101
5102 unsigned long link_time, cur_time;
5103
5104 u32 ability_match_cfg;
5105 int ability_match_count;
5106
5107 char ability_match, idle_match, ack_match;
5108
5109 u32 txconfig, rxconfig;
5110#define ANEG_CFG_NP 0x00000080
5111#define ANEG_CFG_ACK 0x00000040
5112#define ANEG_CFG_RF2 0x00000020
5113#define ANEG_CFG_RF1 0x00000010
5114#define ANEG_CFG_PS2 0x00000001
5115#define ANEG_CFG_PS1 0x00008000
5116#define ANEG_CFG_HD 0x00004000
5117#define ANEG_CFG_FD 0x00002000
5118#define ANEG_CFG_INVAL 0x00001f06
5119
5120};
5121#define ANEG_OK 0
5122#define ANEG_DONE 1
5123#define ANEG_TIMER_ENAB 2
5124#define ANEG_FAILED -1
5125
5126#define ANEG_STATE_SETTLE_TIME 10000
5127
5128static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5129 struct tg3_fiber_aneginfo *ap)
5130{
Matt Carlson5be73b42007-12-20 20:09:29 -08005131 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 unsigned long delta;
5133 u32 rx_cfg_reg;
5134 int ret;
5135
5136 if (ap->state == ANEG_STATE_UNKNOWN) {
5137 ap->rxconfig = 0;
5138 ap->link_time = 0;
5139 ap->cur_time = 0;
5140 ap->ability_match_cfg = 0;
5141 ap->ability_match_count = 0;
5142 ap->ability_match = 0;
5143 ap->idle_match = 0;
5144 ap->ack_match = 0;
5145 }
5146 ap->cur_time++;
5147
5148 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5149 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5150
5151 if (rx_cfg_reg != ap->ability_match_cfg) {
5152 ap->ability_match_cfg = rx_cfg_reg;
5153 ap->ability_match = 0;
5154 ap->ability_match_count = 0;
5155 } else {
5156 if (++ap->ability_match_count > 1) {
5157 ap->ability_match = 1;
5158 ap->ability_match_cfg = rx_cfg_reg;
5159 }
5160 }
5161 if (rx_cfg_reg & ANEG_CFG_ACK)
5162 ap->ack_match = 1;
5163 else
5164 ap->ack_match = 0;
5165
5166 ap->idle_match = 0;
5167 } else {
5168 ap->idle_match = 1;
5169 ap->ability_match_cfg = 0;
5170 ap->ability_match_count = 0;
5171 ap->ability_match = 0;
5172 ap->ack_match = 0;
5173
5174 rx_cfg_reg = 0;
5175 }
5176
5177 ap->rxconfig = rx_cfg_reg;
5178 ret = ANEG_OK;
5179
Matt Carlson33f401a2010-04-05 10:19:27 +00005180 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 case ANEG_STATE_UNKNOWN:
5182 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5183 ap->state = ANEG_STATE_AN_ENABLE;
5184
5185 /* fallthru */
5186 case ANEG_STATE_AN_ENABLE:
5187 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5188 if (ap->flags & MR_AN_ENABLE) {
5189 ap->link_time = 0;
5190 ap->cur_time = 0;
5191 ap->ability_match_cfg = 0;
5192 ap->ability_match_count = 0;
5193 ap->ability_match = 0;
5194 ap->idle_match = 0;
5195 ap->ack_match = 0;
5196
5197 ap->state = ANEG_STATE_RESTART_INIT;
5198 } else {
5199 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5200 }
5201 break;
5202
5203 case ANEG_STATE_RESTART_INIT:
5204 ap->link_time = ap->cur_time;
5205 ap->flags &= ~(MR_NP_LOADED);
5206 ap->txconfig = 0;
5207 tw32(MAC_TX_AUTO_NEG, 0);
5208 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5209 tw32_f(MAC_MODE, tp->mac_mode);
5210 udelay(40);
5211
5212 ret = ANEG_TIMER_ENAB;
5213 ap->state = ANEG_STATE_RESTART;
5214
5215 /* fallthru */
5216 case ANEG_STATE_RESTART:
5217 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005218 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005220 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 break;
5223
5224 case ANEG_STATE_DISABLE_LINK_OK:
5225 ret = ANEG_DONE;
5226 break;
5227
5228 case ANEG_STATE_ABILITY_DETECT_INIT:
5229 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005230 ap->txconfig = ANEG_CFG_FD;
5231 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5232 if (flowctrl & ADVERTISE_1000XPAUSE)
5233 ap->txconfig |= ANEG_CFG_PS1;
5234 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5235 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5237 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5238 tw32_f(MAC_MODE, tp->mac_mode);
5239 udelay(40);
5240
5241 ap->state = ANEG_STATE_ABILITY_DETECT;
5242 break;
5243
5244 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005245 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 break;
5248
5249 case ANEG_STATE_ACK_DETECT_INIT:
5250 ap->txconfig |= ANEG_CFG_ACK;
5251 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5252 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5253 tw32_f(MAC_MODE, tp->mac_mode);
5254 udelay(40);
5255
5256 ap->state = ANEG_STATE_ACK_DETECT;
5257
5258 /* fallthru */
5259 case ANEG_STATE_ACK_DETECT:
5260 if (ap->ack_match != 0) {
5261 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5262 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5263 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5264 } else {
5265 ap->state = ANEG_STATE_AN_ENABLE;
5266 }
5267 } else if (ap->ability_match != 0 &&
5268 ap->rxconfig == 0) {
5269 ap->state = ANEG_STATE_AN_ENABLE;
5270 }
5271 break;
5272
5273 case ANEG_STATE_COMPLETE_ACK_INIT:
5274 if (ap->rxconfig & ANEG_CFG_INVAL) {
5275 ret = ANEG_FAILED;
5276 break;
5277 }
5278 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5279 MR_LP_ADV_HALF_DUPLEX |
5280 MR_LP_ADV_SYM_PAUSE |
5281 MR_LP_ADV_ASYM_PAUSE |
5282 MR_LP_ADV_REMOTE_FAULT1 |
5283 MR_LP_ADV_REMOTE_FAULT2 |
5284 MR_LP_ADV_NEXT_PAGE |
5285 MR_TOGGLE_RX |
5286 MR_NP_RX);
5287 if (ap->rxconfig & ANEG_CFG_FD)
5288 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5289 if (ap->rxconfig & ANEG_CFG_HD)
5290 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5291 if (ap->rxconfig & ANEG_CFG_PS1)
5292 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5293 if (ap->rxconfig & ANEG_CFG_PS2)
5294 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5295 if (ap->rxconfig & ANEG_CFG_RF1)
5296 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5297 if (ap->rxconfig & ANEG_CFG_RF2)
5298 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5299 if (ap->rxconfig & ANEG_CFG_NP)
5300 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5301
5302 ap->link_time = ap->cur_time;
5303
5304 ap->flags ^= (MR_TOGGLE_TX);
5305 if (ap->rxconfig & 0x0008)
5306 ap->flags |= MR_TOGGLE_RX;
5307 if (ap->rxconfig & ANEG_CFG_NP)
5308 ap->flags |= MR_NP_RX;
5309 ap->flags |= MR_PAGE_RX;
5310
5311 ap->state = ANEG_STATE_COMPLETE_ACK;
5312 ret = ANEG_TIMER_ENAB;
5313 break;
5314
5315 case ANEG_STATE_COMPLETE_ACK:
5316 if (ap->ability_match != 0 &&
5317 ap->rxconfig == 0) {
5318 ap->state = ANEG_STATE_AN_ENABLE;
5319 break;
5320 }
5321 delta = ap->cur_time - ap->link_time;
5322 if (delta > ANEG_STATE_SETTLE_TIME) {
5323 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5324 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5325 } else {
5326 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5327 !(ap->flags & MR_NP_RX)) {
5328 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5329 } else {
5330 ret = ANEG_FAILED;
5331 }
5332 }
5333 }
5334 break;
5335
5336 case ANEG_STATE_IDLE_DETECT_INIT:
5337 ap->link_time = ap->cur_time;
5338 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5339 tw32_f(MAC_MODE, tp->mac_mode);
5340 udelay(40);
5341
5342 ap->state = ANEG_STATE_IDLE_DETECT;
5343 ret = ANEG_TIMER_ENAB;
5344 break;
5345
5346 case ANEG_STATE_IDLE_DETECT:
5347 if (ap->ability_match != 0 &&
5348 ap->rxconfig == 0) {
5349 ap->state = ANEG_STATE_AN_ENABLE;
5350 break;
5351 }
5352 delta = ap->cur_time - ap->link_time;
5353 if (delta > ANEG_STATE_SETTLE_TIME) {
5354 /* XXX another gem from the Broadcom driver :( */
5355 ap->state = ANEG_STATE_LINK_OK;
5356 }
5357 break;
5358
5359 case ANEG_STATE_LINK_OK:
5360 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5361 ret = ANEG_DONE;
5362 break;
5363
5364 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5365 /* ??? unimplemented */
5366 break;
5367
5368 case ANEG_STATE_NEXT_PAGE_WAIT:
5369 /* ??? unimplemented */
5370 break;
5371
5372 default:
5373 ret = ANEG_FAILED;
5374 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 return ret;
5378}
5379
Matt Carlson5be73b42007-12-20 20:09:29 -08005380static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381{
5382 int res = 0;
5383 struct tg3_fiber_aneginfo aninfo;
5384 int status = ANEG_FAILED;
5385 unsigned int tick;
5386 u32 tmp;
5387
5388 tw32_f(MAC_TX_AUTO_NEG, 0);
5389
5390 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5391 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5392 udelay(40);
5393
5394 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5395 udelay(40);
5396
5397 memset(&aninfo, 0, sizeof(aninfo));
5398 aninfo.flags |= MR_AN_ENABLE;
5399 aninfo.state = ANEG_STATE_UNKNOWN;
5400 aninfo.cur_time = 0;
5401 tick = 0;
5402 while (++tick < 195000) {
5403 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5404 if (status == ANEG_DONE || status == ANEG_FAILED)
5405 break;
5406
5407 udelay(1);
5408 }
5409
5410 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5411 tw32_f(MAC_MODE, tp->mac_mode);
5412 udelay(40);
5413
Matt Carlson5be73b42007-12-20 20:09:29 -08005414 *txflags = aninfo.txconfig;
5415 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
5417 if (status == ANEG_DONE &&
5418 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5419 MR_LP_ADV_FULL_DUPLEX)))
5420 res = 1;
5421
5422 return res;
5423}
5424
5425static void tg3_init_bcm8002(struct tg3 *tp)
5426{
5427 u32 mac_status = tr32(MAC_STATUS);
5428 int i;
5429
5430 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005431 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 !(mac_status & MAC_STATUS_PCS_SYNCED))
5433 return;
5434
5435 /* Set PLL lock range. */
5436 tg3_writephy(tp, 0x16, 0x8007);
5437
5438 /* SW reset */
5439 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5440
5441 /* Wait for reset to complete. */
5442 /* XXX schedule_timeout() ... */
5443 for (i = 0; i < 500; i++)
5444 udelay(10);
5445
5446 /* Config mode; select PMA/Ch 1 regs. */
5447 tg3_writephy(tp, 0x10, 0x8411);
5448
5449 /* Enable auto-lock and comdet, select txclk for tx. */
5450 tg3_writephy(tp, 0x11, 0x0a10);
5451
5452 tg3_writephy(tp, 0x18, 0x00a0);
5453 tg3_writephy(tp, 0x16, 0x41ff);
5454
5455 /* Assert and deassert POR. */
5456 tg3_writephy(tp, 0x13, 0x0400);
5457 udelay(40);
5458 tg3_writephy(tp, 0x13, 0x0000);
5459
5460 tg3_writephy(tp, 0x11, 0x0a50);
5461 udelay(40);
5462 tg3_writephy(tp, 0x11, 0x0a10);
5463
5464 /* Wait for signal to stabilize */
5465 /* XXX schedule_timeout() ... */
5466 for (i = 0; i < 15000; i++)
5467 udelay(10);
5468
5469 /* Deselect the channel register so we can read the PHYID
5470 * later.
5471 */
5472 tg3_writephy(tp, 0x10, 0x8011);
5473}
5474
Joe Perches953c96e2013-04-09 10:18:14 +00005475static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005477 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005478 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 u32 sg_dig_ctrl, sg_dig_status;
5480 u32 serdes_cfg, expected_sg_dig_ctrl;
5481 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
5483 serdes_cfg = 0;
5484 expected_sg_dig_ctrl = 0;
5485 workaround = 0;
5486 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005487 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Joe Perches41535772013-02-16 11:20:04 +00005489 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5490 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 workaround = 1;
5492 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5493 port_a = 0;
5494
5495 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5496 /* preserve bits 20-23 for voltage regulator */
5497 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5498 }
5499
5500 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5501
5502 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005503 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 if (workaround) {
5505 u32 val = serdes_cfg;
5506
5507 if (port_a)
5508 val |= 0xc010000;
5509 else
5510 val |= 0x4010000;
5511 tw32_f(MAC_SERDES_CFG, val);
5512 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005513
5514 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 }
5516 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5517 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005518 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 }
5520 goto out;
5521 }
5522
5523 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005524 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Matt Carlson82cd3d12007-12-20 20:09:00 -08005526 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5527 if (flowctrl & ADVERTISE_1000XPAUSE)
5528 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5529 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5530 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005533 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005534 tp->serdes_counter &&
5535 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5536 MAC_STATUS_RCVD_CFG)) ==
5537 MAC_STATUS_PCS_SYNCED)) {
5538 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005539 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005540 goto out;
5541 }
5542restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 if (workaround)
5544 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005545 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 udelay(5);
5547 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5548
Michael Chan3d3ebe72006-09-27 15:59:15 -07005549 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005550 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5552 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005553 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 mac_status = tr32(MAC_STATUS);
5555
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005556 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005558 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Matt Carlson82cd3d12007-12-20 20:09:00 -08005560 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5561 local_adv |= ADVERTISE_1000XPAUSE;
5562 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5563 local_adv |= ADVERTISE_1000XPSE_ASYM;
5564
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005565 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005566 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005567 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005568 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
Matt Carlson859edb22011-12-08 14:40:16 +00005570 tp->link_config.rmt_adv =
5571 mii_adv_to_ethtool_adv_x(remote_adv);
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005574 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005575 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005576 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005577 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005578 if (tp->serdes_counter)
5579 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 else {
5581 if (workaround) {
5582 u32 val = serdes_cfg;
5583
5584 if (port_a)
5585 val |= 0xc010000;
5586 else
5587 val |= 0x4010000;
5588
5589 tw32_f(MAC_SERDES_CFG, val);
5590 }
5591
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005592 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 udelay(40);
5594
5595 /* Link parallel detection - link is up */
5596 /* only if we have PCS_SYNC and not */
5597 /* receiving config code words */
5598 mac_status = tr32(MAC_STATUS);
5599 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5600 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5601 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005602 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005603 tp->phy_flags |=
5604 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005605 tp->serdes_counter =
5606 SERDES_PARALLEL_DET_TIMEOUT;
5607 } else
5608 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 }
5610 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005611 } else {
5612 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005613 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 }
5615
5616out:
5617 return current_link_up;
5618}
5619
Joe Perches953c96e2013-04-09 10:18:14 +00005620static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
Joe Perches953c96e2013-04-09 10:18:14 +00005622 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005624 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
5627 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005628 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005630
Matt Carlson5be73b42007-12-20 20:09:29 -08005631 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5632 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
Matt Carlson5be73b42007-12-20 20:09:29 -08005634 if (txflags & ANEG_CFG_PS1)
5635 local_adv |= ADVERTISE_1000XPAUSE;
5636 if (txflags & ANEG_CFG_PS2)
5637 local_adv |= ADVERTISE_1000XPSE_ASYM;
5638
5639 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5640 remote_adv |= LPA_1000XPAUSE;
5641 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5642 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
Matt Carlson859edb22011-12-08 14:40:16 +00005644 tp->link_config.rmt_adv =
5645 mii_adv_to_ethtool_adv_x(remote_adv);
5646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 tg3_setup_flow_control(tp, local_adv, remote_adv);
5648
Joe Perches953c96e2013-04-09 10:18:14 +00005649 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 }
5651 for (i = 0; i < 30; i++) {
5652 udelay(20);
5653 tw32_f(MAC_STATUS,
5654 (MAC_STATUS_SYNC_CHANGED |
5655 MAC_STATUS_CFG_CHANGED));
5656 udelay(40);
5657 if ((tr32(MAC_STATUS) &
5658 (MAC_STATUS_SYNC_CHANGED |
5659 MAC_STATUS_CFG_CHANGED)) == 0)
5660 break;
5661 }
5662
5663 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005664 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5666 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005667 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005669 tg3_setup_flow_control(tp, 0, 0);
5670
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005672 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
5674 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5675 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005676
5677 tw32_f(MAC_MODE, tp->mac_mode);
5678 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
5680
5681out:
5682 return current_link_up;
5683}
5684
Joe Perches953c96e2013-04-09 10:18:14 +00005685static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
5687 u32 orig_pause_cfg;
5688 u16 orig_active_speed;
5689 u8 orig_active_duplex;
5690 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005691 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 int i;
5693
Matt Carlson8d018622007-12-20 20:05:44 -08005694 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 orig_active_speed = tp->link_config.active_speed;
5696 orig_active_duplex = tp->link_config.active_duplex;
5697
Joe Perches63c3a662011-04-26 08:12:10 +00005698 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005699 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005700 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 mac_status = tr32(MAC_STATUS);
5702 mac_status &= (MAC_STATUS_PCS_SYNCED |
5703 MAC_STATUS_SIGNAL_DET |
5704 MAC_STATUS_CFG_CHANGED |
5705 MAC_STATUS_RCVD_CFG);
5706 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5707 MAC_STATUS_SIGNAL_DET)) {
5708 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5709 MAC_STATUS_CFG_CHANGED));
5710 return 0;
5711 }
5712 }
5713
5714 tw32_f(MAC_TX_AUTO_NEG, 0);
5715
5716 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5717 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5718 tw32_f(MAC_MODE, tp->mac_mode);
5719 udelay(40);
5720
Matt Carlson79eb6902010-02-17 15:17:03 +00005721 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 tg3_init_bcm8002(tp);
5723
5724 /* Enable link change event even when serdes polling. */
5725 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5726 udelay(40);
5727
Joe Perches953c96e2013-04-09 10:18:14 +00005728 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005729 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 mac_status = tr32(MAC_STATUS);
5731
Joe Perches63c3a662011-04-26 08:12:10 +00005732 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5734 else
5735 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5736
Matt Carlson898a56f2009-08-28 14:02:40 +00005737 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005739 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
5741 for (i = 0; i < 100; i++) {
5742 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5743 MAC_STATUS_CFG_CHANGED));
5744 udelay(5);
5745 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005746 MAC_STATUS_CFG_CHANGED |
5747 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 break;
5749 }
5750
5751 mac_status = tr32(MAC_STATUS);
5752 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005753 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005754 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5755 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 tw32_f(MAC_MODE, (tp->mac_mode |
5757 MAC_MODE_SEND_CONFIGS));
5758 udelay(1);
5759 tw32_f(MAC_MODE, tp->mac_mode);
5760 }
5761 }
5762
Joe Perches953c96e2013-04-09 10:18:14 +00005763 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 tp->link_config.active_speed = SPEED_1000;
5765 tp->link_config.active_duplex = DUPLEX_FULL;
5766 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5767 LED_CTRL_LNKLED_OVERRIDE |
5768 LED_CTRL_1000MBPS_ON));
5769 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005770 tp->link_config.active_speed = SPEED_UNKNOWN;
5771 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5773 LED_CTRL_LNKLED_OVERRIDE |
5774 LED_CTRL_TRAFFIC_OVERRIDE));
5775 }
5776
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005777 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005778 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 if (orig_pause_cfg != now_pause_cfg ||
5780 orig_active_speed != tp->link_config.active_speed ||
5781 orig_active_duplex != tp->link_config.active_duplex)
5782 tg3_link_report(tp);
5783 }
5784
5785 return 0;
5786}
5787
Joe Perches953c96e2013-04-09 10:18:14 +00005788static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005789{
Joe Perches953c96e2013-04-09 10:18:14 +00005790 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005791 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005792 u16 current_speed = SPEED_UNKNOWN;
5793 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005794 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005795 u32 local_adv, remote_adv, sgsr;
5796
5797 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5798 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5799 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5800 (sgsr & SERDES_TG3_SGMII_MODE)) {
5801
5802 if (force_reset)
5803 tg3_phy_reset(tp);
5804
5805 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5806
5807 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5808 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5809 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005810 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005811 if (sgsr & SERDES_TG3_SPEED_1000) {
5812 current_speed = SPEED_1000;
5813 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5814 } else if (sgsr & SERDES_TG3_SPEED_100) {
5815 current_speed = SPEED_100;
5816 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5817 } else {
5818 current_speed = SPEED_10;
5819 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5820 }
5821
5822 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5823 current_duplex = DUPLEX_FULL;
5824 else
5825 current_duplex = DUPLEX_HALF;
5826 }
5827
5828 tw32_f(MAC_MODE, tp->mac_mode);
5829 udelay(40);
5830
5831 tg3_clear_mac_status(tp);
5832
5833 goto fiber_setup_done;
5834 }
Michael Chan747e8f82005-07-25 12:33:22 -07005835
5836 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5837 tw32_f(MAC_MODE, tp->mac_mode);
5838 udelay(40);
5839
Michael Chan3310e242013-04-09 08:48:05 +00005840 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005841
5842 if (force_reset)
5843 tg3_phy_reset(tp);
5844
Matt Carlson859edb22011-12-08 14:40:16 +00005845 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005846
5847 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5848 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005849 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005850 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5851 bmsr |= BMSR_LSTATUS;
5852 else
5853 bmsr &= ~BMSR_LSTATUS;
5854 }
Michael Chan747e8f82005-07-25 12:33:22 -07005855
5856 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5857
5858 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005859 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005860 /* do nothing, just check for link up at the end */
5861 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005862 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005863
5864 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005865 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5866 ADVERTISE_1000XPAUSE |
5867 ADVERTISE_1000XPSE_ASYM |
5868 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005869
Matt Carlson28011cf2011-11-16 18:36:59 -05005870 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005871 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005872
Matt Carlson28011cf2011-11-16 18:36:59 -05005873 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5874 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005875 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5876 tg3_writephy(tp, MII_BMCR, bmcr);
5877
5878 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005879 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005880 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005881
5882 return err;
5883 }
5884 } else {
5885 u32 new_bmcr;
5886
5887 bmcr &= ~BMCR_SPEED1000;
5888 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5889
5890 if (tp->link_config.duplex == DUPLEX_FULL)
5891 new_bmcr |= BMCR_FULLDPLX;
5892
5893 if (new_bmcr != bmcr) {
5894 /* BMCR_SPEED1000 is a reserved bit that needs
5895 * to be set on write.
5896 */
5897 new_bmcr |= BMCR_SPEED1000;
5898
5899 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005900 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005901 u32 adv;
5902
5903 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5904 adv &= ~(ADVERTISE_1000XFULL |
5905 ADVERTISE_1000XHALF |
5906 ADVERTISE_SLCT);
5907 tg3_writephy(tp, MII_ADVERTISE, adv);
5908 tg3_writephy(tp, MII_BMCR, bmcr |
5909 BMCR_ANRESTART |
5910 BMCR_ANENABLE);
5911 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005912 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005913 }
5914 tg3_writephy(tp, MII_BMCR, new_bmcr);
5915 bmcr = new_bmcr;
5916 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5917 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005918 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005919 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5920 bmsr |= BMSR_LSTATUS;
5921 else
5922 bmsr &= ~BMSR_LSTATUS;
5923 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005924 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005925 }
5926 }
5927
5928 if (bmsr & BMSR_LSTATUS) {
5929 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005930 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005931 if (bmcr & BMCR_FULLDPLX)
5932 current_duplex = DUPLEX_FULL;
5933 else
5934 current_duplex = DUPLEX_HALF;
5935
Matt Carlsonef167e22007-12-20 20:10:01 -08005936 local_adv = 0;
5937 remote_adv = 0;
5938
Michael Chan747e8f82005-07-25 12:33:22 -07005939 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005940 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005941
5942 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5943 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5944 common = local_adv & remote_adv;
5945 if (common & (ADVERTISE_1000XHALF |
5946 ADVERTISE_1000XFULL)) {
5947 if (common & ADVERTISE_1000XFULL)
5948 current_duplex = DUPLEX_FULL;
5949 else
5950 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005951
5952 tp->link_config.rmt_adv =
5953 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005954 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005955 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005956 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005957 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005958 }
Michael Chan747e8f82005-07-25 12:33:22 -07005959 }
5960 }
5961
Michael Chan85730a62013-04-09 08:48:06 +00005962fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005963 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005964 tg3_setup_flow_control(tp, local_adv, remote_adv);
5965
Michael Chan747e8f82005-07-25 12:33:22 -07005966 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5967 if (tp->link_config.active_duplex == DUPLEX_HALF)
5968 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5969
5970 tw32_f(MAC_MODE, tp->mac_mode);
5971 udelay(40);
5972
5973 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5974
5975 tp->link_config.active_speed = current_speed;
5976 tp->link_config.active_duplex = current_duplex;
5977
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005978 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005979 return err;
5980}
5981
5982static void tg3_serdes_parallel_detect(struct tg3 *tp)
5983{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005984 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005985 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005986 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005987 return;
5988 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005989
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005990 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005991 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5992 u32 bmcr;
5993
5994 tg3_readphy(tp, MII_BMCR, &bmcr);
5995 if (bmcr & BMCR_ANENABLE) {
5996 u32 phy1, phy2;
5997
5998 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005999 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6000 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006001
6002 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006003 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6004 MII_TG3_DSP_EXP1_INT_STAT);
6005 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6006 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006007
6008 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6009 /* We have signal detect and not receiving
6010 * config code words, link is up by parallel
6011 * detection.
6012 */
6013
6014 bmcr &= ~BMCR_ANENABLE;
6015 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6016 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006017 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006018 }
6019 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006020 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006021 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006022 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006023 u32 phy2;
6024
6025 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006026 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6027 MII_TG3_DSP_EXP1_INT_STAT);
6028 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006029 if (phy2 & 0x20) {
6030 u32 bmcr;
6031
6032 /* Config code words received, turn on autoneg. */
6033 tg3_readphy(tp, MII_BMCR, &bmcr);
6034 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6035
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006036 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006037
6038 }
6039 }
6040}
6041
Joe Perches953c96e2013-04-09 10:18:14 +00006042static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006044 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 int err;
6046
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006047 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006049 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006050 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006051 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Joe Perches41535772013-02-16 11:20:04 +00006054 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006055 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006056
6057 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6058 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6059 scale = 65;
6060 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6061 scale = 6;
6062 else
6063 scale = 12;
6064
6065 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6066 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6067 tw32(GRC_MISC_CFG, val);
6068 }
6069
Matt Carlsonf2096f92011-04-05 14:22:48 +00006070 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6071 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006072 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6073 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006074 val |= tr32(MAC_TX_LENGTHS) &
6075 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6076 TX_LENGTHS_CNT_DWN_VAL_MSK);
6077
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 if (tp->link_config.active_speed == SPEED_1000 &&
6079 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006080 tw32(MAC_TX_LENGTHS, val |
6081 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006083 tw32(MAC_TX_LENGTHS, val |
6084 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
Joe Perches63c3a662011-04-26 08:12:10 +00006086 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006087 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006089 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 } else {
6091 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6092 }
6093 }
6094
Joe Perches63c3a662011-04-26 08:12:10 +00006095 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006096 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006097 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006098 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6099 tp->pwrmgmt_thresh;
6100 else
6101 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6102 tw32(PCIE_PWR_MGMT_THRESH, val);
6103 }
6104
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 return err;
6106}
6107
Matt Carlsonbe947302012-12-03 19:36:57 +00006108/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006109static u64 tg3_refclk_read(struct tg3 *tp)
6110{
6111 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6112 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6113}
6114
6115/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006116static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6117{
Nithin Sujir92e64572013-07-29 13:58:38 -07006118 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6119
6120 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006121 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6122 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006123 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006124}
6125
Matt Carlson7d41e492012-12-03 19:36:58 +00006126static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6127static inline void tg3_full_unlock(struct tg3 *tp);
6128static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6129{
6130 struct tg3 *tp = netdev_priv(dev);
6131
6132 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6133 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006134 SOF_TIMESTAMPING_SOFTWARE;
6135
6136 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006137 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006138 SOF_TIMESTAMPING_RX_HARDWARE |
6139 SOF_TIMESTAMPING_RAW_HARDWARE;
6140 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006141
6142 if (tp->ptp_clock)
6143 info->phc_index = ptp_clock_index(tp->ptp_clock);
6144 else
6145 info->phc_index = -1;
6146
6147 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6148
6149 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6150 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6151 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6152 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6153 return 0;
6154}
6155
6156static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6157{
6158 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6159 bool neg_adj = false;
6160 u32 correction = 0;
6161
6162 if (ppb < 0) {
6163 neg_adj = true;
6164 ppb = -ppb;
6165 }
6166
6167 /* Frequency adjustment is performed using hardware with a 24 bit
6168 * accumulator and a programmable correction value. On each clk, the
6169 * correction value gets added to the accumulator and when it
6170 * overflows, the time counter is incremented/decremented.
6171 *
6172 * So conversion from ppb to correction value is
6173 * ppb * (1 << 24) / 1000000000
6174 */
6175 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6176 TG3_EAV_REF_CLK_CORRECT_MASK;
6177
6178 tg3_full_lock(tp, 0);
6179
6180 if (correction)
6181 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6182 TG3_EAV_REF_CLK_CORRECT_EN |
6183 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6184 else
6185 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6186
6187 tg3_full_unlock(tp);
6188
6189 return 0;
6190}
6191
6192static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6193{
6194 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6195
6196 tg3_full_lock(tp, 0);
6197 tp->ptp_adjust += delta;
6198 tg3_full_unlock(tp);
6199
6200 return 0;
6201}
6202
6203static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6204{
6205 u64 ns;
6206 u32 remainder;
6207 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6208
6209 tg3_full_lock(tp, 0);
6210 ns = tg3_refclk_read(tp);
6211 ns += tp->ptp_adjust;
6212 tg3_full_unlock(tp);
6213
6214 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6215 ts->tv_nsec = remainder;
6216
6217 return 0;
6218}
6219
6220static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6221 const struct timespec *ts)
6222{
6223 u64 ns;
6224 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6225
6226 ns = timespec_to_ns(ts);
6227
6228 tg3_full_lock(tp, 0);
6229 tg3_refclk_write(tp, ns);
6230 tp->ptp_adjust = 0;
6231 tg3_full_unlock(tp);
6232
6233 return 0;
6234}
6235
6236static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6237 struct ptp_clock_request *rq, int on)
6238{
Nithin Sujir92e64572013-07-29 13:58:38 -07006239 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6240 u32 clock_ctl;
6241 int rval = 0;
6242
6243 switch (rq->type) {
6244 case PTP_CLK_REQ_PEROUT:
6245 if (rq->perout.index != 0)
6246 return -EINVAL;
6247
6248 tg3_full_lock(tp, 0);
6249 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6250 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6251
6252 if (on) {
6253 u64 nsec;
6254
6255 nsec = rq->perout.start.sec * 1000000000ULL +
6256 rq->perout.start.nsec;
6257
6258 if (rq->perout.period.sec || rq->perout.period.nsec) {
6259 netdev_warn(tp->dev,
6260 "Device supports only a one-shot timesync output, period must be 0\n");
6261 rval = -EINVAL;
6262 goto err_out;
6263 }
6264
6265 if (nsec & (1ULL << 63)) {
6266 netdev_warn(tp->dev,
6267 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6268 rval = -EINVAL;
6269 goto err_out;
6270 }
6271
6272 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6273 tw32(TG3_EAV_WATCHDOG0_MSB,
6274 TG3_EAV_WATCHDOG0_EN |
6275 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6276
6277 tw32(TG3_EAV_REF_CLCK_CTL,
6278 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6279 } else {
6280 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6281 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6282 }
6283
6284err_out:
6285 tg3_full_unlock(tp);
6286 return rval;
6287
6288 default:
6289 break;
6290 }
6291
Matt Carlson7d41e492012-12-03 19:36:58 +00006292 return -EOPNOTSUPP;
6293}
6294
6295static const struct ptp_clock_info tg3_ptp_caps = {
6296 .owner = THIS_MODULE,
6297 .name = "tg3 clock",
6298 .max_adj = 250000000,
6299 .n_alarm = 0,
6300 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006301 .n_per_out = 1,
Matt Carlson7d41e492012-12-03 19:36:58 +00006302 .pps = 0,
6303 .adjfreq = tg3_ptp_adjfreq,
6304 .adjtime = tg3_ptp_adjtime,
6305 .gettime = tg3_ptp_gettime,
6306 .settime = tg3_ptp_settime,
6307 .enable = tg3_ptp_enable,
6308};
6309
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006310static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6311 struct skb_shared_hwtstamps *timestamp)
6312{
6313 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6314 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6315 tp->ptp_adjust);
6316}
6317
Matt Carlsonbe947302012-12-03 19:36:57 +00006318/* tp->lock must be held */
6319static void tg3_ptp_init(struct tg3 *tp)
6320{
6321 if (!tg3_flag(tp, PTP_CAPABLE))
6322 return;
6323
6324 /* Initialize the hardware clock to the system time. */
6325 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6326 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006327 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006328}
6329
6330/* tp->lock must be held */
6331static void tg3_ptp_resume(struct tg3 *tp)
6332{
6333 if (!tg3_flag(tp, PTP_CAPABLE))
6334 return;
6335
6336 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6337 tp->ptp_adjust = 0;
6338}
6339
6340static void tg3_ptp_fini(struct tg3 *tp)
6341{
6342 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6343 return;
6344
Matt Carlson7d41e492012-12-03 19:36:58 +00006345 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006346 tp->ptp_clock = NULL;
6347 tp->ptp_adjust = 0;
6348}
6349
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006350static inline int tg3_irq_sync(struct tg3 *tp)
6351{
6352 return tp->irq_sync;
6353}
6354
Matt Carlson97bd8e42011-04-13 11:05:04 +00006355static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6356{
6357 int i;
6358
6359 dst = (u32 *)((u8 *)dst + off);
6360 for (i = 0; i < len; i += sizeof(u32))
6361 *dst++ = tr32(off + i);
6362}
6363
6364static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6365{
6366 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6367 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6368 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6369 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6370 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6371 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6372 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6373 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6374 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6375 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6376 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6377 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6378 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6379 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6380 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6381 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6382 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6383 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6384 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6385
Joe Perches63c3a662011-04-26 08:12:10 +00006386 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006387 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6388
6389 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6390 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6391 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6392 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6393 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6394 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6395 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6396 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6397
Joe Perches63c3a662011-04-26 08:12:10 +00006398 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006399 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6400 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6401 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6402 }
6403
6404 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6405 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6406 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6407 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6408 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6409
Joe Perches63c3a662011-04-26 08:12:10 +00006410 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006411 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6412}
6413
6414static void tg3_dump_state(struct tg3 *tp)
6415{
6416 int i;
6417 u32 *regs;
6418
6419 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006420 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006421 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006422
Joe Perches63c3a662011-04-26 08:12:10 +00006423 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006424 /* Read up to but not including private PCI registers */
6425 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6426 regs[i / sizeof(u32)] = tr32(i);
6427 } else
6428 tg3_dump_legacy_regs(tp, regs);
6429
6430 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6431 if (!regs[i + 0] && !regs[i + 1] &&
6432 !regs[i + 2] && !regs[i + 3])
6433 continue;
6434
6435 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6436 i * 4,
6437 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6438 }
6439
6440 kfree(regs);
6441
6442 for (i = 0; i < tp->irq_cnt; i++) {
6443 struct tg3_napi *tnapi = &tp->napi[i];
6444
6445 /* SW status block */
6446 netdev_err(tp->dev,
6447 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6448 i,
6449 tnapi->hw_status->status,
6450 tnapi->hw_status->status_tag,
6451 tnapi->hw_status->rx_jumbo_consumer,
6452 tnapi->hw_status->rx_consumer,
6453 tnapi->hw_status->rx_mini_consumer,
6454 tnapi->hw_status->idx[0].rx_producer,
6455 tnapi->hw_status->idx[0].tx_consumer);
6456
6457 netdev_err(tp->dev,
6458 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6459 i,
6460 tnapi->last_tag, tnapi->last_irq_tag,
6461 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6462 tnapi->rx_rcb_ptr,
6463 tnapi->prodring.rx_std_prod_idx,
6464 tnapi->prodring.rx_std_cons_idx,
6465 tnapi->prodring.rx_jmb_prod_idx,
6466 tnapi->prodring.rx_jmb_cons_idx);
6467 }
6468}
6469
Michael Chandf3e6542006-05-26 17:48:07 -07006470/* This is called whenever we suspect that the system chipset is re-
6471 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6472 * is bogus tx completions. We try to recover by setting the
6473 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6474 * in the workqueue.
6475 */
6476static void tg3_tx_recover(struct tg3 *tp)
6477{
Joe Perches63c3a662011-04-26 08:12:10 +00006478 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006479 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6480
Matt Carlson5129c3a2010-04-05 10:19:23 +00006481 netdev_warn(tp->dev,
6482 "The system may be re-ordering memory-mapped I/O "
6483 "cycles to the network device, attempting to recover. "
6484 "Please report the problem to the driver maintainer "
6485 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006486
Joe Perches63c3a662011-04-26 08:12:10 +00006487 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006488}
6489
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006490static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006491{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006492 /* Tell compiler to fetch tx indices from memory. */
6493 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006494 return tnapi->tx_pending -
6495 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006496}
6497
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498/* Tigon3 never reports partial packet sends. So we do not
6499 * need special logic to handle SKBs that have not had all
6500 * of their frags sent yet, like SunGEM does.
6501 */
Matt Carlson17375d22009-08-28 14:02:18 +00006502static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503{
Matt Carlson17375d22009-08-28 14:02:18 +00006504 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006505 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006506 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006507 struct netdev_queue *txq;
6508 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006509 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006510
Joe Perches63c3a662011-04-26 08:12:10 +00006511 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006512 index--;
6513
6514 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515
6516 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006517 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006519 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520
Michael Chandf3e6542006-05-26 17:48:07 -07006521 if (unlikely(skb == NULL)) {
6522 tg3_tx_recover(tp);
6523 return;
6524 }
6525
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006526 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6527 struct skb_shared_hwtstamps timestamp;
6528 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6529 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6530
6531 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6532
6533 skb_tstamp_tx(skb, &timestamp);
6534 }
6535
Alexander Duyckf4188d82009-12-02 16:48:38 +00006536 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006537 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006538 skb_headlen(skb),
6539 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540
6541 ri->skb = NULL;
6542
Matt Carlsone01ee142011-07-27 14:20:50 +00006543 while (ri->fragmented) {
6544 ri->fragmented = false;
6545 sw_idx = NEXT_TX(sw_idx);
6546 ri = &tnapi->tx_buffers[sw_idx];
6547 }
6548
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 sw_idx = NEXT_TX(sw_idx);
6550
6551 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006552 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006553 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6554 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006555
6556 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006557 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006558 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006559 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006560
6561 while (ri->fragmented) {
6562 ri->fragmented = false;
6563 sw_idx = NEXT_TX(sw_idx);
6564 ri = &tnapi->tx_buffers[sw_idx];
6565 }
6566
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 sw_idx = NEXT_TX(sw_idx);
6568 }
6569
Tom Herbert298376d2011-11-28 16:33:30 +00006570 pkts_compl++;
6571 bytes_compl += skb->len;
6572
David S. Millerf47c11e2005-06-24 20:18:35 -07006573 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006574
6575 if (unlikely(tx_bug)) {
6576 tg3_tx_recover(tp);
6577 return;
6578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 }
6580
Tom Herbert5cb917b2012-03-05 19:53:50 +00006581 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006582
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006583 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
Michael Chan1b2a7202006-08-07 21:46:02 -07006585 /* Need to make the tx_cons update visible to tg3_start_xmit()
6586 * before checking for netif_queue_stopped(). Without the
6587 * memory barrier, there is a small possibility that tg3_start_xmit()
6588 * will miss it and cause the queue to be stopped forever.
6589 */
6590 smp_mb();
6591
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006592 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006593 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006594 __netif_tx_lock(txq, smp_processor_id());
6595 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006596 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006597 netif_tx_wake_queue(txq);
6598 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600}
6601
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006602static void tg3_frag_free(bool is_frag, void *data)
6603{
6604 if (is_frag)
6605 put_page(virt_to_head_page(data));
6606 else
6607 kfree(data);
6608}
6609
Eric Dumazet9205fd92011-11-18 06:47:01 +00006610static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006611{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006612 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6613 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6614
Eric Dumazet9205fd92011-11-18 06:47:01 +00006615 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006616 return;
6617
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006618 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006619 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006620 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006621 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006622}
6623
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006624
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625/* Returns size of skb allocated or < 0 on error.
6626 *
6627 * We only need to fill in the address because the other members
6628 * of the RX descriptor are invariant, see tg3_init_rings.
6629 *
6630 * Note the purposeful assymetry of cpu vs. chip accesses. For
6631 * posting buffers we only dirty the first cache line of the RX
6632 * descriptor (containing the address). Whereas for the RX status
6633 * buffers the cpu only reads the last cacheline of the RX descriptor
6634 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6635 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006636static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006637 u32 opaque_key, u32 dest_idx_unmasked,
6638 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639{
6640 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006641 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006642 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006644 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 switch (opaque_key) {
6647 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006648 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006649 desc = &tpr->rx_std[dest_idx];
6650 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006651 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 break;
6653
6654 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006655 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006656 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006657 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006658 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 break;
6660
6661 default:
6662 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
6665 /* Do not overwrite any of the map or rp information
6666 * until we are sure we can commit to a new buffer.
6667 *
6668 * Callers depend upon this behavior and assume that
6669 * we leave everything unchanged if we fail.
6670 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006671 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6672 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006673 if (skb_size <= PAGE_SIZE) {
6674 data = netdev_alloc_frag(skb_size);
6675 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006676 } else {
6677 data = kmalloc(skb_size, GFP_ATOMIC);
6678 *frag_size = 0;
6679 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006680 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 return -ENOMEM;
6682
Eric Dumazet9205fd92011-11-18 06:47:01 +00006683 mapping = pci_map_single(tp->pdev,
6684 data + TG3_RX_OFFSET(tp),
6685 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006687 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006688 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006689 return -EIO;
6690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
Eric Dumazet9205fd92011-11-18 06:47:01 +00006692 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006693 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 desc->addr_hi = ((u64)mapping >> 32);
6696 desc->addr_lo = ((u64)mapping & 0xffffffff);
6697
Eric Dumazet9205fd92011-11-18 06:47:01 +00006698 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699}
6700
6701/* We only need to move over in the address because the other
6702 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006703 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 */
Matt Carlsona3896162009-11-13 13:03:44 +00006705static void tg3_recycle_rx(struct tg3_napi *tnapi,
6706 struct tg3_rx_prodring_set *dpr,
6707 u32 opaque_key, int src_idx,
6708 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709{
Matt Carlson17375d22009-08-28 14:02:18 +00006710 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6712 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006713 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006714 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715
6716 switch (opaque_key) {
6717 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006718 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006719 dest_desc = &dpr->rx_std[dest_idx];
6720 dest_map = &dpr->rx_std_buffers[dest_idx];
6721 src_desc = &spr->rx_std[src_idx];
6722 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 break;
6724
6725 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006726 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006727 dest_desc = &dpr->rx_jmb[dest_idx].std;
6728 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6729 src_desc = &spr->rx_jmb[src_idx].std;
6730 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 break;
6732
6733 default:
6734 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
Eric Dumazet9205fd92011-11-18 06:47:01 +00006737 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006738 dma_unmap_addr_set(dest_map, mapping,
6739 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 dest_desc->addr_hi = src_desc->addr_hi;
6741 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006742
6743 /* Ensure that the update to the skb happens after the physical
6744 * addresses have been transferred to the new BD location.
6745 */
6746 smp_wmb();
6747
Eric Dumazet9205fd92011-11-18 06:47:01 +00006748 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749}
6750
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751/* The RX ring scheme is composed of multiple rings which post fresh
6752 * buffers to the chip, and one special ring the chip uses to report
6753 * status back to the host.
6754 *
6755 * The special ring reports the status of received packets to the
6756 * host. The chip does not write into the original descriptor the
6757 * RX buffer was obtained from. The chip simply takes the original
6758 * descriptor as provided by the host, updates the status and length
6759 * field, then writes this into the next status ring entry.
6760 *
6761 * Each ring the host uses to post buffers to the chip is described
6762 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6763 * it is first placed into the on-chip ram. When the packet's length
6764 * is known, it walks down the TG3_BDINFO entries to select the ring.
6765 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6766 * which is within the range of the new packet's length is chosen.
6767 *
6768 * The "separate ring for rx status" scheme may sound queer, but it makes
6769 * sense from a cache coherency perspective. If only the host writes
6770 * to the buffer post rings, and only the chip writes to the rx status
6771 * rings, then cache lines never move beyond shared-modified state.
6772 * If both the host and chip were to write into the same ring, cache line
6773 * eviction could occur since both entities want it in an exclusive state.
6774 */
Matt Carlson17375d22009-08-28 14:02:18 +00006775static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776{
Matt Carlson17375d22009-08-28 14:02:18 +00006777 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006778 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006779 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006780 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006781 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006783 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006785 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 /*
6787 * We need to order the read of hw_idx and the read of
6788 * the opaque cookie.
6789 */
6790 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 work_mask = 0;
6792 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006793 std_prod_idx = tpr->rx_std_prod_idx;
6794 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006796 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006797 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 unsigned int len;
6799 struct sk_buff *skb;
6800 dma_addr_t dma_addr;
6801 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006802 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006803 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
6805 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6806 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6807 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006808 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006809 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006810 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006811 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006812 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006814 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006815 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006816 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006817 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006818 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
6821 work_mask |= opaque_key;
6822
6823 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6824 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6825 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006826 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 desc_idx, *post_ptr);
6828 drop_it_no_recycle:
6829 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006830 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 goto next_pkt;
6832 }
6833
Eric Dumazet9205fd92011-11-18 06:47:01 +00006834 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006835 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6836 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006838 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6839 RXD_FLAG_PTPSTAT_PTPV1 ||
6840 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6841 RXD_FLAG_PTPSTAT_PTPV2) {
6842 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6843 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6844 }
6845
Matt Carlsond2757fc2010-04-12 06:58:27 +00006846 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006848 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
Eric Dumazet9205fd92011-11-18 06:47:01 +00006850 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006851 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 if (skb_size < 0)
6853 goto drop_it;
6854
Matt Carlson287be122009-08-28 13:58:46 +00006855 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 PCI_DMA_FROMDEVICE);
6857
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006858 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006859 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006860 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006861 goto drop_it_no_recycle;
6862 }
6863 skb_reserve(skb, TG3_RX_OFFSET(tp));
6864 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006865 * after the usage of the old DMA mapping.
6866 */
6867 smp_wmb();
6868
Eric Dumazet9205fd92011-11-18 06:47:01 +00006869 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006870
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006872 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 desc_idx, *post_ptr);
6874
Eric Dumazet9205fd92011-11-18 06:47:01 +00006875 skb = netdev_alloc_skb(tp->dev,
6876 len + TG3_RAW_IP_ALIGN);
6877 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 goto drop_it_no_recycle;
6879
Eric Dumazet9205fd92011-11-18 06:47:01 +00006880 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006882 memcpy(skb->data,
6883 data + TG3_RX_OFFSET(tp),
6884 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 }
6887
Eric Dumazet9205fd92011-11-18 06:47:01 +00006888 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006889 if (tstamp)
6890 tg3_hwclock_to_timestamp(tp, tstamp,
6891 skb_hwtstamps(skb));
6892
Michał Mirosławdc668912011-04-07 03:35:07 +00006893 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6895 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6896 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6897 skb->ip_summed = CHECKSUM_UNNECESSARY;
6898 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006899 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
6901 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006902
6903 if (len > (tp->dev->mtu + ETH_HLEN) &&
6904 skb->protocol != htons(ETH_P_8021Q)) {
6905 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006906 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006907 }
6908
Matt Carlson9dc7a112010-04-12 06:58:28 +00006909 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006910 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006911 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006912 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006913
Matt Carlsonbf933c82011-01-25 15:58:49 +00006914 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 received++;
6917 budget--;
6918
6919next_pkt:
6920 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006921
6922 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006923 tpr->rx_std_prod_idx = std_prod_idx &
6924 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006925 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6926 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006927 work_mask &= ~RXD_OPAQUE_RING_STD;
6928 rx_std_posted = 0;
6929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006931 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006932 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006933
6934 /* Refresh hw_idx to see if there is new work */
6935 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006936 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006937 rmb();
6938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 }
6940
6941 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006942 tnapi->rx_rcb_ptr = sw_idx;
6943 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944
6945 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006946 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006947 /* Sync BD data before updating mailbox */
6948 wmb();
6949
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006950 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006951 tpr->rx_std_prod_idx = std_prod_idx &
6952 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006953 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6954 tpr->rx_std_prod_idx);
6955 }
6956 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006957 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6958 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006959 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6960 tpr->rx_jmb_prod_idx);
6961 }
6962 mmiowb();
6963 } else if (work_mask) {
6964 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6965 * updated before the producer indices can be updated.
6966 */
6967 smp_wmb();
6968
Matt Carlson2c49a442010-09-30 10:34:35 +00006969 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6970 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006971
Michael Chan7ae52892012-03-21 15:38:33 +00006972 if (tnapi != &tp->napi[1]) {
6973 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006974 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977
6978 return received;
6979}
6980
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006981static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006984 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006985 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6986
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 if (sblk->status & SD_STATUS_LINK_CHG) {
6988 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006989 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006990 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006991 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006992 tw32_f(MAC_STATUS,
6993 (MAC_STATUS_SYNC_CHANGED |
6994 MAC_STATUS_CFG_CHANGED |
6995 MAC_STATUS_MI_COMPLETION |
6996 MAC_STATUS_LNKSTATE_CHANGED));
6997 udelay(40);
6998 } else
Joe Perches953c96e2013-04-09 10:18:14 +00006999 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007000 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 }
7002 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007003}
7004
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007005static int tg3_rx_prodring_xfer(struct tg3 *tp,
7006 struct tg3_rx_prodring_set *dpr,
7007 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007008{
7009 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007010 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007011
7012 while (1) {
7013 src_prod_idx = spr->rx_std_prod_idx;
7014
7015 /* Make sure updates to the rx_std_buffers[] entries and the
7016 * standard producer index are seen in the correct order.
7017 */
7018 smp_rmb();
7019
7020 if (spr->rx_std_cons_idx == src_prod_idx)
7021 break;
7022
7023 if (spr->rx_std_cons_idx < src_prod_idx)
7024 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7025 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007026 cpycnt = tp->rx_std_ring_mask + 1 -
7027 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007028
Matt Carlson2c49a442010-09-30 10:34:35 +00007029 cpycnt = min(cpycnt,
7030 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007031
7032 si = spr->rx_std_cons_idx;
7033 di = dpr->rx_std_prod_idx;
7034
Matt Carlsone92967b2010-02-12 14:47:06 +00007035 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007036 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007037 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007038 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007039 break;
7040 }
7041 }
7042
7043 if (!cpycnt)
7044 break;
7045
7046 /* Ensure that updates to the rx_std_buffers ring and the
7047 * shadowed hardware producer ring from tg3_recycle_skb() are
7048 * ordered correctly WRT the skb check above.
7049 */
7050 smp_rmb();
7051
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007052 memcpy(&dpr->rx_std_buffers[di],
7053 &spr->rx_std_buffers[si],
7054 cpycnt * sizeof(struct ring_info));
7055
7056 for (i = 0; i < cpycnt; i++, di++, si++) {
7057 struct tg3_rx_buffer_desc *sbd, *dbd;
7058 sbd = &spr->rx_std[si];
7059 dbd = &dpr->rx_std[di];
7060 dbd->addr_hi = sbd->addr_hi;
7061 dbd->addr_lo = sbd->addr_lo;
7062 }
7063
Matt Carlson2c49a442010-09-30 10:34:35 +00007064 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7065 tp->rx_std_ring_mask;
7066 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7067 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007068 }
7069
7070 while (1) {
7071 src_prod_idx = spr->rx_jmb_prod_idx;
7072
7073 /* Make sure updates to the rx_jmb_buffers[] entries and
7074 * the jumbo producer index are seen in the correct order.
7075 */
7076 smp_rmb();
7077
7078 if (spr->rx_jmb_cons_idx == src_prod_idx)
7079 break;
7080
7081 if (spr->rx_jmb_cons_idx < src_prod_idx)
7082 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7083 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007084 cpycnt = tp->rx_jmb_ring_mask + 1 -
7085 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007086
7087 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007088 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007089
7090 si = spr->rx_jmb_cons_idx;
7091 di = dpr->rx_jmb_prod_idx;
7092
Matt Carlsone92967b2010-02-12 14:47:06 +00007093 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007094 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007095 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007096 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007097 break;
7098 }
7099 }
7100
7101 if (!cpycnt)
7102 break;
7103
7104 /* Ensure that updates to the rx_jmb_buffers ring and the
7105 * shadowed hardware producer ring from tg3_recycle_skb() are
7106 * ordered correctly WRT the skb check above.
7107 */
7108 smp_rmb();
7109
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007110 memcpy(&dpr->rx_jmb_buffers[di],
7111 &spr->rx_jmb_buffers[si],
7112 cpycnt * sizeof(struct ring_info));
7113
7114 for (i = 0; i < cpycnt; i++, di++, si++) {
7115 struct tg3_rx_buffer_desc *sbd, *dbd;
7116 sbd = &spr->rx_jmb[si].std;
7117 dbd = &dpr->rx_jmb[di].std;
7118 dbd->addr_hi = sbd->addr_hi;
7119 dbd->addr_lo = sbd->addr_lo;
7120 }
7121
Matt Carlson2c49a442010-09-30 10:34:35 +00007122 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7123 tp->rx_jmb_ring_mask;
7124 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7125 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007126 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007127
7128 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007129}
7130
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007131static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7132{
7133 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134
7135 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007136 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007137 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007138 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007139 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 }
7141
Matt Carlsonf891ea12012-04-24 13:37:01 +00007142 if (!tnapi->rx_rcb_prod_idx)
7143 return work_done;
7144
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 /* run RX thread, within the bounds set by NAPI.
7146 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007147 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007149 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007150 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
Joe Perches63c3a662011-04-26 08:12:10 +00007152 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007153 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007154 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007155 u32 std_prod_idx = dpr->rx_std_prod_idx;
7156 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007157
Michael Chan7ae52892012-03-21 15:38:33 +00007158 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007159 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007160 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007161 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007162
7163 wmb();
7164
Matt Carlsone4af1af2010-02-12 14:47:05 +00007165 if (std_prod_idx != dpr->rx_std_prod_idx)
7166 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7167 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007168
Matt Carlsone4af1af2010-02-12 14:47:05 +00007169 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7170 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7171 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007172
7173 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007174
7175 if (err)
7176 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007177 }
7178
David S. Miller6f535762007-10-11 18:08:29 -07007179 return work_done;
7180}
David S. Millerf7383c222005-05-18 22:50:53 -07007181
Matt Carlsondb219972011-11-04 09:15:03 +00007182static inline void tg3_reset_task_schedule(struct tg3 *tp)
7183{
7184 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7185 schedule_work(&tp->reset_task);
7186}
7187
7188static inline void tg3_reset_task_cancel(struct tg3 *tp)
7189{
7190 cancel_work_sync(&tp->reset_task);
7191 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007192 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007193}
7194
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007195static int tg3_poll_msix(struct napi_struct *napi, int budget)
7196{
7197 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7198 struct tg3 *tp = tnapi->tp;
7199 int work_done = 0;
7200 struct tg3_hw_status *sblk = tnapi->hw_status;
7201
7202 while (1) {
7203 work_done = tg3_poll_work(tnapi, work_done, budget);
7204
Joe Perches63c3a662011-04-26 08:12:10 +00007205 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007206 goto tx_recovery;
7207
7208 if (unlikely(work_done >= budget))
7209 break;
7210
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007211 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007212 * to tell the hw how much work has been processed,
7213 * so we must read it before checking for more work.
7214 */
7215 tnapi->last_tag = sblk->status_tag;
7216 tnapi->last_irq_tag = tnapi->last_tag;
7217 rmb();
7218
7219 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007220 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7221 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007222
7223 /* This test here is not race free, but will reduce
7224 * the number of interrupts by looping again.
7225 */
7226 if (tnapi == &tp->napi[1] && tp->rx_refill)
7227 continue;
7228
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007229 napi_complete(napi);
7230 /* Reenable interrupts. */
7231 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007232
7233 /* This test here is synchronized by napi_schedule()
7234 * and napi_complete() to close the race condition.
7235 */
7236 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7237 tw32(HOSTCC_MODE, tp->coalesce_mode |
7238 HOSTCC_MODE_ENABLE |
7239 tnapi->coal_now);
7240 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007241 mmiowb();
7242 break;
7243 }
7244 }
7245
7246 return work_done;
7247
7248tx_recovery:
7249 /* work_done is guaranteed to be less than budget. */
7250 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007251 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007252 return work_done;
7253}
7254
Matt Carlsone64de4e2011-04-13 11:05:05 +00007255static void tg3_process_error(struct tg3 *tp)
7256{
7257 u32 val;
7258 bool real_error = false;
7259
Joe Perches63c3a662011-04-26 08:12:10 +00007260 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007261 return;
7262
7263 /* Check Flow Attention register */
7264 val = tr32(HOSTCC_FLOW_ATTN);
7265 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7266 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7267 real_error = true;
7268 }
7269
7270 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7271 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7272 real_error = true;
7273 }
7274
7275 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7276 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7277 real_error = true;
7278 }
7279
7280 if (!real_error)
7281 return;
7282
7283 tg3_dump_state(tp);
7284
Joe Perches63c3a662011-04-26 08:12:10 +00007285 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007286 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007287}
7288
David S. Miller6f535762007-10-11 18:08:29 -07007289static int tg3_poll(struct napi_struct *napi, int budget)
7290{
Matt Carlson8ef04422009-08-28 14:01:37 +00007291 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7292 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007293 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007294 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007295
7296 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007297 if (sblk->status & SD_STATUS_ERROR)
7298 tg3_process_error(tp);
7299
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007300 tg3_poll_link(tp);
7301
Matt Carlson17375d22009-08-28 14:02:18 +00007302 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007303
Joe Perches63c3a662011-04-26 08:12:10 +00007304 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007305 goto tx_recovery;
7306
7307 if (unlikely(work_done >= budget))
7308 break;
7309
Joe Perches63c3a662011-04-26 08:12:10 +00007310 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007311 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007312 * to tell the hw how much work has been processed,
7313 * so we must read it before checking for more work.
7314 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007315 tnapi->last_tag = sblk->status_tag;
7316 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007317 rmb();
7318 } else
7319 sblk->status &= ~SD_STATUS_UPDATED;
7320
Matt Carlson17375d22009-08-28 14:02:18 +00007321 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007322 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007323 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007324 break;
7325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 }
7327
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007328 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007329
7330tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007331 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007332 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007333 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007334 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335}
7336
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007337static void tg3_napi_disable(struct tg3 *tp)
7338{
7339 int i;
7340
7341 for (i = tp->irq_cnt - 1; i >= 0; i--)
7342 napi_disable(&tp->napi[i].napi);
7343}
7344
7345static void tg3_napi_enable(struct tg3 *tp)
7346{
7347 int i;
7348
7349 for (i = 0; i < tp->irq_cnt; i++)
7350 napi_enable(&tp->napi[i].napi);
7351}
7352
7353static void tg3_napi_init(struct tg3 *tp)
7354{
7355 int i;
7356
7357 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7358 for (i = 1; i < tp->irq_cnt; i++)
7359 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7360}
7361
7362static void tg3_napi_fini(struct tg3 *tp)
7363{
7364 int i;
7365
7366 for (i = 0; i < tp->irq_cnt; i++)
7367 netif_napi_del(&tp->napi[i].napi);
7368}
7369
7370static inline void tg3_netif_stop(struct tg3 *tp)
7371{
7372 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7373 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007374 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007375 netif_tx_disable(tp->dev);
7376}
7377
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007378/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007379static inline void tg3_netif_start(struct tg3 *tp)
7380{
Matt Carlsonbe947302012-12-03 19:36:57 +00007381 tg3_ptp_resume(tp);
7382
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007383 /* NOTE: unconditional netif_tx_wake_all_queues is only
7384 * appropriate so long as all callers are assured to
7385 * have free tx slots (such as after tg3_init_hw)
7386 */
7387 netif_tx_wake_all_queues(tp->dev);
7388
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007389 if (tp->link_up)
7390 netif_carrier_on(tp->dev);
7391
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007392 tg3_napi_enable(tp);
7393 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7394 tg3_enable_ints(tp);
7395}
7396
David S. Millerf47c11e2005-06-24 20:18:35 -07007397static void tg3_irq_quiesce(struct tg3 *tp)
7398{
Matt Carlson4f125f42009-09-01 12:55:02 +00007399 int i;
7400
David S. Millerf47c11e2005-06-24 20:18:35 -07007401 BUG_ON(tp->irq_sync);
7402
7403 tp->irq_sync = 1;
7404 smp_mb();
7405
Matt Carlson4f125f42009-09-01 12:55:02 +00007406 for (i = 0; i < tp->irq_cnt; i++)
7407 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007408}
7409
David S. Millerf47c11e2005-06-24 20:18:35 -07007410/* Fully shutdown all tg3 driver activity elsewhere in the system.
7411 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7412 * with as well. Most of the time, this is not necessary except when
7413 * shutting down the device.
7414 */
7415static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7416{
Michael Chan46966542007-07-11 19:47:19 -07007417 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007418 if (irq_sync)
7419 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007420}
7421
7422static inline void tg3_full_unlock(struct tg3 *tp)
7423{
David S. Millerf47c11e2005-06-24 20:18:35 -07007424 spin_unlock_bh(&tp->lock);
7425}
7426
Michael Chanfcfa0a32006-03-20 22:28:41 -08007427/* One-shot MSI handler - Chip automatically disables interrupt
7428 * after sending MSI so driver doesn't have to do it.
7429 */
David Howells7d12e782006-10-05 14:55:46 +01007430static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007431{
Matt Carlson09943a12009-08-28 14:01:57 +00007432 struct tg3_napi *tnapi = dev_id;
7433 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007434
Matt Carlson898a56f2009-08-28 14:02:40 +00007435 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007436 if (tnapi->rx_rcb)
7437 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007438
7439 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007440 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007441
7442 return IRQ_HANDLED;
7443}
7444
Michael Chan88b06bc22005-04-21 17:13:25 -07007445/* MSI ISR - No need to check for interrupt sharing and no need to
7446 * flush status block and interrupt mailbox. PCI ordering rules
7447 * guarantee that MSI will arrive after the status block.
7448 */
David Howells7d12e782006-10-05 14:55:46 +01007449static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007450{
Matt Carlson09943a12009-08-28 14:01:57 +00007451 struct tg3_napi *tnapi = dev_id;
7452 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007453
Matt Carlson898a56f2009-08-28 14:02:40 +00007454 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007455 if (tnapi->rx_rcb)
7456 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007457 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007458 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007459 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007460 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007461 * NIC to stop sending us irqs, engaging "in-intr-handler"
7462 * event coalescing.
7463 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007464 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007465 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007466 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007467
Michael Chan88b06bc22005-04-21 17:13:25 -07007468 return IRQ_RETVAL(1);
7469}
7470
David Howells7d12e782006-10-05 14:55:46 +01007471static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472{
Matt Carlson09943a12009-08-28 14:01:57 +00007473 struct tg3_napi *tnapi = dev_id;
7474 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007475 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 unsigned int handled = 1;
7477
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 /* In INTx mode, it is possible for the interrupt to arrive at
7479 * the CPU before the status block posted prior to the interrupt.
7480 * Reading the PCI State register will confirm whether the
7481 * interrupt is ours and will flush the status block.
7482 */
Michael Chand18edcb2007-03-24 20:57:11 -07007483 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007484 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007485 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7486 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007487 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007488 }
Michael Chand18edcb2007-03-24 20:57:11 -07007489 }
7490
7491 /*
7492 * Writing any value to intr-mbox-0 clears PCI INTA# and
7493 * chip-internal interrupt pending events.
7494 * Writing non-zero to intr-mbox-0 additional tells the
7495 * NIC to stop sending us irqs, engaging "in-intr-handler"
7496 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007497 *
7498 * Flush the mailbox to de-assert the IRQ immediately to prevent
7499 * spurious interrupts. The flush impacts performance but
7500 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007501 */
Michael Chanc04cb342007-05-07 00:26:15 -07007502 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007503 if (tg3_irq_sync(tp))
7504 goto out;
7505 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007506 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007507 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007508 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007509 } else {
7510 /* No work, shared interrupt perhaps? re-enable
7511 * interrupts, and flush that PCI write
7512 */
7513 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7514 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007515 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007516out:
David S. Millerfac9b832005-05-18 22:46:34 -07007517 return IRQ_RETVAL(handled);
7518}
7519
David Howells7d12e782006-10-05 14:55:46 +01007520static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007521{
Matt Carlson09943a12009-08-28 14:01:57 +00007522 struct tg3_napi *tnapi = dev_id;
7523 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007524 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007525 unsigned int handled = 1;
7526
David S. Millerfac9b832005-05-18 22:46:34 -07007527 /* In INTx mode, it is possible for the interrupt to arrive at
7528 * the CPU before the status block posted prior to the interrupt.
7529 * Reading the PCI State register will confirm whether the
7530 * interrupt is ours and will flush the status block.
7531 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007532 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007533 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007534 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7535 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007536 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 }
Michael Chand18edcb2007-03-24 20:57:11 -07007538 }
7539
7540 /*
7541 * writing any value to intr-mbox-0 clears PCI INTA# and
7542 * chip-internal interrupt pending events.
7543 * writing non-zero to intr-mbox-0 additional tells the
7544 * NIC to stop sending us irqs, engaging "in-intr-handler"
7545 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007546 *
7547 * Flush the mailbox to de-assert the IRQ immediately to prevent
7548 * spurious interrupts. The flush impacts performance but
7549 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007550 */
Michael Chanc04cb342007-05-07 00:26:15 -07007551 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007552
7553 /*
7554 * In a shared interrupt configuration, sometimes other devices'
7555 * interrupts will scream. We record the current status tag here
7556 * so that the above check can report that the screaming interrupts
7557 * are unhandled. Eventually they will be silenced.
7558 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007559 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007560
Michael Chand18edcb2007-03-24 20:57:11 -07007561 if (tg3_irq_sync(tp))
7562 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007563
Matt Carlson72334482009-08-28 14:03:01 +00007564 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007565
Matt Carlson09943a12009-08-28 14:01:57 +00007566 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007567
David S. Millerf47c11e2005-06-24 20:18:35 -07007568out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 return IRQ_RETVAL(handled);
7570}
7571
Michael Chan79381092005-04-21 17:13:59 -07007572/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007573static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007574{
Matt Carlson09943a12009-08-28 14:01:57 +00007575 struct tg3_napi *tnapi = dev_id;
7576 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007577 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007578
Michael Chanf9804dd2005-09-27 12:13:10 -07007579 if ((sblk->status & SD_STATUS_UPDATED) ||
7580 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007581 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007582 return IRQ_RETVAL(1);
7583 }
7584 return IRQ_RETVAL(0);
7585}
7586
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587#ifdef CONFIG_NET_POLL_CONTROLLER
7588static void tg3_poll_controller(struct net_device *dev)
7589{
Matt Carlson4f125f42009-09-01 12:55:02 +00007590 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007591 struct tg3 *tp = netdev_priv(dev);
7592
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007593 if (tg3_irq_sync(tp))
7594 return;
7595
Matt Carlson4f125f42009-09-01 12:55:02 +00007596 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007597 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598}
7599#endif
7600
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601static void tg3_tx_timeout(struct net_device *dev)
7602{
7603 struct tg3 *tp = netdev_priv(dev);
7604
Michael Chanb0408752007-02-13 12:18:30 -08007605 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007606 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007607 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
Matt Carlsondb219972011-11-04 09:15:03 +00007610 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611}
7612
Michael Chanc58ec932005-09-17 00:46:27 -07007613/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7614static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7615{
7616 u32 base = (u32) mapping & 0xffffffff;
7617
Eric Dumazet807540b2010-09-23 05:40:09 +00007618 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007619}
7620
Michael Chan0f0d1512013-05-13 11:04:16 +00007621/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7622 * of any 4GB boundaries: 4G, 8G, etc
7623 */
7624static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7625 u32 len, u32 mss)
7626{
7627 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7628 u32 base = (u32) mapping & 0xffffffff;
7629
7630 return ((base + len + (mss & 0x3fff)) < base);
7631 }
7632 return 0;
7633}
7634
Michael Chan72f2afb2006-03-06 19:28:35 -08007635/* Test for DMA addresses > 40-bit */
7636static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7637 int len)
7638{
7639#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007640 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007641 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007642 return 0;
7643#else
7644 return 0;
7645#endif
7646}
7647
Matt Carlsond1a3b732011-07-27 14:20:51 +00007648static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007649 dma_addr_t mapping, u32 len, u32 flags,
7650 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007651{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007652 txbd->addr_hi = ((u64) mapping >> 32);
7653 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7654 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7655 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007656}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657
Matt Carlson84b67b22011-07-27 14:20:52 +00007658static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007659 dma_addr_t map, u32 len, u32 flags,
7660 u32 mss, u32 vlan)
7661{
7662 struct tg3 *tp = tnapi->tp;
7663 bool hwbug = false;
7664
7665 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007666 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007667
7668 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007669 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007670
Michael Chan0f0d1512013-05-13 11:04:16 +00007671 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7672 hwbug = true;
7673
Matt Carlsond1a3b732011-07-27 14:20:51 +00007674 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007675 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007676
Matt Carlsona4cb4282011-12-14 11:09:58 +00007677 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007678 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007679 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007680 while (len > tp->dma_limit && *budget) {
7681 u32 frag_len = tp->dma_limit;
7682 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007683
Matt Carlsonb9e45482011-11-04 09:14:59 +00007684 /* Avoid the 8byte DMA problem */
7685 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007686 len += tp->dma_limit / 2;
7687 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007688 }
7689
Matt Carlsonb9e45482011-11-04 09:14:59 +00007690 tnapi->tx_buffers[*entry].fragmented = true;
7691
7692 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7693 frag_len, tmp_flag, mss, vlan);
7694 *budget -= 1;
7695 prvidx = *entry;
7696 *entry = NEXT_TX(*entry);
7697
Matt Carlsone31aa982011-07-27 14:20:53 +00007698 map += frag_len;
7699 }
7700
7701 if (len) {
7702 if (*budget) {
7703 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7704 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007705 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007706 *entry = NEXT_TX(*entry);
7707 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007708 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007709 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007710 }
7711 }
7712 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007713 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7714 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007715 *entry = NEXT_TX(*entry);
7716 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007717
7718 return hwbug;
7719}
7720
Matt Carlson0d681b22011-07-27 14:20:49 +00007721static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007722{
7723 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007724 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007725 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007726
Matt Carlson0d681b22011-07-27 14:20:49 +00007727 skb = txb->skb;
7728 txb->skb = NULL;
7729
Matt Carlson432aa7e2011-05-19 12:12:45 +00007730 pci_unmap_single(tnapi->tp->pdev,
7731 dma_unmap_addr(txb, mapping),
7732 skb_headlen(skb),
7733 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007734
7735 while (txb->fragmented) {
7736 txb->fragmented = false;
7737 entry = NEXT_TX(entry);
7738 txb = &tnapi->tx_buffers[entry];
7739 }
7740
Matt Carlsonba1142e2011-11-04 09:15:00 +00007741 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007742 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007743
7744 entry = NEXT_TX(entry);
7745 txb = &tnapi->tx_buffers[entry];
7746
7747 pci_unmap_page(tnapi->tp->pdev,
7748 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007749 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007750
7751 while (txb->fragmented) {
7752 txb->fragmented = false;
7753 entry = NEXT_TX(entry);
7754 txb = &tnapi->tx_buffers[entry];
7755 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007756 }
7757}
7758
Michael Chan72f2afb2006-03-06 19:28:35 -08007759/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007760static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007761 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007762 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007763 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007765 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007766 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007767 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007768 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769
Joe Perches41535772013-02-16 11:20:04 +00007770 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba12008-04-19 18:12:33 -07007771 new_skb = skb_copy(skb, GFP_ATOMIC);
7772 else {
7773 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7774
7775 new_skb = skb_copy_expand(skb,
7776 skb_headroom(skb) + more_headroom,
7777 skb_tailroom(skb), GFP_ATOMIC);
7778 }
7779
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007781 ret = -1;
7782 } else {
7783 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007784 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7785 PCI_DMA_TODEVICE);
7786 /* Make sure the mapping succeeded */
7787 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007788 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007789 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007790 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007791 u32 save_entry = *entry;
7792
Matt Carlson92cd3a12011-07-27 14:20:47 +00007793 base_flags |= TXD_FLAG_END;
7794
Matt Carlson84b67b22011-07-27 14:20:52 +00007795 tnapi->tx_buffers[*entry].skb = new_skb;
7796 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007797 mapping, new_addr);
7798
Matt Carlson84b67b22011-07-27 14:20:52 +00007799 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007800 new_skb->len, base_flags,
7801 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007802 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007803 dev_kfree_skb(new_skb);
7804 ret = -1;
7805 }
Michael Chanc58ec932005-09-17 00:46:27 -07007806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 }
7808
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007810 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007811 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812}
7813
Matt Carlson2ffcc982011-05-19 12:12:44 +00007814static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007815
7816/* Use GSO to workaround a rare TSO bug that may be triggered when the
7817 * TSO header is greater than 80 bytes.
7818 */
7819static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7820{
7821 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007822 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007823
7824 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007825 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007826 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007827
7828 /* netif_tx_stop_queue() must be done before checking
7829 * checking tx index in tg3_tx_avail() below, because in
7830 * tg3_tx(), we update tx index before checking for
7831 * netif_tx_queue_stopped().
7832 */
7833 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007834 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007835 return NETDEV_TX_BUSY;
7836
7837 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007838 }
7839
7840 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007841 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007842 goto tg3_tso_bug_end;
7843
7844 do {
7845 nskb = segs;
7846 segs = segs->next;
7847 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007848 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007849 } while (segs);
7850
7851tg3_tso_bug_end:
7852 dev_kfree_skb(skb);
7853
7854 return NETDEV_TX_OK;
7855}
Michael Chan52c0fd82006-06-29 20:15:54 -07007856
Michael Chan5a6f3072006-03-20 22:28:05 -08007857/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007858 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007859 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007860static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007861{
7862 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007863 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007864 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007865 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007866 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007867 struct tg3_napi *tnapi;
7868 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007869 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007870
Matt Carlson24f4efd2009-11-13 13:03:35 +00007871 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7872 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007873 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007874 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
Matt Carlson84b67b22011-07-27 14:20:52 +00007876 budget = tg3_tx_avail(tnapi);
7877
Michael Chan00b70502006-06-17 21:58:45 -07007878 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007879 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007880 * interrupt. Furthermore, IRQ processing runs lockless so we have
7881 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007883 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007884 if (!netif_tx_queue_stopped(txq)) {
7885 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007886
7887 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007888 netdev_err(dev,
7889 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 return NETDEV_TX_BUSY;
7892 }
7893
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007894 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007896 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007898
Matt Carlsonbe98da62010-07-11 09:31:46 +00007899 mss = skb_shinfo(skb)->gso_size;
7900 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007901 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007902 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903
7904 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007905 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7906 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907
Matt Carlson34195c32010-07-11 09:31:42 +00007908 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007909 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910
Eric Dumazeta5a11952012-01-23 01:22:09 +00007911 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007912
Eric Dumazeta5a11952012-01-23 01:22:09 +00007913 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007914 iph->check = 0;
7915 iph->tot_len = htons(mss + hdr_len);
7916 }
7917
Michael Chan52c0fd82006-06-29 20:15:54 -07007918 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007919 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007920 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007921
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7923 TXD_FLAG_CPU_POST_DMA);
7924
Joe Perches63c3a662011-04-26 08:12:10 +00007925 if (tg3_flag(tp, HW_TSO_1) ||
7926 tg3_flag(tp, HW_TSO_2) ||
7927 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007928 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007930 } else
7931 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7932 iph->daddr, 0,
7933 IPPROTO_TCP,
7934 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
Joe Perches63c3a662011-04-26 08:12:10 +00007936 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007937 mss |= (hdr_len & 0xc) << 12;
7938 if (hdr_len & 0x10)
7939 base_flags |= 0x00000010;
7940 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007941 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007942 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007943 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007944 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007945 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 int tsflags;
7947
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007948 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 mss |= (tsflags << 11);
7950 }
7951 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007952 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 int tsflags;
7954
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007955 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956 base_flags |= tsflags << 12;
7957 }
7958 }
7959 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007960
Matt Carlson93a700a2011-08-31 11:44:54 +00007961 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7962 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7963 base_flags |= TXD_FLAG_JMB_PKT;
7964
Matt Carlson92cd3a12011-07-27 14:20:47 +00007965 if (vlan_tx_tag_present(skb)) {
7966 base_flags |= TXD_FLAG_VLAN;
7967 vlan = vlan_tx_tag_get(skb);
7968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007970 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7971 tg3_flag(tp, TX_TSTAMP_EN)) {
7972 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7973 base_flags |= TXD_FLAG_HWTSTAMP;
7974 }
7975
Alexander Duyckf4188d82009-12-02 16:48:38 +00007976 len = skb_headlen(skb);
7977
7978 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007979 if (pci_dma_mapping_error(tp->pdev, mapping))
7980 goto drop;
7981
David S. Miller90079ce2008-09-11 04:52:51 -07007982
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007983 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007984 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985
7986 would_hit_hwbug = 0;
7987
Joe Perches63c3a662011-04-26 08:12:10 +00007988 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007989 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990
Matt Carlson84b67b22011-07-27 14:20:52 +00007991 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007992 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007993 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007994 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007995 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007996 u32 tmp_mss = mss;
7997
7998 if (!tg3_flag(tp, HW_TSO_1) &&
7999 !tg3_flag(tp, HW_TSO_2) &&
8000 !tg3_flag(tp, HW_TSO_3))
8001 tmp_mss = 0;
8002
Matt Carlsonc5665a52012-02-13 10:20:12 +00008003 /* Now loop through additional data
8004 * fragments, and queue them.
8005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 last = skb_shinfo(skb)->nr_frags - 1;
8007 for (i = 0; i <= last; i++) {
8008 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8009
Eric Dumazet9e903e02011-10-18 21:00:24 +00008010 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008011 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008012 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008014 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008015 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008016 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008017 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008018 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019
Matt Carlsonb9e45482011-11-04 09:14:59 +00008020 if (!budget ||
8021 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008022 len, base_flags |
8023 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008024 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008025 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008026 break;
8027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 }
8029 }
8030
8031 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008032 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033
8034 /* If the workaround fails due to memory/mapping
8035 * failure, silently drop this packet.
8036 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008037 entry = tnapi->tx_prod;
8038 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008039 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008040 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008041 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042 }
8043
Richard Cochrand515b452011-06-19 03:31:41 +00008044 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008045 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008046
Michael Chan6541b802012-03-04 14:48:14 +00008047 /* Sync BD data before updating mailbox */
8048 wmb();
8049
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00008051 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008053 tnapi->tx_prod = entry;
8054 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008055 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008056
8057 /* netif_tx_stop_queue() must be done before checking
8058 * checking tx index in tg3_tx_avail() below, because in
8059 * tg3_tx(), we update tx index before checking for
8060 * netif_tx_queue_stopped().
8061 */
8062 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008063 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008064 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066
Eric Dumazetcdd0db02009-05-28 00:00:41 +00008067 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008069
8070dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008071 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008072 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008073drop:
8074 dev_kfree_skb(skb);
8075drop_nofree:
8076 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008077 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078}
8079
Matt Carlson6e01b202011-08-19 13:58:20 +00008080static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8081{
8082 if (enable) {
8083 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8084 MAC_MODE_PORT_MODE_MASK);
8085
8086 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8087
8088 if (!tg3_flag(tp, 5705_PLUS))
8089 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8090
8091 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8092 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8093 else
8094 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8095 } else {
8096 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8097
8098 if (tg3_flag(tp, 5705_PLUS) ||
8099 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008100 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008101 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8102 }
8103
8104 tw32(MAC_MODE, tp->mac_mode);
8105 udelay(40);
8106}
8107
Matt Carlson941ec902011-08-19 13:58:23 +00008108static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008109{
Matt Carlson941ec902011-08-19 13:58:23 +00008110 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008111
8112 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008113 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008114
Matt Carlson941ec902011-08-19 13:58:23 +00008115 if (extlpbk && tg3_phy_set_extloopbk(tp))
8116 return -EIO;
8117
8118 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008119 switch (speed) {
8120 case SPEED_10:
8121 break;
8122 case SPEED_100:
8123 bmcr |= BMCR_SPEED100;
8124 break;
8125 case SPEED_1000:
8126 default:
8127 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8128 speed = SPEED_100;
8129 bmcr |= BMCR_SPEED100;
8130 } else {
8131 speed = SPEED_1000;
8132 bmcr |= BMCR_SPEED1000;
8133 }
8134 }
8135
Matt Carlson941ec902011-08-19 13:58:23 +00008136 if (extlpbk) {
8137 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8138 tg3_readphy(tp, MII_CTRL1000, &val);
8139 val |= CTL1000_AS_MASTER |
8140 CTL1000_ENABLE_MASTER;
8141 tg3_writephy(tp, MII_CTRL1000, val);
8142 } else {
8143 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8144 MII_TG3_FET_PTEST_TRIM_2;
8145 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8146 }
8147 } else
8148 bmcr |= BMCR_LOOPBACK;
8149
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008150 tg3_writephy(tp, MII_BMCR, bmcr);
8151
8152 /* The write needs to be flushed for the FETs */
8153 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8154 tg3_readphy(tp, MII_BMCR, &bmcr);
8155
8156 udelay(40);
8157
8158 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008159 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008160 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008161 MII_TG3_FET_PTEST_FRC_TX_LINK |
8162 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8163
8164 /* The write needs to be flushed for the AC131 */
8165 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8166 }
8167
8168 /* Reset to prevent losing 1st rx packet intermittently */
8169 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8170 tg3_flag(tp, 5780_CLASS)) {
8171 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8172 udelay(10);
8173 tw32_f(MAC_RX_MODE, tp->rx_mode);
8174 }
8175
8176 mac_mode = tp->mac_mode &
8177 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8178 if (speed == SPEED_1000)
8179 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8180 else
8181 mac_mode |= MAC_MODE_PORT_MODE_MII;
8182
Joe Perches41535772013-02-16 11:20:04 +00008183 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008184 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8185
8186 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8187 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8188 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8189 mac_mode |= MAC_MODE_LINK_POLARITY;
8190
8191 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8192 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8193 }
8194
8195 tw32(MAC_MODE, mac_mode);
8196 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008197
8198 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008199}
8200
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008201static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008202{
8203 struct tg3 *tp = netdev_priv(dev);
8204
8205 if (features & NETIF_F_LOOPBACK) {
8206 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8207 return;
8208
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008209 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008210 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008211 netif_carrier_on(tp->dev);
8212 spin_unlock_bh(&tp->lock);
8213 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8214 } else {
8215 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8216 return;
8217
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008218 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008219 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008220 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008221 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008222 spin_unlock_bh(&tp->lock);
8223 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8224 }
8225}
8226
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008227static netdev_features_t tg3_fix_features(struct net_device *dev,
8228 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008229{
8230 struct tg3 *tp = netdev_priv(dev);
8231
Joe Perches63c3a662011-04-26 08:12:10 +00008232 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008233 features &= ~NETIF_F_ALL_TSO;
8234
8235 return features;
8236}
8237
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008238static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008239{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008240 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008241
8242 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8243 tg3_set_loopback(dev, features);
8244
8245 return 0;
8246}
8247
Matt Carlson21f581a2009-08-28 14:00:25 +00008248static void tg3_rx_prodring_free(struct tg3 *tp,
8249 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008251 int i;
8252
Matt Carlson8fea32b2010-09-15 08:59:58 +00008253 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008254 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008255 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008256 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008257 tp->rx_pkt_map_sz);
8258
Joe Perches63c3a662011-04-26 08:12:10 +00008259 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008260 for (i = tpr->rx_jmb_cons_idx;
8261 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008262 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008263 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008264 TG3_RX_JMB_MAP_SZ);
8265 }
8266 }
8267
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008268 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270
Matt Carlson2c49a442010-09-30 10:34:35 +00008271 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008272 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008273 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274
Joe Perches63c3a662011-04-26 08:12:10 +00008275 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008276 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008277 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008278 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279 }
8280}
8281
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008282/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283 *
8284 * The chip has been shut down and the driver detached from
8285 * the networking, so no interrupts or new tx packets will
8286 * end up in the driver. tp->{tx,}lock are held and thus
8287 * we may not sleep.
8288 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008289static int tg3_rx_prodring_alloc(struct tg3 *tp,
8290 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291{
Matt Carlson287be122009-08-28 13:58:46 +00008292 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008294 tpr->rx_std_cons_idx = 0;
8295 tpr->rx_std_prod_idx = 0;
8296 tpr->rx_jmb_cons_idx = 0;
8297 tpr->rx_jmb_prod_idx = 0;
8298
Matt Carlson8fea32b2010-09-15 08:59:58 +00008299 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008300 memset(&tpr->rx_std_buffers[0], 0,
8301 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008302 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008303 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008304 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008305 goto done;
8306 }
8307
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008309 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310
Matt Carlson287be122009-08-28 13:58:46 +00008311 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008312 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008313 tp->dev->mtu > ETH_DATA_LEN)
8314 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8315 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad42005-07-25 12:31:17 -07008316
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 /* Initialize invariants of the rings, we only set this
8318 * stuff once. This works because the card does not
8319 * write into the rx buffer posting rings.
8320 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008321 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008322 struct tg3_rx_buffer_desc *rxd;
8323
Matt Carlson21f581a2009-08-28 14:00:25 +00008324 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008325 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8327 rxd->opaque = (RXD_OPAQUE_RING_STD |
8328 (i << RXD_OPAQUE_INDEX_SHIFT));
8329 }
8330
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008331 /* Now allocate fresh SKBs for each rx ring. */
8332 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008333 unsigned int frag_size;
8334
8335 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8336 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008337 netdev_warn(tp->dev,
8338 "Using a smaller RX standard ring. Only "
8339 "%d out of %d buffers were allocated "
8340 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008341 if (i == 0)
8342 goto initfail;
8343 tp->rx_pending = i;
8344 break;
8345 }
8346 }
8347
Joe Perches63c3a662011-04-26 08:12:10 +00008348 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008349 goto done;
8350
Matt Carlson2c49a442010-09-30 10:34:35 +00008351 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008352
Joe Perches63c3a662011-04-26 08:12:10 +00008353 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008354 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355
Matt Carlson2c49a442010-09-30 10:34:35 +00008356 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008357 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358
Matt Carlson0d86df82010-02-17 15:17:00 +00008359 rxd = &tpr->rx_jmb[i].std;
8360 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8361 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8362 RXD_FLAG_JUMBO;
8363 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8364 (i << RXD_OPAQUE_INDEX_SHIFT));
8365 }
8366
8367 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008368 unsigned int frag_size;
8369
8370 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8371 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008372 netdev_warn(tp->dev,
8373 "Using a smaller RX jumbo ring. Only %d "
8374 "out of %d buffers were allocated "
8375 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008376 if (i == 0)
8377 goto initfail;
8378 tp->rx_jumbo_pending = i;
8379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 }
8381 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008382
8383done:
Michael Chan32d8c572006-07-25 16:38:29 -07008384 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008385
8386initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008387 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008388 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389}
8390
Matt Carlson21f581a2009-08-28 14:00:25 +00008391static void tg3_rx_prodring_fini(struct tg3 *tp,
8392 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393{
Matt Carlson21f581a2009-08-28 14:00:25 +00008394 kfree(tpr->rx_std_buffers);
8395 tpr->rx_std_buffers = NULL;
8396 kfree(tpr->rx_jmb_buffers);
8397 tpr->rx_jmb_buffers = NULL;
8398 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008399 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8400 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008401 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008403 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008404 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8405 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008406 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008408}
8409
Matt Carlson21f581a2009-08-28 14:00:25 +00008410static int tg3_rx_prodring_init(struct tg3 *tp,
8411 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008412{
Matt Carlson2c49a442010-09-30 10:34:35 +00008413 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8414 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008415 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008416 return -ENOMEM;
8417
Matt Carlson4bae65c2010-11-24 08:31:52 +00008418 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8419 TG3_RX_STD_RING_BYTES(tp),
8420 &tpr->rx_std_mapping,
8421 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008422 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008423 goto err_out;
8424
Joe Perches63c3a662011-04-26 08:12:10 +00008425 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008426 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008427 GFP_KERNEL);
8428 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008429 goto err_out;
8430
Matt Carlson4bae65c2010-11-24 08:31:52 +00008431 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8432 TG3_RX_JMB_RING_BYTES(tp),
8433 &tpr->rx_jmb_mapping,
8434 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008435 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008436 goto err_out;
8437 }
8438
8439 return 0;
8440
8441err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008442 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008443 return -ENOMEM;
8444}
8445
8446/* Free up pending packets in all rx/tx rings.
8447 *
8448 * The chip has been shut down and the driver detached from
8449 * the networking, so no interrupts or new tx packets will
8450 * end up in the driver. tp->{tx,}lock is not held and we are not
8451 * in an interrupt context and thus may sleep.
8452 */
8453static void tg3_free_rings(struct tg3 *tp)
8454{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008455 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008456
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008457 for (j = 0; j < tp->irq_cnt; j++) {
8458 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008459
Matt Carlson8fea32b2010-09-15 08:59:58 +00008460 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008461
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008462 if (!tnapi->tx_buffers)
8463 continue;
8464
Matt Carlson0d681b22011-07-27 14:20:49 +00008465 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8466 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008467
Matt Carlson0d681b22011-07-27 14:20:49 +00008468 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008469 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008470
Matt Carlsonba1142e2011-11-04 09:15:00 +00008471 tg3_tx_skb_unmap(tnapi, i,
8472 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008473
8474 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008475 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008476 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008477 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008478}
8479
8480/* Initialize tx/rx rings for packet processing.
8481 *
8482 * The chip has been shut down and the driver detached from
8483 * the networking, so no interrupts or new tx packets will
8484 * end up in the driver. tp->{tx,}lock are held and thus
8485 * we may not sleep.
8486 */
8487static int tg3_init_rings(struct tg3 *tp)
8488{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008489 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008490
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008491 /* Free up all the SKBs. */
8492 tg3_free_rings(tp);
8493
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008494 for (i = 0; i < tp->irq_cnt; i++) {
8495 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008496
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008497 tnapi->last_tag = 0;
8498 tnapi->last_irq_tag = 0;
8499 tnapi->hw_status->status = 0;
8500 tnapi->hw_status->status_tag = 0;
8501 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8502
8503 tnapi->tx_prod = 0;
8504 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008505 if (tnapi->tx_ring)
8506 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008507
8508 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008509 if (tnapi->rx_rcb)
8510 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008511
Matt Carlson8fea32b2010-09-15 08:59:58 +00008512 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008513 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008514 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008515 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008516 }
Matt Carlson72334482009-08-28 14:03:01 +00008517
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008518 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008519}
8520
Michael Chan49a359e2012-09-28 07:12:37 +00008521static void tg3_mem_tx_release(struct tg3 *tp)
8522{
8523 int i;
8524
8525 for (i = 0; i < tp->irq_max; i++) {
8526 struct tg3_napi *tnapi = &tp->napi[i];
8527
8528 if (tnapi->tx_ring) {
8529 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8530 tnapi->tx_ring, tnapi->tx_desc_mapping);
8531 tnapi->tx_ring = NULL;
8532 }
8533
8534 kfree(tnapi->tx_buffers);
8535 tnapi->tx_buffers = NULL;
8536 }
8537}
8538
8539static int tg3_mem_tx_acquire(struct tg3 *tp)
8540{
8541 int i;
8542 struct tg3_napi *tnapi = &tp->napi[0];
8543
8544 /* If multivector TSS is enabled, vector 0 does not handle
8545 * tx interrupts. Don't allocate any resources for it.
8546 */
8547 if (tg3_flag(tp, ENABLE_TSS))
8548 tnapi++;
8549
8550 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8551 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8552 TG3_TX_RING_SIZE, GFP_KERNEL);
8553 if (!tnapi->tx_buffers)
8554 goto err_out;
8555
8556 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8557 TG3_TX_RING_BYTES,
8558 &tnapi->tx_desc_mapping,
8559 GFP_KERNEL);
8560 if (!tnapi->tx_ring)
8561 goto err_out;
8562 }
8563
8564 return 0;
8565
8566err_out:
8567 tg3_mem_tx_release(tp);
8568 return -ENOMEM;
8569}
8570
8571static void tg3_mem_rx_release(struct tg3 *tp)
8572{
8573 int i;
8574
8575 for (i = 0; i < tp->irq_max; i++) {
8576 struct tg3_napi *tnapi = &tp->napi[i];
8577
8578 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8579
8580 if (!tnapi->rx_rcb)
8581 continue;
8582
8583 dma_free_coherent(&tp->pdev->dev,
8584 TG3_RX_RCB_RING_BYTES(tp),
8585 tnapi->rx_rcb,
8586 tnapi->rx_rcb_mapping);
8587 tnapi->rx_rcb = NULL;
8588 }
8589}
8590
8591static int tg3_mem_rx_acquire(struct tg3 *tp)
8592{
8593 unsigned int i, limit;
8594
8595 limit = tp->rxq_cnt;
8596
8597 /* If RSS is enabled, we need a (dummy) producer ring
8598 * set on vector zero. This is the true hw prodring.
8599 */
8600 if (tg3_flag(tp, ENABLE_RSS))
8601 limit++;
8602
8603 for (i = 0; i < limit; i++) {
8604 struct tg3_napi *tnapi = &tp->napi[i];
8605
8606 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8607 goto err_out;
8608
8609 /* If multivector RSS is enabled, vector 0
8610 * does not handle rx or tx interrupts.
8611 * Don't allocate any resources for it.
8612 */
8613 if (!i && tg3_flag(tp, ENABLE_RSS))
8614 continue;
8615
Joe Perchesede23fa82013-08-26 22:45:23 -07008616 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8617 TG3_RX_RCB_RING_BYTES(tp),
8618 &tnapi->rx_rcb_mapping,
8619 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008620 if (!tnapi->rx_rcb)
8621 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008622 }
8623
8624 return 0;
8625
8626err_out:
8627 tg3_mem_rx_release(tp);
8628 return -ENOMEM;
8629}
8630
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008631/*
8632 * Must not be invoked with interrupt sources disabled and
8633 * the hardware shutdown down.
8634 */
8635static void tg3_free_consistent(struct tg3 *tp)
8636{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008637 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008638
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008639 for (i = 0; i < tp->irq_cnt; i++) {
8640 struct tg3_napi *tnapi = &tp->napi[i];
8641
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008642 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008643 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8644 tnapi->hw_status,
8645 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008646 tnapi->hw_status = NULL;
8647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008648 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008649
Michael Chan49a359e2012-09-28 07:12:37 +00008650 tg3_mem_rx_release(tp);
8651 tg3_mem_tx_release(tp);
8652
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008654 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8655 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656 tp->hw_stats = NULL;
8657 }
8658}
8659
8660/*
8661 * Must not be invoked with interrupt sources disabled and
8662 * the hardware shutdown down. Can sleep.
8663 */
8664static int tg3_alloc_consistent(struct tg3 *tp)
8665{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008666 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008667
Joe Perchesede23fa82013-08-26 22:45:23 -07008668 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8669 sizeof(struct tg3_hw_stats),
8670 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671 if (!tp->hw_stats)
8672 goto err_out;
8673
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008674 for (i = 0; i < tp->irq_cnt; i++) {
8675 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008676 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008677
Joe Perchesede23fa82013-08-26 22:45:23 -07008678 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8679 TG3_HW_STATUS_SIZE,
8680 &tnapi->status_mapping,
8681 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008682 if (!tnapi->hw_status)
8683 goto err_out;
8684
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008685 sblk = tnapi->hw_status;
8686
Michael Chan49a359e2012-09-28 07:12:37 +00008687 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008688 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008689
Michael Chan49a359e2012-09-28 07:12:37 +00008690 /*
8691 * When RSS is enabled, the status block format changes
8692 * slightly. The "rx_jumbo_consumer", "reserved",
8693 * and "rx_mini_consumer" members get mapped to the
8694 * other three rx return ring producer indexes.
8695 */
8696 switch (i) {
8697 case 1:
8698 prodptr = &sblk->idx[0].rx_producer;
8699 break;
8700 case 2:
8701 prodptr = &sblk->rx_jumbo_consumer;
8702 break;
8703 case 3:
8704 prodptr = &sblk->reserved;
8705 break;
8706 case 4:
8707 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008708 break;
8709 }
Michael Chan49a359e2012-09-28 07:12:37 +00008710 tnapi->rx_rcb_prod_idx = prodptr;
8711 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008712 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008713 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008714 }
8715
Michael Chan49a359e2012-09-28 07:12:37 +00008716 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8717 goto err_out;
8718
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719 return 0;
8720
8721err_out:
8722 tg3_free_consistent(tp);
8723 return -ENOMEM;
8724}
8725
8726#define MAX_WAIT_CNT 1000
8727
8728/* To stop a block, clear the enable bit and poll till it
8729 * clears. tp->lock is held.
8730 */
Joe Perches953c96e2013-04-09 10:18:14 +00008731static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732{
8733 unsigned int i;
8734 u32 val;
8735
Joe Perches63c3a662011-04-26 08:12:10 +00008736 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737 switch (ofs) {
8738 case RCVLSC_MODE:
8739 case DMAC_MODE:
8740 case MBFREE_MODE:
8741 case BUFMGR_MODE:
8742 case MEMARB_MODE:
8743 /* We can't enable/disable these bits of the
8744 * 5705/5750, just say success.
8745 */
8746 return 0;
8747
8748 default:
8749 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751 }
8752
8753 val = tr32(ofs);
8754 val &= ~enable_bit;
8755 tw32_f(ofs, val);
8756
8757 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008758 if (pci_channel_offline(tp->pdev)) {
8759 dev_err(&tp->pdev->dev,
8760 "tg3_stop_block device offline, "
8761 "ofs=%lx enable_bit=%x\n",
8762 ofs, enable_bit);
8763 return -ENODEV;
8764 }
8765
Linus Torvalds1da177e2005-04-16 15:20:36 -07008766 udelay(100);
8767 val = tr32(ofs);
8768 if ((val & enable_bit) == 0)
8769 break;
8770 }
8771
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008772 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008773 dev_err(&tp->pdev->dev,
8774 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8775 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776 return -ENODEV;
8777 }
8778
8779 return 0;
8780}
8781
8782/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008783static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784{
8785 int i, err;
8786
8787 tg3_disable_ints(tp);
8788
Gavin Shan6d446ec2013-06-25 15:24:32 +08008789 if (pci_channel_offline(tp->pdev)) {
8790 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8791 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8792 err = -ENODEV;
8793 goto err_no_dev;
8794 }
8795
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796 tp->rx_mode &= ~RX_MODE_ENABLE;
8797 tw32_f(MAC_RX_MODE, tp->rx_mode);
8798 udelay(10);
8799
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008800 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8801 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8802 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8803 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8804 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8805 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008806
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008807 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8808 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8809 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8810 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8811 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8812 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8813 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814
8815 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8816 tw32_f(MAC_MODE, tp->mac_mode);
8817 udelay(40);
8818
8819 tp->tx_mode &= ~TX_MODE_ENABLE;
8820 tw32_f(MAC_TX_MODE, tp->tx_mode);
8821
8822 for (i = 0; i < MAX_WAIT_CNT; i++) {
8823 udelay(100);
8824 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8825 break;
8826 }
8827 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008828 dev_err(&tp->pdev->dev,
8829 "%s timed out, TX_MODE_ENABLE will not clear "
8830 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008831 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832 }
8833
Michael Chane6de8ad2005-05-05 14:42:41 -07008834 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008835 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8836 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837
8838 tw32(FTQ_RESET, 0xffffffff);
8839 tw32(FTQ_RESET, 0x00000000);
8840
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008841 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8842 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843
Gavin Shan6d446ec2013-06-25 15:24:32 +08008844err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008845 for (i = 0; i < tp->irq_cnt; i++) {
8846 struct tg3_napi *tnapi = &tp->napi[i];
8847 if (tnapi->hw_status)
8848 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851 return err;
8852}
8853
Michael Chanee6a99b2007-07-18 21:49:10 -07008854/* Save PCI command register before chip reset */
8855static void tg3_save_pci_state(struct tg3 *tp)
8856{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008857 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008858}
8859
8860/* Restore PCI state after chip reset */
8861static void tg3_restore_pci_state(struct tg3 *tp)
8862{
8863 u32 val;
8864
8865 /* Re-enable indirect register accesses. */
8866 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8867 tp->misc_host_ctrl);
8868
8869 /* Set MAX PCI retry to zero. */
8870 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008871 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008872 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008873 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008874 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008875 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008876 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008877 PCISTATE_ALLOW_APE_SHMEM_WR |
8878 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008879 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8880
Matt Carlson8a6eac92007-10-21 16:17:55 -07008881 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008882
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008883 if (!tg3_flag(tp, PCI_EXPRESS)) {
8884 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8885 tp->pci_cacheline_sz);
8886 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8887 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008888 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008889
Michael Chanee6a99b2007-07-18 21:49:10 -07008890 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008891 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008892 u16 pcix_cmd;
8893
8894 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8895 &pcix_cmd);
8896 pcix_cmd &= ~PCI_X_CMD_ERO;
8897 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8898 pcix_cmd);
8899 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008900
Joe Perches63c3a662011-04-26 08:12:10 +00008901 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008902
8903 /* Chip reset on 5780 will reset MSI enable bit,
8904 * so need to restore it.
8905 */
Joe Perches63c3a662011-04-26 08:12:10 +00008906 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008907 u16 ctrl;
8908
8909 pci_read_config_word(tp->pdev,
8910 tp->msi_cap + PCI_MSI_FLAGS,
8911 &ctrl);
8912 pci_write_config_word(tp->pdev,
8913 tp->msi_cap + PCI_MSI_FLAGS,
8914 ctrl | PCI_MSI_FLAGS_ENABLE);
8915 val = tr32(MSGINT_MODE);
8916 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8917 }
8918 }
8919}
8920
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921/* tp->lock is held. */
8922static int tg3_chip_reset(struct tg3 *tp)
8923{
8924 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008925 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008926 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927
David S. Millerf49639e2006-06-09 11:58:36 -07008928 tg3_nvram_lock(tp);
8929
Matt Carlson77b483f2008-08-15 14:07:24 -07008930 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8931
David S. Millerf49639e2006-06-09 11:58:36 -07008932 /* No matching tg3_nvram_unlock() after this because
8933 * chip reset below will undo the nvram lock.
8934 */
8935 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936
Michael Chanee6a99b2007-07-18 21:49:10 -07008937 /* GRC_MISC_CFG core clock reset will clear the memory
8938 * enable bit in PCI register 4 and the MSI enable bit
8939 * on some chips, so we save relevant registers here.
8940 */
8941 tg3_save_pci_state(tp);
8942
Joe Perches41535772013-02-16 11:20:04 +00008943 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008944 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008945 tw32(GRC_FASTBOOT_PC, 0);
8946
Linus Torvalds1da177e2005-04-16 15:20:36 -07008947 /*
8948 * We must avoid the readl() that normally takes place.
8949 * It locks machines, causes machine checks, and other
8950 * fun things. So, temporarily disable the 5701
8951 * hardware workaround, while we do the reset.
8952 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008953 write_op = tp->write32;
8954 if (write_op == tg3_write_flush_reg32)
8955 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956
Michael Chand18edcb2007-03-24 20:57:11 -07008957 /* Prevent the irq handler from reading or writing PCI registers
8958 * during chip reset when the memory enable bit in the PCI command
8959 * register may be cleared. The chip does not generate interrupt
8960 * at this time, but the irq handler may still be called due to irq
8961 * sharing or irqpoll.
8962 */
Joe Perches63c3a662011-04-26 08:12:10 +00008963 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008964 for (i = 0; i < tp->irq_cnt; i++) {
8965 struct tg3_napi *tnapi = &tp->napi[i];
8966 if (tnapi->hw_status) {
8967 tnapi->hw_status->status = 0;
8968 tnapi->hw_status->status_tag = 0;
8969 }
8970 tnapi->last_tag = 0;
8971 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008972 }
Michael Chand18edcb2007-03-24 20:57:11 -07008973 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008974
8975 for (i = 0; i < tp->irq_cnt; i++)
8976 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008977
Joe Perches41535772013-02-16 11:20:04 +00008978 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008979 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8980 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8981 }
8982
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983 /* do the reset */
8984 val = GRC_MISC_CFG_CORECLK_RESET;
8985
Joe Perches63c3a662011-04-26 08:12:10 +00008986 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008987 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008988 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008989 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008990 tr32(TG3_PCIE_PHY_TSTCTL) ==
8991 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8992 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8993
Joe Perches41535772013-02-16 11:20:04 +00008994 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995 tw32(GRC_MISC_CFG, (1 << 29));
8996 val |= (1 << 29);
8997 }
8998 }
8999
Joe Perches41535772013-02-16 11:20:04 +00009000 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009001 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9002 tw32(GRC_VCPU_EXT_CTRL,
9003 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9004 }
9005
Matt Carlsonf37500d2010-08-02 11:25:59 +00009006 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009007 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009008 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009010 tw32(GRC_MISC_CFG, val);
9011
Michael Chan1ee582d2005-08-09 20:16:46 -07009012 /* restore 5701 hardware bug workaround write method */
9013 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014
9015 /* Unfortunately, we have to delay before the PCI read back.
9016 * Some 575X chips even will not respond to a PCI cfg access
9017 * when the reset command is given to the chip.
9018 *
9019 * How do these hardware designers expect things to work
9020 * properly if the PCI write is posted for a long period
9021 * of time? It is always necessary to have some method by
9022 * which a register read back can occur to push the write
9023 * out which does the reset.
9024 *
9025 * For most tg3 variants the trick below was working.
9026 * Ho hum...
9027 */
9028 udelay(120);
9029
9030 /* Flush PCI posted writes. The normal MMIO registers
9031 * are inaccessible at this time so this is the only
9032 * way to make this reliably (actually, this is no longer
9033 * the case, see above). I tried to use indirect
9034 * register read/write but this upset some 5701 variants.
9035 */
9036 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9037
9038 udelay(120);
9039
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009040 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009041 u16 val16;
9042
Joe Perches41535772013-02-16 11:20:04 +00009043 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009044 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009045 u32 cfg_val;
9046
9047 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009048 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009049 udelay(100);
9050
9051 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9052 pci_write_config_dword(tp->pdev, 0xc4,
9053 cfg_val | (1 << 15));
9054 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009055
Matt Carlsone7126992009-08-25 10:08:16 +00009056 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009057 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009058 /*
9059 * Older PCIe devices only support the 128 byte
9060 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009061 */
Joe Perches63c3a662011-04-26 08:12:10 +00009062 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009063 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9064 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009065
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009066 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009067 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009068 PCI_EXP_DEVSTA_CED |
9069 PCI_EXP_DEVSTA_NFED |
9070 PCI_EXP_DEVSTA_FED |
9071 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072 }
9073
Michael Chanee6a99b2007-07-18 21:49:10 -07009074 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009075
Joe Perches63c3a662011-04-26 08:12:10 +00009076 tg3_flag_clear(tp, CHIP_RESETTING);
9077 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009078
Michael Chanee6a99b2007-07-18 21:49:10 -07009079 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009080 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009081 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009082 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009083
Joe Perches41535772013-02-16 11:20:04 +00009084 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085 tg3_stop_fw(tp);
9086 tw32(0x5000, 0x400);
9087 }
9088
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009089 if (tg3_flag(tp, IS_SSB_CORE)) {
9090 /*
9091 * BCM4785: In order to avoid repercussions from using
9092 * potentially defective internal ROM, stop the Rx RISC CPU,
9093 * which is not required.
9094 */
9095 tg3_stop_fw(tp);
9096 tg3_halt_cpu(tp, RX_CPU_BASE);
9097 }
9098
Nithin Sujirfb03a432013-05-21 12:57:32 +00009099 err = tg3_poll_fw(tp);
9100 if (err)
9101 return err;
9102
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103 tw32(GRC_MODE, tp->grc_mode);
9104
Joe Perches41535772013-02-16 11:20:04 +00009105 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009106 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107
9108 tw32(0xc4, val | (1 << 15));
9109 }
9110
9111 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009112 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009114 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9116 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9117 }
9118
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009119 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009120 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009121 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009122 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009123 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009124 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009126 val = 0;
9127
9128 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129 udelay(40);
9130
Matt Carlson77b483f2008-08-15 14:07:24 -07009131 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9132
Matt Carlson0a9140c2009-08-28 12:27:50 +00009133 tg3_mdio_start(tp);
9134
Joe Perches63c3a662011-04-26 08:12:10 +00009135 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009136 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9137 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009138 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009139 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140
9141 tw32(0x7c00, val | (1 << 25));
9142 }
9143
Joe Perches41535772013-02-16 11:20:04 +00009144 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00009145 val = tr32(TG3_CPMU_CLCK_ORIDE);
9146 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9147 }
9148
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009150 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009151 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9152 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9153
Joe Perches63c3a662011-04-26 08:12:10 +00009154 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9156 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9157 u32 nic_cfg;
9158
9159 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9160 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009161 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009162 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009163 if (tg3_flag(tp, 5750_PLUS))
9164 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009165
9166 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9167 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9168 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9169 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9170 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171 }
9172 }
9173
9174 return 0;
9175}
9176
Matt Carlson65ec6982012-02-28 23:33:37 +00009177static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9178static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009179
Linus Torvalds1da177e2005-04-16 15:20:36 -07009180/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009181static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182{
9183 int err;
9184
9185 tg3_stop_fw(tp);
9186
Michael Chan944d9802005-05-29 14:57:48 -07009187 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009189 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190 err = tg3_chip_reset(tp);
9191
Joe Perches953c96e2013-04-09 10:18:14 +00009192 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009193
Michael Chan944d9802005-05-29 14:57:48 -07009194 tg3_write_sig_legacy(tp, kind);
9195 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196
Matt Carlson92feeab2011-12-08 14:40:14 +00009197 if (tp->hw_stats) {
9198 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009199 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009200 tg3_get_estats(tp, &tp->estats_prev);
9201
9202 /* And make sure the next sample is new data */
9203 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9204 }
9205
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009206 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207}
9208
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209static int tg3_set_mac_addr(struct net_device *dev, void *p)
9210{
9211 struct tg3 *tp = netdev_priv(dev);
9212 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009213 int err = 0;
9214 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215
Michael Chanf9804dd2005-09-27 12:13:10 -07009216 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009217 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009218
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9220
Michael Chane75f7c92006-03-20 21:33:26 -08009221 if (!netif_running(dev))
9222 return 0;
9223
Joe Perches63c3a662011-04-26 08:12:10 +00009224 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009225 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009226
Michael Chan986e0ae2007-05-05 12:10:20 -07009227 addr0_high = tr32(MAC_ADDR_0_HIGH);
9228 addr0_low = tr32(MAC_ADDR_0_LOW);
9229 addr1_high = tr32(MAC_ADDR_1_HIGH);
9230 addr1_low = tr32(MAC_ADDR_1_LOW);
9231
9232 /* Skip MAC addr 1 if ASF is using it. */
9233 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9234 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009235 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009236 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009237 spin_lock_bh(&tp->lock);
9238 __tg3_set_mac_addr(tp, skip_mac_1);
9239 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009240
Michael Chanb9ec6c12006-07-25 16:37:27 -07009241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242}
9243
9244/* tp->lock is held. */
9245static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9246 dma_addr_t mapping, u32 maxlen_flags,
9247 u32 nic_addr)
9248{
9249 tg3_write_mem(tp,
9250 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9251 ((u64) mapping >> 32));
9252 tg3_write_mem(tp,
9253 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9254 ((u64) mapping & 0xffffffff));
9255 tg3_write_mem(tp,
9256 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9257 maxlen_flags);
9258
Joe Perches63c3a662011-04-26 08:12:10 +00009259 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260 tg3_write_mem(tp,
9261 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9262 nic_addr);
9263}
9264
Michael Chana489b6d2012-09-28 07:12:39 +00009265
9266static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009267{
Michael Chana489b6d2012-09-28 07:12:39 +00009268 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009269
Joe Perches63c3a662011-04-26 08:12:10 +00009270 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009271 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9272 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9273 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009274 } else {
9275 tw32(HOSTCC_TXCOL_TICKS, 0);
9276 tw32(HOSTCC_TXMAX_FRAMES, 0);
9277 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009278
9279 for (; i < tp->txq_cnt; i++) {
9280 u32 reg;
9281
9282 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9283 tw32(reg, ec->tx_coalesce_usecs);
9284 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9285 tw32(reg, ec->tx_max_coalesced_frames);
9286 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9287 tw32(reg, ec->tx_max_coalesced_frames_irq);
9288 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009289 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009290
Michael Chana489b6d2012-09-28 07:12:39 +00009291 for (; i < tp->irq_max - 1; i++) {
9292 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9293 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9294 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9295 }
9296}
9297
9298static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9299{
9300 int i = 0;
9301 u32 limit = tp->rxq_cnt;
9302
Joe Perches63c3a662011-04-26 08:12:10 +00009303 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009304 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9305 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9306 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009307 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009308 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009309 tw32(HOSTCC_RXCOL_TICKS, 0);
9310 tw32(HOSTCC_RXMAX_FRAMES, 0);
9311 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009312 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009313
Michael Chana489b6d2012-09-28 07:12:39 +00009314 for (; i < limit; i++) {
9315 u32 reg;
9316
9317 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9318 tw32(reg, ec->rx_coalesce_usecs);
9319 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9320 tw32(reg, ec->rx_max_coalesced_frames);
9321 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9322 tw32(reg, ec->rx_max_coalesced_frames_irq);
9323 }
9324
9325 for (; i < tp->irq_max - 1; i++) {
9326 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9327 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9328 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9329 }
9330}
9331
9332static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9333{
9334 tg3_coal_tx_init(tp, ec);
9335 tg3_coal_rx_init(tp, ec);
9336
Joe Perches63c3a662011-04-26 08:12:10 +00009337 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009338 u32 val = ec->stats_block_coalesce_usecs;
9339
Matt Carlsonb6080e12009-09-01 13:12:00 +00009340 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9341 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9342
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009343 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009344 val = 0;
9345
9346 tw32(HOSTCC_STAT_COAL_TICKS, val);
9347 }
9348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349
9350/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009351static void tg3_tx_rcbs_disable(struct tg3 *tp)
9352{
9353 u32 txrcb, limit;
9354
9355 /* Disable all transmit rings but the first. */
9356 if (!tg3_flag(tp, 5705_PLUS))
9357 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9358 else if (tg3_flag(tp, 5717_PLUS))
9359 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9360 else if (tg3_flag(tp, 57765_CLASS) ||
9361 tg3_asic_rev(tp) == ASIC_REV_5762)
9362 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9363 else
9364 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9365
9366 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9367 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9368 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9369 BDINFO_FLAGS_DISABLED);
9370}
9371
9372/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009373static void tg3_tx_rcbs_init(struct tg3 *tp)
9374{
9375 int i = 0;
9376 u32 txrcb = NIC_SRAM_SEND_RCB;
9377
9378 if (tg3_flag(tp, ENABLE_TSS))
9379 i++;
9380
9381 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9382 struct tg3_napi *tnapi = &tp->napi[i];
9383
9384 if (!tnapi->tx_ring)
9385 continue;
9386
9387 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9388 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9389 NIC_SRAM_TX_BUFFER_DESC);
9390 }
9391}
9392
9393/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009394static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9395{
9396 u32 rxrcb, limit;
9397
9398 /* Disable all receive return rings but the first. */
9399 if (tg3_flag(tp, 5717_PLUS))
9400 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9401 else if (!tg3_flag(tp, 5705_PLUS))
9402 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9403 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9404 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9405 tg3_flag(tp, 57765_CLASS))
9406 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9407 else
9408 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9409
9410 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9411 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9412 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9413 BDINFO_FLAGS_DISABLED);
9414}
9415
9416/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009417static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9418{
9419 int i = 0;
9420 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9421
9422 if (tg3_flag(tp, ENABLE_RSS))
9423 i++;
9424
9425 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9426 struct tg3_napi *tnapi = &tp->napi[i];
9427
9428 if (!tnapi->rx_rcb)
9429 continue;
9430
9431 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9432 (tp->rx_ret_ring_mask + 1) <<
9433 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9434 }
9435}
9436
9437/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009438static void tg3_rings_reset(struct tg3 *tp)
9439{
9440 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009441 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009442 struct tg3_napi *tnapi = &tp->napi[0];
9443
Nithin Sujir328947f2013-05-23 11:11:24 +00009444 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009445
Nithin Sujir328947f2013-05-23 11:11:24 +00009446 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009447
9448 /* Disable interrupts */
9449 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009450 tp->napi[0].chk_msi_cnt = 0;
9451 tp->napi[0].last_rx_cons = 0;
9452 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009453
9454 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009455 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009456 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009457 tp->napi[i].tx_prod = 0;
9458 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009459 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009460 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009461 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9462 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009463 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009464 tp->napi[i].last_rx_cons = 0;
9465 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009466 }
Joe Perches63c3a662011-04-26 08:12:10 +00009467 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009468 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009469 } else {
9470 tp->napi[0].tx_prod = 0;
9471 tp->napi[0].tx_cons = 0;
9472 tw32_mailbox(tp->napi[0].prodmbox, 0);
9473 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9474 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009475
9476 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009477 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009478 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9479 for (i = 0; i < 16; i++)
9480 tw32_tx_mbox(mbox + i * 8, 0);
9481 }
9482
Matt Carlson2d31eca2009-09-01 12:53:31 +00009483 /* Clear status block in ram. */
9484 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9485
9486 /* Set status block DMA address */
9487 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9488 ((u64) tnapi->status_mapping >> 32));
9489 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9490 ((u64) tnapi->status_mapping & 0xffffffff));
9491
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009492 stblk = HOSTCC_STATBLCK_RING1;
9493
9494 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9495 u64 mapping = (u64)tnapi->status_mapping;
9496 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9497 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009498 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009499
9500 /* Clear status block in ram. */
9501 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009502 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009503
9504 tg3_tx_rcbs_init(tp);
9505 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009506}
9507
Matt Carlsoneb07a942011-04-20 07:57:36 +00009508static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9509{
9510 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9511
Joe Perches63c3a662011-04-26 08:12:10 +00009512 if (!tg3_flag(tp, 5750_PLUS) ||
9513 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009514 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9515 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009516 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009517 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009518 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9519 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009520 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9521 else
9522 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9523
9524 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9525 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9526
9527 val = min(nic_rep_thresh, host_rep_thresh);
9528 tw32(RCVBDI_STD_THRESH, val);
9529
Joe Perches63c3a662011-04-26 08:12:10 +00009530 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009531 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9532
Joe Perches63c3a662011-04-26 08:12:10 +00009533 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009534 return;
9535
Matt Carlson513aa6e2011-11-21 15:01:18 +00009536 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009537
9538 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9539
9540 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9541 tw32(RCVBDI_JUMBO_THRESH, val);
9542
Joe Perches63c3a662011-04-26 08:12:10 +00009543 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009544 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9545}
9546
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009547static inline u32 calc_crc(unsigned char *buf, int len)
9548{
9549 u32 reg;
9550 u32 tmp;
9551 int j, k;
9552
9553 reg = 0xffffffff;
9554
9555 for (j = 0; j < len; j++) {
9556 reg ^= buf[j];
9557
9558 for (k = 0; k < 8; k++) {
9559 tmp = reg & 0x01;
9560
9561 reg >>= 1;
9562
9563 if (tmp)
9564 reg ^= 0xedb88320;
9565 }
9566 }
9567
9568 return ~reg;
9569}
9570
9571static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9572{
9573 /* accept or reject all multicast frames */
9574 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9575 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9576 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9577 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9578}
9579
9580static void __tg3_set_rx_mode(struct net_device *dev)
9581{
9582 struct tg3 *tp = netdev_priv(dev);
9583 u32 rx_mode;
9584
9585 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9586 RX_MODE_KEEP_VLAN_TAG);
9587
9588#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9589 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9590 * flag clear.
9591 */
9592 if (!tg3_flag(tp, ENABLE_ASF))
9593 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9594#endif
9595
9596 if (dev->flags & IFF_PROMISC) {
9597 /* Promiscuous mode. */
9598 rx_mode |= RX_MODE_PROMISC;
9599 } else if (dev->flags & IFF_ALLMULTI) {
9600 /* Accept all multicast. */
9601 tg3_set_multi(tp, 1);
9602 } else if (netdev_mc_empty(dev)) {
9603 /* Reject all multicast. */
9604 tg3_set_multi(tp, 0);
9605 } else {
9606 /* Accept one or more multicast(s). */
9607 struct netdev_hw_addr *ha;
9608 u32 mc_filter[4] = { 0, };
9609 u32 regidx;
9610 u32 bit;
9611 u32 crc;
9612
9613 netdev_for_each_mc_addr(ha, dev) {
9614 crc = calc_crc(ha->addr, ETH_ALEN);
9615 bit = ~crc & 0x7f;
9616 regidx = (bit & 0x60) >> 5;
9617 bit &= 0x1f;
9618 mc_filter[regidx] |= (1 << bit);
9619 }
9620
9621 tw32(MAC_HASH_REG_0, mc_filter[0]);
9622 tw32(MAC_HASH_REG_1, mc_filter[1]);
9623 tw32(MAC_HASH_REG_2, mc_filter[2]);
9624 tw32(MAC_HASH_REG_3, mc_filter[3]);
9625 }
9626
9627 if (rx_mode != tp->rx_mode) {
9628 tp->rx_mode = rx_mode;
9629 tw32_f(MAC_RX_MODE, rx_mode);
9630 udelay(10);
9631 }
9632}
9633
Michael Chan91024262012-09-28 07:12:38 +00009634static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009635{
9636 int i;
9637
9638 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009639 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009640}
9641
9642static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009643{
9644 int i;
9645
9646 if (!tg3_flag(tp, SUPPORT_MSIX))
9647 return;
9648
Michael Chan0b3ba052012-11-14 14:44:29 +00009649 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009650 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009651 return;
9652 }
9653
9654 /* Validate table against current IRQ count */
9655 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009656 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009657 break;
9658 }
9659
9660 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009661 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009662}
9663
Matt Carlson90415472011-12-16 13:33:23 +00009664static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009665{
9666 int i = 0;
9667 u32 reg = MAC_RSS_INDIR_TBL_0;
9668
9669 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9670 u32 val = tp->rss_ind_tbl[i];
9671 i++;
9672 for (; i % 8; i++) {
9673 val <<= 4;
9674 val |= tp->rss_ind_tbl[i];
9675 }
9676 tw32(reg, val);
9677 reg += 4;
9678 }
9679}
9680
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009681static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9682{
9683 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9684 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9685 else
9686 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9687}
9688
Matt Carlson2d31eca2009-09-01 12:53:31 +00009689/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009690static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691{
9692 u32 val, rdmac_mode;
9693 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009694 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695
9696 tg3_disable_ints(tp);
9697
9698 tg3_stop_fw(tp);
9699
9700 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9701
Joe Perches63c3a662011-04-26 08:12:10 +00009702 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009703 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009705 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9706 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9707 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009708 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009709 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9710 }
9711
Nithin Sujir400dfba2013-05-18 06:26:53 +00009712 /* Enable MAC control of LPI */
9713 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9714 tg3_setup_eee(tp);
9715
Matt Carlson603f1172010-02-12 14:47:10 +00009716 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009717 tg3_phy_reset(tp);
9718
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719 err = tg3_chip_reset(tp);
9720 if (err)
9721 return err;
9722
9723 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9724
Joe Perches41535772013-02-16 11:20:04 +00009725 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009726 val = tr32(TG3_CPMU_CTRL);
9727 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9728 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009729
9730 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9731 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9732 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9733 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9734
9735 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9736 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9737 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9738 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9739
9740 val = tr32(TG3_CPMU_HST_ACC);
9741 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9742 val |= CPMU_HST_ACC_MACCLK_6_25;
9743 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009744 }
9745
Joe Perches41535772013-02-16 11:20:04 +00009746 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d932009-04-20 06:57:41 +00009747 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9748 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9749 PCIE_PWR_MGMT_L1_THRESH_4MS;
9750 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009751
9752 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9753 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9754
9755 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00009756
Matt Carlsonf40386c2009-11-02 14:24:02 +00009757 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9758 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009759 }
9760
Joe Perches63c3a662011-04-26 08:12:10 +00009761 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009762 u32 grc_mode = tr32(GRC_MODE);
9763
9764 /* Access the lower 1K of PL PCIE block registers. */
9765 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9766 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9767
9768 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9769 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9770 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9771
9772 tw32(GRC_MODE, grc_mode);
9773 }
9774
Matt Carlson55086ad2011-12-14 11:09:59 +00009775 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009776 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009777 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009778
Matt Carlson5093eed2010-11-24 08:31:45 +00009779 /* Access the lower 1K of PL PCIE block registers. */
9780 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9781 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009782
Matt Carlson5093eed2010-11-24 08:31:45 +00009783 val = tr32(TG3_PCIE_TLDLPL_PORT +
9784 TG3_PCIE_PL_LO_PHYCTL5);
9785 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9786 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009787
Matt Carlson5093eed2010-11-24 08:31:45 +00009788 tw32(GRC_MODE, grc_mode);
9789 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009790
Joe Perches41535772013-02-16 11:20:04 +00009791 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009792 u32 grc_mode;
9793
9794 /* Fix transmit hangs */
9795 val = tr32(TG3_CPMU_PADRNG_CTL);
9796 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9797 tw32(TG3_CPMU_PADRNG_CTL, val);
9798
9799 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009800
9801 /* Access the lower 1K of DL PCIE block registers. */
9802 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9803 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9804
9805 val = tr32(TG3_PCIE_TLDLPL_PORT +
9806 TG3_PCIE_DL_LO_FTSMAX);
9807 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9808 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9809 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9810
9811 tw32(GRC_MODE, grc_mode);
9812 }
9813
Matt Carlsona977dbe2010-04-12 06:58:26 +00009814 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9815 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9816 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9817 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009818 }
9819
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820 /* This works around an issue with Athlon chipsets on
9821 * B3 tigon3 silicon. This bit has no effect on any
9822 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009823 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824 */
Joe Perches63c3a662011-04-26 08:12:10 +00009825 if (!tg3_flag(tp, CPMU_PRESENT)) {
9826 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009827 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9828 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830
Joe Perches41535772013-02-16 11:20:04 +00009831 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009832 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833 val = tr32(TG3PCI_PCISTATE);
9834 val |= PCISTATE_RETRY_SAME_DMA;
9835 tw32(TG3PCI_PCISTATE, val);
9836 }
9837
Joe Perches63c3a662011-04-26 08:12:10 +00009838 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009839 /* Allow reads and writes to the
9840 * APE register and memory space.
9841 */
9842 val = tr32(TG3PCI_PCISTATE);
9843 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009844 PCISTATE_ALLOW_APE_SHMEM_WR |
9845 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009846 tw32(TG3PCI_PCISTATE, val);
9847 }
9848
Joe Perches41535772013-02-16 11:20:04 +00009849 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 /* Enable some hw fixes. */
9851 val = tr32(TG3PCI_MSI_DATA);
9852 val |= (1 << 26) | (1 << 28) | (1 << 29);
9853 tw32(TG3PCI_MSI_DATA, val);
9854 }
9855
9856 /* Descriptor ring init may make accesses to the
9857 * NIC SRAM area to setup the TX descriptors, so we
9858 * can only do this after the hardware has been
9859 * successfully reset.
9860 */
Michael Chan32d8c572006-07-25 16:38:29 -07009861 err = tg3_init_rings(tp);
9862 if (err)
9863 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864
Joe Perches63c3a662011-04-26 08:12:10 +00009865 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009866 val = tr32(TG3PCI_DMA_RW_CTRL) &
9867 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009868 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009869 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009870 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009871 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9872 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009873 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009874 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009875 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9876 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009877 /* This value is determined during the probe time DMA
9878 * engine test, tg3_test_dma.
9879 */
9880 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009882
9883 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9884 GRC_MODE_4X_NIC_SEND_RINGS |
9885 GRC_MODE_NO_TX_PHDR_CSUM |
9886 GRC_MODE_NO_RX_PHDR_CSUM);
9887 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009888
9889 /* Pseudo-header checksum is done by hardware logic and not
9890 * the offload processers, so make the chip do the pseudo-
9891 * header checksums on receive. For transmit it is more
9892 * convenient to do the pseudo-header checksum in software
9893 * as Linux does that on transmit for us in all cases.
9894 */
9895 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009896
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009897 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9898 if (tp->rxptpctl)
9899 tw32(TG3_RX_PTP_CTL,
9900 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9901
9902 if (tg3_flag(tp, PTP_CAPABLE))
9903 val |= GRC_MODE_TIME_SYNC_ENABLE;
9904
9905 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009906
9907 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9908 val = tr32(GRC_MISC_CFG);
9909 val &= ~0xff;
9910 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9911 tw32(GRC_MISC_CFG, val);
9912
9913 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009914 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009915 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009916 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009917 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009918 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009919 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9920 else
9921 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9922 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9923 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009924 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009925 int fw_len;
9926
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009927 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009928 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9929 tw32(BUFMGR_MB_POOL_ADDR,
9930 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9931 tw32(BUFMGR_MB_POOL_SIZE,
9932 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009934
Michael Chan0f893dc2005-07-25 12:30:38 -07009935 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009936 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9937 tp->bufmgr_config.mbuf_read_dma_low_water);
9938 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9939 tp->bufmgr_config.mbuf_mac_rx_low_water);
9940 tw32(BUFMGR_MB_HIGH_WATER,
9941 tp->bufmgr_config.mbuf_high_water);
9942 } else {
9943 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9944 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9945 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9946 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9947 tw32(BUFMGR_MB_HIGH_WATER,
9948 tp->bufmgr_config.mbuf_high_water_jumbo);
9949 }
9950 tw32(BUFMGR_DMA_LOW_WATER,
9951 tp->bufmgr_config.dma_low_water);
9952 tw32(BUFMGR_DMA_HIGH_WATER,
9953 tp->bufmgr_config.dma_high_water);
9954
Matt Carlsond309a462010-09-30 10:34:31 +00009955 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009956 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009957 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009958 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9959 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9960 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009961 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009962 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009963 for (i = 0; i < 2000; i++) {
9964 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9965 break;
9966 udelay(10);
9967 }
9968 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009969 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970 return -ENODEV;
9971 }
9972
Joe Perches41535772013-02-16 11:20:04 +00009973 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009974 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009975
Matt Carlsoneb07a942011-04-20 07:57:36 +00009976 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977
9978 /* Initialize TG3_BDINFO's at:
9979 * RCVDBDI_STD_BD: standard eth size rx ring
9980 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9981 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9982 *
9983 * like so:
9984 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9985 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9986 * ring attribute flags
9987 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9988 *
9989 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9990 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9991 *
9992 * The size of each ring is fixed in the firmware, but the location is
9993 * configurable.
9994 */
9995 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009996 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009998 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009999 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010000 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10001 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010003 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010004 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10006 BDINFO_FLAGS_DISABLED);
10007
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010008 /* Program the jumbo buffer descriptor ring control
10009 * blocks on those devices that have them.
10010 */
Joe Perches41535772013-02-16 11:20:04 +000010011 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010012 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013
Joe Perches63c3a662011-04-26 08:12:10 +000010014 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010016 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010018 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010019 val = TG3_RX_JMB_RING_SIZE(tp) <<
10020 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010021 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010022 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010023 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010024 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010025 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010026 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10027 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028 } else {
10029 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10030 BDINFO_FLAGS_DISABLED);
10031 }
10032
Joe Perches63c3a662011-04-26 08:12:10 +000010033 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010034 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010035 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10036 val |= (TG3_RX_STD_DMA_SZ << 2);
10037 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010038 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010039 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010040 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010041
10042 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010043
Matt Carlson411da642009-11-13 13:03:46 +000010044 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +000010045 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046
Joe Perches63c3a662011-04-26 08:12:10 +000010047 tpr->rx_jmb_prod_idx =
10048 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +000010049 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010050
Matt Carlson2d31eca2009-09-01 12:53:31 +000010051 tg3_rings_reset(tp);
10052
Linus Torvalds1da177e2005-04-16 15:20:36 -070010053 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010054 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055
10056 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010057 tw32(MAC_RX_MTU_SIZE,
10058 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059
10060 /* The slot time is changed by tg3_setup_phy if we
10061 * run at gigabit with half duplex.
10062 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010063 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10064 (6 << TX_LENGTHS_IPG_SHIFT) |
10065 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10066
Joe Perches41535772013-02-16 11:20:04 +000010067 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10068 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010069 val |= tr32(MAC_TX_LENGTHS) &
10070 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10071 TX_LENGTHS_CNT_DWN_VAL_MSK);
10072
10073 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074
10075 /* Receive rules. */
10076 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10077 tw32(RCVLPC_CONFIG, 0x0181);
10078
10079 /* Calculate RDMAC_MODE setting early, we need it to determine
10080 * the RCVLPC_STATE_ENABLE mask.
10081 */
10082 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10083 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10084 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10085 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10086 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010087
Joe Perches41535772013-02-16 11:20:04 +000010088 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010089 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10090
Joe Perches41535772013-02-16 11:20:04 +000010091 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10092 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10093 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010094 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10095 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10096 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10097
Joe Perches41535772013-02-16 11:20:04 +000010098 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10099 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010100 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010101 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10103 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010104 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10106 }
10107 }
10108
Joe Perches63c3a662011-04-26 08:12:10 +000010109 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010110 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10111
Joe Perches41535772013-02-16 11:20:04 +000010112 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010113 tp->dma_limit = 0;
10114 if (tp->dev->mtu <= ETH_DATA_LEN) {
10115 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10116 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10117 }
10118 }
10119
Joe Perches63c3a662011-04-26 08:12:10 +000010120 if (tg3_flag(tp, HW_TSO_1) ||
10121 tg3_flag(tp, HW_TSO_2) ||
10122 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010123 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10124
Matt Carlson108a6c12011-05-19 12:12:47 +000010125 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010126 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10127 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010128 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129
Joe Perches41535772013-02-16 11:20:04 +000010130 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10131 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010132 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10133
Joe Perches41535772013-02-16 11:20:04 +000010134 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10135 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10136 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10137 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010138 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010139 u32 tgtreg;
10140
Joe Perches41535772013-02-16 11:20:04 +000010141 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010142 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10143 else
10144 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10145
10146 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010147 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10148 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010149 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10150 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10151 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10152 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10153 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10154 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010155 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010156 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010157 }
10158
Joe Perches41535772013-02-16 11:20:04 +000010159 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10160 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10161 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010162 u32 tgtreg;
10163
Joe Perches41535772013-02-16 11:20:04 +000010164 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010165 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10166 else
10167 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10168
10169 val = tr32(tgtreg);
10170 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010171 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10172 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10173 }
10174
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010176 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010177 val = tr32(RCVLPC_STATS_ENABLE);
10178 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10179 tw32(RCVLPC_STATS_ENABLE, val);
10180 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010181 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 val = tr32(RCVLPC_STATS_ENABLE);
10183 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10184 tw32(RCVLPC_STATS_ENABLE, val);
10185 } else {
10186 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10187 }
10188 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10189 tw32(SNDDATAI_STATSENAB, 0xffffff);
10190 tw32(SNDDATAI_STATSCTRL,
10191 (SNDDATAI_SCTRL_ENABLE |
10192 SNDDATAI_SCTRL_FASTUPD));
10193
10194 /* Setup host coalescing engine. */
10195 tw32(HOSTCC_MODE, 0);
10196 for (i = 0; i < 2000; i++) {
10197 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10198 break;
10199 udelay(10);
10200 }
10201
Michael Chand244c892005-07-05 14:42:33 -070010202 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203
Joe Perches63c3a662011-04-26 08:12:10 +000010204 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010205 /* Status/statistics block address. See tg3_timer,
10206 * the tg3_periodic_fetch_stats call there, and
10207 * tg3_get_stats to see how this works for 5705/5750 chips.
10208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10210 ((u64) tp->stats_mapping >> 32));
10211 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10212 ((u64) tp->stats_mapping & 0xffffffff));
10213 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010214
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010216
10217 /* Clear statistics and status block memory areas */
10218 for (i = NIC_SRAM_STATS_BLK;
10219 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10220 i += sizeof(u32)) {
10221 tg3_write_mem(tp, i, 0);
10222 udelay(40);
10223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010224 }
10225
10226 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10227
10228 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10229 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010230 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10232
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010233 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10234 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010235 /* reset to prevent losing 1st rx packet intermittently */
10236 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10237 udelay(10);
10238 }
10239
Matt Carlson3bda1252008-08-15 14:08:22 -070010240 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010241 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10242 MAC_MODE_FHDE_ENABLE;
10243 if (tg3_flag(tp, ENABLE_APE))
10244 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010245 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010246 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010247 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010248 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010249 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10250 udelay(40);
10251
Michael Chan314fba32005-04-21 17:07:04 -070010252 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010253 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010254 * register to preserve the GPIO settings for LOMs. The GPIOs,
10255 * whether used as inputs or outputs, are set by boot code after
10256 * reset.
10257 */
Joe Perches63c3a662011-04-26 08:12:10 +000010258 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010259 u32 gpio_mask;
10260
Michael Chan9d26e212006-12-07 00:21:14 -080010261 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10262 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10263 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010264
Joe Perches41535772013-02-16 11:20:04 +000010265 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010266 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10267 GRC_LCLCTRL_GPIO_OUTPUT3;
10268
Joe Perches41535772013-02-16 11:20:04 +000010269 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010270 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10271
Gary Zambranoaaf84462007-05-05 11:51:45 -070010272 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010273 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10274
10275 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010276 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010277 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10278 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010280 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10281 udelay(100);
10282
Matt Carlsonc3b50032012-01-17 15:27:23 +000010283 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010284 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010285 val |= MSGINT_MODE_ENABLE;
10286 if (tp->irq_cnt > 1)
10287 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010288 if (!tg3_flag(tp, 1SHOT_MSI))
10289 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010290 tw32(MSGINT_MODE, val);
10291 }
10292
Joe Perches63c3a662011-04-26 08:12:10 +000010293 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10295 udelay(40);
10296 }
10297
10298 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10299 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10300 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10301 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10302 WDMAC_MODE_LNGREAD_ENAB);
10303
Joe Perches41535772013-02-16 11:20:04 +000010304 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10305 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010306 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010307 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10308 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010309 /* nothing */
10310 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010311 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312 val |= WDMAC_MODE_RX_ACCEL;
10313 }
10314 }
10315
Michael Chand9ab5ad12006-03-20 22:27:35 -080010316 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010317 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010318 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010319
Joe Perches41535772013-02-16 11:20:04 +000010320 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010321 val |= WDMAC_MODE_BURST_ALL_DATA;
10322
Linus Torvalds1da177e2005-04-16 15:20:36 -070010323 tw32_f(WDMAC_MODE, val);
10324 udelay(40);
10325
Joe Perches63c3a662011-04-26 08:12:10 +000010326 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010327 u16 pcix_cmd;
10328
10329 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10330 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010331 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010332 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10333 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010334 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010335 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10336 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010337 }
Matt Carlson9974a352007-10-07 23:27:28 -070010338 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10339 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010340 }
10341
10342 tw32_f(RDMAC_MODE, rdmac_mode);
10343 udelay(40);
10344
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010345 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10346 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010347 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10348 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10349 break;
10350 }
10351 if (i < TG3_NUM_RDMA_CHANNELS) {
10352 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010353 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010354 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010355 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010356 }
10357 }
10358
Linus Torvalds1da177e2005-04-16 15:20:36 -070010359 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010360 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010361 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010362
Joe Perches41535772013-02-16 11:20:04 +000010363 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010364 tw32(SNDDATAC_MODE,
10365 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10366 else
10367 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10368
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10370 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010371 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010372 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010373 val |= RCVDBDI_MODE_LRG_RING_SZ;
10374 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010375 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010376 if (tg3_flag(tp, HW_TSO_1) ||
10377 tg3_flag(tp, HW_TSO_2) ||
10378 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010380 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010381 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010382 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10383 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010384 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10385
Joe Perches41535772013-02-16 11:20:04 +000010386 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387 err = tg3_load_5701_a0_firmware_fix(tp);
10388 if (err)
10389 return err;
10390 }
10391
Nithin Sujirc4dab502013-03-06 17:02:34 +000010392 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10393 /* Ignore any errors for the firmware download. If download
10394 * fails, the device will operate with EEE disabled
10395 */
10396 tg3_load_57766_firmware(tp);
10397 }
10398
Joe Perches63c3a662011-04-26 08:12:10 +000010399 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400 err = tg3_load_tso_firmware(tp);
10401 if (err)
10402 return err;
10403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404
10405 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010406
Joe Perches63c3a662011-04-26 08:12:10 +000010407 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010408 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010409 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010410
Joe Perches41535772013-02-16 11:20:04 +000010411 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10412 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010413 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10414 tp->tx_mode &= ~val;
10415 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10416 }
10417
Linus Torvalds1da177e2005-04-16 15:20:36 -070010418 tw32_f(MAC_TX_MODE, tp->tx_mode);
10419 udelay(100);
10420
Joe Perches63c3a662011-04-26 08:12:10 +000010421 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010422 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010423
10424 /* Setup the "secret" hash key. */
10425 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10426 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10427 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10428 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10429 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10430 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10431 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10432 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10433 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10434 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10435 }
10436
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010438 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010439 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10440
Nithin Sujir378b72c2013-07-29 13:58:39 -070010441 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10442 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10443
Joe Perches63c3a662011-04-26 08:12:10 +000010444 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010445 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10446 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10447 RX_MODE_RSS_IPV6_HASH_EN |
10448 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10449 RX_MODE_RSS_IPV4_HASH_EN |
10450 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10451
Linus Torvalds1da177e2005-04-16 15:20:36 -070010452 tw32_f(MAC_RX_MODE, tp->rx_mode);
10453 udelay(10);
10454
Linus Torvalds1da177e2005-04-16 15:20:36 -070010455 tw32(MAC_LED_CTRL, tp->led_ctrl);
10456
10457 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010458 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010459 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10460 udelay(10);
10461 }
10462 tw32_f(MAC_RX_MODE, tp->rx_mode);
10463 udelay(10);
10464
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010465 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010466 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10467 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010468 /* Set drive transmission level to 1.2V */
10469 /* only if the signal pre-emphasis bit is not set */
10470 val = tr32(MAC_SERDES_CFG);
10471 val &= 0xfffff000;
10472 val |= 0x880;
10473 tw32(MAC_SERDES_CFG, val);
10474 }
Joe Perches41535772013-02-16 11:20:04 +000010475 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476 tw32(MAC_SERDES_CFG, 0x616000);
10477 }
10478
10479 /* Prevent chip from dropping frames when flow control
10480 * is enabled.
10481 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010482 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010483 val = 1;
10484 else
10485 val = 2;
10486 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010487
Joe Perches41535772013-02-16 11:20:04 +000010488 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010489 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010490 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010491 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010492 }
10493
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010494 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010495 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010496 u32 tmp;
10497
10498 tmp = tr32(SERDES_RX_CTRL);
10499 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10500 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10501 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10502 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10503 }
10504
Joe Perches63c3a662011-04-26 08:12:10 +000010505 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010506 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010507 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010508
Joe Perches953c96e2013-04-09 10:18:14 +000010509 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010510 if (err)
10511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010512
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010513 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10514 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010515 u32 tmp;
10516
10517 /* Clear CRC stats. */
10518 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10519 tg3_writephy(tp, MII_TG3_TEST1,
10520 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010521 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010523 }
10524 }
10525
10526 __tg3_set_rx_mode(tp->dev);
10527
10528 /* Initialize receive rules. */
10529 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10530 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10531 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10532 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10533
Joe Perches63c3a662011-04-26 08:12:10 +000010534 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010535 limit = 8;
10536 else
10537 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010538 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 limit -= 4;
10540 switch (limit) {
10541 case 16:
10542 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10543 case 15:
10544 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10545 case 14:
10546 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10547 case 13:
10548 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10549 case 12:
10550 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10551 case 11:
10552 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10553 case 10:
10554 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10555 case 9:
10556 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10557 case 8:
10558 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10559 case 7:
10560 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10561 case 6:
10562 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10563 case 5:
10564 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10565 case 4:
10566 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10567 case 3:
10568 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10569 case 2:
10570 case 1:
10571
10572 default:
10573 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010575
Joe Perches63c3a662011-04-26 08:12:10 +000010576 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010577 /* Write our heartbeat update interval to APE. */
10578 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10579 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010580
Linus Torvalds1da177e2005-04-16 15:20:36 -070010581 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10582
Linus Torvalds1da177e2005-04-16 15:20:36 -070010583 return 0;
10584}
10585
10586/* Called at device open time to get the chip ready for
10587 * packet processing. Invoked with tp->lock held.
10588 */
Joe Perches953c96e2013-04-09 10:18:14 +000010589static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010590{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010591 /* Chip may have been just powered on. If so, the boot code may still
10592 * be running initialization. Wait for it to finish to avoid races in
10593 * accessing the hardware.
10594 */
10595 tg3_enable_register_access(tp);
10596 tg3_poll_fw(tp);
10597
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598 tg3_switch_clocks(tp);
10599
10600 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10601
Matt Carlson2f751b62008-08-04 23:17:34 -070010602 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010603}
10604
Michael Chanaed93e02012-07-16 16:24:02 +000010605static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10606{
10607 int i;
10608
10609 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10610 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10611
10612 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10613 off += len;
10614
10615 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10616 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10617 memset(ocir, 0, TG3_OCIR_LEN);
10618 }
10619}
10620
10621/* sysfs attributes for hwmon */
10622static ssize_t tg3_show_temp(struct device *dev,
10623 struct device_attribute *devattr, char *buf)
10624{
10625 struct pci_dev *pdev = to_pci_dev(dev);
10626 struct net_device *netdev = pci_get_drvdata(pdev);
10627 struct tg3 *tp = netdev_priv(netdev);
10628 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10629 u32 temperature;
10630
10631 spin_lock_bh(&tp->lock);
10632 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10633 sizeof(temperature));
10634 spin_unlock_bh(&tp->lock);
10635 return sprintf(buf, "%u\n", temperature);
10636}
10637
10638
10639static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10640 TG3_TEMP_SENSOR_OFFSET);
10641static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10642 TG3_TEMP_CAUTION_OFFSET);
10643static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10644 TG3_TEMP_MAX_OFFSET);
10645
10646static struct attribute *tg3_attributes[] = {
10647 &sensor_dev_attr_temp1_input.dev_attr.attr,
10648 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10649 &sensor_dev_attr_temp1_max.dev_attr.attr,
10650 NULL
10651};
10652
10653static const struct attribute_group tg3_group = {
10654 .attrs = tg3_attributes,
10655};
10656
Michael Chanaed93e02012-07-16 16:24:02 +000010657static void tg3_hwmon_close(struct tg3 *tp)
10658{
Michael Chanaed93e02012-07-16 16:24:02 +000010659 if (tp->hwmon_dev) {
10660 hwmon_device_unregister(tp->hwmon_dev);
10661 tp->hwmon_dev = NULL;
10662 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10663 }
Michael Chanaed93e02012-07-16 16:24:02 +000010664}
10665
10666static void tg3_hwmon_open(struct tg3 *tp)
10667{
Michael Chanaed93e02012-07-16 16:24:02 +000010668 int i, err;
10669 u32 size = 0;
10670 struct pci_dev *pdev = tp->pdev;
10671 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10672
10673 tg3_sd_scan_scratchpad(tp, ocirs);
10674
10675 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10676 if (!ocirs[i].src_data_length)
10677 continue;
10678
10679 size += ocirs[i].src_hdr_length;
10680 size += ocirs[i].src_data_length;
10681 }
10682
10683 if (!size)
10684 return;
10685
10686 /* Register hwmon sysfs hooks */
10687 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10688 if (err) {
10689 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10690 return;
10691 }
10692
10693 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10694 if (IS_ERR(tp->hwmon_dev)) {
10695 tp->hwmon_dev = NULL;
10696 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10697 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10698 }
Michael Chanaed93e02012-07-16 16:24:02 +000010699}
10700
10701
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702#define TG3_STAT_ADD32(PSTAT, REG) \
10703do { u32 __val = tr32(REG); \
10704 (PSTAT)->low += __val; \
10705 if ((PSTAT)->low < __val) \
10706 (PSTAT)->high += 1; \
10707} while (0)
10708
10709static void tg3_periodic_fetch_stats(struct tg3 *tp)
10710{
10711 struct tg3_hw_stats *sp = tp->hw_stats;
10712
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010713 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010714 return;
10715
10716 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10717 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10718 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10719 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10720 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10721 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10722 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10723 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10724 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10725 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10726 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10727 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10728 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010729 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010730 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10731 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10732 u32 val;
10733
10734 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010735 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010736 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010737 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739
10740 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10741 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10742 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10743 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10744 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10745 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10746 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10747 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10748 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10749 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10750 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10751 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10752 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10753 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010754
10755 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010756 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10757 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10758 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010759 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10760 } else {
10761 u32 val = tr32(HOSTCC_FLOW_ATTN);
10762 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10763 if (val) {
10764 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10765 sp->rx_discards.low += val;
10766 if (sp->rx_discards.low < val)
10767 sp->rx_discards.high += 1;
10768 }
10769 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10770 }
Michael Chan463d3052006-05-22 16:36:27 -070010771 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010772}
10773
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010774static void tg3_chk_missed_msi(struct tg3 *tp)
10775{
10776 u32 i;
10777
10778 for (i = 0; i < tp->irq_cnt; i++) {
10779 struct tg3_napi *tnapi = &tp->napi[i];
10780
10781 if (tg3_has_work(tnapi)) {
10782 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10783 tnapi->last_tx_cons == tnapi->tx_cons) {
10784 if (tnapi->chk_msi_cnt < 1) {
10785 tnapi->chk_msi_cnt++;
10786 return;
10787 }
Matt Carlson7f230732011-08-31 11:44:48 +000010788 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010789 }
10790 }
10791 tnapi->chk_msi_cnt = 0;
10792 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10793 tnapi->last_tx_cons = tnapi->tx_cons;
10794 }
10795}
10796
Linus Torvalds1da177e2005-04-16 15:20:36 -070010797static void tg3_timer(unsigned long __opaque)
10798{
10799 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010800
Matt Carlson5b190622011-11-04 09:15:04 +000010801 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010802 goto restart_timer;
10803
David S. Millerf47c11e2005-06-24 20:18:35 -070010804 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010805
Joe Perches41535772013-02-16 11:20:04 +000010806 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010807 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010808 tg3_chk_missed_msi(tp);
10809
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010810 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10811 /* BCM4785: Flush posted writes from GbE to host memory. */
10812 tr32(HOSTCC_MODE);
10813 }
10814
Joe Perches63c3a662011-04-26 08:12:10 +000010815 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010816 /* All of this garbage is because when using non-tagged
10817 * IRQ status the mailbox/status_block protocol the chip
10818 * uses with the cpu is race prone.
10819 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010820 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010821 tw32(GRC_LOCAL_CTRL,
10822 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10823 } else {
10824 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010825 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010827
David S. Millerfac9b832005-05-18 22:46:34 -070010828 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010829 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010830 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010831 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010833 }
10834
Linus Torvalds1da177e2005-04-16 15:20:36 -070010835 /* This part only runs once per second. */
10836 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010837 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010838 tg3_periodic_fetch_stats(tp);
10839
Matt Carlsonb0c59432011-05-19 12:12:48 +000010840 if (tp->setlpicnt && !--tp->setlpicnt)
10841 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010842
Joe Perches63c3a662011-04-26 08:12:10 +000010843 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010844 u32 mac_stat;
10845 int phy_event;
10846
10847 mac_stat = tr32(MAC_STATUS);
10848
10849 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010850 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010851 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10852 phy_event = 1;
10853 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10854 phy_event = 1;
10855
10856 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010857 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010858 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010859 u32 mac_stat = tr32(MAC_STATUS);
10860 int need_setup = 0;
10861
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010862 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010863 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10864 need_setup = 1;
10865 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010866 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010867 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10868 MAC_STATUS_SIGNAL_DET))) {
10869 need_setup = 1;
10870 }
10871 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010872 if (!tp->serdes_counter) {
10873 tw32_f(MAC_MODE,
10874 (tp->mac_mode &
10875 ~MAC_MODE_PORT_MODE_MASK));
10876 udelay(40);
10877 tw32_f(MAC_MODE, tp->mac_mode);
10878 udelay(40);
10879 }
Joe Perches953c96e2013-04-09 10:18:14 +000010880 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010881 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010882 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010883 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010884 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010886
10887 tp->timer_counter = tp->timer_multiplier;
10888 }
10889
Michael Chan130b8e42006-09-27 16:00:40 -070010890 /* Heartbeat is only sent once every 2 seconds.
10891 *
10892 * The heartbeat is to tell the ASF firmware that the host
10893 * driver is still alive. In the event that the OS crashes,
10894 * ASF needs to reset the hardware to free up the FIFO space
10895 * that may be filled with rx packets destined for the host.
10896 * If the FIFO is full, ASF will no longer function properly.
10897 *
10898 * Unintended resets have been reported on real time kernels
10899 * where the timer doesn't run on time. Netpoll will also have
10900 * same problem.
10901 *
10902 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10903 * to check the ring condition when the heartbeat is expiring
10904 * before doing the reset. This will prevent most unintended
10905 * resets.
10906 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010907 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010908 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010909 tg3_wait_for_event_ack(tp);
10910
Michael Chanbbadf502006-04-06 21:46:34 -070010911 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010912 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010913 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010914 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10915 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010916
10917 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010918 }
10919 tp->asf_counter = tp->asf_multiplier;
10920 }
10921
David S. Millerf47c11e2005-06-24 20:18:35 -070010922 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010923
Michael Chanf475f162006-03-27 23:20:14 -080010924restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010925 tp->timer.expires = jiffies + tp->timer_offset;
10926 add_timer(&tp->timer);
10927}
10928
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010929static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010930{
10931 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010932 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010933 !tg3_flag(tp, 57765_CLASS))
10934 tp->timer_offset = HZ;
10935 else
10936 tp->timer_offset = HZ / 10;
10937
10938 BUG_ON(tp->timer_offset > HZ);
10939
10940 tp->timer_multiplier = (HZ / tp->timer_offset);
10941 tp->asf_multiplier = (HZ / tp->timer_offset) *
10942 TG3_FW_UPDATE_FREQ_SEC;
10943
10944 init_timer(&tp->timer);
10945 tp->timer.data = (unsigned long) tp;
10946 tp->timer.function = tg3_timer;
10947}
10948
10949static void tg3_timer_start(struct tg3 *tp)
10950{
10951 tp->asf_counter = tp->asf_multiplier;
10952 tp->timer_counter = tp->timer_multiplier;
10953
10954 tp->timer.expires = jiffies + tp->timer_offset;
10955 add_timer(&tp->timer);
10956}
10957
10958static void tg3_timer_stop(struct tg3 *tp)
10959{
10960 del_timer_sync(&tp->timer);
10961}
10962
10963/* Restart hardware after configuration changes, self-test, etc.
10964 * Invoked with tp->lock held.
10965 */
Joe Perches953c96e2013-04-09 10:18:14 +000010966static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000010967 __releases(tp->lock)
10968 __acquires(tp->lock)
10969{
10970 int err;
10971
10972 err = tg3_init_hw(tp, reset_phy);
10973 if (err) {
10974 netdev_err(tp->dev,
10975 "Failed to re-initialize device, aborting\n");
10976 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10977 tg3_full_unlock(tp);
10978 tg3_timer_stop(tp);
10979 tp->irq_sync = 0;
10980 tg3_napi_enable(tp);
10981 dev_close(tp->dev);
10982 tg3_full_lock(tp, 0);
10983 }
10984 return err;
10985}
10986
10987static void tg3_reset_task(struct work_struct *work)
10988{
10989 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10990 int err;
10991
10992 tg3_full_lock(tp, 0);
10993
10994 if (!netif_running(tp->dev)) {
10995 tg3_flag_clear(tp, RESET_TASK_PENDING);
10996 tg3_full_unlock(tp);
10997 return;
10998 }
10999
11000 tg3_full_unlock(tp);
11001
11002 tg3_phy_stop(tp);
11003
11004 tg3_netif_stop(tp);
11005
11006 tg3_full_lock(tp, 1);
11007
11008 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11009 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11010 tp->write32_rx_mbox = tg3_write_flush_reg32;
11011 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11012 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11013 }
11014
11015 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011016 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011017 if (err)
11018 goto out;
11019
11020 tg3_netif_start(tp);
11021
11022out:
11023 tg3_full_unlock(tp);
11024
11025 if (!err)
11026 tg3_phy_start(tp);
11027
11028 tg3_flag_clear(tp, RESET_TASK_PENDING);
11029}
11030
Matt Carlson4f125f42009-09-01 12:55:02 +000011031static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011032{
David Howells7d12e782006-10-05 14:55:46 +010011033 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011034 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011035 char *name;
11036 struct tg3_napi *tnapi = &tp->napi[irq_num];
11037
11038 if (tp->irq_cnt == 1)
11039 name = tp->dev->name;
11040 else {
11041 name = &tnapi->irq_lbl[0];
11042 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
11043 name[IFNAMSIZ-1] = 0;
11044 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011045
Joe Perches63c3a662011-04-26 08:12:10 +000011046 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011047 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011048 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011049 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011050 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011051 } else {
11052 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011053 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011054 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011055 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011056 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011057
11058 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011059}
11060
Michael Chan79381092005-04-21 17:13:59 -070011061static int tg3_test_interrupt(struct tg3 *tp)
11062{
Matt Carlson09943a12009-08-28 14:01:57 +000011063 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011064 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011065 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011066 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011067
Michael Chand4bc3922005-05-29 14:59:20 -070011068 if (!netif_running(dev))
11069 return -ENODEV;
11070
Michael Chan79381092005-04-21 17:13:59 -070011071 tg3_disable_ints(tp);
11072
Matt Carlson4f125f42009-09-01 12:55:02 +000011073 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011074
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011075 /*
11076 * Turn off MSI one shot mode. Otherwise this test has no
11077 * observable way to know whether the interrupt was delivered.
11078 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011079 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011080 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11081 tw32(MSGINT_MODE, val);
11082 }
11083
Matt Carlson4f125f42009-09-01 12:55:02 +000011084 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011085 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011086 if (err)
11087 return err;
11088
Matt Carlson898a56f2009-08-28 14:02:40 +000011089 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011090 tg3_enable_ints(tp);
11091
11092 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011093 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011094
11095 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011096 u32 int_mbox, misc_host_ctrl;
11097
Matt Carlson898a56f2009-08-28 14:02:40 +000011098 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011099 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11100
11101 if ((int_mbox != 0) ||
11102 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11103 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011104 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011105 }
11106
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011107 if (tg3_flag(tp, 57765_PLUS) &&
11108 tnapi->hw_status->status_tag != tnapi->last_tag)
11109 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11110
Michael Chan79381092005-04-21 17:13:59 -070011111 msleep(10);
11112 }
11113
11114 tg3_disable_ints(tp);
11115
Matt Carlson4f125f42009-09-01 12:55:02 +000011116 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011117
Matt Carlson4f125f42009-09-01 12:55:02 +000011118 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011119
11120 if (err)
11121 return err;
11122
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011123 if (intr_ok) {
11124 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011125 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011126 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11127 tw32(MSGINT_MODE, val);
11128 }
Michael Chan79381092005-04-21 17:13:59 -070011129 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011130 }
Michael Chan79381092005-04-21 17:13:59 -070011131
11132 return -EIO;
11133}
11134
11135/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11136 * successfully restored
11137 */
11138static int tg3_test_msi(struct tg3 *tp)
11139{
Michael Chan79381092005-04-21 17:13:59 -070011140 int err;
11141 u16 pci_cmd;
11142
Joe Perches63c3a662011-04-26 08:12:10 +000011143 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011144 return 0;
11145
11146 /* Turn off SERR reporting in case MSI terminates with Master
11147 * Abort.
11148 */
11149 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11150 pci_write_config_word(tp->pdev, PCI_COMMAND,
11151 pci_cmd & ~PCI_COMMAND_SERR);
11152
11153 err = tg3_test_interrupt(tp);
11154
11155 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11156
11157 if (!err)
11158 return 0;
11159
11160 /* other failures */
11161 if (err != -EIO)
11162 return err;
11163
11164 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011165 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11166 "to INTx mode. Please report this failure to the PCI "
11167 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011168
Matt Carlson4f125f42009-09-01 12:55:02 +000011169 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011170
Michael Chan79381092005-04-21 17:13:59 -070011171 pci_disable_msi(tp->pdev);
11172
Joe Perches63c3a662011-04-26 08:12:10 +000011173 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011174 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011175
Matt Carlson4f125f42009-09-01 12:55:02 +000011176 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011177 if (err)
11178 return err;
11179
11180 /* Need to reset the chip because the MSI cycle may have terminated
11181 * with Master Abort.
11182 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011183 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011184
Michael Chan944d9802005-05-29 14:57:48 -070011185 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011186 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011187
David S. Millerf47c11e2005-06-24 20:18:35 -070011188 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011189
11190 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011191 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011192
11193 return err;
11194}
11195
Matt Carlson9e9fd122009-01-19 16:57:45 -080011196static int tg3_request_firmware(struct tg3 *tp)
11197{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011198 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011199
11200 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011201 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11202 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011203 return -ENOENT;
11204 }
11205
Nithin Sujir77997ea2013-03-06 17:02:32 +000011206 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011207
11208 /* Firmware blob starts with version numbers, followed by
11209 * start address and _full_ length including BSS sections
11210 * (which must be longer than the actual data, of course
11211 */
11212
Nithin Sujir77997ea2013-03-06 17:02:32 +000011213 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11214 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011215 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11216 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011217 release_firmware(tp->fw);
11218 tp->fw = NULL;
11219 return -EINVAL;
11220 }
11221
11222 /* We no longer need firmware; we have it. */
11223 tp->fw_needed = NULL;
11224 return 0;
11225}
11226
Michael Chan91024262012-09-28 07:12:38 +000011227static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011228{
Michael Chan91024262012-09-28 07:12:38 +000011229 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011230
Michael Chan91024262012-09-28 07:12:38 +000011231 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011232 /* We want as many rx rings enabled as there are cpus.
11233 * In multiqueue MSI-X mode, the first MSI-X vector
11234 * only deals with link interrupts, etc, so we add
11235 * one to the number of vectors we are requesting.
11236 */
Michael Chan91024262012-09-28 07:12:38 +000011237 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011238 }
Matt Carlson679563f2009-09-01 12:55:46 +000011239
Michael Chan91024262012-09-28 07:12:38 +000011240 return irq_cnt;
11241}
11242
11243static bool tg3_enable_msix(struct tg3 *tp)
11244{
11245 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011246 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011247
Michael Chan09681692012-09-28 07:12:42 +000011248 tp->txq_cnt = tp->txq_req;
11249 tp->rxq_cnt = tp->rxq_req;
11250 if (!tp->rxq_cnt)
11251 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011252 if (tp->rxq_cnt > tp->rxq_max)
11253 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011254
11255 /* Disable multiple TX rings by default. Simple round-robin hardware
11256 * scheduling of the TX rings can cause starvation of rings with
11257 * small packets when other rings have TSO or jumbo packets.
11258 */
11259 if (!tp->txq_req)
11260 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011261
11262 tp->irq_cnt = tg3_irq_count(tp);
11263
Matt Carlson679563f2009-09-01 12:55:46 +000011264 for (i = 0; i < tp->irq_max; i++) {
11265 msix_ent[i].entry = i;
11266 msix_ent[i].vector = 0;
11267 }
11268
11269 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011270 if (rc < 0) {
11271 return false;
11272 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011273 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11274 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011275 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11276 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011277 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011278 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011279 if (tp->txq_cnt)
11280 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011281 }
11282
11283 for (i = 0; i < tp->irq_max; i++)
11284 tp->napi[i].irq_vec = msix_ent[i].vector;
11285
Michael Chan49a359e2012-09-28 07:12:37 +000011286 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011287 pci_disable_msix(tp->pdev);
11288 return false;
11289 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011290
Michael Chan91024262012-09-28 07:12:38 +000011291 if (tp->irq_cnt == 1)
11292 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011293
Michael Chan91024262012-09-28 07:12:38 +000011294 tg3_flag_set(tp, ENABLE_RSS);
11295
11296 if (tp->txq_cnt > 1)
11297 tg3_flag_set(tp, ENABLE_TSS);
11298
11299 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011300
Matt Carlson679563f2009-09-01 12:55:46 +000011301 return true;
11302}
11303
Matt Carlson07b01732009-08-28 14:01:15 +000011304static void tg3_ints_init(struct tg3 *tp)
11305{
Joe Perches63c3a662011-04-26 08:12:10 +000011306 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11307 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011308 /* All MSI supporting chips should support tagged
11309 * status. Assert that this is the case.
11310 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011311 netdev_warn(tp->dev,
11312 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011313 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011314 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011315
Joe Perches63c3a662011-04-26 08:12:10 +000011316 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11317 tg3_flag_set(tp, USING_MSIX);
11318 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11319 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011320
Joe Perches63c3a662011-04-26 08:12:10 +000011321 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011322 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011323 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011324 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011325 if (!tg3_flag(tp, 1SHOT_MSI))
11326 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011327 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11328 }
11329defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011330 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011331 tp->irq_cnt = 1;
11332 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011333 }
11334
11335 if (tp->irq_cnt == 1) {
11336 tp->txq_cnt = 1;
11337 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011338 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011339 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011340 }
Matt Carlson07b01732009-08-28 14:01:15 +000011341}
11342
11343static void tg3_ints_fini(struct tg3 *tp)
11344{
Joe Perches63c3a662011-04-26 08:12:10 +000011345 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011346 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011347 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011348 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011349 tg3_flag_clear(tp, USING_MSI);
11350 tg3_flag_clear(tp, USING_MSIX);
11351 tg3_flag_clear(tp, ENABLE_RSS);
11352 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011353}
11354
Matt Carlsonbe947302012-12-03 19:36:57 +000011355static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11356 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011357{
Michael Chand8f4cd32012-09-28 07:12:40 +000011358 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011359 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011360
Matt Carlson679563f2009-09-01 12:55:46 +000011361 /*
11362 * Setup interrupts first so we know how
11363 * many NAPI resources to allocate
11364 */
11365 tg3_ints_init(tp);
11366
Matt Carlson90415472011-12-16 13:33:23 +000011367 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011368
Linus Torvalds1da177e2005-04-16 15:20:36 -070011369 /* The placement of this call is tied
11370 * to the setup and use of Host TX descriptors.
11371 */
11372 err = tg3_alloc_consistent(tp);
11373 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011374 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011375
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011376 tg3_napi_init(tp);
11377
Matt Carlsonfed97812009-09-01 13:10:19 +000011378 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011379
Matt Carlson4f125f42009-09-01 12:55:02 +000011380 for (i = 0; i < tp->irq_cnt; i++) {
11381 struct tg3_napi *tnapi = &tp->napi[i];
11382 err = tg3_request_irq(tp, i);
11383 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011384 for (i--; i >= 0; i--) {
11385 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011386 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011387 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011388 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011389 }
11390 }
Matt Carlson07b01732009-08-28 14:01:15 +000011391
David S. Millerf47c11e2005-06-24 20:18:35 -070011392 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011393
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011394 if (init)
11395 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11396
Michael Chand8f4cd32012-09-28 07:12:40 +000011397 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011398 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011399 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011400 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011401 }
11402
David S. Millerf47c11e2005-06-24 20:18:35 -070011403 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011404
Matt Carlson07b01732009-08-28 14:01:15 +000011405 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011406 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011407
Michael Chand8f4cd32012-09-28 07:12:40 +000011408 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011409 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011410
Michael Chan79381092005-04-21 17:13:59 -070011411 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011412 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011413 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011414 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011415 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011416
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011417 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011418 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011419
Joe Perches63c3a662011-04-26 08:12:10 +000011420 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011421 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011422
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011423 tw32(PCIE_TRANSACTION_CFG,
11424 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011425 }
Michael Chan79381092005-04-21 17:13:59 -070011426 }
11427
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011428 tg3_phy_start(tp);
11429
Michael Chanaed93e02012-07-16 16:24:02 +000011430 tg3_hwmon_open(tp);
11431
David S. Millerf47c11e2005-06-24 20:18:35 -070011432 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011433
Matt Carlson21f76382012-02-22 12:35:21 +000011434 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011435 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011436 tg3_enable_ints(tp);
11437
Matt Carlsonbe947302012-12-03 19:36:57 +000011438 if (init)
11439 tg3_ptp_init(tp);
11440 else
11441 tg3_ptp_resume(tp);
11442
11443
David S. Millerf47c11e2005-06-24 20:18:35 -070011444 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011445
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011446 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011447
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011448 /*
11449 * Reset loopback feature if it was turned on while the device was down
11450 * make sure that it's installed properly now.
11451 */
11452 if (dev->features & NETIF_F_LOOPBACK)
11453 tg3_set_loopback(dev, dev->features);
11454
Linus Torvalds1da177e2005-04-16 15:20:36 -070011455 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011456
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011457out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011458 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11459 struct tg3_napi *tnapi = &tp->napi[i];
11460 free_irq(tnapi->irq_vec, tnapi);
11461 }
Matt Carlson07b01732009-08-28 14:01:15 +000011462
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011463out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011464 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011465 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011466 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011467
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011468out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011469 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011470
Matt Carlson07b01732009-08-28 14:01:15 +000011471 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011472}
11473
Michael Chan65138592012-09-28 07:12:41 +000011474static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011475{
Matt Carlson4f125f42009-09-01 12:55:02 +000011476 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011477
Matt Carlsondb219972011-11-04 09:15:03 +000011478 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011479 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011480
Matt Carlson21f76382012-02-22 12:35:21 +000011481 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482
Michael Chanaed93e02012-07-16 16:24:02 +000011483 tg3_hwmon_close(tp);
11484
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011485 tg3_phy_stop(tp);
11486
David S. Millerf47c11e2005-06-24 20:18:35 -070011487 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011488
11489 tg3_disable_ints(tp);
11490
Michael Chan944d9802005-05-29 14:57:48 -070011491 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011492 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011493 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011494
David S. Millerf47c11e2005-06-24 20:18:35 -070011495 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011496
Matt Carlson4f125f42009-09-01 12:55:02 +000011497 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11498 struct tg3_napi *tnapi = &tp->napi[i];
11499 free_irq(tnapi->irq_vec, tnapi);
11500 }
Matt Carlson07b01732009-08-28 14:01:15 +000011501
11502 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011503
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011504 tg3_napi_fini(tp);
11505
Linus Torvalds1da177e2005-04-16 15:20:36 -070011506 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011507}
11508
Michael Chand8f4cd32012-09-28 07:12:40 +000011509static int tg3_open(struct net_device *dev)
11510{
11511 struct tg3 *tp = netdev_priv(dev);
11512 int err;
11513
11514 if (tp->fw_needed) {
11515 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011516 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11517 if (err) {
11518 netdev_warn(tp->dev, "EEE capability disabled\n");
11519 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11520 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11521 netdev_warn(tp->dev, "EEE capability restored\n");
11522 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11523 }
11524 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011525 if (err)
11526 return err;
11527 } else if (err) {
11528 netdev_warn(tp->dev, "TSO capability disabled\n");
11529 tg3_flag_clear(tp, TSO_CAPABLE);
11530 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11531 netdev_notice(tp->dev, "TSO capability restored\n");
11532 tg3_flag_set(tp, TSO_CAPABLE);
11533 }
11534 }
11535
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011536 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011537
11538 err = tg3_power_up(tp);
11539 if (err)
11540 return err;
11541
11542 tg3_full_lock(tp, 0);
11543
11544 tg3_disable_ints(tp);
11545 tg3_flag_clear(tp, INIT_COMPLETE);
11546
11547 tg3_full_unlock(tp);
11548
Nithin Sujir942d1af2013-04-09 08:48:07 +000011549 err = tg3_start(tp,
11550 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11551 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011552 if (err) {
11553 tg3_frob_aux_power(tp, false);
11554 pci_set_power_state(tp->pdev, PCI_D3hot);
11555 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011556
Matt Carlson7d41e492012-12-03 19:36:58 +000011557 if (tg3_flag(tp, PTP_CAPABLE)) {
11558 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11559 &tp->pdev->dev);
11560 if (IS_ERR(tp->ptp_clock))
11561 tp->ptp_clock = NULL;
11562 }
11563
Linus Torvalds1da177e2005-04-16 15:20:36 -070011564 return err;
11565}
11566
11567static int tg3_close(struct net_device *dev)
11568{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011569 struct tg3 *tp = netdev_priv(dev);
11570
Matt Carlsonbe947302012-12-03 19:36:57 +000011571 tg3_ptp_fini(tp);
11572
Michael Chan65138592012-09-28 07:12:41 +000011573 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011574
11575 /* Clear stats across close / open calls */
11576 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11577 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011578
Nithin Sujir5137a2e2013-07-29 13:58:36 -070011579 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011581 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011582
11583 return 0;
11584}
11585
11586static inline u64 get_stat64(tg3_stat64_t *val)
11587{
11588 return ((u64)val->high << 32) | ((u64)val->low);
11589}
11590
11591static u64 tg3_calc_crc_errors(struct tg3 *tp)
11592{
11593 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11594
11595 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011596 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11597 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598 u32 val;
11599
11600 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11601 tg3_writephy(tp, MII_TG3_TEST1,
11602 val | MII_TG3_TEST1_CRC_EN);
11603 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11604 } else
11605 val = 0;
11606
11607 tp->phy_crc_errors += val;
11608
11609 return tp->phy_crc_errors;
11610 }
11611
11612 return get_stat64(&hw_stats->rx_fcs_errors);
11613}
11614
11615#define ESTAT_ADD(member) \
11616 estats->member = old_estats->member + \
11617 get_stat64(&hw_stats->member)
11618
11619static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11620{
11621 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11622 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11623
11624 ESTAT_ADD(rx_octets);
11625 ESTAT_ADD(rx_fragments);
11626 ESTAT_ADD(rx_ucast_packets);
11627 ESTAT_ADD(rx_mcast_packets);
11628 ESTAT_ADD(rx_bcast_packets);
11629 ESTAT_ADD(rx_fcs_errors);
11630 ESTAT_ADD(rx_align_errors);
11631 ESTAT_ADD(rx_xon_pause_rcvd);
11632 ESTAT_ADD(rx_xoff_pause_rcvd);
11633 ESTAT_ADD(rx_mac_ctrl_rcvd);
11634 ESTAT_ADD(rx_xoff_entered);
11635 ESTAT_ADD(rx_frame_too_long_errors);
11636 ESTAT_ADD(rx_jabbers);
11637 ESTAT_ADD(rx_undersize_packets);
11638 ESTAT_ADD(rx_in_length_errors);
11639 ESTAT_ADD(rx_out_length_errors);
11640 ESTAT_ADD(rx_64_or_less_octet_packets);
11641 ESTAT_ADD(rx_65_to_127_octet_packets);
11642 ESTAT_ADD(rx_128_to_255_octet_packets);
11643 ESTAT_ADD(rx_256_to_511_octet_packets);
11644 ESTAT_ADD(rx_512_to_1023_octet_packets);
11645 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11646 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11647 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11648 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11649 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11650
11651 ESTAT_ADD(tx_octets);
11652 ESTAT_ADD(tx_collisions);
11653 ESTAT_ADD(tx_xon_sent);
11654 ESTAT_ADD(tx_xoff_sent);
11655 ESTAT_ADD(tx_flow_control);
11656 ESTAT_ADD(tx_mac_errors);
11657 ESTAT_ADD(tx_single_collisions);
11658 ESTAT_ADD(tx_mult_collisions);
11659 ESTAT_ADD(tx_deferred);
11660 ESTAT_ADD(tx_excessive_collisions);
11661 ESTAT_ADD(tx_late_collisions);
11662 ESTAT_ADD(tx_collide_2times);
11663 ESTAT_ADD(tx_collide_3times);
11664 ESTAT_ADD(tx_collide_4times);
11665 ESTAT_ADD(tx_collide_5times);
11666 ESTAT_ADD(tx_collide_6times);
11667 ESTAT_ADD(tx_collide_7times);
11668 ESTAT_ADD(tx_collide_8times);
11669 ESTAT_ADD(tx_collide_9times);
11670 ESTAT_ADD(tx_collide_10times);
11671 ESTAT_ADD(tx_collide_11times);
11672 ESTAT_ADD(tx_collide_12times);
11673 ESTAT_ADD(tx_collide_13times);
11674 ESTAT_ADD(tx_collide_14times);
11675 ESTAT_ADD(tx_collide_15times);
11676 ESTAT_ADD(tx_ucast_packets);
11677 ESTAT_ADD(tx_mcast_packets);
11678 ESTAT_ADD(tx_bcast_packets);
11679 ESTAT_ADD(tx_carrier_sense_errors);
11680 ESTAT_ADD(tx_discards);
11681 ESTAT_ADD(tx_errors);
11682
11683 ESTAT_ADD(dma_writeq_full);
11684 ESTAT_ADD(dma_write_prioq_full);
11685 ESTAT_ADD(rxbds_empty);
11686 ESTAT_ADD(rx_discards);
11687 ESTAT_ADD(rx_errors);
11688 ESTAT_ADD(rx_threshold_hit);
11689
11690 ESTAT_ADD(dma_readq_full);
11691 ESTAT_ADD(dma_read_prioq_full);
11692 ESTAT_ADD(tx_comp_queue_full);
11693
11694 ESTAT_ADD(ring_set_send_prod_index);
11695 ESTAT_ADD(ring_status_update);
11696 ESTAT_ADD(nic_irqs);
11697 ESTAT_ADD(nic_avoided_irqs);
11698 ESTAT_ADD(nic_tx_threshold_hit);
11699
Matt Carlson4452d092011-05-19 12:12:51 +000011700 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011701}
11702
Matt Carlson65ec6982012-02-28 23:33:37 +000011703static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011704{
Eric Dumazet511d2222010-07-07 20:44:24 +000011705 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011706 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11707
Linus Torvalds1da177e2005-04-16 15:20:36 -070011708 stats->rx_packets = old_stats->rx_packets +
11709 get_stat64(&hw_stats->rx_ucast_packets) +
11710 get_stat64(&hw_stats->rx_mcast_packets) +
11711 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011712
Linus Torvalds1da177e2005-04-16 15:20:36 -070011713 stats->tx_packets = old_stats->tx_packets +
11714 get_stat64(&hw_stats->tx_ucast_packets) +
11715 get_stat64(&hw_stats->tx_mcast_packets) +
11716 get_stat64(&hw_stats->tx_bcast_packets);
11717
11718 stats->rx_bytes = old_stats->rx_bytes +
11719 get_stat64(&hw_stats->rx_octets);
11720 stats->tx_bytes = old_stats->tx_bytes +
11721 get_stat64(&hw_stats->tx_octets);
11722
11723 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011724 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011725 stats->tx_errors = old_stats->tx_errors +
11726 get_stat64(&hw_stats->tx_errors) +
11727 get_stat64(&hw_stats->tx_mac_errors) +
11728 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11729 get_stat64(&hw_stats->tx_discards);
11730
11731 stats->multicast = old_stats->multicast +
11732 get_stat64(&hw_stats->rx_mcast_packets);
11733 stats->collisions = old_stats->collisions +
11734 get_stat64(&hw_stats->tx_collisions);
11735
11736 stats->rx_length_errors = old_stats->rx_length_errors +
11737 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11738 get_stat64(&hw_stats->rx_undersize_packets);
11739
11740 stats->rx_over_errors = old_stats->rx_over_errors +
11741 get_stat64(&hw_stats->rxbds_empty);
11742 stats->rx_frame_errors = old_stats->rx_frame_errors +
11743 get_stat64(&hw_stats->rx_align_errors);
11744 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11745 get_stat64(&hw_stats->tx_discards);
11746 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11747 get_stat64(&hw_stats->tx_carrier_sense_errors);
11748
11749 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011750 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011751
John W. Linville4f63b872005-09-12 14:43:18 -070011752 stats->rx_missed_errors = old_stats->rx_missed_errors +
11753 get_stat64(&hw_stats->rx_discards);
11754
Eric Dumazetb0057c52010-10-10 19:55:52 +000011755 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011756 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011757}
11758
Linus Torvalds1da177e2005-04-16 15:20:36 -070011759static int tg3_get_regs_len(struct net_device *dev)
11760{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011761 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011762}
11763
11764static void tg3_get_regs(struct net_device *dev,
11765 struct ethtool_regs *regs, void *_p)
11766{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011767 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011768
11769 regs->version = 0;
11770
Matt Carlson97bd8e42011-04-13 11:05:04 +000011771 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011772
Matt Carlson80096062010-08-02 11:26:06 +000011773 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011774 return;
11775
David S. Millerf47c11e2005-06-24 20:18:35 -070011776 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011777
Matt Carlson97bd8e42011-04-13 11:05:04 +000011778 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011779
David S. Millerf47c11e2005-06-24 20:18:35 -070011780 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011781}
11782
11783static int tg3_get_eeprom_len(struct net_device *dev)
11784{
11785 struct tg3 *tp = netdev_priv(dev);
11786
11787 return tp->nvram_size;
11788}
11789
Linus Torvalds1da177e2005-04-16 15:20:36 -070011790static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11791{
11792 struct tg3 *tp = netdev_priv(dev);
11793 int ret;
11794 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011795 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011796 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011797
Joe Perches63c3a662011-04-26 08:12:10 +000011798 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011799 return -EINVAL;
11800
Linus Torvalds1da177e2005-04-16 15:20:36 -070011801 offset = eeprom->offset;
11802 len = eeprom->len;
11803 eeprom->len = 0;
11804
11805 eeprom->magic = TG3_EEPROM_MAGIC;
11806
11807 if (offset & 3) {
11808 /* adjustments to start on required 4 byte boundary */
11809 b_offset = offset & 3;
11810 b_count = 4 - b_offset;
11811 if (b_count > len) {
11812 /* i.e. offset=1 len=2 */
11813 b_count = len;
11814 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011815 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011816 if (ret)
11817 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011818 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011819 len -= b_count;
11820 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011821 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011822 }
11823
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011824 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011825 pd = &data[eeprom->len];
11826 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011827 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011828 if (ret) {
11829 eeprom->len += i;
11830 return ret;
11831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011832 memcpy(pd + i, &val, 4);
11833 }
11834 eeprom->len += i;
11835
11836 if (len & 3) {
11837 /* read last bytes not ending on 4 byte boundary */
11838 pd = &data[eeprom->len];
11839 b_count = len & 3;
11840 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011841 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842 if (ret)
11843 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011844 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011845 eeprom->len += b_count;
11846 }
11847 return 0;
11848}
11849
Linus Torvalds1da177e2005-04-16 15:20:36 -070011850static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11851{
11852 struct tg3 *tp = netdev_priv(dev);
11853 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011854 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011855 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011856 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011857
Joe Perches63c3a662011-04-26 08:12:10 +000011858 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011859 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011860 return -EINVAL;
11861
11862 offset = eeprom->offset;
11863 len = eeprom->len;
11864
11865 if ((b_offset = (offset & 3))) {
11866 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011867 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011868 if (ret)
11869 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011870 len += b_offset;
11871 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070011872 if (len < 4)
11873 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011874 }
11875
11876 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070011877 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011878 /* adjustments to end on required 4 byte boundary */
11879 odd_len = 1;
11880 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011881 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011882 if (ret)
11883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011884 }
11885
11886 buf = data;
11887 if (b_offset || odd_len) {
11888 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011889 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011890 return -ENOMEM;
11891 if (b_offset)
11892 memcpy(buf, &start, 4);
11893 if (odd_len)
11894 memcpy(buf+len-4, &end, 4);
11895 memcpy(buf + b_offset, data, eeprom->len);
11896 }
11897
11898 ret = tg3_nvram_write_block(tp, offset, len, buf);
11899
11900 if (buf != data)
11901 kfree(buf);
11902
11903 return ret;
11904}
11905
11906static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11907{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011908 struct tg3 *tp = netdev_priv(dev);
11909
Joe Perches63c3a662011-04-26 08:12:10 +000011910 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011911 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011912 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011913 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011914 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11915 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011916 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011917
Linus Torvalds1da177e2005-04-16 15:20:36 -070011918 cmd->supported = (SUPPORTED_Autoneg);
11919
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011920 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011921 cmd->supported |= (SUPPORTED_1000baseT_Half |
11922 SUPPORTED_1000baseT_Full);
11923
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011924 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011925 cmd->supported |= (SUPPORTED_100baseT_Half |
11926 SUPPORTED_100baseT_Full |
11927 SUPPORTED_10baseT_Half |
11928 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011929 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011930 cmd->port = PORT_TP;
11931 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011932 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011933 cmd->port = PORT_FIBRE;
11934 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011935
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011937 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11938 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11939 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11940 cmd->advertising |= ADVERTISED_Pause;
11941 } else {
11942 cmd->advertising |= ADVERTISED_Pause |
11943 ADVERTISED_Asym_Pause;
11944 }
11945 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11946 cmd->advertising |= ADVERTISED_Asym_Pause;
11947 }
11948 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011949 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011950 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011952 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011953 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11954 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11955 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11956 else
11957 cmd->eth_tp_mdix = ETH_TP_MDI;
11958 }
Matt Carlson64c22182010-10-14 10:37:44 +000011959 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011960 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11961 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011962 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011963 }
Matt Carlson882e9792009-09-01 13:21:36 +000011964 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011965 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011966 cmd->autoneg = tp->link_config.autoneg;
11967 cmd->maxtxpkt = 0;
11968 cmd->maxrxpkt = 0;
11969 return 0;
11970}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011971
Linus Torvalds1da177e2005-04-16 15:20:36 -070011972static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11973{
11974 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011975 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011976
Joe Perches63c3a662011-04-26 08:12:10 +000011977 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011978 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011979 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011980 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011981 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11982 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011983 }
11984
Matt Carlson7e5856b2009-02-25 14:23:01 +000011985 if (cmd->autoneg != AUTONEG_ENABLE &&
11986 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011987 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011988
11989 if (cmd->autoneg == AUTONEG_DISABLE &&
11990 cmd->duplex != DUPLEX_FULL &&
11991 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011992 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011993
Matt Carlson7e5856b2009-02-25 14:23:01 +000011994 if (cmd->autoneg == AUTONEG_ENABLE) {
11995 u32 mask = ADVERTISED_Autoneg |
11996 ADVERTISED_Pause |
11997 ADVERTISED_Asym_Pause;
11998
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011999 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012000 mask |= ADVERTISED_1000baseT_Half |
12001 ADVERTISED_1000baseT_Full;
12002
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012003 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012004 mask |= ADVERTISED_100baseT_Half |
12005 ADVERTISED_100baseT_Full |
12006 ADVERTISED_10baseT_Half |
12007 ADVERTISED_10baseT_Full |
12008 ADVERTISED_TP;
12009 else
12010 mask |= ADVERTISED_FIBRE;
12011
12012 if (cmd->advertising & ~mask)
12013 return -EINVAL;
12014
12015 mask &= (ADVERTISED_1000baseT_Half |
12016 ADVERTISED_1000baseT_Full |
12017 ADVERTISED_100baseT_Half |
12018 ADVERTISED_100baseT_Full |
12019 ADVERTISED_10baseT_Half |
12020 ADVERTISED_10baseT_Full);
12021
12022 cmd->advertising &= mask;
12023 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012024 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012025 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012026 return -EINVAL;
12027
12028 if (cmd->duplex != DUPLEX_FULL)
12029 return -EINVAL;
12030 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012031 if (speed != SPEED_100 &&
12032 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012033 return -EINVAL;
12034 }
12035 }
12036
David S. Millerf47c11e2005-06-24 20:18:35 -070012037 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012038
12039 tp->link_config.autoneg = cmd->autoneg;
12040 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012041 tp->link_config.advertising = (cmd->advertising |
12042 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012043 tp->link_config.speed = SPEED_UNKNOWN;
12044 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012045 } else {
12046 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012047 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012048 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012049 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012050
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012051 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12052
Nithin Sujirce20f162013-04-09 08:48:04 +000012053 tg3_warn_mgmt_link_flap(tp);
12054
Linus Torvalds1da177e2005-04-16 15:20:36 -070012055 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012056 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012057
David S. Millerf47c11e2005-06-24 20:18:35 -070012058 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012059
Linus Torvalds1da177e2005-04-16 15:20:36 -070012060 return 0;
12061}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012062
Linus Torvalds1da177e2005-04-16 15:20:36 -070012063static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12064{
12065 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012066
Rick Jones68aad782011-11-07 13:29:27 +000012067 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12068 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12069 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12070 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012071}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012072
Linus Torvalds1da177e2005-04-16 15:20:36 -070012073static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12074{
12075 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012076
Joe Perches63c3a662011-04-26 08:12:10 +000012077 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012078 wol->supported = WAKE_MAGIC;
12079 else
12080 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012081 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012082 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012083 wol->wolopts = WAKE_MAGIC;
12084 memset(&wol->sopass, 0, sizeof(wol->sopass));
12085}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012086
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12088{
12089 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012090 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012091
Linus Torvalds1da177e2005-04-16 15:20:36 -070012092 if (wol->wolopts & ~WAKE_MAGIC)
12093 return -EINVAL;
12094 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012095 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012096 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012097
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012098 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12099
David S. Millerf47c11e2005-06-24 20:18:35 -070012100 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012101 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012102 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012103 else
Joe Perches63c3a662011-04-26 08:12:10 +000012104 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070012105 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012106
Linus Torvalds1da177e2005-04-16 15:20:36 -070012107 return 0;
12108}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012109
Linus Torvalds1da177e2005-04-16 15:20:36 -070012110static u32 tg3_get_msglevel(struct net_device *dev)
12111{
12112 struct tg3 *tp = netdev_priv(dev);
12113 return tp->msg_enable;
12114}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012115
Linus Torvalds1da177e2005-04-16 15:20:36 -070012116static void tg3_set_msglevel(struct net_device *dev, u32 value)
12117{
12118 struct tg3 *tp = netdev_priv(dev);
12119 tp->msg_enable = value;
12120}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012121
Linus Torvalds1da177e2005-04-16 15:20:36 -070012122static int tg3_nway_reset(struct net_device *dev)
12123{
12124 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012125 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012126
Linus Torvalds1da177e2005-04-16 15:20:36 -070012127 if (!netif_running(dev))
12128 return -EAGAIN;
12129
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012130 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012131 return -EINVAL;
12132
Nithin Sujirce20f162013-04-09 08:48:04 +000012133 tg3_warn_mgmt_link_flap(tp);
12134
Joe Perches63c3a662011-04-26 08:12:10 +000012135 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012136 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012137 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012138 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012139 } else {
12140 u32 bmcr;
12141
12142 spin_lock_bh(&tp->lock);
12143 r = -EINVAL;
12144 tg3_readphy(tp, MII_BMCR, &bmcr);
12145 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12146 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012147 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012148 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12149 BMCR_ANENABLE);
12150 r = 0;
12151 }
12152 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012153 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012154
Linus Torvalds1da177e2005-04-16 15:20:36 -070012155 return r;
12156}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012157
Linus Torvalds1da177e2005-04-16 15:20:36 -070012158static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12159{
12160 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012161
Matt Carlson2c49a442010-09-30 10:34:35 +000012162 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012163 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012164 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012165 else
12166 ering->rx_jumbo_max_pending = 0;
12167
12168 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012169
12170 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012171 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012172 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12173 else
12174 ering->rx_jumbo_pending = 0;
12175
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012176 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012177}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012178
Linus Torvalds1da177e2005-04-16 15:20:36 -070012179static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12180{
12181 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012182 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012183
Matt Carlson2c49a442010-09-30 10:34:35 +000012184 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12185 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012186 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12187 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012188 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012189 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012190 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012191
Michael Chanbbe832c2005-06-24 20:20:04 -070012192 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012193 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012195 irq_sync = 1;
12196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012197
Michael Chanbbe832c2005-06-24 20:20:04 -070012198 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012199
Linus Torvalds1da177e2005-04-16 15:20:36 -070012200 tp->rx_pending = ering->rx_pending;
12201
Joe Perches63c3a662011-04-26 08:12:10 +000012202 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012203 tp->rx_pending > 63)
12204 tp->rx_pending = 63;
12205 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012206
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012207 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012208 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012209
12210 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012211 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012212 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012213 if (!err)
12214 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012215 }
12216
David S. Millerf47c11e2005-06-24 20:18:35 -070012217 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012218
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012219 if (irq_sync && !err)
12220 tg3_phy_start(tp);
12221
Michael Chanb9ec6c12006-07-25 16:37:27 -070012222 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012223}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012224
Linus Torvalds1da177e2005-04-16 15:20:36 -070012225static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12226{
12227 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012228
Joe Perches63c3a662011-04-26 08:12:10 +000012229 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012230
Matt Carlson4a2db502011-12-08 14:40:17 +000012231 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012232 epause->rx_pause = 1;
12233 else
12234 epause->rx_pause = 0;
12235
Matt Carlson4a2db502011-12-08 14:40:17 +000012236 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012237 epause->tx_pause = 1;
12238 else
12239 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012240}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012241
Linus Torvalds1da177e2005-04-16 15:20:36 -070012242static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12243{
12244 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012245 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012246
Nithin Sujirce20f162013-04-09 08:48:04 +000012247 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12248 tg3_warn_mgmt_link_flap(tp);
12249
Joe Perches63c3a662011-04-26 08:12:10 +000012250 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012251 u32 newadv;
12252 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012253
Matt Carlson27121682010-02-17 15:16:57 +000012254 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012255
Matt Carlson27121682010-02-17 15:16:57 +000012256 if (!(phydev->supported & SUPPORTED_Pause) ||
12257 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012258 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012259 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012260
Matt Carlson27121682010-02-17 15:16:57 +000012261 tp->link_config.flowctrl = 0;
12262 if (epause->rx_pause) {
12263 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012264
Matt Carlson27121682010-02-17 15:16:57 +000012265 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012266 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012267 newadv = ADVERTISED_Pause;
12268 } else
12269 newadv = ADVERTISED_Pause |
12270 ADVERTISED_Asym_Pause;
12271 } else if (epause->tx_pause) {
12272 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12273 newadv = ADVERTISED_Asym_Pause;
12274 } else
12275 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012276
Matt Carlson27121682010-02-17 15:16:57 +000012277 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012278 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012279 else
Joe Perches63c3a662011-04-26 08:12:10 +000012280 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012281
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012282 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012283 u32 oldadv = phydev->advertising &
12284 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12285 if (oldadv != newadv) {
12286 phydev->advertising &=
12287 ~(ADVERTISED_Pause |
12288 ADVERTISED_Asym_Pause);
12289 phydev->advertising |= newadv;
12290 if (phydev->autoneg) {
12291 /*
12292 * Always renegotiate the link to
12293 * inform our link partner of our
12294 * flow control settings, even if the
12295 * flow control is forced. Let
12296 * tg3_adjust_link() do the final
12297 * flow control setup.
12298 */
12299 return phy_start_aneg(phydev);
12300 }
12301 }
12302
12303 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012304 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012305 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012306 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012307 ~(ADVERTISED_Pause |
12308 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012309 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012310 }
12311 } else {
12312 int irq_sync = 0;
12313
12314 if (netif_running(dev)) {
12315 tg3_netif_stop(tp);
12316 irq_sync = 1;
12317 }
12318
12319 tg3_full_lock(tp, irq_sync);
12320
12321 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012322 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012323 else
Joe Perches63c3a662011-04-26 08:12:10 +000012324 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012325 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012326 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012327 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012328 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012329 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012330 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012331 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012332 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012333
12334 if (netif_running(dev)) {
12335 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012336 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012337 if (!err)
12338 tg3_netif_start(tp);
12339 }
12340
12341 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012343
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012344 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12345
Michael Chanb9ec6c12006-07-25 16:37:27 -070012346 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012347}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012348
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012349static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012350{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012351 switch (sset) {
12352 case ETH_SS_TEST:
12353 return TG3_NUM_TEST;
12354 case ETH_SS_STATS:
12355 return TG3_NUM_STATS;
12356 default:
12357 return -EOPNOTSUPP;
12358 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012359}
12360
Matt Carlson90415472011-12-16 13:33:23 +000012361static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12362 u32 *rules __always_unused)
12363{
12364 struct tg3 *tp = netdev_priv(dev);
12365
12366 if (!tg3_flag(tp, SUPPORT_MSIX))
12367 return -EOPNOTSUPP;
12368
12369 switch (info->cmd) {
12370 case ETHTOOL_GRXRINGS:
12371 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012372 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012373 else {
12374 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012375 if (info->data > TG3_RSS_MAX_NUM_QS)
12376 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012377 }
12378
12379 /* The first interrupt vector only
12380 * handles link interrupts.
12381 */
12382 info->data -= 1;
12383 return 0;
12384
12385 default:
12386 return -EOPNOTSUPP;
12387 }
12388}
12389
12390static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12391{
12392 u32 size = 0;
12393 struct tg3 *tp = netdev_priv(dev);
12394
12395 if (tg3_flag(tp, SUPPORT_MSIX))
12396 size = TG3_RSS_INDIR_TBL_SIZE;
12397
12398 return size;
12399}
12400
12401static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12402{
12403 struct tg3 *tp = netdev_priv(dev);
12404 int i;
12405
12406 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12407 indir[i] = tp->rss_ind_tbl[i];
12408
12409 return 0;
12410}
12411
12412static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12413{
12414 struct tg3 *tp = netdev_priv(dev);
12415 size_t i;
12416
12417 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12418 tp->rss_ind_tbl[i] = indir[i];
12419
12420 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12421 return 0;
12422
12423 /* It is legal to write the indirection
12424 * table while the device is running.
12425 */
12426 tg3_full_lock(tp, 0);
12427 tg3_rss_write_indir_tbl(tp);
12428 tg3_full_unlock(tp);
12429
12430 return 0;
12431}
12432
Michael Chan09681692012-09-28 07:12:42 +000012433static void tg3_get_channels(struct net_device *dev,
12434 struct ethtool_channels *channel)
12435{
12436 struct tg3 *tp = netdev_priv(dev);
12437 u32 deflt_qs = netif_get_num_default_rss_queues();
12438
12439 channel->max_rx = tp->rxq_max;
12440 channel->max_tx = tp->txq_max;
12441
12442 if (netif_running(dev)) {
12443 channel->rx_count = tp->rxq_cnt;
12444 channel->tx_count = tp->txq_cnt;
12445 } else {
12446 if (tp->rxq_req)
12447 channel->rx_count = tp->rxq_req;
12448 else
12449 channel->rx_count = min(deflt_qs, tp->rxq_max);
12450
12451 if (tp->txq_req)
12452 channel->tx_count = tp->txq_req;
12453 else
12454 channel->tx_count = min(deflt_qs, tp->txq_max);
12455 }
12456}
12457
12458static int tg3_set_channels(struct net_device *dev,
12459 struct ethtool_channels *channel)
12460{
12461 struct tg3 *tp = netdev_priv(dev);
12462
12463 if (!tg3_flag(tp, SUPPORT_MSIX))
12464 return -EOPNOTSUPP;
12465
12466 if (channel->rx_count > tp->rxq_max ||
12467 channel->tx_count > tp->txq_max)
12468 return -EINVAL;
12469
12470 tp->rxq_req = channel->rx_count;
12471 tp->txq_req = channel->tx_count;
12472
12473 if (!netif_running(dev))
12474 return 0;
12475
12476 tg3_stop(tp);
12477
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012478 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012479
Matt Carlsonbe947302012-12-03 19:36:57 +000012480 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012481
12482 return 0;
12483}
12484
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012485static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012486{
12487 switch (stringset) {
12488 case ETH_SS_STATS:
12489 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12490 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012491 case ETH_SS_TEST:
12492 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12493 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012494 default:
12495 WARN_ON(1); /* we need a WARN() */
12496 break;
12497 }
12498}
12499
stephen hemminger81b87092011-04-04 08:43:50 +000012500static int tg3_set_phys_id(struct net_device *dev,
12501 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012502{
12503 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012504
12505 if (!netif_running(tp->dev))
12506 return -EAGAIN;
12507
stephen hemminger81b87092011-04-04 08:43:50 +000012508 switch (state) {
12509 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012510 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012511
stephen hemminger81b87092011-04-04 08:43:50 +000012512 case ETHTOOL_ID_ON:
12513 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12514 LED_CTRL_1000MBPS_ON |
12515 LED_CTRL_100MBPS_ON |
12516 LED_CTRL_10MBPS_ON |
12517 LED_CTRL_TRAFFIC_OVERRIDE |
12518 LED_CTRL_TRAFFIC_BLINK |
12519 LED_CTRL_TRAFFIC_LED);
12520 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012521
stephen hemminger81b87092011-04-04 08:43:50 +000012522 case ETHTOOL_ID_OFF:
12523 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12524 LED_CTRL_TRAFFIC_OVERRIDE);
12525 break;
Michael Chan4009a932005-09-05 17:52:54 -070012526
stephen hemminger81b87092011-04-04 08:43:50 +000012527 case ETHTOOL_ID_INACTIVE:
12528 tw32(MAC_LED_CTRL, tp->led_ctrl);
12529 break;
Michael Chan4009a932005-09-05 17:52:54 -070012530 }
stephen hemminger81b87092011-04-04 08:43:50 +000012531
Michael Chan4009a932005-09-05 17:52:54 -070012532 return 0;
12533}
12534
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012535static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012536 struct ethtool_stats *estats, u64 *tmp_stats)
12537{
12538 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012539
Matt Carlsonb546e462012-02-13 15:20:09 +000012540 if (tp->hw_stats)
12541 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12542 else
12543 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012544}
12545
Matt Carlson535a4902011-07-20 10:20:56 +000012546static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012547{
12548 int i;
12549 __be32 *buf;
12550 u32 offset = 0, len = 0;
12551 u32 magic, val;
12552
Joe Perches63c3a662011-04-26 08:12:10 +000012553 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012554 return NULL;
12555
12556 if (magic == TG3_EEPROM_MAGIC) {
12557 for (offset = TG3_NVM_DIR_START;
12558 offset < TG3_NVM_DIR_END;
12559 offset += TG3_NVM_DIRENT_SIZE) {
12560 if (tg3_nvram_read(tp, offset, &val))
12561 return NULL;
12562
12563 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12564 TG3_NVM_DIRTYPE_EXTVPD)
12565 break;
12566 }
12567
12568 if (offset != TG3_NVM_DIR_END) {
12569 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12570 if (tg3_nvram_read(tp, offset + 4, &offset))
12571 return NULL;
12572
12573 offset = tg3_nvram_logical_addr(tp, offset);
12574 }
12575 }
12576
12577 if (!offset || !len) {
12578 offset = TG3_NVM_VPD_OFF;
12579 len = TG3_NVM_VPD_LEN;
12580 }
12581
12582 buf = kmalloc(len, GFP_KERNEL);
12583 if (buf == NULL)
12584 return NULL;
12585
12586 if (magic == TG3_EEPROM_MAGIC) {
12587 for (i = 0; i < len; i += 4) {
12588 /* The data is in little-endian format in NVRAM.
12589 * Use the big-endian read routines to preserve
12590 * the byte order as it exists in NVRAM.
12591 */
12592 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12593 goto error;
12594 }
12595 } else {
12596 u8 *ptr;
12597 ssize_t cnt;
12598 unsigned int pos = 0;
12599
12600 ptr = (u8 *)&buf[0];
12601 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12602 cnt = pci_read_vpd(tp->pdev, pos,
12603 len - pos, ptr);
12604 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12605 cnt = 0;
12606 else if (cnt < 0)
12607 goto error;
12608 }
12609 if (pos != len)
12610 goto error;
12611 }
12612
Matt Carlson535a4902011-07-20 10:20:56 +000012613 *vpdlen = len;
12614
Matt Carlsonc3e94502011-04-13 11:05:08 +000012615 return buf;
12616
12617error:
12618 kfree(buf);
12619 return NULL;
12620}
12621
Michael Chan566f86a2005-05-29 14:56:58 -070012622#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012623#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12624#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12625#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012626#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12627#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012628#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012629#define NVRAM_SELFBOOT_HW_SIZE 0x20
12630#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012631
12632static int tg3_test_nvram(struct tg3 *tp)
12633{
Matt Carlson535a4902011-07-20 10:20:56 +000012634 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012635 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012636 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012637
Joe Perches63c3a662011-04-26 08:12:10 +000012638 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012639 return 0;
12640
Matt Carlsone4f34112009-02-25 14:25:00 +000012641 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012642 return -EIO;
12643
Michael Chan1b277772006-03-20 22:27:48 -080012644 if (magic == TG3_EEPROM_MAGIC)
12645 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012646 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012647 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12648 TG3_EEPROM_SB_FORMAT_1) {
12649 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12650 case TG3_EEPROM_SB_REVISION_0:
12651 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12652 break;
12653 case TG3_EEPROM_SB_REVISION_2:
12654 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12655 break;
12656 case TG3_EEPROM_SB_REVISION_3:
12657 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12658 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012659 case TG3_EEPROM_SB_REVISION_4:
12660 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12661 break;
12662 case TG3_EEPROM_SB_REVISION_5:
12663 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12664 break;
12665 case TG3_EEPROM_SB_REVISION_6:
12666 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12667 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012668 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012669 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012670 }
12671 } else
Michael Chan1b277772006-03-20 22:27:48 -080012672 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012673 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12674 size = NVRAM_SELFBOOT_HW_SIZE;
12675 else
Michael Chan1b277772006-03-20 22:27:48 -080012676 return -EIO;
12677
12678 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012679 if (buf == NULL)
12680 return -ENOMEM;
12681
Michael Chan1b277772006-03-20 22:27:48 -080012682 err = -EIO;
12683 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012684 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12685 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012686 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012687 }
Michael Chan1b277772006-03-20 22:27:48 -080012688 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012689 goto out;
12690
Michael Chan1b277772006-03-20 22:27:48 -080012691 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012692 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012693 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012694 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012695 u8 *buf8 = (u8 *) buf, csum8 = 0;
12696
Al Virob9fc7dc2007-12-17 22:59:57 -080012697 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012698 TG3_EEPROM_SB_REVISION_2) {
12699 /* For rev 2, the csum doesn't include the MBA. */
12700 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12701 csum8 += buf8[i];
12702 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12703 csum8 += buf8[i];
12704 } else {
12705 for (i = 0; i < size; i++)
12706 csum8 += buf8[i];
12707 }
Michael Chan1b277772006-03-20 22:27:48 -080012708
Adrian Bunkad96b482006-04-05 22:21:04 -070012709 if (csum8 == 0) {
12710 err = 0;
12711 goto out;
12712 }
12713
12714 err = -EIO;
12715 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012716 }
Michael Chan566f86a2005-05-29 14:56:58 -070012717
Al Virob9fc7dc2007-12-17 22:59:57 -080012718 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012719 TG3_EEPROM_MAGIC_HW) {
12720 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012721 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012722 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012723
12724 /* Separate the parity bits and the data bytes. */
12725 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12726 if ((i == 0) || (i == 8)) {
12727 int l;
12728 u8 msk;
12729
12730 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12731 parity[k++] = buf8[i] & msk;
12732 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012733 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012734 int l;
12735 u8 msk;
12736
12737 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12738 parity[k++] = buf8[i] & msk;
12739 i++;
12740
12741 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12742 parity[k++] = buf8[i] & msk;
12743 i++;
12744 }
12745 data[j++] = buf8[i];
12746 }
12747
12748 err = -EIO;
12749 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12750 u8 hw8 = hweight8(data[i]);
12751
12752 if ((hw8 & 0x1) && parity[i])
12753 goto out;
12754 else if (!(hw8 & 0x1) && !parity[i])
12755 goto out;
12756 }
12757 err = 0;
12758 goto out;
12759 }
12760
Matt Carlson01c3a392011-03-09 16:58:20 +000012761 err = -EIO;
12762
Michael Chan566f86a2005-05-29 14:56:58 -070012763 /* Bootstrap checksum at offset 0x10 */
12764 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012765 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012766 goto out;
12767
12768 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12769 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012770 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012771 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012772
Matt Carlsonc3e94502011-04-13 11:05:08 +000012773 kfree(buf);
12774
Matt Carlson535a4902011-07-20 10:20:56 +000012775 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012776 if (!buf)
12777 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012778
Matt Carlson535a4902011-07-20 10:20:56 +000012779 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012780 if (i > 0) {
12781 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12782 if (j < 0)
12783 goto out;
12784
Matt Carlson535a4902011-07-20 10:20:56 +000012785 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012786 goto out;
12787
12788 i += PCI_VPD_LRDT_TAG_SIZE;
12789 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12790 PCI_VPD_RO_KEYWORD_CHKSUM);
12791 if (j > 0) {
12792 u8 csum8 = 0;
12793
12794 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12795
12796 for (i = 0; i <= j; i++)
12797 csum8 += ((u8 *)buf)[i];
12798
12799 if (csum8)
12800 goto out;
12801 }
12802 }
12803
Michael Chan566f86a2005-05-29 14:56:58 -070012804 err = 0;
12805
12806out:
12807 kfree(buf);
12808 return err;
12809}
12810
Michael Chanca430072005-05-29 14:57:23 -070012811#define TG3_SERDES_TIMEOUT_SEC 2
12812#define TG3_COPPER_TIMEOUT_SEC 6
12813
12814static int tg3_test_link(struct tg3 *tp)
12815{
12816 int i, max;
12817
12818 if (!netif_running(tp->dev))
12819 return -ENODEV;
12820
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012821 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012822 max = TG3_SERDES_TIMEOUT_SEC;
12823 else
12824 max = TG3_COPPER_TIMEOUT_SEC;
12825
12826 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012827 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012828 return 0;
12829
12830 if (msleep_interruptible(1000))
12831 break;
12832 }
12833
12834 return -EIO;
12835}
12836
Michael Chana71116d2005-05-29 14:58:11 -070012837/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012838static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012839{
Michael Chanb16250e2006-09-27 16:10:14 -070012840 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012841 u32 offset, read_mask, write_mask, val, save_val, read_val;
12842 static struct {
12843 u16 offset;
12844 u16 flags;
12845#define TG3_FL_5705 0x1
12846#define TG3_FL_NOT_5705 0x2
12847#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012848#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012849 u32 read_mask;
12850 u32 write_mask;
12851 } reg_tbl[] = {
12852 /* MAC Control Registers */
12853 { MAC_MODE, TG3_FL_NOT_5705,
12854 0x00000000, 0x00ef6f8c },
12855 { MAC_MODE, TG3_FL_5705,
12856 0x00000000, 0x01ef6b8c },
12857 { MAC_STATUS, TG3_FL_NOT_5705,
12858 0x03800107, 0x00000000 },
12859 { MAC_STATUS, TG3_FL_5705,
12860 0x03800100, 0x00000000 },
12861 { MAC_ADDR_0_HIGH, 0x0000,
12862 0x00000000, 0x0000ffff },
12863 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012864 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012865 { MAC_RX_MTU_SIZE, 0x0000,
12866 0x00000000, 0x0000ffff },
12867 { MAC_TX_MODE, 0x0000,
12868 0x00000000, 0x00000070 },
12869 { MAC_TX_LENGTHS, 0x0000,
12870 0x00000000, 0x00003fff },
12871 { MAC_RX_MODE, TG3_FL_NOT_5705,
12872 0x00000000, 0x000007fc },
12873 { MAC_RX_MODE, TG3_FL_5705,
12874 0x00000000, 0x000007dc },
12875 { MAC_HASH_REG_0, 0x0000,
12876 0x00000000, 0xffffffff },
12877 { MAC_HASH_REG_1, 0x0000,
12878 0x00000000, 0xffffffff },
12879 { MAC_HASH_REG_2, 0x0000,
12880 0x00000000, 0xffffffff },
12881 { MAC_HASH_REG_3, 0x0000,
12882 0x00000000, 0xffffffff },
12883
12884 /* Receive Data and Receive BD Initiator Control Registers. */
12885 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12886 0x00000000, 0xffffffff },
12887 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12888 0x00000000, 0xffffffff },
12889 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12890 0x00000000, 0x00000003 },
12891 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12892 0x00000000, 0xffffffff },
12893 { RCVDBDI_STD_BD+0, 0x0000,
12894 0x00000000, 0xffffffff },
12895 { RCVDBDI_STD_BD+4, 0x0000,
12896 0x00000000, 0xffffffff },
12897 { RCVDBDI_STD_BD+8, 0x0000,
12898 0x00000000, 0xffff0002 },
12899 { RCVDBDI_STD_BD+0xc, 0x0000,
12900 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012901
Michael Chana71116d2005-05-29 14:58:11 -070012902 /* Receive BD Initiator Control Registers. */
12903 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12904 0x00000000, 0xffffffff },
12905 { RCVBDI_STD_THRESH, TG3_FL_5705,
12906 0x00000000, 0x000003ff },
12907 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12908 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012909
Michael Chana71116d2005-05-29 14:58:11 -070012910 /* Host Coalescing Control Registers. */
12911 { HOSTCC_MODE, TG3_FL_NOT_5705,
12912 0x00000000, 0x00000004 },
12913 { HOSTCC_MODE, TG3_FL_5705,
12914 0x00000000, 0x000000f6 },
12915 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12916 0x00000000, 0xffffffff },
12917 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12918 0x00000000, 0x000003ff },
12919 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12920 0x00000000, 0xffffffff },
12921 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12922 0x00000000, 0x000003ff },
12923 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12924 0x00000000, 0xffffffff },
12925 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12926 0x00000000, 0x000000ff },
12927 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12928 0x00000000, 0xffffffff },
12929 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12930 0x00000000, 0x000000ff },
12931 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12932 0x00000000, 0xffffffff },
12933 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12934 0x00000000, 0xffffffff },
12935 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12936 0x00000000, 0xffffffff },
12937 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12938 0x00000000, 0x000000ff },
12939 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12940 0x00000000, 0xffffffff },
12941 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12942 0x00000000, 0x000000ff },
12943 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12944 0x00000000, 0xffffffff },
12945 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12946 0x00000000, 0xffffffff },
12947 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12948 0x00000000, 0xffffffff },
12949 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12950 0x00000000, 0xffffffff },
12951 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12952 0x00000000, 0xffffffff },
12953 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12954 0xffffffff, 0x00000000 },
12955 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12956 0xffffffff, 0x00000000 },
12957
12958 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012959 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012960 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012961 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012962 0x00000000, 0x007fffff },
12963 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12964 0x00000000, 0x0000003f },
12965 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12966 0x00000000, 0x000001ff },
12967 { BUFMGR_MB_HIGH_WATER, 0x0000,
12968 0x00000000, 0x000001ff },
12969 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12970 0xffffffff, 0x00000000 },
12971 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12972 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012973
Michael Chana71116d2005-05-29 14:58:11 -070012974 /* Mailbox Registers */
12975 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12976 0x00000000, 0x000001ff },
12977 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12978 0x00000000, 0x000001ff },
12979 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12980 0x00000000, 0x000007ff },
12981 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12982 0x00000000, 0x000001ff },
12983
12984 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12985 };
12986
Michael Chanb16250e2006-09-27 16:10:14 -070012987 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012988 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012989 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012990 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012991 is_5750 = 1;
12992 }
Michael Chana71116d2005-05-29 14:58:11 -070012993
12994 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12995 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12996 continue;
12997
12998 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12999 continue;
13000
Joe Perches63c3a662011-04-26 08:12:10 +000013001 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013002 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13003 continue;
13004
Michael Chanb16250e2006-09-27 16:10:14 -070013005 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13006 continue;
13007
Michael Chana71116d2005-05-29 14:58:11 -070013008 offset = (u32) reg_tbl[i].offset;
13009 read_mask = reg_tbl[i].read_mask;
13010 write_mask = reg_tbl[i].write_mask;
13011
13012 /* Save the original register content */
13013 save_val = tr32(offset);
13014
13015 /* Determine the read-only value. */
13016 read_val = save_val & read_mask;
13017
13018 /* Write zero to the register, then make sure the read-only bits
13019 * are not changed and the read/write bits are all zeros.
13020 */
13021 tw32(offset, 0);
13022
13023 val = tr32(offset);
13024
13025 /* Test the read-only and read/write bits. */
13026 if (((val & read_mask) != read_val) || (val & write_mask))
13027 goto out;
13028
13029 /* Write ones to all the bits defined by RdMask and WrMask, then
13030 * make sure the read-only bits are not changed and the
13031 * read/write bits are all ones.
13032 */
13033 tw32(offset, read_mask | write_mask);
13034
13035 val = tr32(offset);
13036
13037 /* Test the read-only bits. */
13038 if ((val & read_mask) != read_val)
13039 goto out;
13040
13041 /* Test the read/write bits. */
13042 if ((val & write_mask) != write_mask)
13043 goto out;
13044
13045 tw32(offset, save_val);
13046 }
13047
13048 return 0;
13049
13050out:
Michael Chan9f88f292006-12-07 00:22:54 -080013051 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013052 netdev_err(tp->dev,
13053 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013054 tw32(offset, save_val);
13055 return -EIO;
13056}
13057
Michael Chan7942e1d2005-05-29 14:58:36 -070013058static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13059{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013060 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013061 int i;
13062 u32 j;
13063
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013064 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013065 for (j = 0; j < len; j += 4) {
13066 u32 val;
13067
13068 tg3_write_mem(tp, offset + j, test_pattern[i]);
13069 tg3_read_mem(tp, offset + j, &val);
13070 if (val != test_pattern[i])
13071 return -EIO;
13072 }
13073 }
13074 return 0;
13075}
13076
13077static int tg3_test_memory(struct tg3 *tp)
13078{
13079 static struct mem_entry {
13080 u32 offset;
13081 u32 len;
13082 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013083 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013084 { 0x00002000, 0x1c000},
13085 { 0xffffffff, 0x00000}
13086 }, mem_tbl_5705[] = {
13087 { 0x00000100, 0x0000c},
13088 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013089 { 0x00004000, 0x00800},
13090 { 0x00006000, 0x01000},
13091 { 0x00008000, 0x02000},
13092 { 0x00010000, 0x0e000},
13093 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013094 }, mem_tbl_5755[] = {
13095 { 0x00000200, 0x00008},
13096 { 0x00004000, 0x00800},
13097 { 0x00006000, 0x00800},
13098 { 0x00008000, 0x02000},
13099 { 0x00010000, 0x0c000},
13100 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013101 }, mem_tbl_5906[] = {
13102 { 0x00000200, 0x00008},
13103 { 0x00004000, 0x00400},
13104 { 0x00006000, 0x00400},
13105 { 0x00008000, 0x01000},
13106 { 0x00010000, 0x01000},
13107 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013108 }, mem_tbl_5717[] = {
13109 { 0x00000200, 0x00008},
13110 { 0x00010000, 0x0a000},
13111 { 0x00020000, 0x13c00},
13112 { 0xffffffff, 0x00000}
13113 }, mem_tbl_57765[] = {
13114 { 0x00000200, 0x00008},
13115 { 0x00004000, 0x00800},
13116 { 0x00006000, 0x09800},
13117 { 0x00010000, 0x0a000},
13118 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013119 };
13120 struct mem_entry *mem_tbl;
13121 int err = 0;
13122 int i;
13123
Joe Perches63c3a662011-04-26 08:12:10 +000013124 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013125 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013126 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013127 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013128 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013129 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013130 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013131 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013132 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013133 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013134 mem_tbl = mem_tbl_5705;
13135 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013136 mem_tbl = mem_tbl_570x;
13137
13138 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013139 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13140 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013141 break;
13142 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013143
Michael Chan7942e1d2005-05-29 14:58:36 -070013144 return err;
13145}
13146
Matt Carlsonbb158d62011-04-25 12:42:47 +000013147#define TG3_TSO_MSS 500
13148
13149#define TG3_TSO_IP_HDR_LEN 20
13150#define TG3_TSO_TCP_HDR_LEN 20
13151#define TG3_TSO_TCP_OPT_LEN 12
13152
13153static const u8 tg3_tso_header[] = {
131540x08, 0x00,
131550x45, 0x00, 0x00, 0x00,
131560x00, 0x00, 0x40, 0x00,
131570x40, 0x06, 0x00, 0x00,
131580x0a, 0x00, 0x00, 0x01,
131590x0a, 0x00, 0x00, 0x02,
131600x0d, 0x00, 0xe0, 0x00,
131610x00, 0x00, 0x01, 0x00,
131620x00, 0x00, 0x02, 0x00,
131630x80, 0x10, 0x10, 0x00,
131640x14, 0x09, 0x00, 0x00,
131650x01, 0x01, 0x08, 0x0a,
131660x11, 0x11, 0x11, 0x11,
131670x11, 0x11, 0x11, 0x11,
13168};
Michael Chan9f40dea2005-09-05 17:53:06 -070013169
Matt Carlson28a45952011-08-19 13:58:22 +000013170static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013171{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013172 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013173 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013174 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013175 struct sk_buff *skb;
13176 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013177 dma_addr_t map;
13178 int num_pkts, tx_len, rx_len, i, err;
13179 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013180 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013181 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013182
Matt Carlsonc8873402010-02-12 14:47:11 +000013183 tnapi = &tp->napi[0];
13184 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013185 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013186 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013187 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013188 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013189 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013190 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013191 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013192
Michael Chanc76949a2005-05-29 14:58:59 -070013193 err = -EIO;
13194
Matt Carlson4852a862011-04-13 11:05:07 +000013195 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013196 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013197 if (!skb)
13198 return -ENOMEM;
13199
Michael Chanc76949a2005-05-29 14:58:59 -070013200 tx_data = skb_put(skb, tx_len);
13201 memcpy(tx_data, tp->dev->dev_addr, 6);
13202 memset(tx_data + 6, 0x0, 8);
13203
Matt Carlson4852a862011-04-13 11:05:07 +000013204 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013205
Matt Carlson28a45952011-08-19 13:58:22 +000013206 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013207 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13208
13209 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13210 TG3_TSO_TCP_OPT_LEN;
13211
13212 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13213 sizeof(tg3_tso_header));
13214 mss = TG3_TSO_MSS;
13215
13216 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13217 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13218
13219 /* Set the total length field in the IP header */
13220 iph->tot_len = htons((u16)(mss + hdr_len));
13221
13222 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13223 TXD_FLAG_CPU_POST_DMA);
13224
Joe Perches63c3a662011-04-26 08:12:10 +000013225 if (tg3_flag(tp, HW_TSO_1) ||
13226 tg3_flag(tp, HW_TSO_2) ||
13227 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013228 struct tcphdr *th;
13229 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13230 th = (struct tcphdr *)&tx_data[val];
13231 th->check = 0;
13232 } else
13233 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13234
Joe Perches63c3a662011-04-26 08:12:10 +000013235 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013236 mss |= (hdr_len & 0xc) << 12;
13237 if (hdr_len & 0x10)
13238 base_flags |= 0x00000010;
13239 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013240 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013241 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013242 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013243 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013244 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13245 } else {
13246 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13247 }
13248
13249 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13250 } else {
13251 num_pkts = 1;
13252 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013253
13254 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13255 tx_len > VLAN_ETH_FRAME_LEN)
13256 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013257 }
13258
13259 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013260 tx_data[i] = (u8) (i & 0xff);
13261
Alexander Duyckf4188d82009-12-02 16:48:38 +000013262 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13263 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013264 dev_kfree_skb(skb);
13265 return -EIO;
13266 }
Michael Chanc76949a2005-05-29 14:58:59 -070013267
Matt Carlson0d681b22011-07-27 14:20:49 +000013268 val = tnapi->tx_prod;
13269 tnapi->tx_buffers[val].skb = skb;
13270 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13271
Michael Chanc76949a2005-05-29 14:58:59 -070013272 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013273 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013274
13275 udelay(10);
13276
Matt Carlson898a56f2009-08-28 14:02:40 +000013277 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013278
Matt Carlson84b67b22011-07-27 14:20:52 +000013279 budget = tg3_tx_avail(tnapi);
13280 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013281 base_flags | TXD_FLAG_END, mss, 0)) {
13282 tnapi->tx_buffers[val].skb = NULL;
13283 dev_kfree_skb(skb);
13284 return -EIO;
13285 }
Michael Chanc76949a2005-05-29 14:58:59 -070013286
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013287 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013288
Michael Chan6541b802012-03-04 14:48:14 +000013289 /* Sync BD data before updating mailbox */
13290 wmb();
13291
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013292 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13293 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013294
13295 udelay(10);
13296
Matt Carlson303fc922009-11-02 14:27:34 +000013297 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13298 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013299 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013300 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013301
13302 udelay(10);
13303
Matt Carlson898a56f2009-08-28 14:02:40 +000013304 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13305 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013306 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013307 (rx_idx == (rx_start_idx + num_pkts)))
13308 break;
13309 }
13310
Matt Carlsonba1142e2011-11-04 09:15:00 +000013311 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013312 dev_kfree_skb(skb);
13313
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013314 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013315 goto out;
13316
13317 if (rx_idx != rx_start_idx + num_pkts)
13318 goto out;
13319
Matt Carlsonbb158d62011-04-25 12:42:47 +000013320 val = data_off;
13321 while (rx_idx != rx_start_idx) {
13322 desc = &rnapi->rx_rcb[rx_start_idx++];
13323 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13324 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013325
Matt Carlsonbb158d62011-04-25 12:42:47 +000013326 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13327 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013328 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013329
Matt Carlsonbb158d62011-04-25 12:42:47 +000013330 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13331 - ETH_FCS_LEN;
13332
Matt Carlson28a45952011-08-19 13:58:22 +000013333 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013334 if (rx_len != tx_len)
13335 goto out;
13336
13337 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13338 if (opaque_key != RXD_OPAQUE_RING_STD)
13339 goto out;
13340 } else {
13341 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13342 goto out;
13343 }
13344 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13345 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013346 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013347 goto out;
13348 }
13349
13350 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013351 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013352 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13353 mapping);
13354 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013355 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013356 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13357 mapping);
13358 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013359 goto out;
13360
Matt Carlsonbb158d62011-04-25 12:42:47 +000013361 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13362 PCI_DMA_FROMDEVICE);
13363
Eric Dumazet9205fd92011-11-18 06:47:01 +000013364 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013365 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013366 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013367 goto out;
13368 }
Matt Carlson4852a862011-04-13 11:05:07 +000013369 }
13370
Michael Chanc76949a2005-05-29 14:58:59 -070013371 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013372
Eric Dumazet9205fd92011-11-18 06:47:01 +000013373 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013374out:
13375 return err;
13376}
13377
Matt Carlson00c266b2011-04-25 12:42:46 +000013378#define TG3_STD_LOOPBACK_FAILED 1
13379#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013380#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013381#define TG3_LOOPBACK_FAILED \
13382 (TG3_STD_LOOPBACK_FAILED | \
13383 TG3_JMB_LOOPBACK_FAILED | \
13384 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013385
Matt Carlson941ec902011-08-19 13:58:23 +000013386static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013387{
Matt Carlson28a45952011-08-19 13:58:22 +000013388 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013389 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013390 u32 jmb_pkt_sz = 9000;
13391
13392 if (tp->dma_limit)
13393 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013394
Matt Carlsonab789042011-01-25 15:58:54 +000013395 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13396 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13397
Matt Carlson28a45952011-08-19 13:58:22 +000013398 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013399 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13400 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013401 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013402 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013403 goto done;
13404 }
13405
Joe Perches953c96e2013-04-09 10:18:14 +000013406 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013407 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013408 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13409 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013410 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013411 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013412 goto done;
13413 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013414
Joe Perches63c3a662011-04-26 08:12:10 +000013415 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013416 int i;
13417
13418 /* Reroute all rx packets to the 1st queue */
13419 for (i = MAC_RSS_INDIR_TBL_0;
13420 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13421 tw32(i, 0x0);
13422 }
13423
Matt Carlson6e01b202011-08-19 13:58:20 +000013424 /* HW errata - mac loopback fails in some cases on 5780.
13425 * Normal traffic and PHY loopback are not affected by
13426 * errata. Also, the MAC loopback test is deprecated for
13427 * all newer ASIC revisions.
13428 */
Joe Perches41535772013-02-16 11:20:04 +000013429 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013430 !tg3_flag(tp, CPMU_PRESENT)) {
13431 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013432
Matt Carlson28a45952011-08-19 13:58:22 +000013433 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013434 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013435
13436 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013437 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013438 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013439
13440 tg3_mac_loopback(tp, false);
13441 }
Matt Carlson4852a862011-04-13 11:05:07 +000013442
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013443 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013444 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013445 int i;
13446
Matt Carlson941ec902011-08-19 13:58:23 +000013447 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013448
13449 /* Wait for link */
13450 for (i = 0; i < 100; i++) {
13451 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13452 break;
13453 mdelay(1);
13454 }
13455
Matt Carlson28a45952011-08-19 13:58:22 +000013456 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013457 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013458 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013459 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013460 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013461 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013462 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013463 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013464
Matt Carlson941ec902011-08-19 13:58:23 +000013465 if (do_extlpbk) {
13466 tg3_phy_lpbk_set(tp, 0, true);
13467
13468 /* All link indications report up, but the hardware
13469 * isn't really ready for about 20 msec. Double it
13470 * to be sure.
13471 */
13472 mdelay(40);
13473
13474 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013475 data[TG3_EXT_LOOPB_TEST] |=
13476 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013477 if (tg3_flag(tp, TSO_CAPABLE) &&
13478 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013479 data[TG3_EXT_LOOPB_TEST] |=
13480 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013481 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013482 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013483 data[TG3_EXT_LOOPB_TEST] |=
13484 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013485 }
13486
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013487 /* Re-enable gphy autopowerdown. */
13488 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13489 tg3_phy_toggle_apd(tp, true);
13490 }
Matt Carlson6833c042008-11-21 17:18:59 -080013491
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013492 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13493 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013494
Matt Carlsonab789042011-01-25 15:58:54 +000013495done:
13496 tp->phy_flags |= eee_cap;
13497
Michael Chan9f40dea2005-09-05 17:53:06 -070013498 return err;
13499}
13500
Michael Chan4cafd3f2005-05-29 14:56:34 -070013501static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13502 u64 *data)
13503{
Michael Chan566f86a2005-05-29 14:56:58 -070013504 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013505 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013506
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013507 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13508 if (tg3_power_up(tp)) {
13509 etest->flags |= ETH_TEST_FL_FAILED;
13510 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13511 return;
13512 }
13513 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013514 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013515
Michael Chan566f86a2005-05-29 14:56:58 -070013516 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13517
13518 if (tg3_test_nvram(tp) != 0) {
13519 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013520 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013521 }
Matt Carlson941ec902011-08-19 13:58:23 +000013522 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013523 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013524 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013525 }
Michael Chana71116d2005-05-29 14:58:11 -070013526 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013527 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013528
Michael Chanbbe832c2005-06-24 20:20:04 -070013529 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013530 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013531 tg3_netif_stop(tp);
13532 irq_sync = 1;
13533 }
13534
13535 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013536 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013537 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013538 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013539 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013540 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013541 if (!err)
13542 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013543
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013544 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013545 tg3_phy_reset(tp);
13546
Michael Chana71116d2005-05-29 14:58:11 -070013547 if (tg3_test_registers(tp) != 0) {
13548 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013549 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013550 }
Matt Carlson28a45952011-08-19 13:58:22 +000013551
Michael Chan7942e1d2005-05-29 14:58:36 -070013552 if (tg3_test_memory(tp) != 0) {
13553 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013554 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013555 }
Matt Carlson28a45952011-08-19 13:58:22 +000013556
Matt Carlson941ec902011-08-19 13:58:23 +000013557 if (doextlpbk)
13558 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13559
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013560 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013561 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013562
David S. Millerf47c11e2005-06-24 20:18:35 -070013563 tg3_full_unlock(tp);
13564
Michael Chand4bc3922005-05-29 14:59:20 -070013565 if (tg3_test_interrupt(tp) != 0) {
13566 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013567 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013568 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013569
13570 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013571
Michael Chana71116d2005-05-29 14:58:11 -070013572 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13573 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013574 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013575 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013576 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013577 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013578 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013579
13580 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013581
13582 if (irq_sync && !err2)
13583 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013584 }
Matt Carlson80096062010-08-02 11:26:06 +000013585 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013586 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013587
Michael Chan4cafd3f2005-05-29 14:56:34 -070013588}
13589
Matt Carlson0a633ac2012-12-03 19:36:59 +000013590static int tg3_hwtstamp_ioctl(struct net_device *dev,
13591 struct ifreq *ifr, int cmd)
13592{
13593 struct tg3 *tp = netdev_priv(dev);
13594 struct hwtstamp_config stmpconf;
13595
13596 if (!tg3_flag(tp, PTP_CAPABLE))
13597 return -EINVAL;
13598
13599 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13600 return -EFAULT;
13601
13602 if (stmpconf.flags)
13603 return -EINVAL;
13604
13605 switch (stmpconf.tx_type) {
13606 case HWTSTAMP_TX_ON:
13607 tg3_flag_set(tp, TX_TSTAMP_EN);
13608 break;
13609 case HWTSTAMP_TX_OFF:
13610 tg3_flag_clear(tp, TX_TSTAMP_EN);
13611 break;
13612 default:
13613 return -ERANGE;
13614 }
13615
13616 switch (stmpconf.rx_filter) {
13617 case HWTSTAMP_FILTER_NONE:
13618 tp->rxptpctl = 0;
13619 break;
13620 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13621 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13622 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13623 break;
13624 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13625 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13626 TG3_RX_PTP_CTL_SYNC_EVNT;
13627 break;
13628 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13629 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13630 TG3_RX_PTP_CTL_DELAY_REQ;
13631 break;
13632 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13633 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13634 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13635 break;
13636 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13637 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13638 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13639 break;
13640 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13641 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13642 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13643 break;
13644 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13645 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13646 TG3_RX_PTP_CTL_SYNC_EVNT;
13647 break;
13648 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13649 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13650 TG3_RX_PTP_CTL_SYNC_EVNT;
13651 break;
13652 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13653 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13654 TG3_RX_PTP_CTL_SYNC_EVNT;
13655 break;
13656 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13657 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13658 TG3_RX_PTP_CTL_DELAY_REQ;
13659 break;
13660 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13661 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13662 TG3_RX_PTP_CTL_DELAY_REQ;
13663 break;
13664 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13665 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13666 TG3_RX_PTP_CTL_DELAY_REQ;
13667 break;
13668 default:
13669 return -ERANGE;
13670 }
13671
13672 if (netif_running(dev) && tp->rxptpctl)
13673 tw32(TG3_RX_PTP_CTL,
13674 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13675
13676 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13677 -EFAULT : 0;
13678}
13679
Linus Torvalds1da177e2005-04-16 15:20:36 -070013680static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13681{
13682 struct mii_ioctl_data *data = if_mii(ifr);
13683 struct tg3 *tp = netdev_priv(dev);
13684 int err;
13685
Joe Perches63c3a662011-04-26 08:12:10 +000013686 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013687 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013688 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013689 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013690 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013691 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013692 }
13693
Matt Carlson33f401a2010-04-05 10:19:27 +000013694 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013695 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013696 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013697
13698 /* fallthru */
13699 case SIOCGMIIREG: {
13700 u32 mii_regval;
13701
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013702 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013703 break; /* We have no PHY */
13704
Matt Carlson34eea5a2011-04-20 07:57:38 +000013705 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013706 return -EAGAIN;
13707
David S. Millerf47c11e2005-06-24 20:18:35 -070013708 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013709 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13710 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013711 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013712
13713 data->val_out = mii_regval;
13714
13715 return err;
13716 }
13717
13718 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013719 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013720 break; /* We have no PHY */
13721
Matt Carlson34eea5a2011-04-20 07:57:38 +000013722 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013723 return -EAGAIN;
13724
David S. Millerf47c11e2005-06-24 20:18:35 -070013725 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013726 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13727 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013728 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013729
13730 return err;
13731
Matt Carlson0a633ac2012-12-03 19:36:59 +000013732 case SIOCSHWTSTAMP:
13733 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13734
Linus Torvalds1da177e2005-04-16 15:20:36 -070013735 default:
13736 /* do nothing */
13737 break;
13738 }
13739 return -EOPNOTSUPP;
13740}
13741
David S. Miller15f98502005-05-18 22:49:26 -070013742static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13743{
13744 struct tg3 *tp = netdev_priv(dev);
13745
13746 memcpy(ec, &tp->coal, sizeof(*ec));
13747 return 0;
13748}
13749
Michael Chand244c892005-07-05 14:42:33 -070013750static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13751{
13752 struct tg3 *tp = netdev_priv(dev);
13753 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13754 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13755
Joe Perches63c3a662011-04-26 08:12:10 +000013756 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013757 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13758 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13759 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13760 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13761 }
13762
13763 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13764 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13765 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13766 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13767 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13768 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13769 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13770 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13771 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13772 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13773 return -EINVAL;
13774
13775 /* No rx interrupts will be generated if both are zero */
13776 if ((ec->rx_coalesce_usecs == 0) &&
13777 (ec->rx_max_coalesced_frames == 0))
13778 return -EINVAL;
13779
13780 /* No tx interrupts will be generated if both are zero */
13781 if ((ec->tx_coalesce_usecs == 0) &&
13782 (ec->tx_max_coalesced_frames == 0))
13783 return -EINVAL;
13784
13785 /* Only copy relevant parameters, ignore all others. */
13786 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13787 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13788 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13789 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13790 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13791 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13792 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13793 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13794 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13795
13796 if (netif_running(dev)) {
13797 tg3_full_lock(tp, 0);
13798 __tg3_set_coalesce(tp, &tp->coal);
13799 tg3_full_unlock(tp);
13800 }
13801 return 0;
13802}
13803
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013804static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
13805{
13806 struct tg3 *tp = netdev_priv(dev);
13807
13808 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13809 netdev_warn(tp->dev, "Board does not support EEE!\n");
13810 return -EOPNOTSUPP;
13811 }
13812
13813 if (edata->advertised != tp->eee.advertised) {
13814 netdev_warn(tp->dev,
13815 "Direct manipulation of EEE advertisement is not supported\n");
13816 return -EINVAL;
13817 }
13818
13819 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
13820 netdev_warn(tp->dev,
13821 "Maximal Tx Lpi timer supported is %#x(u)\n",
13822 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
13823 return -EINVAL;
13824 }
13825
13826 tp->eee = *edata;
13827
13828 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
13829 tg3_warn_mgmt_link_flap(tp);
13830
13831 if (netif_running(tp->dev)) {
13832 tg3_full_lock(tp, 0);
13833 tg3_setup_eee(tp);
13834 tg3_phy_reset(tp);
13835 tg3_full_unlock(tp);
13836 }
13837
13838 return 0;
13839}
13840
13841static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
13842{
13843 struct tg3 *tp = netdev_priv(dev);
13844
13845 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13846 netdev_warn(tp->dev,
13847 "Board does not support EEE!\n");
13848 return -EOPNOTSUPP;
13849 }
13850
13851 *edata = tp->eee;
13852 return 0;
13853}
13854
Jeff Garzik7282d492006-09-13 14:30:00 -040013855static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013856 .get_settings = tg3_get_settings,
13857 .set_settings = tg3_set_settings,
13858 .get_drvinfo = tg3_get_drvinfo,
13859 .get_regs_len = tg3_get_regs_len,
13860 .get_regs = tg3_get_regs,
13861 .get_wol = tg3_get_wol,
13862 .set_wol = tg3_set_wol,
13863 .get_msglevel = tg3_get_msglevel,
13864 .set_msglevel = tg3_set_msglevel,
13865 .nway_reset = tg3_nway_reset,
13866 .get_link = ethtool_op_get_link,
13867 .get_eeprom_len = tg3_get_eeprom_len,
13868 .get_eeprom = tg3_get_eeprom,
13869 .set_eeprom = tg3_set_eeprom,
13870 .get_ringparam = tg3_get_ringparam,
13871 .set_ringparam = tg3_set_ringparam,
13872 .get_pauseparam = tg3_get_pauseparam,
13873 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013874 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013875 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013876 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013877 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013878 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013879 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013880 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013881 .get_rxnfc = tg3_get_rxnfc,
13882 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13883 .get_rxfh_indir = tg3_get_rxfh_indir,
13884 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013885 .get_channels = tg3_get_channels,
13886 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013887 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013888 .get_eee = tg3_get_eee,
13889 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013890};
13891
David S. Millerb4017c52012-03-01 17:57:40 -050013892static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13893 struct rtnl_link_stats64 *stats)
13894{
13895 struct tg3 *tp = netdev_priv(dev);
13896
David S. Millerb4017c52012-03-01 17:57:40 -050013897 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013898 if (!tp->hw_stats) {
13899 spin_unlock_bh(&tp->lock);
13900 return &tp->net_stats_prev;
13901 }
13902
David S. Millerb4017c52012-03-01 17:57:40 -050013903 tg3_get_nstats(tp, stats);
13904 spin_unlock_bh(&tp->lock);
13905
13906 return stats;
13907}
13908
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013909static void tg3_set_rx_mode(struct net_device *dev)
13910{
13911 struct tg3 *tp = netdev_priv(dev);
13912
13913 if (!netif_running(dev))
13914 return;
13915
13916 tg3_full_lock(tp, 0);
13917 __tg3_set_rx_mode(dev);
13918 tg3_full_unlock(tp);
13919}
13920
Matt Carlsonfaf16272012-02-13 10:20:07 +000013921static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13922 int new_mtu)
13923{
13924 dev->mtu = new_mtu;
13925
13926 if (new_mtu > ETH_DATA_LEN) {
13927 if (tg3_flag(tp, 5780_CLASS)) {
13928 netdev_update_features(dev);
13929 tg3_flag_clear(tp, TSO_CAPABLE);
13930 } else {
13931 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13932 }
13933 } else {
13934 if (tg3_flag(tp, 5780_CLASS)) {
13935 tg3_flag_set(tp, TSO_CAPABLE);
13936 netdev_update_features(dev);
13937 }
13938 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13939 }
13940}
13941
13942static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13943{
13944 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000013945 int err;
13946 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013947
13948 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13949 return -EINVAL;
13950
13951 if (!netif_running(dev)) {
13952 /* We'll just catch it later when the
13953 * device is up'd.
13954 */
13955 tg3_set_mtu(dev, tp, new_mtu);
13956 return 0;
13957 }
13958
13959 tg3_phy_stop(tp);
13960
13961 tg3_netif_stop(tp);
13962
13963 tg3_full_lock(tp, 1);
13964
13965 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13966
13967 tg3_set_mtu(dev, tp, new_mtu);
13968
Michael Chan2fae5e32012-03-04 14:48:15 +000013969 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13970 * breaks all requests to 256 bytes.
13971 */
Joe Perches41535772013-02-16 11:20:04 +000013972 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000013973 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000013974
13975 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013976
13977 if (!err)
13978 tg3_netif_start(tp);
13979
13980 tg3_full_unlock(tp);
13981
13982 if (!err)
13983 tg3_phy_start(tp);
13984
13985 return err;
13986}
13987
13988static const struct net_device_ops tg3_netdev_ops = {
13989 .ndo_open = tg3_open,
13990 .ndo_stop = tg3_close,
13991 .ndo_start_xmit = tg3_start_xmit,
13992 .ndo_get_stats64 = tg3_get_stats64,
13993 .ndo_validate_addr = eth_validate_addr,
13994 .ndo_set_rx_mode = tg3_set_rx_mode,
13995 .ndo_set_mac_address = tg3_set_mac_addr,
13996 .ndo_do_ioctl = tg3_ioctl,
13997 .ndo_tx_timeout = tg3_tx_timeout,
13998 .ndo_change_mtu = tg3_change_mtu,
13999 .ndo_fix_features = tg3_fix_features,
14000 .ndo_set_features = tg3_set_features,
14001#ifdef CONFIG_NET_POLL_CONTROLLER
14002 .ndo_poll_controller = tg3_poll_controller,
14003#endif
14004};
14005
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014006static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014007{
Michael Chan1b277772006-03-20 22:27:48 -080014008 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014009
14010 tp->nvram_size = EEPROM_CHIP_SIZE;
14011
Matt Carlsone4f34112009-02-25 14:25:00 +000014012 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013 return;
14014
Michael Chanb16250e2006-09-27 16:10:14 -070014015 if ((magic != TG3_EEPROM_MAGIC) &&
14016 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14017 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014018 return;
14019
14020 /*
14021 * Size the chip by reading offsets at increasing powers of two.
14022 * When we encounter our validation signature, we know the addressing
14023 * has wrapped around, and thus have our chip size.
14024 */
Michael Chan1b277772006-03-20 22:27:48 -080014025 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014026
14027 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014028 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 return;
14030
Michael Chan18201802006-03-20 22:29:15 -080014031 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014032 break;
14033
14034 cursize <<= 1;
14035 }
14036
14037 tp->nvram_size = cursize;
14038}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014039
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014040static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014041{
14042 u32 val;
14043
Joe Perches63c3a662011-04-26 08:12:10 +000014044 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014045 return;
14046
14047 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014048 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014049 tg3_get_eeprom_size(tp);
14050 return;
14051 }
14052
Matt Carlson6d348f22009-02-25 14:25:52 +000014053 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014054 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014055 /* This is confusing. We want to operate on the
14056 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14057 * call will read from NVRAM and byteswap the data
14058 * according to the byteswapping settings for all
14059 * other register accesses. This ensures the data we
14060 * want will always reside in the lower 16-bits.
14061 * However, the data in NVRAM is in LE format, which
14062 * means the data from the NVRAM read will always be
14063 * opposite the endianness of the CPU. The 16-bit
14064 * byteswap then brings the data to CPU endianness.
14065 */
14066 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014067 return;
14068 }
14069 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014070 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014071}
14072
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014073static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014074{
14075 u32 nvcfg1;
14076
14077 nvcfg1 = tr32(NVRAM_CFG1);
14078 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014079 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014080 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014081 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14082 tw32(NVRAM_CFG1, nvcfg1);
14083 }
14084
Joe Perches41535772013-02-16 11:20:04 +000014085 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014086 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014087 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014088 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14089 tp->nvram_jedecnum = JEDEC_ATMEL;
14090 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014091 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014092 break;
14093 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14094 tp->nvram_jedecnum = JEDEC_ATMEL;
14095 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14096 break;
14097 case FLASH_VENDOR_ATMEL_EEPROM:
14098 tp->nvram_jedecnum = JEDEC_ATMEL;
14099 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014100 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014101 break;
14102 case FLASH_VENDOR_ST:
14103 tp->nvram_jedecnum = JEDEC_ST;
14104 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014105 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014106 break;
14107 case FLASH_VENDOR_SAIFUN:
14108 tp->nvram_jedecnum = JEDEC_SAIFUN;
14109 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14110 break;
14111 case FLASH_VENDOR_SST_SMALL:
14112 case FLASH_VENDOR_SST_LARGE:
14113 tp->nvram_jedecnum = JEDEC_SST;
14114 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014116 }
Matt Carlson8590a602009-08-28 12:29:16 +000014117 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118 tp->nvram_jedecnum = JEDEC_ATMEL;
14119 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014120 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014121 }
14122}
14123
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014124static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014125{
14126 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14127 case FLASH_5752PAGE_SIZE_256:
14128 tp->nvram_pagesize = 256;
14129 break;
14130 case FLASH_5752PAGE_SIZE_512:
14131 tp->nvram_pagesize = 512;
14132 break;
14133 case FLASH_5752PAGE_SIZE_1K:
14134 tp->nvram_pagesize = 1024;
14135 break;
14136 case FLASH_5752PAGE_SIZE_2K:
14137 tp->nvram_pagesize = 2048;
14138 break;
14139 case FLASH_5752PAGE_SIZE_4K:
14140 tp->nvram_pagesize = 4096;
14141 break;
14142 case FLASH_5752PAGE_SIZE_264:
14143 tp->nvram_pagesize = 264;
14144 break;
14145 case FLASH_5752PAGE_SIZE_528:
14146 tp->nvram_pagesize = 528;
14147 break;
14148 }
14149}
14150
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014151static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014152{
14153 u32 nvcfg1;
14154
14155 nvcfg1 = tr32(NVRAM_CFG1);
14156
Michael Chane6af3012005-04-21 17:12:05 -070014157 /* NVRAM protection for TPM */
14158 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014159 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014160
Michael Chan361b4ac2005-04-21 17:11:21 -070014161 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014162 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14163 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14164 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014165 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014166 break;
14167 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14168 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014169 tg3_flag_set(tp, NVRAM_BUFFERED);
14170 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014171 break;
14172 case FLASH_5752VENDOR_ST_M45PE10:
14173 case FLASH_5752VENDOR_ST_M45PE20:
14174 case FLASH_5752VENDOR_ST_M45PE40:
14175 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014176 tg3_flag_set(tp, NVRAM_BUFFERED);
14177 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014178 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014179 }
14180
Joe Perches63c3a662011-04-26 08:12:10 +000014181 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014182 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014183 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014184 /* For eeprom, set pagesize to maximum eeprom size */
14185 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14186
14187 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14188 tw32(NVRAM_CFG1, nvcfg1);
14189 }
14190}
14191
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014192static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014193{
Matt Carlson989a9d22007-05-05 11:51:05 -070014194 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014195
14196 nvcfg1 = tr32(NVRAM_CFG1);
14197
14198 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014199 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014200 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014201 protect = 1;
14202 }
Michael Chand3c7b882006-03-23 01:28:25 -080014203
Matt Carlson989a9d22007-05-05 11:51:05 -070014204 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14205 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014206 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14207 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14208 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14209 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14210 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014211 tg3_flag_set(tp, NVRAM_BUFFERED);
14212 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014213 tp->nvram_pagesize = 264;
14214 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14215 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14216 tp->nvram_size = (protect ? 0x3e200 :
14217 TG3_NVRAM_SIZE_512KB);
14218 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14219 tp->nvram_size = (protect ? 0x1f200 :
14220 TG3_NVRAM_SIZE_256KB);
14221 else
14222 tp->nvram_size = (protect ? 0x1f200 :
14223 TG3_NVRAM_SIZE_128KB);
14224 break;
14225 case FLASH_5752VENDOR_ST_M45PE10:
14226 case FLASH_5752VENDOR_ST_M45PE20:
14227 case FLASH_5752VENDOR_ST_M45PE40:
14228 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014229 tg3_flag_set(tp, NVRAM_BUFFERED);
14230 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014231 tp->nvram_pagesize = 256;
14232 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14233 tp->nvram_size = (protect ?
14234 TG3_NVRAM_SIZE_64KB :
14235 TG3_NVRAM_SIZE_128KB);
14236 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14237 tp->nvram_size = (protect ?
14238 TG3_NVRAM_SIZE_64KB :
14239 TG3_NVRAM_SIZE_256KB);
14240 else
14241 tp->nvram_size = (protect ?
14242 TG3_NVRAM_SIZE_128KB :
14243 TG3_NVRAM_SIZE_512KB);
14244 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014245 }
14246}
14247
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014248static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014249{
14250 u32 nvcfg1;
14251
14252 nvcfg1 = tr32(NVRAM_CFG1);
14253
14254 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014255 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14256 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14257 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14258 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14259 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014260 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014261 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014262
Matt Carlson8590a602009-08-28 12:29:16 +000014263 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14264 tw32(NVRAM_CFG1, nvcfg1);
14265 break;
14266 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14267 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14268 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14269 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14270 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014271 tg3_flag_set(tp, NVRAM_BUFFERED);
14272 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014273 tp->nvram_pagesize = 264;
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 Carlson8590a602009-08-28 12:29:16 +000014281 tp->nvram_pagesize = 256;
14282 break;
Michael Chan1b277772006-03-20 22:27:48 -080014283 }
14284}
14285
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014286static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014287{
14288 u32 nvcfg1, protect = 0;
14289
14290 nvcfg1 = tr32(NVRAM_CFG1);
14291
14292 /* NVRAM protection for TPM */
14293 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014294 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014295 protect = 1;
14296 }
14297
14298 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14299 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014300 case FLASH_5761VENDOR_ATMEL_ADB021D:
14301 case FLASH_5761VENDOR_ATMEL_ADB041D:
14302 case FLASH_5761VENDOR_ATMEL_ADB081D:
14303 case FLASH_5761VENDOR_ATMEL_ADB161D:
14304 case FLASH_5761VENDOR_ATMEL_MDB021D:
14305 case FLASH_5761VENDOR_ATMEL_MDB041D:
14306 case FLASH_5761VENDOR_ATMEL_MDB081D:
14307 case FLASH_5761VENDOR_ATMEL_MDB161D:
14308 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014309 tg3_flag_set(tp, NVRAM_BUFFERED);
14310 tg3_flag_set(tp, FLASH);
14311 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014312 tp->nvram_pagesize = 256;
14313 break;
14314 case FLASH_5761VENDOR_ST_A_M45PE20:
14315 case FLASH_5761VENDOR_ST_A_M45PE40:
14316 case FLASH_5761VENDOR_ST_A_M45PE80:
14317 case FLASH_5761VENDOR_ST_A_M45PE16:
14318 case FLASH_5761VENDOR_ST_M_M45PE20:
14319 case FLASH_5761VENDOR_ST_M_M45PE40:
14320 case FLASH_5761VENDOR_ST_M_M45PE80:
14321 case FLASH_5761VENDOR_ST_M_M45PE16:
14322 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014323 tg3_flag_set(tp, NVRAM_BUFFERED);
14324 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014325 tp->nvram_pagesize = 256;
14326 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014327 }
14328
14329 if (protect) {
14330 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14331 } else {
14332 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014333 case FLASH_5761VENDOR_ATMEL_ADB161D:
14334 case FLASH_5761VENDOR_ATMEL_MDB161D:
14335 case FLASH_5761VENDOR_ST_A_M45PE16:
14336 case FLASH_5761VENDOR_ST_M_M45PE16:
14337 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14338 break;
14339 case FLASH_5761VENDOR_ATMEL_ADB081D:
14340 case FLASH_5761VENDOR_ATMEL_MDB081D:
14341 case FLASH_5761VENDOR_ST_A_M45PE80:
14342 case FLASH_5761VENDOR_ST_M_M45PE80:
14343 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14344 break;
14345 case FLASH_5761VENDOR_ATMEL_ADB041D:
14346 case FLASH_5761VENDOR_ATMEL_MDB041D:
14347 case FLASH_5761VENDOR_ST_A_M45PE40:
14348 case FLASH_5761VENDOR_ST_M_M45PE40:
14349 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14350 break;
14351 case FLASH_5761VENDOR_ATMEL_ADB021D:
14352 case FLASH_5761VENDOR_ATMEL_MDB021D:
14353 case FLASH_5761VENDOR_ST_A_M45PE20:
14354 case FLASH_5761VENDOR_ST_M_M45PE20:
14355 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14356 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014357 }
14358 }
14359}
14360
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014361static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014362{
14363 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014364 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014365 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14366}
14367
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014368static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014369{
14370 u32 nvcfg1;
14371
14372 nvcfg1 = tr32(NVRAM_CFG1);
14373
14374 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14375 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14376 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14377 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014378 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014379 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14380
14381 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14382 tw32(NVRAM_CFG1, nvcfg1);
14383 return;
14384 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14385 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14386 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14387 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14388 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14389 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14390 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14391 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014392 tg3_flag_set(tp, NVRAM_BUFFERED);
14393 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014394
14395 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14396 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14397 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14398 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14399 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14400 break;
14401 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14402 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14403 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14404 break;
14405 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14406 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14407 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14408 break;
14409 }
14410 break;
14411 case FLASH_5752VENDOR_ST_M45PE10:
14412 case FLASH_5752VENDOR_ST_M45PE20:
14413 case FLASH_5752VENDOR_ST_M45PE40:
14414 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014415 tg3_flag_set(tp, NVRAM_BUFFERED);
14416 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014417
14418 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14419 case FLASH_5752VENDOR_ST_M45PE10:
14420 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14421 break;
14422 case FLASH_5752VENDOR_ST_M45PE20:
14423 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14424 break;
14425 case FLASH_5752VENDOR_ST_M45PE40:
14426 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14427 break;
14428 }
14429 break;
14430 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014431 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014432 return;
14433 }
14434
Matt Carlsona1b950d2009-09-01 13:20:17 +000014435 tg3_nvram_get_pagesize(tp, nvcfg1);
14436 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014437 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014438}
14439
14440
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014441static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014442{
14443 u32 nvcfg1;
14444
14445 nvcfg1 = tr32(NVRAM_CFG1);
14446
14447 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14448 case FLASH_5717VENDOR_ATMEL_EEPROM:
14449 case FLASH_5717VENDOR_MICRO_EEPROM:
14450 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014451 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014452 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14453
14454 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14455 tw32(NVRAM_CFG1, nvcfg1);
14456 return;
14457 case FLASH_5717VENDOR_ATMEL_MDB011D:
14458 case FLASH_5717VENDOR_ATMEL_ADB011B:
14459 case FLASH_5717VENDOR_ATMEL_ADB011D:
14460 case FLASH_5717VENDOR_ATMEL_MDB021D:
14461 case FLASH_5717VENDOR_ATMEL_ADB021B:
14462 case FLASH_5717VENDOR_ATMEL_ADB021D:
14463 case FLASH_5717VENDOR_ATMEL_45USPT:
14464 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014465 tg3_flag_set(tp, NVRAM_BUFFERED);
14466 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014467
14468 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14469 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014470 /* Detect size with tg3_nvram_get_size() */
14471 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014472 case FLASH_5717VENDOR_ATMEL_ADB021B:
14473 case FLASH_5717VENDOR_ATMEL_ADB021D:
14474 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14475 break;
14476 default:
14477 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14478 break;
14479 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014480 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014481 case FLASH_5717VENDOR_ST_M_M25PE10:
14482 case FLASH_5717VENDOR_ST_A_M25PE10:
14483 case FLASH_5717VENDOR_ST_M_M45PE10:
14484 case FLASH_5717VENDOR_ST_A_M45PE10:
14485 case FLASH_5717VENDOR_ST_M_M25PE20:
14486 case FLASH_5717VENDOR_ST_A_M25PE20:
14487 case FLASH_5717VENDOR_ST_M_M45PE20:
14488 case FLASH_5717VENDOR_ST_A_M45PE20:
14489 case FLASH_5717VENDOR_ST_25USPT:
14490 case FLASH_5717VENDOR_ST_45USPT:
14491 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014492 tg3_flag_set(tp, NVRAM_BUFFERED);
14493 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014494
14495 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14496 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014497 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014498 /* Detect size with tg3_nvram_get_size() */
14499 break;
14500 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014501 case FLASH_5717VENDOR_ST_A_M45PE20:
14502 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14503 break;
14504 default:
14505 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14506 break;
14507 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014508 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014509 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014510 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014511 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014512 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014513
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);
Matt Carlson321d32a2008-11-21 17:22:19 -080014517}
14518
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014519static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014520{
14521 u32 nvcfg1, nvmpinstrp;
14522
14523 nvcfg1 = tr32(NVRAM_CFG1);
14524 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14525
Joe Perches41535772013-02-16 11:20:04 +000014526 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014527 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14528 tg3_flag_set(tp, NO_NVRAM);
14529 return;
14530 }
14531
14532 switch (nvmpinstrp) {
14533 case FLASH_5762_EEPROM_HD:
14534 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014535 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014536 case FLASH_5762_EEPROM_LD:
14537 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014538 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014539 case FLASH_5720VENDOR_M_ST_M45PE20:
14540 /* This pinstrap supports multiple sizes, so force it
14541 * to read the actual size from location 0xf0.
14542 */
14543 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14544 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014545 }
14546 }
14547
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014548 switch (nvmpinstrp) {
14549 case FLASH_5720_EEPROM_HD:
14550 case FLASH_5720_EEPROM_LD:
14551 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014552 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014553
14554 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14555 tw32(NVRAM_CFG1, nvcfg1);
14556 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14557 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14558 else
14559 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14560 return;
14561 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14562 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14563 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14564 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14565 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14566 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14567 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14568 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14569 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14570 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14571 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14572 case FLASH_5720VENDOR_ATMEL_45USPT:
14573 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014574 tg3_flag_set(tp, NVRAM_BUFFERED);
14575 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014576
14577 switch (nvmpinstrp) {
14578 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14579 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14580 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14581 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14582 break;
14583 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14584 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14585 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14586 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14587 break;
14588 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14589 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14590 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14591 break;
14592 default:
Joe Perches41535772013-02-16 11:20:04 +000014593 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014594 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014595 break;
14596 }
14597 break;
14598 case FLASH_5720VENDOR_M_ST_M25PE10:
14599 case FLASH_5720VENDOR_M_ST_M45PE10:
14600 case FLASH_5720VENDOR_A_ST_M25PE10:
14601 case FLASH_5720VENDOR_A_ST_M45PE10:
14602 case FLASH_5720VENDOR_M_ST_M25PE20:
14603 case FLASH_5720VENDOR_M_ST_M45PE20:
14604 case FLASH_5720VENDOR_A_ST_M25PE20:
14605 case FLASH_5720VENDOR_A_ST_M45PE20:
14606 case FLASH_5720VENDOR_M_ST_M25PE40:
14607 case FLASH_5720VENDOR_M_ST_M45PE40:
14608 case FLASH_5720VENDOR_A_ST_M25PE40:
14609 case FLASH_5720VENDOR_A_ST_M45PE40:
14610 case FLASH_5720VENDOR_M_ST_M25PE80:
14611 case FLASH_5720VENDOR_M_ST_M45PE80:
14612 case FLASH_5720VENDOR_A_ST_M25PE80:
14613 case FLASH_5720VENDOR_A_ST_M45PE80:
14614 case FLASH_5720VENDOR_ST_25USPT:
14615 case FLASH_5720VENDOR_ST_45USPT:
14616 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014617 tg3_flag_set(tp, NVRAM_BUFFERED);
14618 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014619
14620 switch (nvmpinstrp) {
14621 case FLASH_5720VENDOR_M_ST_M25PE20:
14622 case FLASH_5720VENDOR_M_ST_M45PE20:
14623 case FLASH_5720VENDOR_A_ST_M25PE20:
14624 case FLASH_5720VENDOR_A_ST_M45PE20:
14625 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14626 break;
14627 case FLASH_5720VENDOR_M_ST_M25PE40:
14628 case FLASH_5720VENDOR_M_ST_M45PE40:
14629 case FLASH_5720VENDOR_A_ST_M25PE40:
14630 case FLASH_5720VENDOR_A_ST_M45PE40:
14631 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14632 break;
14633 case FLASH_5720VENDOR_M_ST_M25PE80:
14634 case FLASH_5720VENDOR_M_ST_M45PE80:
14635 case FLASH_5720VENDOR_A_ST_M25PE80:
14636 case FLASH_5720VENDOR_A_ST_M45PE80:
14637 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14638 break;
14639 default:
Joe Perches41535772013-02-16 11:20:04 +000014640 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014641 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014642 break;
14643 }
14644 break;
14645 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014646 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014647 return;
14648 }
14649
14650 tg3_nvram_get_pagesize(tp, nvcfg1);
14651 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014652 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014653
Joe Perches41535772013-02-16 11:20:04 +000014654 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014655 u32 val;
14656
14657 if (tg3_nvram_read(tp, 0, &val))
14658 return;
14659
14660 if (val != TG3_EEPROM_MAGIC &&
14661 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14662 tg3_flag_set(tp, NO_NVRAM);
14663 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014664}
14665
Linus Torvalds1da177e2005-04-16 15:20:36 -070014666/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014667static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014669 if (tg3_flag(tp, IS_SSB_CORE)) {
14670 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14671 tg3_flag_clear(tp, NVRAM);
14672 tg3_flag_clear(tp, NVRAM_BUFFERED);
14673 tg3_flag_set(tp, NO_NVRAM);
14674 return;
14675 }
14676
Linus Torvalds1da177e2005-04-16 15:20:36 -070014677 tw32_f(GRC_EEPROM_ADDR,
14678 (EEPROM_ADDR_FSM_RESET |
14679 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14680 EEPROM_ADDR_CLKPERD_SHIFT)));
14681
Michael Chan9d57f012006-12-07 00:23:25 -080014682 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014683
14684 /* Enable seeprom accesses. */
14685 tw32_f(GRC_LOCAL_CTRL,
14686 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14687 udelay(100);
14688
Joe Perches41535772013-02-16 11:20:04 +000014689 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14690 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014691 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014692
Michael Chanec41c7d2006-01-17 02:40:55 -080014693 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014694 netdev_warn(tp->dev,
14695 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014696 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014697 return;
14698 }
Michael Chane6af3012005-04-21 17:12:05 -070014699 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014700
Matt Carlson989a9d22007-05-05 11:51:05 -070014701 tp->nvram_size = 0;
14702
Joe Perches41535772013-02-16 11:20:04 +000014703 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014704 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014705 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014706 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014707 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14708 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14709 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014710 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014711 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014712 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014713 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014714 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014715 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014716 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014717 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014718 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14719 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014720 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014721 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14722 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014723 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014724 else
14725 tg3_get_nvram_info(tp);
14726
Matt Carlson989a9d22007-05-05 11:51:05 -070014727 if (tp->nvram_size == 0)
14728 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014729
Michael Chane6af3012005-04-21 17:12:05 -070014730 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014731 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014732
14733 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014734 tg3_flag_clear(tp, NVRAM);
14735 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014736
14737 tg3_get_eeprom_size(tp);
14738 }
14739}
14740
Linus Torvalds1da177e2005-04-16 15:20:36 -070014741struct subsys_tbl_ent {
14742 u16 subsys_vendor, subsys_devid;
14743 u32 phy_id;
14744};
14745
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014746static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014747 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014748 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014749 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014750 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014751 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014752 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014753 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014754 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14755 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14756 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014757 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014758 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014759 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014760 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14761 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14762 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014763 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014764 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014765 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014766 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014767 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014768 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014769 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014770
14771 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014772 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014773 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014774 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014775 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014776 { TG3PCI_SUBVENDOR_ID_3COM,
14777 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14778 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014779 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014780 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014781 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014782
14783 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014784 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014785 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014786 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014787 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014788 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014789 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014790 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014791 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014792
14793 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014794 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014795 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014796 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014797 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014798 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14799 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14800 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014801 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014802 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014803 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014804
14805 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014806 { TG3PCI_SUBVENDOR_ID_IBM,
14807 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014808};
14809
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014810static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014811{
14812 int i;
14813
14814 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14815 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14816 tp->pdev->subsystem_vendor) &&
14817 (subsys_id_to_phy_id[i].subsys_devid ==
14818 tp->pdev->subsystem_device))
14819 return &subsys_id_to_phy_id[i];
14820 }
14821 return NULL;
14822}
14823
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014824static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014825{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014826 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014827
Matt Carlson79eb6902010-02-17 15:17:03 +000014828 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014829 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14830
Gary Zambranoa85feb82007-05-05 11:52:19 -070014831 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014832 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14833 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014834
Joe Perches41535772013-02-16 11:20:04 +000014835 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014836 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014837 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14838 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014839 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014840 val = tr32(VCPU_CFGSHDW);
14841 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014842 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014843 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014844 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014845 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014846 device_set_wakeup_enable(&tp->pdev->dev, true);
14847 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014848 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014849 }
14850
Linus Torvalds1da177e2005-04-16 15:20:36 -070014851 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14852 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14853 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014854 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014855 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014856
14857 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14858 tp->nic_sram_data_cfg = nic_cfg;
14859
14860 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14861 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014862 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14863 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14864 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014865 (ver > 0) && (ver < 0x100))
14866 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14867
Joe Perches41535772013-02-16 11:20:04 +000014868 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014869 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14870
Linus Torvalds1da177e2005-04-16 15:20:36 -070014871 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14872 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14873 eeprom_phy_serdes = 1;
14874
14875 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14876 if (nic_phy_id != 0) {
14877 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14878 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14879
14880 eeprom_phy_id = (id1 >> 16) << 10;
14881 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14882 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14883 } else
14884 eeprom_phy_id = 0;
14885
Michael Chan7d0c41e2005-04-21 17:06:20 -070014886 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014887 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014888 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014889 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014890 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014891 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014892 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014893
Joe Perches63c3a662011-04-26 08:12:10 +000014894 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014895 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14896 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014897 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014898 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14899
14900 switch (led_cfg) {
14901 default:
14902 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14903 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14904 break;
14905
14906 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14907 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14908 break;
14909
14910 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14911 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014912
14913 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14914 * read on some older 5700/5701 bootcode.
14915 */
Joe Perches41535772013-02-16 11:20:04 +000014916 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14917 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014918 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14919
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920 break;
14921
14922 case SHASTA_EXT_LED_SHARED:
14923 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014924 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14925 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14927 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070014928
14929 if (tg3_flag(tp, 5717_PLUS) ||
14930 tg3_asic_rev(tp) == ASIC_REV_5762)
14931 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
14932 LED_CTRL_BLINK_RATE_MASK;
14933
Linus Torvalds1da177e2005-04-16 15:20:36 -070014934 break;
14935
14936 case SHASTA_EXT_LED_MAC:
14937 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14938 break;
14939
14940 case SHASTA_EXT_LED_COMBO:
14941 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014942 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014943 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14944 LED_CTRL_MODE_PHY_2);
14945 break;
14946
Stephen Hemminger855e1112008-04-16 16:37:28 -070014947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014948
Joe Perches41535772013-02-16 11:20:04 +000014949 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14950 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014951 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14952 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14953
Joe Perches41535772013-02-16 11:20:04 +000014954 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014955 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014956
Michael Chan9d26e212006-12-07 00:21:14 -080014957 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014958 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014959 if ((tp->pdev->subsystem_vendor ==
14960 PCI_VENDOR_ID_ARIMA) &&
14961 (tp->pdev->subsystem_device == 0x205a ||
14962 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014963 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014964 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014965 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14966 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014968
14969 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014970 tg3_flag_set(tp, ENABLE_ASF);
14971 if (tg3_flag(tp, 5750_PLUS))
14972 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014973 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014974
14975 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014976 tg3_flag(tp, 5750_PLUS))
14977 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014978
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014979 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014980 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014981 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014982
Joe Perches63c3a662011-04-26 08:12:10 +000014983 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014984 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014985 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014986 device_set_wakeup_enable(&tp->pdev->dev, true);
14987 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014988
Linus Torvalds1da177e2005-04-16 15:20:36 -070014989 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014990 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014991
14992 /* serdes signal pre-emphasis in register 0x590 set by */
14993 /* bootcode if bit 18 is set */
14994 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014995 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014996
Joe Perches63c3a662011-04-26 08:12:10 +000014997 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014998 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14999 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015000 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015001 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015002
Nithin Sujir942d1af2013-04-09 08:48:07 +000015003 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015004 u32 cfg3;
15005
15006 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015007 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15008 !tg3_flag(tp, 57765_PLUS) &&
15009 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015010 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015011 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15012 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15013 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15014 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015015 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015016
Matt Carlson14417062010-02-17 15:16:59 +000015017 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015018 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015019 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015020 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015021 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015022 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015023 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015024done:
Joe Perches63c3a662011-04-26 08:12:10 +000015025 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015026 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015027 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015028 else
15029 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015030}
15031
Michael Chanc86a8562013-01-06 12:51:08 +000015032static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15033{
15034 int i, err;
15035 u32 val2, off = offset * 8;
15036
15037 err = tg3_nvram_lock(tp);
15038 if (err)
15039 return err;
15040
15041 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15042 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15043 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15044 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15045 udelay(10);
15046
15047 for (i = 0; i < 100; i++) {
15048 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15049 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15050 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15051 break;
15052 }
15053 udelay(10);
15054 }
15055
15056 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15057
15058 tg3_nvram_unlock(tp);
15059 if (val2 & APE_OTP_STATUS_CMD_DONE)
15060 return 0;
15061
15062 return -EBUSY;
15063}
15064
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015065static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015066{
15067 int i;
15068 u32 val;
15069
15070 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15071 tw32(OTP_CTRL, cmd);
15072
15073 /* Wait for up to 1 ms for command to execute. */
15074 for (i = 0; i < 100; i++) {
15075 val = tr32(OTP_STATUS);
15076 if (val & OTP_STATUS_CMD_DONE)
15077 break;
15078 udelay(10);
15079 }
15080
15081 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15082}
15083
15084/* Read the gphy configuration from the OTP region of the chip. The gphy
15085 * configuration is a 32-bit value that straddles the alignment boundary.
15086 * We do two 32-bit reads and then shift and merge the results.
15087 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015088static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015089{
15090 u32 bhalf_otp, thalf_otp;
15091
15092 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15093
15094 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15095 return 0;
15096
15097 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15098
15099 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15100 return 0;
15101
15102 thalf_otp = tr32(OTP_READ_DATA);
15103
15104 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15105
15106 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15107 return 0;
15108
15109 bhalf_otp = tr32(OTP_READ_DATA);
15110
15111 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15112}
15113
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015114static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015115{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015116 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015117
15118 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15119 adv |= ADVERTISED_1000baseT_Half |
15120 ADVERTISED_1000baseT_Full;
15121
15122 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15123 adv |= ADVERTISED_100baseT_Half |
15124 ADVERTISED_100baseT_Full |
15125 ADVERTISED_10baseT_Half |
15126 ADVERTISED_10baseT_Full |
15127 ADVERTISED_TP;
15128 else
15129 adv |= ADVERTISED_FIBRE;
15130
15131 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015132 tp->link_config.speed = SPEED_UNKNOWN;
15133 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015134 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015135 tp->link_config.active_speed = SPEED_UNKNOWN;
15136 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015137
15138 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015139}
15140
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015141static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015142{
15143 u32 hw_phy_id_1, hw_phy_id_2;
15144 u32 hw_phy_id, hw_phy_id_masked;
15145 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015146
Matt Carlsone256f8a2011-03-09 16:58:24 +000015147 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015148 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015149 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15150
Michael Chan8151ad52012-07-29 19:15:41 +000015151 if (tg3_flag(tp, ENABLE_APE)) {
15152 switch (tp->pci_fn) {
15153 case 0:
15154 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15155 break;
15156 case 1:
15157 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15158 break;
15159 case 2:
15160 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15161 break;
15162 case 3:
15163 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15164 break;
15165 }
15166 }
15167
Nithin Sujir942d1af2013-04-09 08:48:07 +000015168 if (!tg3_flag(tp, ENABLE_ASF) &&
15169 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15170 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15171 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15172 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15173
Joe Perches63c3a662011-04-26 08:12:10 +000015174 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015175 return tg3_phy_init(tp);
15176
Linus Torvalds1da177e2005-04-16 15:20:36 -070015177 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015178 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015179 */
15180 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015181 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015182 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015183 } else {
15184 /* Now read the physical PHY_ID from the chip and verify
15185 * that it is sane. If it doesn't look good, we fall back
15186 * to either the hard-coded table based PHY_ID and failing
15187 * that the value found in the eeprom area.
15188 */
15189 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15190 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15191
15192 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15193 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15194 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15195
Matt Carlson79eb6902010-02-17 15:17:03 +000015196 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015197 }
15198
Matt Carlson79eb6902010-02-17 15:17:03 +000015199 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015200 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015201 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015202 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015203 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015204 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015205 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015206 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015207 /* Do nothing, phy ID already set up in
15208 * tg3_get_eeprom_hw_cfg().
15209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015210 } else {
15211 struct subsys_tbl_ent *p;
15212
15213 /* No eeprom signature? Try the hardcoded
15214 * subsys device table.
15215 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015216 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015217 if (p) {
15218 tp->phy_id = p->phy_id;
15219 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15220 /* For now we saw the IDs 0xbc050cd0,
15221 * 0xbc050f80 and 0xbc050c30 on devices
15222 * connected to an BCM4785 and there are
15223 * probably more. Just assume that the phy is
15224 * supported when it is connected to a SSB core
15225 * for now.
15226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015227 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015229
Linus Torvalds1da177e2005-04-16 15:20:36 -070015230 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015231 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015232 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015233 }
15234 }
15235
Matt Carlsona6b68da2010-12-06 08:28:52 +000015236 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015237 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15238 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015239 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015240 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15241 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15242 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15243 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015244 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015245 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15246
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015247 tp->eee.supported = SUPPORTED_100baseT_Full |
15248 SUPPORTED_1000baseT_Full;
15249 tp->eee.advertised = ADVERTISED_100baseT_Full |
15250 ADVERTISED_1000baseT_Full;
15251 tp->eee.eee_enabled = 1;
15252 tp->eee.tx_lpi_enabled = 1;
15253 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15254 }
15255
Matt Carlsone256f8a2011-03-09 16:58:24 +000015256 tg3_phy_init_link_config(tp);
15257
Nithin Sujir942d1af2013-04-09 08:48:07 +000015258 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15259 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015260 !tg3_flag(tp, ENABLE_APE) &&
15261 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015262 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015263
15264 tg3_readphy(tp, MII_BMSR, &bmsr);
15265 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15266 (bmsr & BMSR_LSTATUS))
15267 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015268
Linus Torvalds1da177e2005-04-16 15:20:36 -070015269 err = tg3_phy_reset(tp);
15270 if (err)
15271 return err;
15272
Matt Carlson42b64a42011-05-19 12:12:49 +000015273 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015274
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015275 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015276 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15277 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015278
15279 tg3_writephy(tp, MII_BMCR,
15280 BMCR_ANENABLE | BMCR_ANRESTART);
15281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015282 }
15283
15284skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015285 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286 err = tg3_init_5401phy_dsp(tp);
15287 if (err)
15288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015289
Linus Torvalds1da177e2005-04-16 15:20:36 -070015290 err = tg3_init_5401phy_dsp(tp);
15291 }
15292
Linus Torvalds1da177e2005-04-16 15:20:36 -070015293 return err;
15294}
15295
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015296static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015297{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015298 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015299 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015300 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015301 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015302
Matt Carlson535a4902011-07-20 10:20:56 +000015303 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015304 if (!vpd_data)
15305 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306
Matt Carlson535a4902011-07-20 10:20:56 +000015307 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015308 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015309 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015310
15311 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15312 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15313 i += PCI_VPD_LRDT_TAG_SIZE;
15314
Matt Carlson535a4902011-07-20 10:20:56 +000015315 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015316 goto out_not_found;
15317
Matt Carlson184b8902010-04-05 10:19:25 +000015318 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15319 PCI_VPD_RO_KEYWORD_MFR_ID);
15320 if (j > 0) {
15321 len = pci_vpd_info_field_size(&vpd_data[j]);
15322
15323 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15324 if (j + len > block_end || len != 4 ||
15325 memcmp(&vpd_data[j], "1028", 4))
15326 goto partno;
15327
15328 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15329 PCI_VPD_RO_KEYWORD_VENDOR0);
15330 if (j < 0)
15331 goto partno;
15332
15333 len = pci_vpd_info_field_size(&vpd_data[j]);
15334
15335 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15336 if (j + len > block_end)
15337 goto partno;
15338
Kees Cook715230a2013-03-27 06:40:50 +000015339 if (len >= sizeof(tp->fw_ver))
15340 len = sizeof(tp->fw_ver) - 1;
15341 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15342 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15343 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015344 }
15345
15346partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015347 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15348 PCI_VPD_RO_KEYWORD_PARTNO);
15349 if (i < 0)
15350 goto out_not_found;
15351
15352 len = pci_vpd_info_field_size(&vpd_data[i]);
15353
15354 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15355 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015356 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015357 goto out_not_found;
15358
15359 memcpy(tp->board_part_number, &vpd_data[i], len);
15360
Linus Torvalds1da177e2005-04-16 15:20:36 -070015361out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015362 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015363 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015364 return;
15365
15366out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015367 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015368 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15369 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015370 strcpy(tp->board_part_number, "BCM5717");
15371 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15372 strcpy(tp->board_part_number, "BCM5718");
15373 else
15374 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015375 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015376 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15377 strcpy(tp->board_part_number, "BCM57780");
15378 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15379 strcpy(tp->board_part_number, "BCM57760");
15380 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15381 strcpy(tp->board_part_number, "BCM57790");
15382 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15383 strcpy(tp->board_part_number, "BCM57788");
15384 else
15385 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015386 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015387 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15388 strcpy(tp->board_part_number, "BCM57761");
15389 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15390 strcpy(tp->board_part_number, "BCM57765");
15391 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15392 strcpy(tp->board_part_number, "BCM57781");
15393 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15394 strcpy(tp->board_part_number, "BCM57785");
15395 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15396 strcpy(tp->board_part_number, "BCM57791");
15397 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15398 strcpy(tp->board_part_number, "BCM57795");
15399 else
15400 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015401 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015402 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15403 strcpy(tp->board_part_number, "BCM57762");
15404 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15405 strcpy(tp->board_part_number, "BCM57766");
15406 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15407 strcpy(tp->board_part_number, "BCM57782");
15408 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15409 strcpy(tp->board_part_number, "BCM57786");
15410 else
15411 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015412 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015413 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015414 } else {
15415nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015416 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015418}
15419
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015420static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015421{
15422 u32 val;
15423
Matt Carlsone4f34112009-02-25 14:25:00 +000015424 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015425 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015426 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015427 val != 0)
15428 return 0;
15429
15430 return 1;
15431}
15432
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015433static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015434{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015435 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015436 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015437 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015438
15439 if (tg3_nvram_read(tp, 0xc, &offset) ||
15440 tg3_nvram_read(tp, 0x4, &start))
15441 return;
15442
15443 offset = tg3_nvram_logical_addr(tp, offset);
15444
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015445 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015446 return;
15447
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015448 if ((val & 0xfc000000) == 0x0c000000) {
15449 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015450 return;
15451
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015452 if (val == 0)
15453 newver = true;
15454 }
15455
Matt Carlson75f99362010-04-05 10:19:24 +000015456 dst_off = strlen(tp->fw_ver);
15457
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015458 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015459 if (TG3_VER_SIZE - dst_off < 16 ||
15460 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015461 return;
15462
15463 offset = offset + ver_offset - start;
15464 for (i = 0; i < 16; i += 4) {
15465 __be32 v;
15466 if (tg3_nvram_read_be32(tp, offset + i, &v))
15467 return;
15468
Matt Carlson75f99362010-04-05 10:19:24 +000015469 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015470 }
15471 } else {
15472 u32 major, minor;
15473
15474 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15475 return;
15476
15477 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15478 TG3_NVM_BCVER_MAJSFT;
15479 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015480 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15481 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015482 }
15483}
15484
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015485static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015486{
15487 u32 val, major, minor;
15488
15489 /* Use native endian representation */
15490 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15491 return;
15492
15493 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15494 TG3_NVM_HWSB_CFG1_MAJSFT;
15495 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15496 TG3_NVM_HWSB_CFG1_MINSFT;
15497
15498 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15499}
15500
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015501static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015502{
15503 u32 offset, major, minor, build;
15504
Matt Carlson75f99362010-04-05 10:19:24 +000015505 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015506
15507 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15508 return;
15509
15510 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15511 case TG3_EEPROM_SB_REVISION_0:
15512 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15513 break;
15514 case TG3_EEPROM_SB_REVISION_2:
15515 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15516 break;
15517 case TG3_EEPROM_SB_REVISION_3:
15518 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15519 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015520 case TG3_EEPROM_SB_REVISION_4:
15521 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15522 break;
15523 case TG3_EEPROM_SB_REVISION_5:
15524 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15525 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015526 case TG3_EEPROM_SB_REVISION_6:
15527 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15528 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015529 default:
15530 return;
15531 }
15532
Matt Carlsone4f34112009-02-25 14:25:00 +000015533 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015534 return;
15535
15536 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15537 TG3_EEPROM_SB_EDH_BLD_SHFT;
15538 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15539 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15540 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15541
15542 if (minor > 99 || build > 26)
15543 return;
15544
Matt Carlson75f99362010-04-05 10:19:24 +000015545 offset = strlen(tp->fw_ver);
15546 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15547 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015548
15549 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015550 offset = strlen(tp->fw_ver);
15551 if (offset < TG3_VER_SIZE - 1)
15552 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015553 }
15554}
15555
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015556static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015557{
15558 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015559 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015560
15561 for (offset = TG3_NVM_DIR_START;
15562 offset < TG3_NVM_DIR_END;
15563 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015564 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015565 return;
15566
15567 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15568 break;
15569 }
15570
15571 if (offset == TG3_NVM_DIR_END)
15572 return;
15573
Joe Perches63c3a662011-04-26 08:12:10 +000015574 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015575 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015576 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015577 return;
15578
Matt Carlsone4f34112009-02-25 14:25:00 +000015579 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015580 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015581 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015582 return;
15583
15584 offset += val - start;
15585
Matt Carlsonacd9c112009-02-25 14:26:33 +000015586 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015587
Matt Carlsonacd9c112009-02-25 14:26:33 +000015588 tp->fw_ver[vlen++] = ',';
15589 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015590
15591 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015592 __be32 v;
15593 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015594 return;
15595
Al Virob9fc7dc2007-12-17 22:59:57 -080015596 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015597
Matt Carlsonacd9c112009-02-25 14:26:33 +000015598 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15599 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015600 break;
15601 }
15602
Matt Carlsonacd9c112009-02-25 14:26:33 +000015603 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15604 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015605 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015606}
15607
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015608static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015609{
Matt Carlson7fd76442009-02-25 14:27:20 +000015610 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015611
15612 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15613 if (apedata != APE_SEG_SIG_MAGIC)
15614 return;
15615
15616 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15617 if (!(apedata & APE_FW_STATUS_READY))
15618 return;
15619
Michael Chan165f4d12012-07-16 16:23:59 +000015620 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15621 tg3_flag_set(tp, APE_HAS_NCSI);
15622}
15623
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015624static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015625{
15626 int vlen;
15627 u32 apedata;
15628 char *fwtype;
15629
Matt Carlson7fd76442009-02-25 14:27:20 +000015630 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15631
Michael Chan165f4d12012-07-16 16:23:59 +000015632 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015633 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015634 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15635 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015636 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015637 fwtype = "DASH";
15638
Matt Carlson7fd76442009-02-25 14:27:20 +000015639 vlen = strlen(tp->fw_ver);
15640
Matt Carlsonecc79642010-08-02 11:26:01 +000015641 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15642 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015643 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15644 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15645 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15646 (apedata & APE_FW_VERSION_BLDMSK));
15647}
15648
Michael Chanc86a8562013-01-06 12:51:08 +000015649static void tg3_read_otp_ver(struct tg3 *tp)
15650{
15651 u32 val, val2;
15652
Joe Perches41535772013-02-16 11:20:04 +000015653 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015654 return;
15655
15656 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15657 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15658 TG3_OTP_MAGIC0_VALID(val)) {
15659 u64 val64 = (u64) val << 32 | val2;
15660 u32 ver = 0;
15661 int i, vlen;
15662
15663 for (i = 0; i < 7; i++) {
15664 if ((val64 & 0xff) == 0)
15665 break;
15666 ver = val64 & 0xff;
15667 val64 >>= 8;
15668 }
15669 vlen = strlen(tp->fw_ver);
15670 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15671 }
15672}
15673
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015674static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015675{
15676 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015677 bool vpd_vers = false;
15678
15679 if (tp->fw_ver[0] != 0)
15680 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015681
Joe Perches63c3a662011-04-26 08:12:10 +000015682 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015683 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015684 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015685 return;
15686 }
15687
Matt Carlsonacd9c112009-02-25 14:26:33 +000015688 if (tg3_nvram_read(tp, 0, &val))
15689 return;
15690
15691 if (val == TG3_EEPROM_MAGIC)
15692 tg3_read_bc_ver(tp);
15693 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15694 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015695 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15696 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015697
Michael Chan165f4d12012-07-16 16:23:59 +000015698 if (tg3_flag(tp, ENABLE_ASF)) {
15699 if (tg3_flag(tp, ENABLE_APE)) {
15700 tg3_probe_ncsi(tp);
15701 if (!vpd_vers)
15702 tg3_read_dash_ver(tp);
15703 } else if (!vpd_vers) {
15704 tg3_read_mgmtfw_ver(tp);
15705 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015706 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015707
15708 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015709}
15710
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015711static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15712{
Joe Perches63c3a662011-04-26 08:12:10 +000015713 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015714 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015715 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015716 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015717 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015718 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015719}
15720
Matt Carlson41434702011-03-09 16:58:22 +000015721static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015722 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15723 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15724 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15725 { },
15726};
15727
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015728static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015729{
15730 struct pci_dev *peer;
15731 unsigned int func, devnr = tp->pdev->devfn & ~7;
15732
15733 for (func = 0; func < 8; func++) {
15734 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15735 if (peer && peer != tp->pdev)
15736 break;
15737 pci_dev_put(peer);
15738 }
15739 /* 5704 can be configured in single-port mode, set peer to
15740 * tp->pdev in that case.
15741 */
15742 if (!peer) {
15743 peer = tp->pdev;
15744 return peer;
15745 }
15746
15747 /*
15748 * We don't need to keep the refcount elevated; there's no way
15749 * to remove one half of this device without removing the other
15750 */
15751 pci_dev_put(peer);
15752
15753 return peer;
15754}
15755
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015756static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015757{
15758 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015759 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015760 u32 reg;
15761
15762 /* All devices that use the alternate
15763 * ASIC REV location have a CPMU.
15764 */
15765 tg3_flag_set(tp, CPMU_PRESENT);
15766
15767 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015768 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015769 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15770 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015771 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070015772 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
15773 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015774 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15775 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070015776 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
15777 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000015778 reg = TG3PCI_GEN2_PRODID_ASICREV;
15779 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15780 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15781 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15782 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15783 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15784 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15785 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15786 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15787 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15788 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15789 reg = TG3PCI_GEN15_PRODID_ASICREV;
15790 else
15791 reg = TG3PCI_PRODID_ASICREV;
15792
15793 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15794 }
15795
15796 /* Wrong chip ID in 5752 A0. This code can be removed later
15797 * as A0 is not in production.
15798 */
Joe Perches41535772013-02-16 11:20:04 +000015799 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015800 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15801
Joe Perches41535772013-02-16 11:20:04 +000015802 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015803 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15804
Joe Perches41535772013-02-16 11:20:04 +000015805 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15806 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15807 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015808 tg3_flag_set(tp, 5717_PLUS);
15809
Joe Perches41535772013-02-16 11:20:04 +000015810 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15811 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015812 tg3_flag_set(tp, 57765_CLASS);
15813
Michael Chanc65a17f2013-01-06 12:51:07 +000015814 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015815 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015816 tg3_flag_set(tp, 57765_PLUS);
15817
15818 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015819 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15820 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15821 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15822 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15823 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15824 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015825 tg3_flag(tp, 57765_PLUS))
15826 tg3_flag_set(tp, 5755_PLUS);
15827
Joe Perches41535772013-02-16 11:20:04 +000015828 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15829 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015830 tg3_flag_set(tp, 5780_CLASS);
15831
Joe Perches41535772013-02-16 11:20:04 +000015832 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15833 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15834 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015835 tg3_flag(tp, 5755_PLUS) ||
15836 tg3_flag(tp, 5780_CLASS))
15837 tg3_flag_set(tp, 5750_PLUS);
15838
Joe Perches41535772013-02-16 11:20:04 +000015839 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015840 tg3_flag(tp, 5750_PLUS))
15841 tg3_flag_set(tp, 5705_PLUS);
15842}
15843
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015844static bool tg3_10_100_only_device(struct tg3 *tp,
15845 const struct pci_device_id *ent)
15846{
15847 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15848
Joe Perches41535772013-02-16 11:20:04 +000015849 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15850 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015851 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15852 return true;
15853
15854 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015855 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015856 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15857 return true;
15858 } else {
15859 return true;
15860 }
15861 }
15862
15863 return false;
15864}
15865
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015866static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015867{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015868 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015869 u32 pci_state_reg, grc_misc_cfg;
15870 u32 val;
15871 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015872 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015873
Linus Torvalds1da177e2005-04-16 15:20:36 -070015874 /* Force memory write invalidate off. If we leave it on,
15875 * then on 5700_BX chips we have to enable a workaround.
15876 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15877 * to match the cacheline size. The Broadcom driver have this
15878 * workaround but turns MWI off all the times so never uses
15879 * it. This seems to suggest that the workaround is insufficient.
15880 */
15881 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15882 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15883 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15884
Matt Carlson16821282011-07-13 09:27:28 +000015885 /* Important! -- Make sure register accesses are byteswapped
15886 * correctly. Also, for those chips that require it, make
15887 * sure that indirect register accesses are enabled before
15888 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015889 */
15890 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15891 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015892 tp->misc_host_ctrl |= (misc_ctrl_reg &
15893 MISC_HOST_CTRL_CHIPREV);
15894 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15895 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015896
Matt Carlson42b123b2012-02-13 15:20:13 +000015897 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015898
Michael Chan68929142005-08-09 20:17:14 -070015899 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15900 * we need to disable memory and use config. cycles
15901 * only to access all registers. The 5702/03 chips
15902 * can mistakenly decode the special cycles from the
15903 * ICH chipsets as memory write cycles, causing corruption
15904 * of register and memory space. Only certain ICH bridges
15905 * will drive special cycles with non-zero data during the
15906 * address phase which can fall within the 5703's address
15907 * range. This is not an ICH bug as the PCI spec allows
15908 * non-zero address during special cycles. However, only
15909 * these ICH bridges are known to drive non-zero addresses
15910 * during special cycles.
15911 *
15912 * Since special cycles do not cross PCI bridges, we only
15913 * enable this workaround if the 5703 is on the secondary
15914 * bus of these ICH bridges.
15915 */
Joe Perches41535772013-02-16 11:20:04 +000015916 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15917 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015918 static struct tg3_dev_id {
15919 u32 vendor;
15920 u32 device;
15921 u32 rev;
15922 } ich_chipsets[] = {
15923 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15924 PCI_ANY_ID },
15925 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15926 PCI_ANY_ID },
15927 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15928 0xa },
15929 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15930 PCI_ANY_ID },
15931 { },
15932 };
15933 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15934 struct pci_dev *bridge = NULL;
15935
15936 while (pci_id->vendor != 0) {
15937 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15938 bridge);
15939 if (!bridge) {
15940 pci_id++;
15941 continue;
15942 }
15943 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015944 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015945 continue;
15946 }
15947 if (bridge->subordinate &&
15948 (bridge->subordinate->number ==
15949 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015950 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015951 pci_dev_put(bridge);
15952 break;
15953 }
15954 }
15955 }
15956
Joe Perches41535772013-02-16 11:20:04 +000015957 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba12008-04-19 18:12:33 -070015958 static struct tg3_dev_id {
15959 u32 vendor;
15960 u32 device;
15961 } bridge_chipsets[] = {
15962 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15963 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15964 { },
15965 };
15966 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15967 struct pci_dev *bridge = NULL;
15968
15969 while (pci_id->vendor != 0) {
15970 bridge = pci_get_device(pci_id->vendor,
15971 pci_id->device,
15972 bridge);
15973 if (!bridge) {
15974 pci_id++;
15975 continue;
15976 }
15977 if (bridge->subordinate &&
15978 (bridge->subordinate->number <=
15979 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015980 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba12008-04-19 18:12:33 -070015981 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015982 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba12008-04-19 18:12:33 -070015983 pci_dev_put(bridge);
15984 break;
15985 }
15986 }
15987 }
15988
Michael Chan4a29cc22006-03-19 13:21:12 -080015989 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15990 * DMA addresses > 40-bit. This bridge may have other additional
15991 * 57xx devices behind it in some 4-port NIC designs for example.
15992 * Any tg3 device found behind the bridge will also need the 40-bit
15993 * DMA workaround.
15994 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015995 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015996 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080015997 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000015998 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015999 struct pci_dev *bridge = NULL;
16000
16001 do {
16002 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16003 PCI_DEVICE_ID_SERVERWORKS_EPB,
16004 bridge);
16005 if (bridge && bridge->subordinate &&
16006 (bridge->subordinate->number <=
16007 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016008 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016009 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016010 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016011 pci_dev_put(bridge);
16012 break;
16013 }
16014 } while (bridge);
16015 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016016
Joe Perches41535772013-02-16 11:20:04 +000016017 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16018 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016019 tp->pdev_peer = tg3_find_peer(tp);
16020
Matt Carlson507399f2009-11-13 13:03:37 +000016021 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016022 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016023 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016024 else if (tg3_flag(tp, 57765_PLUS))
16025 tg3_flag_set(tp, HW_TSO_3);
16026 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016027 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016028 tg3_flag_set(tp, HW_TSO_2);
16029 else if (tg3_flag(tp, 5750_PLUS)) {
16030 tg3_flag_set(tp, HW_TSO_1);
16031 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016032 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16033 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016034 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016035 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16036 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16037 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016038 tg3_flag_set(tp, FW_TSO);
16039 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016040 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016041 tp->fw_needed = FIRMWARE_TG3TSO5;
16042 else
16043 tp->fw_needed = FIRMWARE_TG3TSO;
16044 }
16045
Matt Carlsondabc5c62011-05-19 12:12:52 +000016046 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016047 if (tg3_flag(tp, HW_TSO_1) ||
16048 tg3_flag(tp, HW_TSO_2) ||
16049 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016050 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016051 /* For firmware TSO, assume ASF is disabled.
16052 * We'll disable TSO later if we discover ASF
16053 * is enabled in tg3_get_eeprom_hw_cfg().
16054 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016055 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016056 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016057 tg3_flag_clear(tp, TSO_CAPABLE);
16058 tg3_flag_clear(tp, TSO_BUG);
16059 tp->fw_needed = NULL;
16060 }
16061
Joe Perches41535772013-02-16 11:20:04 +000016062 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016063 tp->fw_needed = FIRMWARE_TG3;
16064
Nithin Sujirc4dab502013-03-06 17:02:34 +000016065 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16066 tp->fw_needed = FIRMWARE_TG357766;
16067
Matt Carlson507399f2009-11-13 13:03:37 +000016068 tp->irq_max = 1;
16069
Joe Perches63c3a662011-04-26 08:12:10 +000016070 if (tg3_flag(tp, 5750_PLUS)) {
16071 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016072 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16073 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16074 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16075 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016076 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016077 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016078
Joe Perches63c3a662011-04-26 08:12:10 +000016079 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016080 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016081 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016083
Joe Perches63c3a662011-04-26 08:12:10 +000016084 if (tg3_flag(tp, 57765_PLUS)) {
16085 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016086 tp->irq_max = TG3_IRQ_MAX_VECS;
16087 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016088 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016089
Michael Chan91024262012-09-28 07:12:38 +000016090 tp->txq_max = 1;
16091 tp->rxq_max = 1;
16092 if (tp->irq_max > 1) {
16093 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16094 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16095
Joe Perches41535772013-02-16 11:20:04 +000016096 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16097 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016098 tp->txq_max = tp->irq_max - 1;
16099 }
16100
Matt Carlsonb7abee62012-06-07 12:56:54 +000016101 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016102 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016103 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016104
Joe Perches41535772013-02-16 11:20:04 +000016105 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016106 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016107
Joe Perches41535772013-02-16 11:20:04 +000016108 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16109 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16110 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16111 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016112 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016113
Joe Perches63c3a662011-04-26 08:12:10 +000016114 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016115 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016116 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016117
Joe Perches63c3a662011-04-26 08:12:10 +000016118 if (!tg3_flag(tp, 5705_PLUS) ||
16119 tg3_flag(tp, 5780_CLASS) ||
16120 tg3_flag(tp, USE_JUMBO_BDFLAG))
16121 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016122
Matt Carlson52f44902008-11-21 17:17:04 -080016123 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16124 &pci_state_reg);
16125
Jon Mason708ebb3a2011-06-27 12:56:50 +000016126 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016127 u16 lnkctl;
16128
Joe Perches63c3a662011-04-26 08:12:10 +000016129 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016130
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016131 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016132 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016133 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016134 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016135 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016136 }
Joe Perches41535772013-02-16 11:20:04 +000016137 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16138 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16139 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16140 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016141 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016142 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016143 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016144 }
Joe Perches41535772013-02-16 11:20:04 +000016145 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016146 /* BCM5785 devices are effectively PCIe devices, and should
16147 * follow PCIe codepaths, but do not have a PCIe capabilities
16148 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016149 */
Joe Perches63c3a662011-04-26 08:12:10 +000016150 tg3_flag_set(tp, PCI_EXPRESS);
16151 } else if (!tg3_flag(tp, 5705_PLUS) ||
16152 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016153 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16154 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016155 dev_err(&tp->pdev->dev,
16156 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016157 return -EIO;
16158 }
16159
16160 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016161 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016163
Michael Chan399de502005-10-03 14:02:39 -070016164 /* If we have an AMD 762 or VIA K8T800 chipset, write
16165 * reordering to the mailbox registers done by the host
16166 * controller can cause major troubles. We read back from
16167 * every mailbox register write to force the writes to be
16168 * posted to the chip in order.
16169 */
Matt Carlson41434702011-03-09 16:58:22 +000016170 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016171 !tg3_flag(tp, PCI_EXPRESS))
16172 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016173
Matt Carlson69fc4052008-12-21 20:19:57 -080016174 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16175 &tp->pci_cacheline_sz);
16176 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16177 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016178 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016179 tp->pci_lat_timer < 64) {
16180 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016181 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16182 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016183 }
16184
Matt Carlson16821282011-07-13 09:27:28 +000016185 /* Important! -- It is critical that the PCI-X hw workaround
16186 * situation is decided before the first MMIO register access.
16187 */
Joe Perches41535772013-02-16 11:20:04 +000016188 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016189 /* 5700 BX chips need to have their TX producer index
16190 * mailboxes written twice to workaround a bug.
16191 */
Joe Perches63c3a662011-04-26 08:12:10 +000016192 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016193
Matt Carlson52f44902008-11-21 17:17:04 -080016194 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016195 *
16196 * The workaround is to use indirect register accesses
16197 * for all chip writes not to mailbox registers.
16198 */
Joe Perches63c3a662011-04-26 08:12:10 +000016199 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016200 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016201
Joe Perches63c3a662011-04-26 08:12:10 +000016202 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016203
16204 /* The chip can have it's power management PCI config
16205 * space registers clobbered due to this bug.
16206 * So explicitly force the chip into D0 here.
16207 */
Matt Carlson9974a352007-10-07 23:27:28 -070016208 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016209 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016210 &pm_reg);
16211 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16212 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016213 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016214 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016215 pm_reg);
16216
16217 /* Also, force SERR#/PERR# in PCI command. */
16218 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16219 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16220 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16221 }
16222 }
16223
Linus Torvalds1da177e2005-04-16 15:20:36 -070016224 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016225 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016226 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016227 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016228
16229 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016230 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016231 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16232 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16233 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16234 }
16235
Michael Chan1ee582d2005-08-09 20:16:46 -070016236 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016237 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016238 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016239 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016240 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016241 tp->write32_tx_mbox = tg3_write32;
16242 tp->write32_rx_mbox = tg3_write32;
16243
16244 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016245 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016246 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016247 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016248 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016249 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016250 /*
16251 * Back to back register writes can cause problems on these
16252 * chips, the workaround is to read back all reg writes
16253 * except those to mailbox regs.
16254 *
16255 * See tg3_write_indirect_reg32().
16256 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016257 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016258 }
16259
Joe Perches63c3a662011-04-26 08:12:10 +000016260 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016261 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016262 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016263 tp->write32_rx_mbox = tg3_write_flush_reg32;
16264 }
Michael Chan20094932005-08-09 20:16:32 -070016265
Joe Perches63c3a662011-04-26 08:12:10 +000016266 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016267 tp->read32 = tg3_read_indirect_reg32;
16268 tp->write32 = tg3_write_indirect_reg32;
16269 tp->read32_mbox = tg3_read_indirect_mbox;
16270 tp->write32_mbox = tg3_write_indirect_mbox;
16271 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16272 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16273
16274 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016275 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016276
16277 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16278 pci_cmd &= ~PCI_COMMAND_MEMORY;
16279 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16280 }
Joe Perches41535772013-02-16 11:20:04 +000016281 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016282 tp->read32_mbox = tg3_read32_mbox_5906;
16283 tp->write32_mbox = tg3_write32_mbox_5906;
16284 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16285 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16286 }
Michael Chan68929142005-08-09 20:17:14 -070016287
Michael Chanbbadf502006-04-06 21:46:34 -070016288 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016289 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016290 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16291 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016292 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016293
Matt Carlson16821282011-07-13 09:27:28 +000016294 /* The memory arbiter has to be enabled in order for SRAM accesses
16295 * to succeed. Normally on powerup the tg3 chip firmware will make
16296 * sure it is enabled, but other entities such as system netboot
16297 * code might disable it.
16298 */
16299 val = tr32(MEMARB_MODE);
16300 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16301
Matt Carlson9dc5e342011-11-04 09:15:02 +000016302 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016303 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016304 tg3_flag(tp, 5780_CLASS)) {
16305 if (tg3_flag(tp, PCIX_MODE)) {
16306 pci_read_config_dword(tp->pdev,
16307 tp->pcix_cap + PCI_X_STATUS,
16308 &val);
16309 tp->pci_fn = val & 0x7;
16310 }
Joe Perches41535772013-02-16 11:20:04 +000016311 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16312 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16313 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016314 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016315 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16316 val = tr32(TG3_CPMU_STATUS);
16317
Joe Perches41535772013-02-16 11:20:04 +000016318 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016319 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16320 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016321 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16322 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016323 }
16324
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016325 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16326 tp->write32_tx_mbox = tg3_write_flush_reg32;
16327 tp->write32_rx_mbox = tg3_write_flush_reg32;
16328 }
16329
Michael Chan7d0c41e2005-04-21 17:06:20 -070016330 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016331 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016332 * determined before calling tg3_set_power_state() so that
16333 * we know whether or not to switch out of Vaux power.
16334 * When the flag is set, it means that GPIO1 is used for eeprom
16335 * write protect and also implies that it is a LOM where GPIOs
16336 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016337 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016338 tg3_get_eeprom_hw_cfg(tp);
16339
Matt Carlson1caf13e2013-03-06 17:02:29 +000016340 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016341 tg3_flag_clear(tp, TSO_CAPABLE);
16342 tg3_flag_clear(tp, TSO_BUG);
16343 tp->fw_needed = NULL;
16344 }
16345
Joe Perches63c3a662011-04-26 08:12:10 +000016346 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016347 /* Allow reads and writes to the
16348 * APE register and memory space.
16349 */
16350 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016351 PCISTATE_ALLOW_APE_SHMEM_WR |
16352 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016353 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16354 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016355
16356 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016357 }
16358
Matt Carlson16821282011-07-13 09:27:28 +000016359 /* Set up tp->grc_local_ctrl before calling
16360 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16361 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016362 * It is also used as eeprom write protect on LOMs.
16363 */
16364 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016365 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016366 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016367 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16368 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016369 /* Unused GPIO3 must be driven as output on 5752 because there
16370 * are no pull-up resistors on unused GPIO pins.
16371 */
Joe Perches41535772013-02-16 11:20:04 +000016372 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016373 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016374
Joe Perches41535772013-02-16 11:20:04 +000016375 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16376 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016377 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016378 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16379
Matt Carlson8d519ab2009-04-20 06:58:01 +000016380 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16381 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016382 /* Turn off the debug UART. */
16383 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016384 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016385 /* Keep VMain power. */
16386 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16387 GRC_LCLCTRL_GPIO_OUTPUT0;
16388 }
16389
Joe Perches41535772013-02-16 11:20:04 +000016390 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016391 tp->grc_local_ctrl |=
16392 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16393
Matt Carlson16821282011-07-13 09:27:28 +000016394 /* Switch out of Vaux if it is a NIC */
16395 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016396
Linus Torvalds1da177e2005-04-16 15:20:36 -070016397 /* Derive initial jumbo mode from MTU assigned in
16398 * ether_setup() via the alloc_etherdev() call
16399 */
Joe Perches63c3a662011-04-26 08:12:10 +000016400 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16401 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016402
16403 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016404 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16405 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16406 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16407 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016408 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016409 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016410 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016411 }
16412
Joe Perches41535772013-02-16 11:20:04 +000016413 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016414 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016415
Linus Torvalds1da177e2005-04-16 15:20:36 -070016416 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016417 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16418 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16419 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16420 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016421 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16422 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16423 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016424
Joe Perches41535772013-02-16 11:20:04 +000016425 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16426 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016427 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016428 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016429 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016430
Joe Perches63c3a662011-04-26 08:12:10 +000016431 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016432 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016433 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16434 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016435 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016436 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16437 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16438 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16439 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016440 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16441 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016442 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016443 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016444 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016445 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016446 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016448
Joe Perches41535772013-02-16 11:20:04 +000016449 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16450 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016451 tp->phy_otp = tg3_read_otp_phycfg(tp);
16452 if (tp->phy_otp == 0)
16453 tp->phy_otp = TG3_OTP_DEFAULT;
16454 }
16455
Joe Perches63c3a662011-04-26 08:12:10 +000016456 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016457 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16458 else
16459 tp->mi_mode = MAC_MI_MODE_BASE;
16460
Linus Torvalds1da177e2005-04-16 15:20:36 -070016461 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016462 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16463 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016464 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16465
Matt Carlson4d958472011-04-20 07:57:35 +000016466 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016467 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16468 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16469 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016470 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16471 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16472 }
16473
Joe Perches41535772013-02-16 11:20:04 +000016474 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16475 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016476 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016477
Matt Carlson158d7ab2008-05-29 01:37:54 -070016478 err = tg3_mdio_init(tp);
16479 if (err)
16480 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016481
16482 /* Initialize data/descriptor byte/word swapping. */
16483 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016484 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16485 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016486 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16487 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16488 GRC_MODE_B2HRX_ENABLE |
16489 GRC_MODE_HTX2B_ENABLE |
16490 GRC_MODE_HOST_STACKUP);
16491 else
16492 val &= GRC_MODE_HOST_STACKUP;
16493
Linus Torvalds1da177e2005-04-16 15:20:36 -070016494 tw32(GRC_MODE, val | tp->grc_mode);
16495
16496 tg3_switch_clocks(tp);
16497
16498 /* Clear this out for sanity. */
16499 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16500
16501 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16502 &pci_state_reg);
16503 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016504 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016505 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16506 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16507 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16508 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016509 void __iomem *sram_base;
16510
16511 /* Write some dummy words into the SRAM status block
16512 * area, see if it reads back correctly. If the return
16513 * value is bad, force enable the PCIX workaround.
16514 */
16515 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16516
16517 writel(0x00000000, sram_base);
16518 writel(0x00000000, sram_base + 4);
16519 writel(0xffffffff, sram_base + 4);
16520 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016521 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016522 }
16523 }
16524
16525 udelay(50);
16526 tg3_nvram_init(tp);
16527
Nithin Sujirc4dab502013-03-06 17:02:34 +000016528 /* If the device has an NVRAM, no need to load patch firmware */
16529 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16530 !tg3_flag(tp, NO_NVRAM))
16531 tp->fw_needed = NULL;
16532
Linus Torvalds1da177e2005-04-16 15:20:36 -070016533 grc_misc_cfg = tr32(GRC_MISC_CFG);
16534 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16535
Joe Perches41535772013-02-16 11:20:04 +000016536 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016537 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16538 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016539 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016540
Joe Perches63c3a662011-04-26 08:12:10 +000016541 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016542 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016543 tg3_flag_set(tp, TAGGED_STATUS);
16544 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016545 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16546 HOSTCC_MODE_CLRTICK_TXBD);
16547
16548 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16549 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16550 tp->misc_host_ctrl);
16551 }
16552
Matt Carlson3bda1252008-08-15 14:08:22 -070016553 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016554 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016555 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016556 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016557 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016558
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016559 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016560 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016561
16562 err = tg3_phy_probe(tp);
16563 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016564 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016565 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016566 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016567 }
16568
Matt Carlson184b8902010-04-05 10:19:25 +000016569 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016570 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016571
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016572 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16573 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016574 } else {
Joe Perches41535772013-02-16 11:20:04 +000016575 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016576 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016577 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016578 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016579 }
16580
16581 /* 5700 {AX,BX} chips have a broken status block link
16582 * change bit implementation, so we must use the
16583 * status register in those cases.
16584 */
Joe Perches41535772013-02-16 11:20:04 +000016585 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016586 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016587 else
Joe Perches63c3a662011-04-26 08:12:10 +000016588 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016589
16590 /* The led_ctrl is set during tg3_phy_probe, here we might
16591 * have to force the link status polling mechanism based
16592 * upon subsystem IDs.
16593 */
16594 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016595 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016596 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16597 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016598 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016599 }
16600
16601 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016602 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016603 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016604 else
Joe Perches63c3a662011-04-26 08:12:10 +000016605 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016606
Eric Dumazet9205fd92011-11-18 06:47:01 +000016607 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016608 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016609 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016610 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016611 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016612#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016613 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016614#endif
16615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016616
Matt Carlson2c49a442010-09-30 10:34:35 +000016617 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16618 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016619 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16620
Matt Carlson2c49a442010-09-30 10:34:35 +000016621 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016622
16623 /* Increment the rx prod index on the rx std ring by at most
16624 * 8 for these chips to workaround hw errata.
16625 */
Joe Perches41535772013-02-16 11:20:04 +000016626 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16627 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16628 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016629 tp->rx_std_max_post = 8;
16630
Joe Perches63c3a662011-04-26 08:12:10 +000016631 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016632 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16633 PCIE_PWR_MGMT_L1_THRESH_MSK;
16634
Linus Torvalds1da177e2005-04-16 15:20:36 -070016635 return err;
16636}
16637
David S. Miller49b6e95f2007-03-29 01:38:42 -070016638#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016639static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016640{
16641 struct net_device *dev = tp->dev;
16642 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016643 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016644 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016645 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016646
David S. Miller49b6e95f2007-03-29 01:38:42 -070016647 addr = of_get_property(dp, "local-mac-address", &len);
16648 if (addr && len == 6) {
16649 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016651 }
16652 return -ENODEV;
16653}
16654
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016655static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016656{
16657 struct net_device *dev = tp->dev;
16658
16659 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
16660 return 0;
16661}
16662#endif
16663
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016664static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016665{
16666 struct net_device *dev = tp->dev;
16667 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016668 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016669 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016670
David S. Miller49b6e95f2007-03-29 01:38:42 -070016671#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016672 if (!tg3_get_macaddr_sparc(tp))
16673 return 0;
16674#endif
16675
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016676 if (tg3_flag(tp, IS_SSB_CORE)) {
16677 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16678 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16679 return 0;
16680 }
16681
Linus Torvalds1da177e2005-04-16 15:20:36 -070016682 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016683 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016684 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016685 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16686 mac_offset = 0xcc;
16687 if (tg3_nvram_lock(tp))
16688 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16689 else
16690 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016691 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016692 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016693 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016694 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016695 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016696 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016697 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016698
16699 /* First try to get it from MAC address mailbox. */
16700 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16701 if ((hi >> 16) == 0x484b) {
16702 dev->dev_addr[0] = (hi >> 8) & 0xff;
16703 dev->dev_addr[1] = (hi >> 0) & 0xff;
16704
16705 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16706 dev->dev_addr[2] = (lo >> 24) & 0xff;
16707 dev->dev_addr[3] = (lo >> 16) & 0xff;
16708 dev->dev_addr[4] = (lo >> 8) & 0xff;
16709 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016710
Michael Chan008652b2006-03-27 23:14:53 -080016711 /* Some old bootcode may report a 0 MAC address in SRAM */
16712 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16713 }
16714 if (!addr_ok) {
16715 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016716 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016717 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016718 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016719 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16720 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016721 }
16722 /* Finally just fetch it out of the MAC control regs. */
16723 else {
16724 hi = tr32(MAC_ADDR_0_HIGH);
16725 lo = tr32(MAC_ADDR_0_LOW);
16726
16727 dev->dev_addr[5] = lo & 0xff;
16728 dev->dev_addr[4] = (lo >> 8) & 0xff;
16729 dev->dev_addr[3] = (lo >> 16) & 0xff;
16730 dev->dev_addr[2] = (lo >> 24) & 0xff;
16731 dev->dev_addr[1] = hi & 0xff;
16732 dev->dev_addr[0] = (hi >> 8) & 0xff;
16733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016734 }
16735
16736 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016737#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016738 if (!tg3_get_default_macaddr_sparc(tp))
16739 return 0;
16740#endif
16741 return -EINVAL;
16742 }
16743 return 0;
16744}
16745
David S. Miller59e6b432005-05-18 22:50:10 -070016746#define BOUNDARY_SINGLE_CACHELINE 1
16747#define BOUNDARY_MULTI_CACHELINE 2
16748
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016749static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016750{
16751 int cacheline_size;
16752 u8 byte;
16753 int goal;
16754
16755 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16756 if (byte == 0)
16757 cacheline_size = 1024;
16758 else
16759 cacheline_size = (int) byte * 4;
16760
16761 /* On 5703 and later chips, the boundary bits have no
16762 * effect.
16763 */
Joe Perches41535772013-02-16 11:20:04 +000016764 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16765 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016766 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016767 goto out;
16768
16769#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16770 goal = BOUNDARY_MULTI_CACHELINE;
16771#else
16772#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16773 goal = BOUNDARY_SINGLE_CACHELINE;
16774#else
16775 goal = 0;
16776#endif
16777#endif
16778
Joe Perches63c3a662011-04-26 08:12:10 +000016779 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016780 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16781 goto out;
16782 }
16783
David S. Miller59e6b432005-05-18 22:50:10 -070016784 if (!goal)
16785 goto out;
16786
16787 /* PCI controllers on most RISC systems tend to disconnect
16788 * when a device tries to burst across a cache-line boundary.
16789 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16790 *
16791 * Unfortunately, for PCI-E there are only limited
16792 * write-side controls for this, and thus for reads
16793 * we will still get the disconnects. We'll also waste
16794 * these PCI cycles for both read and write for chips
16795 * other than 5700 and 5701 which do not implement the
16796 * boundary bits.
16797 */
Joe Perches63c3a662011-04-26 08:12:10 +000016798 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016799 switch (cacheline_size) {
16800 case 16:
16801 case 32:
16802 case 64:
16803 case 128:
16804 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16805 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16806 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16807 } else {
16808 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16809 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16810 }
16811 break;
16812
16813 case 256:
16814 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16815 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16816 break;
16817
16818 default:
16819 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16820 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16821 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016822 }
Joe Perches63c3a662011-04-26 08:12:10 +000016823 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016824 switch (cacheline_size) {
16825 case 16:
16826 case 32:
16827 case 64:
16828 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16829 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16830 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16831 break;
16832 }
16833 /* fallthrough */
16834 case 128:
16835 default:
16836 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16837 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16838 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016839 }
David S. Miller59e6b432005-05-18 22:50:10 -070016840 } else {
16841 switch (cacheline_size) {
16842 case 16:
16843 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16844 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16845 DMA_RWCTRL_WRITE_BNDRY_16);
16846 break;
16847 }
16848 /* fallthrough */
16849 case 32:
16850 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16851 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16852 DMA_RWCTRL_WRITE_BNDRY_32);
16853 break;
16854 }
16855 /* fallthrough */
16856 case 64:
16857 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16858 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16859 DMA_RWCTRL_WRITE_BNDRY_64);
16860 break;
16861 }
16862 /* fallthrough */
16863 case 128:
16864 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16865 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16866 DMA_RWCTRL_WRITE_BNDRY_128);
16867 break;
16868 }
16869 /* fallthrough */
16870 case 256:
16871 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16872 DMA_RWCTRL_WRITE_BNDRY_256);
16873 break;
16874 case 512:
16875 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16876 DMA_RWCTRL_WRITE_BNDRY_512);
16877 break;
16878 case 1024:
16879 default:
16880 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16881 DMA_RWCTRL_WRITE_BNDRY_1024);
16882 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016883 }
David S. Miller59e6b432005-05-18 22:50:10 -070016884 }
16885
16886out:
16887 return val;
16888}
16889
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016890static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000016891 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016892{
16893 struct tg3_internal_buffer_desc test_desc;
16894 u32 sram_dma_descs;
16895 int i, ret;
16896
16897 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16898
16899 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16900 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16901 tw32(RDMAC_STATUS, 0);
16902 tw32(WDMAC_STATUS, 0);
16903
16904 tw32(BUFMGR_MODE, 0);
16905 tw32(FTQ_RESET, 0);
16906
16907 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16908 test_desc.addr_lo = buf_dma & 0xffffffff;
16909 test_desc.nic_mbuf = 0x00002100;
16910 test_desc.len = size;
16911
16912 /*
16913 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16914 * the *second* time the tg3 driver was getting loaded after an
16915 * initial scan.
16916 *
16917 * Broadcom tells me:
16918 * ...the DMA engine is connected to the GRC block and a DMA
16919 * reset may affect the GRC block in some unpredictable way...
16920 * The behavior of resets to individual blocks has not been tested.
16921 *
16922 * Broadcom noted the GRC reset will also reset all sub-components.
16923 */
16924 if (to_device) {
16925 test_desc.cqid_sqid = (13 << 8) | 2;
16926
16927 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16928 udelay(40);
16929 } else {
16930 test_desc.cqid_sqid = (16 << 8) | 7;
16931
16932 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16933 udelay(40);
16934 }
16935 test_desc.flags = 0x00000005;
16936
16937 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16938 u32 val;
16939
16940 val = *(((u32 *)&test_desc) + i);
16941 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16942 sram_dma_descs + (i * sizeof(u32)));
16943 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16944 }
16945 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16946
Matt Carlson859a588792010-04-05 10:19:28 +000016947 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016948 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016949 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016950 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016951
16952 ret = -ENODEV;
16953 for (i = 0; i < 40; i++) {
16954 u32 val;
16955
16956 if (to_device)
16957 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16958 else
16959 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16960 if ((val & 0xffff) == sram_dma_descs) {
16961 ret = 0;
16962 break;
16963 }
16964
16965 udelay(100);
16966 }
16967
16968 return ret;
16969}
16970
David S. Millerded73402005-05-23 13:59:47 -070016971#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016972
Matt Carlson41434702011-03-09 16:58:22 +000016973static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016974 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16975 { },
16976};
16977
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016978static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016979{
16980 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016981 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016982 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016983
Matt Carlson4bae65c2010-11-24 08:31:52 +000016984 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16985 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016986 if (!buf) {
16987 ret = -ENOMEM;
16988 goto out_nofree;
16989 }
16990
16991 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16992 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16993
David S. Miller59e6b432005-05-18 22:50:10 -070016994 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016995
Joe Perches63c3a662011-04-26 08:12:10 +000016996 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016997 goto out;
16998
Joe Perches63c3a662011-04-26 08:12:10 +000016999 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017000 /* DMA read watermark not used on PCIE */
17001 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017002 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017003 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17004 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017005 tp->dma_rwctrl |= 0x003f0000;
17006 else
17007 tp->dma_rwctrl |= 0x003f000f;
17008 } else {
Joe Perches41535772013-02-16 11:20:04 +000017009 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17010 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017011 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017012 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017013
Michael Chan4a29cc22006-03-19 13:21:12 -080017014 /* If the 5704 is behind the EPB bridge, we can
17015 * do the less restrictive ONE_DMA workaround for
17016 * better performance.
17017 */
Joe Perches63c3a662011-04-26 08:12:10 +000017018 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017019 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017020 tp->dma_rwctrl |= 0x8000;
17021 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017022 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17023
Joe Perches41535772013-02-16 11:20:04 +000017024 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017025 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017026 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017027 tp->dma_rwctrl |=
17028 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17029 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17030 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017031 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017032 /* 5780 always in PCIX mode */
17033 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017034 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017035 /* 5714 always in PCIX mode */
17036 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017037 } else {
17038 tp->dma_rwctrl |= 0x001b000f;
17039 }
17040 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017041 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17042 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017043
Joe Perches41535772013-02-16 11:20:04 +000017044 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17045 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017046 tp->dma_rwctrl &= 0xfffffff0;
17047
Joe Perches41535772013-02-16 11:20:04 +000017048 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17049 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017050 /* Remove this if it causes problems for some boards. */
17051 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17052
17053 /* On 5700/5701 chips, we need to set this bit.
17054 * Otherwise the chip will issue cacheline transactions
17055 * to streamable DMA memory with not all the byte
17056 * enables turned on. This is an error on several
17057 * RISC PCI controllers, in particular sparc64.
17058 *
17059 * On 5703/5704 chips, this bit has been reassigned
17060 * a different meaning. In particular, it is used
17061 * on those chips to enable a PCI-X workaround.
17062 */
17063 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17064 }
17065
17066 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17067
Linus Torvalds1da177e2005-04-16 15:20:36 -070017068
Joe Perches41535772013-02-16 11:20:04 +000017069 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17070 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017071 goto out;
17072
David S. Miller59e6b432005-05-18 22:50:10 -070017073 /* It is best to perform DMA test with maximum write burst size
17074 * to expose the 5700/5701 write DMA bug.
17075 */
17076 saved_dma_rwctrl = tp->dma_rwctrl;
17077 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17078 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17079
Linus Torvalds1da177e2005-04-16 15:20:36 -070017080 while (1) {
17081 u32 *p = buf, i;
17082
17083 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17084 p[i] = i;
17085
17086 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017087 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017088 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017089 dev_err(&tp->pdev->dev,
17090 "%s: Buffer write failed. err = %d\n",
17091 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017092 break;
17093 }
17094
Linus Torvalds1da177e2005-04-16 15:20:36 -070017095 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017096 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017097 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017098 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17099 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017100 break;
17101 }
17102
17103 /* Verify it. */
17104 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17105 if (p[i] == i)
17106 continue;
17107
David S. Miller59e6b432005-05-18 22:50:10 -070017108 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17109 DMA_RWCTRL_WRITE_BNDRY_16) {
17110 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017111 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17112 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17113 break;
17114 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017115 dev_err(&tp->pdev->dev,
17116 "%s: Buffer corrupted on read back! "
17117 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017118 ret = -ENODEV;
17119 goto out;
17120 }
17121 }
17122
17123 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17124 /* Success. */
17125 ret = 0;
17126 break;
17127 }
17128 }
David S. Miller59e6b432005-05-18 22:50:10 -070017129 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17130 DMA_RWCTRL_WRITE_BNDRY_16) {
17131 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017132 * now look for chipsets that are known to expose the
17133 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017134 */
Matt Carlson41434702011-03-09 16:58:22 +000017135 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017136 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17137 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017138 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017139 /* Safe to use the calculated DMA boundary. */
17140 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017141 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017142
David S. Miller59e6b432005-05-18 22:50:10 -070017143 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017145
17146out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017147 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017148out_nofree:
17149 return ret;
17150}
17151
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017152static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017153{
Joe Perches63c3a662011-04-26 08:12:10 +000017154 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017155 tp->bufmgr_config.mbuf_read_dma_low_water =
17156 DEFAULT_MB_RDMA_LOW_WATER_5705;
17157 tp->bufmgr_config.mbuf_mac_rx_low_water =
17158 DEFAULT_MB_MACRX_LOW_WATER_57765;
17159 tp->bufmgr_config.mbuf_high_water =
17160 DEFAULT_MB_HIGH_WATER_57765;
17161
17162 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17163 DEFAULT_MB_RDMA_LOW_WATER_5705;
17164 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17165 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17166 tp->bufmgr_config.mbuf_high_water_jumbo =
17167 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017168 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017169 tp->bufmgr_config.mbuf_read_dma_low_water =
17170 DEFAULT_MB_RDMA_LOW_WATER_5705;
17171 tp->bufmgr_config.mbuf_mac_rx_low_water =
17172 DEFAULT_MB_MACRX_LOW_WATER_5705;
17173 tp->bufmgr_config.mbuf_high_water =
17174 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017175 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017176 tp->bufmgr_config.mbuf_mac_rx_low_water =
17177 DEFAULT_MB_MACRX_LOW_WATER_5906;
17178 tp->bufmgr_config.mbuf_high_water =
17179 DEFAULT_MB_HIGH_WATER_5906;
17180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017181
Michael Chanfdfec1722005-07-25 12:31:48 -070017182 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17183 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17184 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17185 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17186 tp->bufmgr_config.mbuf_high_water_jumbo =
17187 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17188 } else {
17189 tp->bufmgr_config.mbuf_read_dma_low_water =
17190 DEFAULT_MB_RDMA_LOW_WATER;
17191 tp->bufmgr_config.mbuf_mac_rx_low_water =
17192 DEFAULT_MB_MACRX_LOW_WATER;
17193 tp->bufmgr_config.mbuf_high_water =
17194 DEFAULT_MB_HIGH_WATER;
17195
17196 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17197 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17198 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17199 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17200 tp->bufmgr_config.mbuf_high_water_jumbo =
17201 DEFAULT_MB_HIGH_WATER_JUMBO;
17202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017203
17204 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17205 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17206}
17207
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017208static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017209{
Matt Carlson79eb6902010-02-17 15:17:03 +000017210 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17211 case TG3_PHY_ID_BCM5400: return "5400";
17212 case TG3_PHY_ID_BCM5401: return "5401";
17213 case TG3_PHY_ID_BCM5411: return "5411";
17214 case TG3_PHY_ID_BCM5701: return "5701";
17215 case TG3_PHY_ID_BCM5703: return "5703";
17216 case TG3_PHY_ID_BCM5704: return "5704";
17217 case TG3_PHY_ID_BCM5705: return "5705";
17218 case TG3_PHY_ID_BCM5750: return "5750";
17219 case TG3_PHY_ID_BCM5752: return "5752";
17220 case TG3_PHY_ID_BCM5714: return "5714";
17221 case TG3_PHY_ID_BCM5780: return "5780";
17222 case TG3_PHY_ID_BCM5755: return "5755";
17223 case TG3_PHY_ID_BCM5787: return "5787";
17224 case TG3_PHY_ID_BCM5784: return "5784";
17225 case TG3_PHY_ID_BCM5756: return "5722/5756";
17226 case TG3_PHY_ID_BCM5906: return "5906";
17227 case TG3_PHY_ID_BCM5761: return "5761";
17228 case TG3_PHY_ID_BCM5718C: return "5718C";
17229 case TG3_PHY_ID_BCM5718S: return "5718S";
17230 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017231 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017232 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017233 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017234 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017235 case 0: return "serdes";
17236 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017238}
17239
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017240static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017241{
Joe Perches63c3a662011-04-26 08:12:10 +000017242 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017243 strcpy(str, "PCI Express");
17244 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017245 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017246 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17247
17248 strcpy(str, "PCIX:");
17249
17250 if ((clock_ctrl == 7) ||
17251 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17252 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17253 strcat(str, "133MHz");
17254 else if (clock_ctrl == 0)
17255 strcat(str, "33MHz");
17256 else if (clock_ctrl == 2)
17257 strcat(str, "50MHz");
17258 else if (clock_ctrl == 4)
17259 strcat(str, "66MHz");
17260 else if (clock_ctrl == 6)
17261 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017262 } else {
17263 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017264 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017265 strcat(str, "66MHz");
17266 else
17267 strcat(str, "33MHz");
17268 }
Joe Perches63c3a662011-04-26 08:12:10 +000017269 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017270 strcat(str, ":32-bit");
17271 else
17272 strcat(str, ":64-bit");
17273 return str;
17274}
17275
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017276static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017277{
17278 struct ethtool_coalesce *ec = &tp->coal;
17279
17280 memset(ec, 0, sizeof(*ec));
17281 ec->cmd = ETHTOOL_GCOALESCE;
17282 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17283 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17284 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17285 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17286 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17287 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17288 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17289 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17290 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17291
17292 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17293 HOSTCC_MODE_CLRTICK_TXBD)) {
17294 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17295 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17296 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17297 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17298 }
Michael Chand244c892005-07-05 14:42:33 -070017299
Joe Perches63c3a662011-04-26 08:12:10 +000017300 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017301 ec->rx_coalesce_usecs_irq = 0;
17302 ec->tx_coalesce_usecs_irq = 0;
17303 ec->stats_block_coalesce_usecs = 0;
17304 }
David S. Miller15f98502005-05-18 22:49:26 -070017305}
17306
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017307static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017308 const struct pci_device_id *ent)
17309{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017310 struct net_device *dev;
17311 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017312 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017313 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017314 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017315 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017316 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017317
Joe Perches05dbe002010-02-17 19:44:19 +000017318 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017319
17320 err = pci_enable_device(pdev);
17321 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017322 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017323 return err;
17324 }
17325
Linus Torvalds1da177e2005-04-16 15:20:36 -070017326 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17327 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017328 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017329 goto err_out_disable_pdev;
17330 }
17331
17332 pci_set_master(pdev);
17333
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017334 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017335 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017336 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017337 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017338 }
17339
Linus Torvalds1da177e2005-04-16 15:20:36 -070017340 SET_NETDEV_DEV(dev, &pdev->dev);
17341
Linus Torvalds1da177e2005-04-16 15:20:36 -070017342 tp = netdev_priv(dev);
17343 tp->pdev = pdev;
17344 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017345 tp->rx_mode = TG3_DEF_RX_MODE;
17346 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017347 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017348
Linus Torvalds1da177e2005-04-16 15:20:36 -070017349 if (tg3_debug > 0)
17350 tp->msg_enable = tg3_debug;
17351 else
17352 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17353
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017354 if (pdev_is_ssb_gige_core(pdev)) {
17355 tg3_flag_set(tp, IS_SSB_CORE);
17356 if (ssb_gige_must_flush_posted_writes(pdev))
17357 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17358 if (ssb_gige_one_dma_at_once(pdev))
17359 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
17360 if (ssb_gige_have_roboswitch(pdev))
17361 tg3_flag_set(tp, ROBOSWITCH);
17362 if (ssb_gige_is_rgmii(pdev))
17363 tg3_flag_set(tp, RGMII_MODE);
17364 }
17365
Linus Torvalds1da177e2005-04-16 15:20:36 -070017366 /* The word/byte swap controls here control register access byte
17367 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17368 * setting below.
17369 */
17370 tp->misc_host_ctrl =
17371 MISC_HOST_CTRL_MASK_PCI_INT |
17372 MISC_HOST_CTRL_WORD_SWAP |
17373 MISC_HOST_CTRL_INDIR_ACCESS |
17374 MISC_HOST_CTRL_PCISTATE_RW;
17375
17376 /* The NONFRM (non-frame) byte/word swap controls take effect
17377 * on descriptor entries, anything which isn't packet data.
17378 *
17379 * The StrongARM chips on the board (one for tx, one for rx)
17380 * are running in big-endian mode.
17381 */
17382 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17383 GRC_MODE_WSWAP_NONFRM_DATA);
17384#ifdef __BIG_ENDIAN
17385 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17386#endif
17387 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017388 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017389 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017390
Matt Carlsond5fe4882008-11-21 17:20:32 -080017391 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017392 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017393 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017394 err = -ENOMEM;
17395 goto err_out_free_dev;
17396 }
17397
Matt Carlsonc9cab242011-07-13 09:27:27 +000017398 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17399 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17400 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17401 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17402 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017403 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017404 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17405 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017406 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017407 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17408 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017409 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17410 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017411 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17412 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017413 tg3_flag_set(tp, ENABLE_APE);
17414 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17415 if (!tp->aperegs) {
17416 dev_err(&pdev->dev,
17417 "Cannot map APE registers, aborting\n");
17418 err = -ENOMEM;
17419 goto err_out_iounmap;
17420 }
17421 }
17422
Linus Torvalds1da177e2005-04-16 15:20:36 -070017423 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17424 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017425
Linus Torvalds1da177e2005-04-16 15:20:36 -070017426 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017427 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017428 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017429 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017430
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017431 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017432 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017433 dev_err(&pdev->dev,
17434 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017435 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017436 }
17437
Michael Chan4a29cc22006-03-19 13:21:12 -080017438 /* The EPB bridge inside 5714, 5715, and 5780 and any
17439 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017440 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17441 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17442 * do DMA address check in tg3_start_xmit().
17443 */
Joe Perches63c3a662011-04-26 08:12:10 +000017444 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017445 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017446 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017447 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017448#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017449 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017450#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017451 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017452 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017453
17454 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017455 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017456 err = pci_set_dma_mask(pdev, dma_mask);
17457 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017458 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017459 err = pci_set_consistent_dma_mask(pdev,
17460 persist_dma_mask);
17461 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017462 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17463 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017464 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017465 }
17466 }
17467 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017468 if (err || dma_mask == DMA_BIT_MASK(32)) {
17469 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017470 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017471 dev_err(&pdev->dev,
17472 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017473 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017474 }
17475 }
17476
Michael Chanfdfec1722005-07-25 12:31:48 -070017477 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017478
Patrick McHardyf6469682013-04-19 02:04:27 +000017479 features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlson0da06062011-05-19 12:12:53 +000017480
17481 /* 5700 B0 chips do not support checksumming correctly due
17482 * to hardware bugs.
17483 */
Joe Perches41535772013-02-16 11:20:04 +000017484 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017485 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17486
17487 if (tg3_flag(tp, 5755_PLUS))
17488 features |= NETIF_F_IPV6_CSUM;
17489 }
17490
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017491 /* TSO is on by default on chips that support hardware TSO.
17492 * Firmware TSO on older chips gives lower performance, so it
17493 * is off by default, but can be enabled using ethtool.
17494 */
Joe Perches63c3a662011-04-26 08:12:10 +000017495 if ((tg3_flag(tp, HW_TSO_1) ||
17496 tg3_flag(tp, HW_TSO_2) ||
17497 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017498 (features & NETIF_F_IP_CSUM))
17499 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017500 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017501 if (features & NETIF_F_IPV6_CSUM)
17502 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017503 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017504 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17505 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17506 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17507 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17508 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017509 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017511
Matt Carlsond542fe22011-05-19 16:02:43 +000017512 dev->features |= features;
17513 dev->vlan_features |= features;
17514
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017515 /*
17516 * Add loopback capability only for a subset of devices that support
17517 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17518 * loopback for the remaining devices.
17519 */
Joe Perches41535772013-02-16 11:20:04 +000017520 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017521 !tg3_flag(tp, CPMU_PRESENT))
17522 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017523 features |= NETIF_F_LOOPBACK;
17524
Matt Carlson0da06062011-05-19 12:12:53 +000017525 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017526
Joe Perches41535772013-02-16 11:20:04 +000017527 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017528 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017529 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017530 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017531 tp->rx_pending = 63;
17532 }
17533
Linus Torvalds1da177e2005-04-16 15:20:36 -070017534 err = tg3_get_device_address(tp);
17535 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017536 dev_err(&pdev->dev,
17537 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017538 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017539 }
17540
Matt Carlsonc88864d2007-11-12 21:07:01 -080017541 /*
17542 * Reset chip in case UNDI or EFI driver did not shutdown
17543 * DMA self test will enable WDMAC and we'll see (spurious)
17544 * pending DMA on the PCI bus at that point.
17545 */
17546 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17547 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17548 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17549 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17550 }
17551
17552 err = tg3_test_dma(tp);
17553 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017554 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017555 goto err_out_apeunmap;
17556 }
17557
Matt Carlson78f90dc2009-11-13 13:03:42 +000017558 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17559 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17560 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017561 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017562 struct tg3_napi *tnapi = &tp->napi[i];
17563
17564 tnapi->tp = tp;
17565 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17566
17567 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017568 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017569 intmbx += 0x8;
17570 else
17571 intmbx += 0x4;
17572
17573 tnapi->consmbox = rcvmbx;
17574 tnapi->prodmbox = sndmbx;
17575
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017576 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017577 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017578 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017579 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017580
Joe Perches63c3a662011-04-26 08:12:10 +000017581 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017582 break;
17583
17584 /*
17585 * If we support MSIX, we'll be using RSS. If we're using
17586 * RSS, the first vector only handles link interrupts and the
17587 * remaining vectors handle rx and tx interrupts. Reuse the
17588 * mailbox values for the next iteration. The values we setup
17589 * above are still useful for the single vectored mode.
17590 */
17591 if (!i)
17592 continue;
17593
17594 rcvmbx += 0x8;
17595
17596 if (sndmbx & 0x4)
17597 sndmbx -= 0x4;
17598 else
17599 sndmbx += 0xc;
17600 }
17601
Matt Carlsonc88864d2007-11-12 21:07:01 -080017602 tg3_init_coal(tp);
17603
Michael Chanc49a1562006-12-17 17:07:29 -080017604 pci_set_drvdata(pdev, dev);
17605
Joe Perches41535772013-02-16 11:20:04 +000017606 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17607 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17608 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017609 tg3_flag_set(tp, PTP_CAPABLE);
17610
Matt Carlson21f76382012-02-22 12:35:21 +000017611 tg3_timer_init(tp);
17612
Michael Chan402e1392013-02-14 12:13:41 +000017613 tg3_carrier_off(tp);
17614
Linus Torvalds1da177e2005-04-16 15:20:36 -070017615 err = register_netdev(dev);
17616 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017617 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017618 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017619 }
17620
Joe Perches05dbe002010-02-17 19:44:19 +000017621 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17622 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017623 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017624 tg3_bus_string(tp, str),
17625 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017626
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017627 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017628 struct phy_device *phydev;
17629 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017630 netdev_info(dev,
17631 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017632 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017633 } else {
17634 char *ethtype;
17635
17636 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17637 ethtype = "10/100Base-TX";
17638 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17639 ethtype = "1000Base-SX";
17640 else
17641 ethtype = "10/100/1000Base-T";
17642
Matt Carlson5129c3a2010-04-05 10:19:23 +000017643 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017644 "(WireSpeed[%d], EEE[%d])\n",
17645 tg3_phy_string(tp), ethtype,
17646 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17647 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017648 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017649
Joe Perches05dbe002010-02-17 19:44:19 +000017650 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017651 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017652 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017653 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017654 tg3_flag(tp, ENABLE_ASF) != 0,
17655 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017656 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17657 tp->dma_rwctrl,
17658 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17659 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017660
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017661 pci_save_state(pdev);
17662
Linus Torvalds1da177e2005-04-16 15:20:36 -070017663 return 0;
17664
Matt Carlson0d3031d2007-10-10 18:02:43 -070017665err_out_apeunmap:
17666 if (tp->aperegs) {
17667 iounmap(tp->aperegs);
17668 tp->aperegs = NULL;
17669 }
17670
Linus Torvalds1da177e2005-04-16 15:20:36 -070017671err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017672 if (tp->regs) {
17673 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017674 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017676
17677err_out_free_dev:
17678 free_netdev(dev);
17679
17680err_out_free_res:
17681 pci_release_regions(pdev);
17682
17683err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080017684 if (pci_is_enabled(pdev))
17685 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017686 pci_set_drvdata(pdev, NULL);
17687 return err;
17688}
17689
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017690static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017691{
17692 struct net_device *dev = pci_get_drvdata(pdev);
17693
17694 if (dev) {
17695 struct tg3 *tp = netdev_priv(dev);
17696
Jesper Juhle3c55302012-04-09 22:50:15 +020017697 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017698
Matt Carlsondb219972011-11-04 09:15:03 +000017699 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017700
David S. Miller1805b2f2011-10-24 18:18:09 -040017701 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017702 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017703 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017704 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017705
Linus Torvalds1da177e2005-04-16 15:20:36 -070017706 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017707 if (tp->aperegs) {
17708 iounmap(tp->aperegs);
17709 tp->aperegs = NULL;
17710 }
Michael Chan68929142005-08-09 20:17:14 -070017711 if (tp->regs) {
17712 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017713 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017715 free_netdev(dev);
17716 pci_release_regions(pdev);
17717 pci_disable_device(pdev);
17718 pci_set_drvdata(pdev, NULL);
17719 }
17720}
17721
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017722#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017723static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017724{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017725 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017726 struct net_device *dev = pci_get_drvdata(pdev);
17727 struct tg3 *tp = netdev_priv(dev);
17728 int err;
17729
17730 if (!netif_running(dev))
17731 return 0;
17732
Matt Carlsondb219972011-11-04 09:15:03 +000017733 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017734 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017735 tg3_netif_stop(tp);
17736
Matt Carlson21f76382012-02-22 12:35:21 +000017737 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017738
David S. Millerf47c11e2005-06-24 20:18:35 -070017739 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017740 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017741 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017742
17743 netif_device_detach(dev);
17744
David S. Millerf47c11e2005-06-24 20:18:35 -070017745 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017746 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017747 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017748 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017749
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017750 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017751 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017752 int err2;
17753
David S. Millerf47c11e2005-06-24 20:18:35 -070017754 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017755
Joe Perches63c3a662011-04-26 08:12:10 +000017756 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017757 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017758 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017760
Matt Carlson21f76382012-02-22 12:35:21 +000017761 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017762
17763 netif_device_attach(dev);
17764 tg3_netif_start(tp);
17765
Michael Chanb9ec6c12006-07-25 16:37:27 -070017766out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017767 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017768
17769 if (!err2)
17770 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017771 }
17772
17773 return err;
17774}
17775
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017776static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017777{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017778 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017779 struct net_device *dev = pci_get_drvdata(pdev);
17780 struct tg3 *tp = netdev_priv(dev);
17781 int err;
17782
17783 if (!netif_running(dev))
17784 return 0;
17785
Linus Torvalds1da177e2005-04-16 15:20:36 -070017786 netif_device_attach(dev);
17787
David S. Millerf47c11e2005-06-24 20:18:35 -070017788 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017789
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017790 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
17791
Joe Perches63c3a662011-04-26 08:12:10 +000017792 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017793 err = tg3_restart_hw(tp,
17794 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017795 if (err)
17796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017797
Matt Carlson21f76382012-02-22 12:35:21 +000017798 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017799
Linus Torvalds1da177e2005-04-16 15:20:36 -070017800 tg3_netif_start(tp);
17801
Michael Chanb9ec6c12006-07-25 16:37:27 -070017802out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017803 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017804
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017805 if (!err)
17806 tg3_phy_start(tp);
17807
Michael Chanb9ec6c12006-07-25 16:37:27 -070017808 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017809}
Fabio Estevam42df36a2013-04-16 09:28:29 +000017810#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017811
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017812static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
17813
Nithin Sujir4c305fa2013-07-29 13:58:37 -070017814static void tg3_shutdown(struct pci_dev *pdev)
17815{
17816 struct net_device *dev = pci_get_drvdata(pdev);
17817 struct tg3 *tp = netdev_priv(dev);
17818
17819 rtnl_lock();
17820 netif_device_detach(dev);
17821
17822 if (netif_running(dev))
17823 dev_close(dev);
17824
17825 if (system_state == SYSTEM_POWER_OFF)
17826 tg3_power_down(tp);
17827
17828 rtnl_unlock();
17829}
17830
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017831/**
17832 * tg3_io_error_detected - called when PCI error is detected
17833 * @pdev: Pointer to PCI device
17834 * @state: The current pci connection state
17835 *
17836 * This function is called after a PCI bus error affecting
17837 * this device has been detected.
17838 */
17839static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17840 pci_channel_state_t state)
17841{
17842 struct net_device *netdev = pci_get_drvdata(pdev);
17843 struct tg3 *tp = netdev_priv(netdev);
17844 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17845
17846 netdev_info(netdev, "PCI I/O error detected\n");
17847
17848 rtnl_lock();
17849
Gavin Shand8af4df2013-07-24 17:25:08 +080017850 /* We probably don't have netdev yet */
17851 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017852 goto done;
17853
17854 tg3_phy_stop(tp);
17855
17856 tg3_netif_stop(tp);
17857
Matt Carlson21f76382012-02-22 12:35:21 +000017858 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017859
17860 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017861 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017862
17863 netif_device_detach(netdev);
17864
17865 /* Clean up software state, even if MMIO is blocked */
17866 tg3_full_lock(tp, 0);
17867 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17868 tg3_full_unlock(tp);
17869
17870done:
Michael Chan72bb72b2013-06-17 13:47:25 -070017871 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070017872 if (netdev) {
17873 tg3_napi_enable(tp);
17874 dev_close(netdev);
17875 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017876 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070017877 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017878 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070017879 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017880
17881 rtnl_unlock();
17882
17883 return err;
17884}
17885
17886/**
17887 * tg3_io_slot_reset - called after the pci bus has been reset.
17888 * @pdev: Pointer to PCI device
17889 *
17890 * Restart the card from scratch, as if from a cold-boot.
17891 * At this point, the card has exprienced a hard reset,
17892 * followed by fixups by BIOS, and has its config space
17893 * set up identically to what it was at cold boot.
17894 */
17895static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17896{
17897 struct net_device *netdev = pci_get_drvdata(pdev);
17898 struct tg3 *tp = netdev_priv(netdev);
17899 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17900 int err;
17901
17902 rtnl_lock();
17903
17904 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070017905 dev_err(&pdev->dev,
17906 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017907 goto done;
17908 }
17909
17910 pci_set_master(pdev);
17911 pci_restore_state(pdev);
17912 pci_save_state(pdev);
17913
Daniel Borkmann68293092013-08-13 11:45:13 -070017914 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017915 rc = PCI_ERS_RESULT_RECOVERED;
17916 goto done;
17917 }
17918
17919 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017920 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017921 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017922
17923 rc = PCI_ERS_RESULT_RECOVERED;
17924
17925done:
Daniel Borkmann68293092013-08-13 11:45:13 -070017926 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070017927 tg3_napi_enable(tp);
17928 dev_close(netdev);
17929 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017930 rtnl_unlock();
17931
17932 return rc;
17933}
17934
17935/**
17936 * tg3_io_resume - called when traffic can start flowing again.
17937 * @pdev: Pointer to PCI device
17938 *
17939 * This callback is called when the error recovery driver tells
17940 * us that its OK to resume normal operation.
17941 */
17942static void tg3_io_resume(struct pci_dev *pdev)
17943{
17944 struct net_device *netdev = pci_get_drvdata(pdev);
17945 struct tg3 *tp = netdev_priv(netdev);
17946 int err;
17947
17948 rtnl_lock();
17949
17950 if (!netif_running(netdev))
17951 goto done;
17952
17953 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017954 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000017955 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017956 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017957 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017958 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017959 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17960 goto done;
17961 }
17962
17963 netif_device_attach(netdev);
17964
Matt Carlson21f76382012-02-22 12:35:21 +000017965 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017966
17967 tg3_netif_start(tp);
17968
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017969 tg3_full_unlock(tp);
17970
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017971 tg3_phy_start(tp);
17972
17973done:
17974 rtnl_unlock();
17975}
17976
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017977static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017978 .error_detected = tg3_io_error_detected,
17979 .slot_reset = tg3_io_slot_reset,
17980 .resume = tg3_io_resume
17981};
17982
Linus Torvalds1da177e2005-04-16 15:20:36 -070017983static struct pci_driver tg3_driver = {
17984 .name = DRV_MODULE_NAME,
17985 .id_table = tg3_pci_tbl,
17986 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017987 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017988 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000017989 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070017990 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017991};
17992
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000017993module_pci_driver(tg3_driver);