blob: 4f0e3fe99e174816b4fe22cbe2fdabaa7430e6c1 [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 Sujir7a28fde2013-04-09 08:48:10 +000097#define TG3_MIN_NUM 131
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Sujir7a28fde2013-04-09 08:48:10 +0000100#define DRV_MODULE_RELDATE "April 09, 2013"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000102#define RESET_KIND_SHUTDOWN 0
103#define RESET_KIND_INIT 1
104#define RESET_KIND_SUSPEND 2
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_MODE 0
107#define TG3_DEF_TX_MODE 0
108#define TG3_DEF_MSG_ENABLE \
109 (NETIF_MSG_DRV | \
110 NETIF_MSG_PROBE | \
111 NETIF_MSG_LINK | \
112 NETIF_MSG_TIMER | \
113 NETIF_MSG_IFDOWN | \
114 NETIF_MSG_IFUP | \
115 NETIF_MSG_RX_ERR | \
116 NETIF_MSG_TX_ERR)
117
Matt Carlson520b2752011-06-13 13:39:02 +0000118#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* length of time before we decide the hardware is borked,
121 * and dev->tx_timeout() should be called to fix the problem
122 */
Joe Perches63c3a662011-04-26 08:12:10 +0000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_TX_TIMEOUT (5 * HZ)
125
126/* hardware minimum and maximum for a single frame's data payload */
127#define TG3_MIN_MTU 60
128#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* These numbers seem to be hard coded in the NIC firmware somehow.
132 * You can't change the ring sizes, but you can change where you place
133 * them in the NIC onboard memory.
134 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000135#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000137 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000139#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000140 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000141 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define TG3_DEF_RX_JUMBO_RING_PENDING 100
143
144/* Do not place this n-ring entries value into the tp struct itself,
145 * we really want to expose these constants to GCC so that modulo et
146 * al. operations are done with shifts and masks instead of with
147 * hw multiply/modulo instructions. Another solution would be to
148 * replace things like '% foo' with '& (foo - 1)'.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define TG3_TX_RING_SIZE 512
152#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
153
Matt Carlson2c49a442010-09-30 10:34:35 +0000154#define TG3_RX_STD_RING_BYTES(tp) \
155 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
156#define TG3_RX_JMB_RING_BYTES(tp) \
157 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
158#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000159 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
161 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
163
Matt Carlson287be122009-08-28 13:58:46 +0000164#define TG3_DMA_BYTE_ENAB 64
165
166#define TG3_RX_STD_DMA_SZ 1536
167#define TG3_RX_JMB_DMA_SZ 9046
168
169#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
170
171#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
172#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Matt Carlson2c49a442010-09-30 10:34:35 +0000174#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
175 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000176
Matt Carlson2c49a442010-09-30 10:34:35 +0000177#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
178 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000179
Matt Carlsond2757fc2010-04-12 06:58:27 +0000180/* Due to a hardware bug, the 5701 can only DMA to memory addresses
181 * that are at least dword aligned when used in PCIX mode. The driver
182 * works around this bug by double copying the packet. This workaround
183 * is built into the normal double copy length check for efficiency.
184 *
185 * However, the double copy is only necessary on those architectures
186 * where unaligned memory accesses are inefficient. For those architectures
187 * where unaligned memory accesses incur little penalty, we can reintegrate
188 * the 5701 in the normal rx path. Doing so saves a device structure
189 * dereference by hardcoding the double copy threshold in place.
190 */
191#define TG3_RX_COPY_THRESHOLD 256
192#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
193 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
194#else
195 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
196#endif
197
Matt Carlson81389f52011-08-31 11:44:49 +0000198#if (NET_IP_ALIGN != 0)
199#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
200#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000201#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000202#endif
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000205#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000206#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000207#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Matt Carlsonad829262008-11-21 17:16:16 -0800209#define TG3_RAW_IP_ALIGN 2
210
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000211#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000212#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000213
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800214#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000215#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800216#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
217#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
218
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500219static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000220 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
223MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
224MODULE_LICENSE("GPL");
225MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800226MODULE_FIRMWARE(FIRMWARE_TG3);
227MODULE_FIRMWARE(FIRMWARE_TG3TSO);
228MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
231module_param(tg3_debug, int, 0);
232MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
233
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000234#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
235#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
236
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000237static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
257 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
258 TG3_DRV_DATA_FLAG_5705_10_100},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
260 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
261 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
264 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
265 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
272 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
278 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000286 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
287 PCI_VENDOR_ID_LENOVO,
288 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
289 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
292 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000311 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
312 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
313 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
314 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
315 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
316 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800317 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
320 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
330 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
332 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700340 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
341 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
342 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
343 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
344 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
345 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
346 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000347 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700348 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
351MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
352
Andreas Mohr50da8592006-08-14 23:54:30 -0700353static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000355} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 { "rx_octets" },
357 { "rx_fragments" },
358 { "rx_ucast_packets" },
359 { "rx_mcast_packets" },
360 { "rx_bcast_packets" },
361 { "rx_fcs_errors" },
362 { "rx_align_errors" },
363 { "rx_xon_pause_rcvd" },
364 { "rx_xoff_pause_rcvd" },
365 { "rx_mac_ctrl_rcvd" },
366 { "rx_xoff_entered" },
367 { "rx_frame_too_long_errors" },
368 { "rx_jabbers" },
369 { "rx_undersize_packets" },
370 { "rx_in_length_errors" },
371 { "rx_out_length_errors" },
372 { "rx_64_or_less_octet_packets" },
373 { "rx_65_to_127_octet_packets" },
374 { "rx_128_to_255_octet_packets" },
375 { "rx_256_to_511_octet_packets" },
376 { "rx_512_to_1023_octet_packets" },
377 { "rx_1024_to_1522_octet_packets" },
378 { "rx_1523_to_2047_octet_packets" },
379 { "rx_2048_to_4095_octet_packets" },
380 { "rx_4096_to_8191_octet_packets" },
381 { "rx_8192_to_9022_octet_packets" },
382
383 { "tx_octets" },
384 { "tx_collisions" },
385
386 { "tx_xon_sent" },
387 { "tx_xoff_sent" },
388 { "tx_flow_control" },
389 { "tx_mac_errors" },
390 { "tx_single_collisions" },
391 { "tx_mult_collisions" },
392 { "tx_deferred" },
393 { "tx_excessive_collisions" },
394 { "tx_late_collisions" },
395 { "tx_collide_2times" },
396 { "tx_collide_3times" },
397 { "tx_collide_4times" },
398 { "tx_collide_5times" },
399 { "tx_collide_6times" },
400 { "tx_collide_7times" },
401 { "tx_collide_8times" },
402 { "tx_collide_9times" },
403 { "tx_collide_10times" },
404 { "tx_collide_11times" },
405 { "tx_collide_12times" },
406 { "tx_collide_13times" },
407 { "tx_collide_14times" },
408 { "tx_collide_15times" },
409 { "tx_ucast_packets" },
410 { "tx_mcast_packets" },
411 { "tx_bcast_packets" },
412 { "tx_carrier_sense_errors" },
413 { "tx_discards" },
414 { "tx_errors" },
415
416 { "dma_writeq_full" },
417 { "dma_write_prioq_full" },
418 { "rxbds_empty" },
419 { "rx_discards" },
420 { "rx_errors" },
421 { "rx_threshold_hit" },
422
423 { "dma_readq_full" },
424 { "dma_read_prioq_full" },
425 { "tx_comp_queue_full" },
426
427 { "ring_set_send_prod_index" },
428 { "ring_status_update" },
429 { "nic_irqs" },
430 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000431 { "nic_tx_threshold_hit" },
432
433 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
435
Matt Carlson48fa55a2011-04-13 11:05:06 +0000436#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000437#define TG3_NVRAM_TEST 0
438#define TG3_LINK_TEST 1
439#define TG3_REGISTER_TEST 2
440#define TG3_MEMORY_TEST 3
441#define TG3_MAC_LOOPB_TEST 4
442#define TG3_PHY_LOOPB_TEST 5
443#define TG3_EXT_LOOPB_TEST 6
444#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000445
446
Andreas Mohr50da8592006-08-14 23:54:30 -0700447static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700448 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000449} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000450 [TG3_NVRAM_TEST] = { "nvram test (online) " },
451 [TG3_LINK_TEST] = { "link test (online) " },
452 [TG3_REGISTER_TEST] = { "register test (offline)" },
453 [TG3_MEMORY_TEST] = { "memory test (offline)" },
454 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
455 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
456 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
457 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700458};
459
Matt Carlson48fa55a2011-04-13 11:05:06 +0000460#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
461
462
Michael Chanb401e9e2005-12-19 16:27:04 -0800463static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
464{
465 writel(val, tp->regs + off);
466}
467
468static u32 tg3_read32(struct tg3 *tp, u32 off)
469{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000470 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800471}
472
Matt Carlson0d3031d2007-10-10 18:02:43 -0700473static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
474{
475 writel(val, tp->aperegs + off);
476}
477
478static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
479{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000480 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
484{
Michael Chan68929142005-08-09 20:17:14 -0700485 unsigned long flags;
486
487 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700488 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
489 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700490 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700491}
492
493static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
494{
495 writel(val, tp->regs + off);
496 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Michael Chan68929142005-08-09 20:17:14 -0700499static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
500{
501 unsigned long flags;
502 u32 val;
503
504 spin_lock_irqsave(&tp->indirect_lock, flags);
505 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
506 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
507 spin_unlock_irqrestore(&tp->indirect_lock, flags);
508 return val;
509}
510
511static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
512{
513 unsigned long flags;
514
515 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
516 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
517 TG3_64BIT_REG_LOW, val);
518 return;
519 }
Matt Carlson66711e662009-11-13 13:03:49 +0000520 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700521 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
522 TG3_64BIT_REG_LOW, val);
523 return;
524 }
525
526 spin_lock_irqsave(&tp->indirect_lock, flags);
527 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
528 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
529 spin_unlock_irqrestore(&tp->indirect_lock, flags);
530
531 /* In indirect mode when disabling interrupts, we also need
532 * to clear the interrupt bit in the GRC local ctrl register.
533 */
534 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
535 (val == 0x1)) {
536 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
537 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
538 }
539}
540
541static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
542{
543 unsigned long flags;
544 u32 val;
545
546 spin_lock_irqsave(&tp->indirect_lock, flags);
547 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
548 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
549 spin_unlock_irqrestore(&tp->indirect_lock, flags);
550 return val;
551}
552
Michael Chanb401e9e2005-12-19 16:27:04 -0800553/* usec_wait specifies the wait time in usec when writing to certain registers
554 * where it is unsafe to read back the register without some delay.
555 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
556 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
557 */
558static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Joe Perches63c3a662011-04-26 08:12:10 +0000560 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800561 /* Non-posted methods */
562 tp->write32(tp, off, val);
563 else {
564 /* Posted method */
565 tg3_write32(tp, off, val);
566 if (usec_wait)
567 udelay(usec_wait);
568 tp->read32(tp, off);
569 }
570 /* Wait again after the read for the posted method to guarantee that
571 * the wait time is met.
572 */
573 if (usec_wait)
574 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Michael Chan09ee9292005-08-09 20:17:00 -0700577static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
578{
579 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000580 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
581 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
582 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700583 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700584}
585
Michael Chan20094932005-08-09 20:16:32 -0700586static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 void __iomem *mbox = tp->regs + off;
589 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000590 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000592 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
593 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 readl(mbox);
595}
596
Michael Chanb5d37722006-09-27 16:06:21 -0700597static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
598{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000599 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700600}
601
602static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
603{
604 writel(val, tp->regs + off + GRCMBOX_BASE);
605}
606
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000607#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700608#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000609#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
610#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
611#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700612
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000613#define tw32(reg, val) tp->write32(tp, reg, val)
614#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
615#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
616#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
619{
Michael Chan68929142005-08-09 20:17:14 -0700620 unsigned long flags;
621
Joe Perches41535772013-02-16 11:20:04 +0000622 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700623 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
624 return;
625
Michael Chan68929142005-08-09 20:17:14 -0700626 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000627 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700628 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
629 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Michael Chanbbadf502006-04-06 21:46:34 -0700631 /* Always leave this as zero. */
632 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
633 } else {
634 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
635 tw32_f(TG3PCI_MEM_WIN_DATA, val);
636
637 /* Always leave this as zero. */
638 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
639 }
Michael Chan68929142005-08-09 20:17:14 -0700640 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
644{
Michael Chan68929142005-08-09 20:17:14 -0700645 unsigned long flags;
646
Joe Perches41535772013-02-16 11:20:04 +0000647 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700648 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
649 *val = 0;
650 return;
651 }
652
Michael Chan68929142005-08-09 20:17:14 -0700653 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000654 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700655 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
656 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Michael Chanbbadf502006-04-06 21:46:34 -0700658 /* Always leave this as zero. */
659 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
660 } else {
661 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
662 *val = tr32(TG3PCI_MEM_WIN_DATA);
663
664 /* Always leave this as zero. */
665 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
666 }
Michael Chan68929142005-08-09 20:17:14 -0700667 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Matt Carlson0d3031d2007-10-10 18:02:43 -0700670static void tg3_ape_lock_init(struct tg3 *tp)
671{
672 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000673 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674
Joe Perches41535772013-02-16 11:20:04 +0000675 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000676 regbase = TG3_APE_LOCK_GRANT;
677 else
678 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700679
680 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000681 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
682 switch (i) {
683 case TG3_APE_LOCK_PHY0:
684 case TG3_APE_LOCK_PHY1:
685 case TG3_APE_LOCK_PHY2:
686 case TG3_APE_LOCK_PHY3:
687 bit = APE_LOCK_GRANT_DRIVER;
688 break;
689 default:
690 if (!tp->pci_fn)
691 bit = APE_LOCK_GRANT_DRIVER;
692 else
693 bit = 1 << tp->pci_fn;
694 }
695 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 }
697
Matt Carlson0d3031d2007-10-10 18:02:43 -0700698}
699
700static int tg3_ape_lock(struct tg3 *tp, int locknum)
701{
702 int i, off;
703 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000704 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700705
Joe Perches63c3a662011-04-26 08:12:10 +0000706 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707 return 0;
708
709 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000710 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000711 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000713 case TG3_APE_LOCK_GRC:
714 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000715 if (!tp->pci_fn)
716 bit = APE_LOCK_REQ_DRIVER;
717 else
718 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000719 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000720 case TG3_APE_LOCK_PHY0:
721 case TG3_APE_LOCK_PHY1:
722 case TG3_APE_LOCK_PHY2:
723 case TG3_APE_LOCK_PHY3:
724 bit = APE_LOCK_REQ_DRIVER;
725 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000726 default:
727 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700728 }
729
Joe Perches41535772013-02-16 11:20:04 +0000730 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000731 req = TG3_APE_LOCK_REQ;
732 gnt = TG3_APE_LOCK_GRANT;
733 } else {
734 req = TG3_APE_PER_LOCK_REQ;
735 gnt = TG3_APE_PER_LOCK_GRANT;
736 }
737
Matt Carlson0d3031d2007-10-10 18:02:43 -0700738 off = 4 * locknum;
739
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000740 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700741
742 /* Wait for up to 1 millisecond to acquire lock. */
743 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000744 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000745 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746 break;
747 udelay(10);
748 }
749
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000750 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700751 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000752 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700753 ret = -EBUSY;
754 }
755
756 return ret;
757}
758
759static void tg3_ape_unlock(struct tg3 *tp, int locknum)
760{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000761 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700762
Joe Perches63c3a662011-04-26 08:12:10 +0000763 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700764 return;
765
766 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000767 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000768 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000769 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000770 case TG3_APE_LOCK_GRC:
771 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000772 if (!tp->pci_fn)
773 bit = APE_LOCK_GRANT_DRIVER;
774 else
775 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000776 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000777 case TG3_APE_LOCK_PHY0:
778 case TG3_APE_LOCK_PHY1:
779 case TG3_APE_LOCK_PHY2:
780 case TG3_APE_LOCK_PHY3:
781 bit = APE_LOCK_GRANT_DRIVER;
782 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000783 default:
784 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700785 }
786
Joe Perches41535772013-02-16 11:20:04 +0000787 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000788 gnt = TG3_APE_LOCK_GRANT;
789 else
790 gnt = TG3_APE_PER_LOCK_GRANT;
791
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000792 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700793}
794
Matt Carlsonb65a3722012-07-16 16:24:00 +0000795static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000796{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000797 u32 apedata;
798
Matt Carlsonb65a3722012-07-16 16:24:00 +0000799 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000800 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000801 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000802
803 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000804 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
805 break;
806
Matt Carlsonb65a3722012-07-16 16:24:00 +0000807 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
808
809 udelay(10);
810 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000811 }
812
Matt Carlsonb65a3722012-07-16 16:24:00 +0000813 return timeout_us ? 0 : -EBUSY;
814}
815
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000816static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
817{
818 u32 i, apedata;
819
820 for (i = 0; i < timeout_us / 10; i++) {
821 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
822
823 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
824 break;
825
826 udelay(10);
827 }
828
829 return i == timeout_us / 10;
830}
831
Michael Chan86449942012-10-02 20:31:14 -0700832static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
833 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000834{
835 int err;
836 u32 i, bufoff, msgoff, maxlen, apedata;
837
838 if (!tg3_flag(tp, APE_HAS_NCSI))
839 return 0;
840
841 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
842 if (apedata != APE_SEG_SIG_MAGIC)
843 return -ENODEV;
844
845 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
846 if (!(apedata & APE_FW_STATUS_READY))
847 return -EAGAIN;
848
849 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
850 TG3_APE_SHMEM_BASE;
851 msgoff = bufoff + 2 * sizeof(u32);
852 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
853
854 while (len) {
855 u32 length;
856
857 /* Cap xfer sizes to scratchpad limits. */
858 length = (len > maxlen) ? maxlen : len;
859 len -= length;
860
861 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
862 if (!(apedata & APE_FW_STATUS_READY))
863 return -EAGAIN;
864
865 /* Wait for up to 1 msec for APE to service previous event. */
866 err = tg3_ape_event_lock(tp, 1000);
867 if (err)
868 return err;
869
870 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
871 APE_EVENT_STATUS_SCRTCHPD_READ |
872 APE_EVENT_STATUS_EVENT_PENDING;
873 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
874
875 tg3_ape_write32(tp, bufoff, base_off);
876 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
877
878 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
879 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
880
881 base_off += length;
882
883 if (tg3_ape_wait_for_event(tp, 30000))
884 return -EAGAIN;
885
886 for (i = 0; length; i += 4, length -= 4) {
887 u32 val = tg3_ape_read32(tp, msgoff + i);
888 memcpy(data, &val, sizeof(u32));
889 data++;
890 }
891 }
892
893 return 0;
894}
895
Matt Carlsonb65a3722012-07-16 16:24:00 +0000896static int tg3_ape_send_event(struct tg3 *tp, u32 event)
897{
898 int err;
899 u32 apedata;
900
901 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
902 if (apedata != APE_SEG_SIG_MAGIC)
903 return -EAGAIN;
904
905 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
906 if (!(apedata & APE_FW_STATUS_READY))
907 return -EAGAIN;
908
909 /* Wait for up to 1 millisecond for APE to service previous event. */
910 err = tg3_ape_event_lock(tp, 1000);
911 if (err)
912 return err;
913
914 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
915 event | APE_EVENT_STATUS_EVENT_PENDING);
916
917 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
918 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
919
920 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000921}
922
923static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
924{
925 u32 event;
926 u32 apedata;
927
928 if (!tg3_flag(tp, ENABLE_APE))
929 return;
930
931 switch (kind) {
932 case RESET_KIND_INIT:
933 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
934 APE_HOST_SEG_SIG_MAGIC);
935 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
936 APE_HOST_SEG_LEN_MAGIC);
937 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
938 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
939 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
940 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
941 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
942 APE_HOST_BEHAV_NO_PHYLOCK);
943 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
944 TG3_APE_HOST_DRVR_STATE_START);
945
946 event = APE_EVENT_STATUS_STATE_START;
947 break;
948 case RESET_KIND_SHUTDOWN:
949 /* With the interface we are currently using,
950 * APE does not track driver state. Wiping
951 * out the HOST SEGMENT SIGNATURE forces
952 * the APE to assume OS absent status.
953 */
954 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
955
956 if (device_may_wakeup(&tp->pdev->dev) &&
957 tg3_flag(tp, WOL_ENABLE)) {
958 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
959 TG3_APE_HOST_WOL_SPEED_AUTO);
960 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
961 } else
962 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
963
964 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
965
966 event = APE_EVENT_STATUS_STATE_UNLOAD;
967 break;
968 case RESET_KIND_SUSPEND:
969 event = APE_EVENT_STATUS_STATE_SUSPEND;
970 break;
971 default:
972 return;
973 }
974
975 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
976
977 tg3_ape_send_event(tp, event);
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static void tg3_disable_ints(struct tg3 *tp)
981{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000982 int i;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 tw32(TG3PCI_MISC_HOST_CTRL,
985 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000986 for (i = 0; i < tp->irq_max; i++)
987 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990static void tg3_enable_ints(struct tg3 *tp)
991{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000992 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000993
Michael Chanbbe832c2005-06-24 20:20:04 -0700994 tp->irq_sync = 0;
995 wmb();
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 tw32(TG3PCI_MISC_HOST_CTRL,
998 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000999
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001000 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001 for (i = 0; i < tp->irq_cnt; i++) {
1002 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001003
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001004 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001005 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001006 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1007
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001008 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001010
1011 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001012 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001013 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1014 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1015 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001016 tw32(HOSTCC_MODE, tp->coal_now);
1017
1018 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Matt Carlson17375d22009-08-28 14:02:18 +00001021static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001022{
Matt Carlson17375d22009-08-28 14:02:18 +00001023 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001024 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001025 unsigned int work_exists = 0;
1026
1027 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001028 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001029 if (sblk->status & SD_STATUS_LINK_CHG)
1030 work_exists = 1;
1031 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001032
1033 /* check for TX work to do */
1034 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1035 work_exists = 1;
1036
1037 /* check for RX work to do */
1038 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001039 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001040 work_exists = 1;
1041
1042 return work_exists;
1043}
1044
Matt Carlson17375d22009-08-28 14:02:18 +00001045/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001046 * similar to tg3_enable_ints, but it accurately determines whether there
1047 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001048 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Matt Carlson17375d22009-08-28 14:02:18 +00001050static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Matt Carlson17375d22009-08-28 14:02:18 +00001052 struct tg3 *tp = tnapi->tp;
1053
Matt Carlson898a56f2009-08-28 14:02:40 +00001054 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 mmiowb();
1056
David S. Millerfac9b832005-05-18 22:46:34 -07001057 /* When doing tagged status, this work check is unnecessary.
1058 * The last_tag we write above tells the chip which piece of
1059 * work we've completed.
1060 */
Joe Perches63c3a662011-04-26 08:12:10 +00001061 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001062 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001063 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066static void tg3_switch_clocks(struct tg3 *tp)
1067{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001068 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 u32 orig_clock_ctrl;
1070
Joe Perches63c3a662011-04-26 08:12:10 +00001071 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001072 return;
1073
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001074 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 orig_clock_ctrl = clock_ctrl;
1077 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1078 CLOCK_CTRL_CLKRUN_OENABLE |
1079 0x1f);
1080 tp->pci_clock_ctrl = clock_ctrl;
1081
Joe Perches63c3a662011-04-26 08:12:10 +00001082 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001084 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1085 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001088 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1089 clock_ctrl |
1090 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1091 40);
1092 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1093 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1094 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001096 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099#define PHY_BUSY_LOOPS 5000
1100
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001101static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1102 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 u32 frame_val;
1105 unsigned int loops;
1106 int ret;
1107
1108 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1109 tw32_f(MAC_MI_MODE,
1110 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1111 udelay(80);
1112 }
1113
Michael Chan8151ad52012-07-29 19:15:41 +00001114 tg3_ape_lock(tp, tp->phy_ape_lock);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *val = 0x0;
1117
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001118 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 MI_COM_PHY_ADDR_MASK);
1120 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1121 MI_COM_REG_ADDR_MASK);
1122 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 tw32_f(MAC_MI_COM, frame_val);
1125
1126 loops = PHY_BUSY_LOOPS;
1127 while (loops != 0) {
1128 udelay(10);
1129 frame_val = tr32(MAC_MI_COM);
1130
1131 if ((frame_val & MI_COM_BUSY) == 0) {
1132 udelay(5);
1133 frame_val = tr32(MAC_MI_COM);
1134 break;
1135 }
1136 loops -= 1;
1137 }
1138
1139 ret = -EBUSY;
1140 if (loops != 0) {
1141 *val = frame_val & MI_COM_DATA_MASK;
1142 ret = 0;
1143 }
1144
1145 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1146 tw32_f(MAC_MI_MODE, tp->mi_mode);
1147 udelay(80);
1148 }
1149
Michael Chan8151ad52012-07-29 19:15:41 +00001150 tg3_ape_unlock(tp, tp->phy_ape_lock);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return ret;
1153}
1154
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001155static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1156{
1157 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1158}
1159
1160static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1161 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 u32 frame_val;
1164 unsigned int loops;
1165 int ret;
1166
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001167 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001168 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001169 return 0;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1172 tw32_f(MAC_MI_MODE,
1173 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1174 udelay(80);
1175 }
1176
Michael Chan8151ad52012-07-29 19:15:41 +00001177 tg3_ape_lock(tp, tp->phy_ape_lock);
1178
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001179 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 MI_COM_PHY_ADDR_MASK);
1181 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1182 MI_COM_REG_ADDR_MASK);
1183 frame_val |= (val & MI_COM_DATA_MASK);
1184 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 tw32_f(MAC_MI_COM, frame_val);
1187
1188 loops = PHY_BUSY_LOOPS;
1189 while (loops != 0) {
1190 udelay(10);
1191 frame_val = tr32(MAC_MI_COM);
1192 if ((frame_val & MI_COM_BUSY) == 0) {
1193 udelay(5);
1194 frame_val = tr32(MAC_MI_COM);
1195 break;
1196 }
1197 loops -= 1;
1198 }
1199
1200 ret = -EBUSY;
1201 if (loops != 0)
1202 ret = 0;
1203
1204 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1205 tw32_f(MAC_MI_MODE, tp->mi_mode);
1206 udelay(80);
1207 }
1208
Michael Chan8151ad52012-07-29 19:15:41 +00001209 tg3_ape_unlock(tp, tp->phy_ape_lock);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return ret;
1212}
1213
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001214static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1215{
1216 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1217}
1218
Matt Carlsonb0988c12011-04-20 07:57:39 +00001219static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1220{
1221 int err;
1222
1223 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1224 if (err)
1225 goto done;
1226
1227 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1228 if (err)
1229 goto done;
1230
1231 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1232 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1233 if (err)
1234 goto done;
1235
1236 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1237
1238done:
1239 return err;
1240}
1241
1242static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1243{
1244 int err;
1245
1246 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1247 if (err)
1248 goto done;
1249
1250 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1251 if (err)
1252 goto done;
1253
1254 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1255 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1256 if (err)
1257 goto done;
1258
1259 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1260
1261done:
1262 return err;
1263}
1264
1265static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1266{
1267 int err;
1268
1269 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1270 if (!err)
1271 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1272
1273 return err;
1274}
1275
1276static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1277{
1278 int err;
1279
1280 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1281 if (!err)
1282 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1283
1284 return err;
1285}
1286
Matt Carlson15ee95c2011-04-20 07:57:40 +00001287static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1288{
1289 int err;
1290
1291 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1292 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1293 MII_TG3_AUXCTL_SHDWSEL_MISC);
1294 if (!err)
1295 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1296
1297 return err;
1298}
1299
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001300static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1301{
1302 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1303 set |= MII_TG3_AUXCTL_MISC_WREN;
1304
1305 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1306}
1307
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001308static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1309{
1310 u32 val;
1311 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001312
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001313 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1314
1315 if (err)
1316 return err;
1317 if (enable)
1318
1319 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1320 else
1321 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1322
1323 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1324 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1325
1326 return err;
1327}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001328
Matt Carlson95e28692008-05-25 23:44:14 -07001329static int tg3_bmcr_reset(struct tg3 *tp)
1330{
1331 u32 phy_control;
1332 int limit, err;
1333
1334 /* OK, reset it, and poll the BMCR_RESET bit until it
1335 * clears or we time out.
1336 */
1337 phy_control = BMCR_RESET;
1338 err = tg3_writephy(tp, MII_BMCR, phy_control);
1339 if (err != 0)
1340 return -EBUSY;
1341
1342 limit = 5000;
1343 while (limit--) {
1344 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1345 if (err != 0)
1346 return -EBUSY;
1347
1348 if ((phy_control & BMCR_RESET) == 0) {
1349 udelay(40);
1350 break;
1351 }
1352 udelay(10);
1353 }
Roel Kluind4675b52009-02-12 16:33:27 -08001354 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001355 return -EBUSY;
1356
1357 return 0;
1358}
1359
Matt Carlson158d7ab2008-05-29 01:37:54 -07001360static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1361{
Francois Romieu3d165432009-01-19 16:56:50 -08001362 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001363 u32 val;
1364
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001365 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001366
1367 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001368 val = -EIO;
1369
1370 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001371
1372 return val;
1373}
1374
1375static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1376{
Francois Romieu3d165432009-01-19 16:56:50 -08001377 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001378 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001379
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001380 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001381
1382 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001383 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001384
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001385 spin_unlock_bh(&tp->lock);
1386
1387 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001388}
1389
1390static int tg3_mdio_reset(struct mii_bus *bp)
1391{
1392 return 0;
1393}
1394
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001395static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001396{
1397 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001398 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001399
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001400 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001401 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001402 case PHY_ID_BCM50610:
1403 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001404 val = MAC_PHYCFG2_50610_LED_MODES;
1405 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001406 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001407 val = MAC_PHYCFG2_AC131_LED_MODES;
1408 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001409 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001410 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1411 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001412 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001413 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1414 break;
1415 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001416 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001417 }
1418
1419 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1420 tw32(MAC_PHYCFG2, val);
1421
1422 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001423 val &= ~(MAC_PHYCFG1_RGMII_INT |
1424 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1425 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001426 tw32(MAC_PHYCFG1, val);
1427
1428 return;
1429 }
1430
Joe Perches63c3a662011-04-26 08:12:10 +00001431 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001432 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1433 MAC_PHYCFG2_FMODE_MASK_MASK |
1434 MAC_PHYCFG2_GMODE_MASK_MASK |
1435 MAC_PHYCFG2_ACT_MASK_MASK |
1436 MAC_PHYCFG2_QUAL_MASK_MASK |
1437 MAC_PHYCFG2_INBAND_ENABLE;
1438
1439 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001440
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001441 val = tr32(MAC_PHYCFG1);
1442 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1443 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001444 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1445 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001446 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001447 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001448 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1449 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001450 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1451 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1452 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001453
Matt Carlsona9daf362008-05-25 23:49:44 -07001454 val = tr32(MAC_EXT_RGMII_MODE);
1455 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1456 MAC_RGMII_MODE_RX_QUALITY |
1457 MAC_RGMII_MODE_RX_ACTIVITY |
1458 MAC_RGMII_MODE_RX_ENG_DET |
1459 MAC_RGMII_MODE_TX_ENABLE |
1460 MAC_RGMII_MODE_TX_LOWPWR |
1461 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001462 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1463 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001464 val |= MAC_RGMII_MODE_RX_INT_B |
1465 MAC_RGMII_MODE_RX_QUALITY |
1466 MAC_RGMII_MODE_RX_ACTIVITY |
1467 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001468 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001469 val |= MAC_RGMII_MODE_TX_ENABLE |
1470 MAC_RGMII_MODE_TX_LOWPWR |
1471 MAC_RGMII_MODE_TX_RESET;
1472 }
1473 tw32(MAC_EXT_RGMII_MODE, val);
1474}
1475
Matt Carlson158d7ab2008-05-29 01:37:54 -07001476static void tg3_mdio_start(struct tg3 *tp)
1477{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001478 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1479 tw32_f(MAC_MI_MODE, tp->mi_mode);
1480 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001481
Joe Perches63c3a662011-04-26 08:12:10 +00001482 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001483 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001484 tg3_mdio_config_5785(tp);
1485}
1486
1487static int tg3_mdio_init(struct tg3 *tp)
1488{
1489 int i;
1490 u32 reg;
1491 struct phy_device *phydev;
1492
Joe Perches63c3a662011-04-26 08:12:10 +00001493 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001494 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001495
Matt Carlson69f11c92011-07-13 09:27:30 +00001496 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001497
Joe Perches41535772013-02-16 11:20:04 +00001498 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001499 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1500 else
1501 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1502 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001503 if (is_serdes)
1504 tp->phy_addr += 7;
1505 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001506 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001507
Matt Carlson158d7ab2008-05-29 01:37:54 -07001508 tg3_mdio_start(tp);
1509
Joe Perches63c3a662011-04-26 08:12:10 +00001510 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001511 return 0;
1512
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001513 tp->mdio_bus = mdiobus_alloc();
1514 if (tp->mdio_bus == NULL)
1515 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001516
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001517 tp->mdio_bus->name = "tg3 mdio bus";
1518 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001519 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001520 tp->mdio_bus->priv = tp;
1521 tp->mdio_bus->parent = &tp->pdev->dev;
1522 tp->mdio_bus->read = &tg3_mdio_read;
1523 tp->mdio_bus->write = &tg3_mdio_write;
1524 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001525 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001526 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001527
1528 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001529 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001530
1531 /* The bus registration will look for all the PHYs on the mdio bus.
1532 * Unfortunately, it does not ensure the PHY is powered up before
1533 * accessing the PHY ID registers. A chip reset is the
1534 * quickest way to bring the device back to an operational state..
1535 */
1536 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1537 tg3_bmcr_reset(tp);
1538
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001539 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001540 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001541 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001542 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001543 return i;
1544 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001545
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001546 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001547
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001548 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001549 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001550 mdiobus_unregister(tp->mdio_bus);
1551 mdiobus_free(tp->mdio_bus);
1552 return -ENODEV;
1553 }
1554
1555 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001556 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001557 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001558 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001559 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001560 case PHY_ID_BCM50610:
1561 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001562 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001563 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001564 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001565 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001566 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001567 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001568 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001569 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001570 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001571 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001572 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001573 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001574 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001575 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001576 case PHY_ID_RTL8201E:
1577 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001578 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001579 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001580 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001581 break;
1582 }
1583
Joe Perches63c3a662011-04-26 08:12:10 +00001584 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001585
Joe Perches41535772013-02-16 11:20:04 +00001586 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001587 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001588
1589 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001590}
1591
1592static void tg3_mdio_fini(struct tg3 *tp)
1593{
Joe Perches63c3a662011-04-26 08:12:10 +00001594 if (tg3_flag(tp, MDIOBUS_INITED)) {
1595 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001596 mdiobus_unregister(tp->mdio_bus);
1597 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001598 }
1599}
1600
Matt Carlson95e28692008-05-25 23:44:14 -07001601/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001602static inline void tg3_generate_fw_event(struct tg3 *tp)
1603{
1604 u32 val;
1605
1606 val = tr32(GRC_RX_CPU_EVENT);
1607 val |= GRC_RX_CPU_DRIVER_EVENT;
1608 tw32_f(GRC_RX_CPU_EVENT, val);
1609
1610 tp->last_event_jiffies = jiffies;
1611}
1612
1613#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1614
1615/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001616static void tg3_wait_for_event_ack(struct tg3 *tp)
1617{
1618 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001619 unsigned int delay_cnt;
1620 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001621
Matt Carlson4ba526c2008-08-15 14:10:04 -07001622 /* If enough time has passed, no wait is necessary. */
1623 time_remain = (long)(tp->last_event_jiffies + 1 +
1624 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1625 (long)jiffies;
1626 if (time_remain < 0)
1627 return;
1628
1629 /* Check if we can shorten the wait time. */
1630 delay_cnt = jiffies_to_usecs(time_remain);
1631 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1632 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1633 delay_cnt = (delay_cnt >> 3) + 1;
1634
1635 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001636 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1637 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001638 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001639 }
1640}
1641
1642/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001643static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001644{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001645 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001646
1647 val = 0;
1648 if (!tg3_readphy(tp, MII_BMCR, &reg))
1649 val = reg << 16;
1650 if (!tg3_readphy(tp, MII_BMSR, &reg))
1651 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001652 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001653
1654 val = 0;
1655 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1656 val = reg << 16;
1657 if (!tg3_readphy(tp, MII_LPA, &reg))
1658 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001659 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001660
1661 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001662 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001663 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1664 val = reg << 16;
1665 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1666 val |= (reg & 0xffff);
1667 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001668 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001669
1670 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1671 val = reg << 16;
1672 else
1673 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001674 *data++ = val;
1675}
1676
1677/* tp->lock is held. */
1678static void tg3_ump_link_report(struct tg3 *tp)
1679{
1680 u32 data[4];
1681
1682 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1683 return;
1684
1685 tg3_phy_gather_ump_data(tp, data);
1686
1687 tg3_wait_for_event_ack(tp);
1688
1689 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1690 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1691 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1692 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1693 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1694 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001695
Matt Carlson4ba526c2008-08-15 14:10:04 -07001696 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001697}
1698
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001699/* tp->lock is held. */
1700static void tg3_stop_fw(struct tg3 *tp)
1701{
1702 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1703 /* Wait for RX cpu to ACK the previous event. */
1704 tg3_wait_for_event_ack(tp);
1705
1706 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1707
1708 tg3_generate_fw_event(tp);
1709
1710 /* Wait for RX cpu to ACK this event. */
1711 tg3_wait_for_event_ack(tp);
1712 }
1713}
1714
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001715/* tp->lock is held. */
1716static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1717{
1718 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1719 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1720
1721 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1722 switch (kind) {
1723 case RESET_KIND_INIT:
1724 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1725 DRV_STATE_START);
1726 break;
1727
1728 case RESET_KIND_SHUTDOWN:
1729 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1730 DRV_STATE_UNLOAD);
1731 break;
1732
1733 case RESET_KIND_SUSPEND:
1734 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1735 DRV_STATE_SUSPEND);
1736 break;
1737
1738 default:
1739 break;
1740 }
1741 }
1742
1743 if (kind == RESET_KIND_INIT ||
1744 kind == RESET_KIND_SUSPEND)
1745 tg3_ape_driver_state_change(tp, kind);
1746}
1747
1748/* tp->lock is held. */
1749static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1750{
1751 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1752 switch (kind) {
1753 case RESET_KIND_INIT:
1754 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1755 DRV_STATE_START_DONE);
1756 break;
1757
1758 case RESET_KIND_SHUTDOWN:
1759 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1760 DRV_STATE_UNLOAD_DONE);
1761 break;
1762
1763 default:
1764 break;
1765 }
1766 }
1767
1768 if (kind == RESET_KIND_SHUTDOWN)
1769 tg3_ape_driver_state_change(tp, kind);
1770}
1771
1772/* tp->lock is held. */
1773static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1774{
1775 if (tg3_flag(tp, ENABLE_ASF)) {
1776 switch (kind) {
1777 case RESET_KIND_INIT:
1778 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1779 DRV_STATE_START);
1780 break;
1781
1782 case RESET_KIND_SHUTDOWN:
1783 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1784 DRV_STATE_UNLOAD);
1785 break;
1786
1787 case RESET_KIND_SUSPEND:
1788 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1789 DRV_STATE_SUSPEND);
1790 break;
1791
1792 default:
1793 break;
1794 }
1795 }
1796}
1797
1798static int tg3_poll_fw(struct tg3 *tp)
1799{
1800 int i;
1801 u32 val;
1802
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001803 if (tg3_flag(tp, IS_SSB_CORE)) {
1804 /* We don't use firmware. */
1805 return 0;
1806 }
1807
Joe Perches41535772013-02-16 11:20:04 +00001808 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001809 /* Wait up to 20ms for init done. */
1810 for (i = 0; i < 200; i++) {
1811 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1812 return 0;
1813 udelay(100);
1814 }
1815 return -ENODEV;
1816 }
1817
1818 /* Wait for firmware initialization to complete. */
1819 for (i = 0; i < 100000; i++) {
1820 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1821 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1822 break;
1823 udelay(10);
1824 }
1825
1826 /* Chip might not be fitted with firmware. Some Sun onboard
1827 * parts are configured like that. So don't signal the timeout
1828 * of the above loop as an error, but do report the lack of
1829 * running firmware once.
1830 */
1831 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1832 tg3_flag_set(tp, NO_FWARE_REPORTED);
1833
1834 netdev_info(tp->dev, "No firmware running\n");
1835 }
1836
Joe Perches41535772013-02-16 11:20:04 +00001837 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001838 /* The 57765 A0 needs a little more
1839 * time to do some important work.
1840 */
1841 mdelay(10);
1842 }
1843
1844 return 0;
1845}
1846
Matt Carlson95e28692008-05-25 23:44:14 -07001847static void tg3_link_report(struct tg3 *tp)
1848{
1849 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001850 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001851 tg3_ump_link_report(tp);
1852 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001853 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1854 (tp->link_config.active_speed == SPEED_1000 ?
1855 1000 :
1856 (tp->link_config.active_speed == SPEED_100 ?
1857 100 : 10)),
1858 (tp->link_config.active_duplex == DUPLEX_FULL ?
1859 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001860
Joe Perches05dbe002010-02-17 19:44:19 +00001861 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1862 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1863 "on" : "off",
1864 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1865 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001866
1867 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1868 netdev_info(tp->dev, "EEE is %s\n",
1869 tp->setlpicnt ? "enabled" : "disabled");
1870
Matt Carlson95e28692008-05-25 23:44:14 -07001871 tg3_ump_link_report(tp);
1872 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001873
1874 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001875}
1876
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001877static u32 tg3_decode_flowctrl_1000T(u32 adv)
1878{
1879 u32 flowctrl = 0;
1880
1881 if (adv & ADVERTISE_PAUSE_CAP) {
1882 flowctrl |= FLOW_CTRL_RX;
1883 if (!(adv & ADVERTISE_PAUSE_ASYM))
1884 flowctrl |= FLOW_CTRL_TX;
1885 } else if (adv & ADVERTISE_PAUSE_ASYM)
1886 flowctrl |= FLOW_CTRL_TX;
1887
1888 return flowctrl;
1889}
1890
Matt Carlson95e28692008-05-25 23:44:14 -07001891static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1892{
1893 u16 miireg;
1894
Steve Glendinninge18ce342008-12-16 02:00:00 -08001895 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001896 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001897 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001898 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001899 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001900 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1901 else
1902 miireg = 0;
1903
1904 return miireg;
1905}
1906
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001907static u32 tg3_decode_flowctrl_1000X(u32 adv)
1908{
1909 u32 flowctrl = 0;
1910
1911 if (adv & ADVERTISE_1000XPAUSE) {
1912 flowctrl |= FLOW_CTRL_RX;
1913 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1914 flowctrl |= FLOW_CTRL_TX;
1915 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1916 flowctrl |= FLOW_CTRL_TX;
1917
1918 return flowctrl;
1919}
1920
Matt Carlson95e28692008-05-25 23:44:14 -07001921static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1922{
1923 u8 cap = 0;
1924
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001925 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1926 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1927 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1928 if (lcladv & ADVERTISE_1000XPAUSE)
1929 cap = FLOW_CTRL_RX;
1930 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001931 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001932 }
1933
1934 return cap;
1935}
1936
Matt Carlsonf51f3562008-05-25 23:45:08 -07001937static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001938{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001939 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001940 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001941 u32 old_rx_mode = tp->rx_mode;
1942 u32 old_tx_mode = tp->tx_mode;
1943
Joe Perches63c3a662011-04-26 08:12:10 +00001944 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001945 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001946 else
1947 autoneg = tp->link_config.autoneg;
1948
Joe Perches63c3a662011-04-26 08:12:10 +00001949 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001950 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001951 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001952 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001953 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001954 } else
1955 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001956
Matt Carlsonf51f3562008-05-25 23:45:08 -07001957 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001958
Steve Glendinninge18ce342008-12-16 02:00:00 -08001959 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001960 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1961 else
1962 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1963
Matt Carlsonf51f3562008-05-25 23:45:08 -07001964 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001965 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001966
Steve Glendinninge18ce342008-12-16 02:00:00 -08001967 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001968 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1969 else
1970 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1971
Matt Carlsonf51f3562008-05-25 23:45:08 -07001972 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001973 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001974}
1975
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001976static void tg3_adjust_link(struct net_device *dev)
1977{
1978 u8 oldflowctrl, linkmesg = 0;
1979 u32 mac_mode, lcl_adv, rmt_adv;
1980 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001981 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001982
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001983 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001984
1985 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1986 MAC_MODE_HALF_DUPLEX);
1987
1988 oldflowctrl = tp->link_config.active_flowctrl;
1989
1990 if (phydev->link) {
1991 lcl_adv = 0;
1992 rmt_adv = 0;
1993
1994 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1995 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001996 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00001997 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001998 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001999 else
2000 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002001
2002 if (phydev->duplex == DUPLEX_HALF)
2003 mac_mode |= MAC_MODE_HALF_DUPLEX;
2004 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002005 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002006 tp->link_config.flowctrl);
2007
2008 if (phydev->pause)
2009 rmt_adv = LPA_PAUSE_CAP;
2010 if (phydev->asym_pause)
2011 rmt_adv |= LPA_PAUSE_ASYM;
2012 }
2013
2014 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2015 } else
2016 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2017
2018 if (mac_mode != tp->mac_mode) {
2019 tp->mac_mode = mac_mode;
2020 tw32_f(MAC_MODE, tp->mac_mode);
2021 udelay(40);
2022 }
2023
Joe Perches41535772013-02-16 11:20:04 +00002024 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002025 if (phydev->speed == SPEED_10)
2026 tw32(MAC_MI_STAT,
2027 MAC_MI_STAT_10MBPS_MODE |
2028 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2029 else
2030 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2031 }
2032
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002033 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2034 tw32(MAC_TX_LENGTHS,
2035 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2036 (6 << TX_LENGTHS_IPG_SHIFT) |
2037 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2038 else
2039 tw32(MAC_TX_LENGTHS,
2040 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2041 (6 << TX_LENGTHS_IPG_SHIFT) |
2042 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2043
Matt Carlson34655ad2012-02-22 12:35:18 +00002044 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002045 phydev->speed != tp->link_config.active_speed ||
2046 phydev->duplex != tp->link_config.active_duplex ||
2047 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002048 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002049
Matt Carlson34655ad2012-02-22 12:35:18 +00002050 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002051 tp->link_config.active_speed = phydev->speed;
2052 tp->link_config.active_duplex = phydev->duplex;
2053
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002054 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002055
2056 if (linkmesg)
2057 tg3_link_report(tp);
2058}
2059
2060static int tg3_phy_init(struct tg3 *tp)
2061{
2062 struct phy_device *phydev;
2063
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002064 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002065 return 0;
2066
2067 /* Bring the PHY back to a known state. */
2068 tg3_bmcr_reset(tp);
2069
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002070 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002071
2072 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002073 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2074 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002075 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002076 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002077 return PTR_ERR(phydev);
2078 }
2079
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002080 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002081 switch (phydev->interface) {
2082 case PHY_INTERFACE_MODE_GMII:
2083 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002084 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002085 phydev->supported &= (PHY_GBIT_FEATURES |
2086 SUPPORTED_Pause |
2087 SUPPORTED_Asym_Pause);
2088 break;
2089 }
2090 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002091 case PHY_INTERFACE_MODE_MII:
2092 phydev->supported &= (PHY_BASIC_FEATURES |
2093 SUPPORTED_Pause |
2094 SUPPORTED_Asym_Pause);
2095 break;
2096 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002097 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002098 return -EINVAL;
2099 }
2100
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002101 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002102
2103 phydev->advertising = phydev->supported;
2104
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002105 return 0;
2106}
2107
2108static void tg3_phy_start(struct tg3 *tp)
2109{
2110 struct phy_device *phydev;
2111
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002112 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002113 return;
2114
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002115 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002116
Matt Carlson80096062010-08-02 11:26:06 +00002117 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2118 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002119 phydev->speed = tp->link_config.speed;
2120 phydev->duplex = tp->link_config.duplex;
2121 phydev->autoneg = tp->link_config.autoneg;
2122 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002123 }
2124
2125 phy_start(phydev);
2126
2127 phy_start_aneg(phydev);
2128}
2129
2130static void tg3_phy_stop(struct tg3 *tp)
2131{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002132 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002133 return;
2134
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002135 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002136}
2137
2138static void tg3_phy_fini(struct tg3 *tp)
2139{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002140 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002141 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002142 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002143 }
2144}
2145
Matt Carlson941ec902011-08-19 13:58:23 +00002146static int tg3_phy_set_extloopbk(struct tg3 *tp)
2147{
2148 int err;
2149 u32 val;
2150
2151 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2152 return 0;
2153
2154 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2155 /* Cannot do read-modify-write on 5401 */
2156 err = tg3_phy_auxctl_write(tp,
2157 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2158 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2159 0x4c20);
2160 goto done;
2161 }
2162
2163 err = tg3_phy_auxctl_read(tp,
2164 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2165 if (err)
2166 return err;
2167
2168 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2169 err = tg3_phy_auxctl_write(tp,
2170 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2171
2172done:
2173 return err;
2174}
2175
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002176static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2177{
2178 u32 phytest;
2179
2180 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2181 u32 phy;
2182
2183 tg3_writephy(tp, MII_TG3_FET_TEST,
2184 phytest | MII_TG3_FET_SHADOW_EN);
2185 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2186 if (enable)
2187 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2188 else
2189 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2190 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2191 }
2192 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2193 }
2194}
2195
Matt Carlson6833c042008-11-21 17:18:59 -08002196static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2197{
2198 u32 reg;
2199
Joe Perches63c3a662011-04-26 08:12:10 +00002200 if (!tg3_flag(tp, 5705_PLUS) ||
2201 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002202 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002203 return;
2204
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002205 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002206 tg3_phy_fet_toggle_apd(tp, enable);
2207 return;
2208 }
2209
Matt Carlson6833c042008-11-21 17:18:59 -08002210 reg = MII_TG3_MISC_SHDW_WREN |
2211 MII_TG3_MISC_SHDW_SCR5_SEL |
2212 MII_TG3_MISC_SHDW_SCR5_LPED |
2213 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2214 MII_TG3_MISC_SHDW_SCR5_SDTL |
2215 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002216 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002217 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2218
2219 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2220
2221
2222 reg = MII_TG3_MISC_SHDW_WREN |
2223 MII_TG3_MISC_SHDW_APD_SEL |
2224 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2225 if (enable)
2226 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2227
2228 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2229}
2230
Joe Perches953c96e2013-04-09 10:18:14 +00002231static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002232{
2233 u32 phy;
2234
Joe Perches63c3a662011-04-26 08:12:10 +00002235 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002236 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002237 return;
2238
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002239 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002240 u32 ephy;
2241
Matt Carlson535ef6e2009-08-25 10:09:36 +00002242 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2243 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2244
2245 tg3_writephy(tp, MII_TG3_FET_TEST,
2246 ephy | MII_TG3_FET_SHADOW_EN);
2247 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002248 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002249 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002250 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002251 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2252 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002253 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002254 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002255 }
2256 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002257 int ret;
2258
2259 ret = tg3_phy_auxctl_read(tp,
2260 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2261 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002262 if (enable)
2263 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2264 else
2265 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002266 tg3_phy_auxctl_write(tp,
2267 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002268 }
2269 }
2270}
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272static void tg3_phy_set_wirespeed(struct tg3 *tp)
2273{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002274 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 u32 val;
2276
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002277 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return;
2279
Matt Carlson15ee95c2011-04-20 07:57:40 +00002280 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2281 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002282 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2283 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002286static void tg3_phy_apply_otp(struct tg3 *tp)
2287{
2288 u32 otp, phy;
2289
2290 if (!tp->phy_otp)
2291 return;
2292
2293 otp = tp->phy_otp;
2294
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002295 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002296 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002297
2298 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2299 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2300 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2301
2302 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2303 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2304 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2305
2306 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2307 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2308 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2309
2310 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2311 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2312
2313 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2314 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2315
2316 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2317 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2318 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2319
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002320 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002321}
2322
Nithin Sujir400dfba2013-05-18 06:26:53 +00002323static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2324{
2325 u32 val;
2326 struct ethtool_eee *dest = &tp->eee;
2327
2328 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2329 return;
2330
2331 if (eee)
2332 dest = eee;
2333
2334 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2335 return;
2336
2337 /* Pull eee_active */
2338 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2339 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2340 dest->eee_active = 1;
2341 } else
2342 dest->eee_active = 0;
2343
2344 /* Pull lp advertised settings */
2345 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2346 return;
2347 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2348
2349 /* Pull advertised and eee_enabled settings */
2350 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2351 return;
2352 dest->eee_enabled = !!val;
2353 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2354
2355 /* Pull tx_lpi_enabled */
2356 val = tr32(TG3_CPMU_EEE_MODE);
2357 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2358
2359 /* Pull lpi timer value */
2360 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2361}
2362
Joe Perches953c96e2013-04-09 10:18:14 +00002363static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002364{
2365 u32 val;
2366
2367 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2368 return;
2369
2370 tp->setlpicnt = 0;
2371
2372 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002373 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002374 tp->link_config.active_duplex == DUPLEX_FULL &&
2375 (tp->link_config.active_speed == SPEED_100 ||
2376 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002377 u32 eeectl;
2378
2379 if (tp->link_config.active_speed == SPEED_1000)
2380 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2381 else
2382 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2383
2384 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2385
Nithin Sujir400dfba2013-05-18 06:26:53 +00002386 tg3_eee_pull_config(tp, NULL);
2387 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002388 tp->setlpicnt = 2;
2389 }
2390
2391 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002392 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002393 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002394 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002395 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002396 }
2397
Matt Carlson52b02d02010-10-14 10:37:41 +00002398 val = tr32(TG3_CPMU_EEE_MODE);
2399 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2400 }
2401}
2402
Matt Carlsonb0c59432011-05-19 12:12:48 +00002403static void tg3_phy_eee_enable(struct tg3 *tp)
2404{
2405 u32 val;
2406
2407 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002408 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2409 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002410 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002411 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002412 val = MII_TG3_DSP_TAP26_ALNOKO |
2413 MII_TG3_DSP_TAP26_RMRXSTO;
2414 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002415 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002416 }
2417
2418 val = tr32(TG3_CPMU_EEE_MODE);
2419 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2420}
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422static int tg3_wait_macro_done(struct tg3 *tp)
2423{
2424 int limit = 100;
2425
2426 while (limit--) {
2427 u32 tmp32;
2428
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002429 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if ((tmp32 & 0x1000) == 0)
2431 break;
2432 }
2433 }
Roel Kluind4675b52009-02-12 16:33:27 -08002434 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return -EBUSY;
2436
2437 return 0;
2438}
2439
2440static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2441{
2442 static const u32 test_pat[4][6] = {
2443 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2444 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2445 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2446 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2447 };
2448 int chan;
2449
2450 for (chan = 0; chan < 4; chan++) {
2451 int i;
2452
2453 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2454 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002455 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 for (i = 0; i < 6; i++)
2458 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2459 test_pat[chan][i]);
2460
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002461 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (tg3_wait_macro_done(tp)) {
2463 *resetp = 1;
2464 return -EBUSY;
2465 }
2466
2467 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2468 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002469 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (tg3_wait_macro_done(tp)) {
2471 *resetp = 1;
2472 return -EBUSY;
2473 }
2474
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002475 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (tg3_wait_macro_done(tp)) {
2477 *resetp = 1;
2478 return -EBUSY;
2479 }
2480
2481 for (i = 0; i < 6; i += 2) {
2482 u32 low, high;
2483
2484 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2485 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2486 tg3_wait_macro_done(tp)) {
2487 *resetp = 1;
2488 return -EBUSY;
2489 }
2490 low &= 0x7fff;
2491 high &= 0x000f;
2492 if (low != test_pat[chan][i] ||
2493 high != test_pat[chan][i+1]) {
2494 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2495 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2496 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2497
2498 return -EBUSY;
2499 }
2500 }
2501 }
2502
2503 return 0;
2504}
2505
2506static int tg3_phy_reset_chanpat(struct tg3 *tp)
2507{
2508 int chan;
2509
2510 for (chan = 0; chan < 4; chan++) {
2511 int i;
2512
2513 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2514 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002515 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 for (i = 0; i < 6; i++)
2517 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002518 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (tg3_wait_macro_done(tp))
2520 return -EBUSY;
2521 }
2522
2523 return 0;
2524}
2525
2526static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2527{
2528 u32 reg32, phy9_orig;
2529 int retries, do_phy_reset, err;
2530
2531 retries = 10;
2532 do_phy_reset = 1;
2533 do {
2534 if (do_phy_reset) {
2535 err = tg3_bmcr_reset(tp);
2536 if (err)
2537 return err;
2538 do_phy_reset = 0;
2539 }
2540
2541 /* Disable transmitter and interrupt. */
2542 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2543 continue;
2544
2545 reg32 |= 0x3000;
2546 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2547
2548 /* Set full-duplex, 1000 mbps. */
2549 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002550 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002553 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 continue;
2555
Matt Carlson221c5632011-06-13 13:39:01 +00002556 tg3_writephy(tp, MII_CTRL1000,
2557 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002559 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002560 if (err)
2561 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002564 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2567 if (!err)
2568 break;
2569 } while (--retries);
2570
2571 err = tg3_phy_reset_chanpat(tp);
2572 if (err)
2573 return err;
2574
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002575 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002578 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002580 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Matt Carlson221c5632011-06-13 13:39:01 +00002582 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2585 reg32 &= ~0x3000;
2586 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2587 } else if (!err)
2588 err = -EBUSY;
2589
2590 return err;
2591}
2592
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002593static void tg3_carrier_off(struct tg3 *tp)
2594{
2595 netif_carrier_off(tp->dev);
2596 tp->link_up = false;
2597}
2598
Nithin Sujirce20f162013-04-09 08:48:04 +00002599static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2600{
2601 if (tg3_flag(tp, ENABLE_ASF))
2602 netdev_warn(tp->dev,
2603 "Management side-band traffic will be interrupted during phy settings change\n");
2604}
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606/* This will reset the tigon3 PHY if there is no valid
2607 * link unless the FORCE argument is non-zero.
2608 */
2609static int tg3_phy_reset(struct tg3 *tp)
2610{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002611 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 int err;
2613
Joe Perches41535772013-02-16 11:20:04 +00002614 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002615 val = tr32(GRC_MISC_CFG);
2616 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2617 udelay(40);
2618 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002619 err = tg3_readphy(tp, MII_BMSR, &val);
2620 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (err != 0)
2622 return -EBUSY;
2623
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002624 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002625 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002626 tg3_link_report(tp);
2627 }
2628
Joe Perches41535772013-02-16 11:20:04 +00002629 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2630 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2631 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 err = tg3_phy_reset_5703_4_5(tp);
2633 if (err)
2634 return err;
2635 goto out;
2636 }
2637
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002638 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002639 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2640 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002641 cpmuctrl = tr32(TG3_CPMU_CTRL);
2642 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2643 tw32(TG3_CPMU_CTRL,
2644 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2645 }
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 err = tg3_bmcr_reset(tp);
2648 if (err)
2649 return err;
2650
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002651 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002652 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2653 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002654
2655 tw32(TG3_CPMU_CTRL, cpmuctrl);
2656 }
2657
Joe Perches41535772013-02-16 11:20:04 +00002658 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2659 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002660 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2661 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2662 CPMU_LSPD_1000MB_MACCLK_12_5) {
2663 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2664 udelay(40);
2665 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2666 }
2667 }
2668
Joe Perches63c3a662011-04-26 08:12:10 +00002669 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002670 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002671 return 0;
2672
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002673 tg3_phy_apply_otp(tp);
2674
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002675 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002676 tg3_phy_toggle_apd(tp, true);
2677 else
2678 tg3_phy_toggle_apd(tp, false);
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002681 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002682 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002683 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2684 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002685 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002687
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002688 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002689 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2690 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002692
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002693 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002694 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002695 tg3_phydsp_write(tp, 0x000a, 0x310b);
2696 tg3_phydsp_write(tp, 0x201f, 0x9506);
2697 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002698 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002699 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002700 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002701 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002702 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2703 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2704 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2705 tg3_writephy(tp, MII_TG3_TEST1,
2706 MII_TG3_TEST1_TRIM_EN | 0x4);
2707 } else
2708 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2709
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002710 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002711 }
Michael Chanc424cb22006-04-29 18:56:34 -07002712 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /* Set Extended packet length bit (bit 14) on all chips that */
2715 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002716 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002718 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002719 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002721 err = tg3_phy_auxctl_read(tp,
2722 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2723 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002724 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2725 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
2727
2728 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2729 * jumbo frames transmission.
2730 */
Joe Perches63c3a662011-04-26 08:12:10 +00002731 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002732 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002733 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002734 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736
Joe Perches41535772013-02-16 11:20:04 +00002737 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002738 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002739 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002740 }
2741
Joe Perches41535772013-02-16 11:20:04 +00002742 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002743 tg3_phydsp_write(tp, 0xffb, 0x4000);
2744
Joe Perches953c96e2013-04-09 10:18:14 +00002745 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 tg3_phy_set_wirespeed(tp);
2747 return 0;
2748}
2749
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002750#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2751#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2752#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2753 TG3_GPIO_MSG_NEED_VAUX)
2754#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2755 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2756 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2757 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2758 (TG3_GPIO_MSG_DRVR_PRES << 12))
2759
2760#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2761 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2762 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2763 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2764 (TG3_GPIO_MSG_NEED_VAUX << 12))
2765
2766static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2767{
2768 u32 status, shift;
2769
Joe Perches41535772013-02-16 11:20:04 +00002770 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2771 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002772 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2773 else
2774 status = tr32(TG3_CPMU_DRV_STATUS);
2775
2776 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2777 status &= ~(TG3_GPIO_MSG_MASK << shift);
2778 status |= (newstat << shift);
2779
Joe Perches41535772013-02-16 11:20:04 +00002780 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2781 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002782 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2783 else
2784 tw32(TG3_CPMU_DRV_STATUS, status);
2785
2786 return status >> TG3_APE_GPIO_MSG_SHIFT;
2787}
2788
Matt Carlson520b2752011-06-13 13:39:02 +00002789static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2790{
2791 if (!tg3_flag(tp, IS_NIC))
2792 return 0;
2793
Joe Perches41535772013-02-16 11:20:04 +00002794 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2795 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2796 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002797 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2798 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002799
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002800 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2801
2802 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2803 TG3_GRC_LCLCTL_PWRSW_DELAY);
2804
2805 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2806 } else {
2807 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2808 TG3_GRC_LCLCTL_PWRSW_DELAY);
2809 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002810
Matt Carlson520b2752011-06-13 13:39:02 +00002811 return 0;
2812}
2813
2814static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2815{
2816 u32 grc_local_ctrl;
2817
2818 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002819 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2820 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002821 return;
2822
2823 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2824
2825 tw32_wait_f(GRC_LOCAL_CTRL,
2826 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2827 TG3_GRC_LCLCTL_PWRSW_DELAY);
2828
2829 tw32_wait_f(GRC_LOCAL_CTRL,
2830 grc_local_ctrl,
2831 TG3_GRC_LCLCTL_PWRSW_DELAY);
2832
2833 tw32_wait_f(GRC_LOCAL_CTRL,
2834 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2835 TG3_GRC_LCLCTL_PWRSW_DELAY);
2836}
2837
2838static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2839{
2840 if (!tg3_flag(tp, IS_NIC))
2841 return;
2842
Joe Perches41535772013-02-16 11:20:04 +00002843 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2844 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002845 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2846 (GRC_LCLCTRL_GPIO_OE0 |
2847 GRC_LCLCTRL_GPIO_OE1 |
2848 GRC_LCLCTRL_GPIO_OE2 |
2849 GRC_LCLCTRL_GPIO_OUTPUT0 |
2850 GRC_LCLCTRL_GPIO_OUTPUT1),
2851 TG3_GRC_LCLCTL_PWRSW_DELAY);
2852 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2853 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2854 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2855 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2856 GRC_LCLCTRL_GPIO_OE1 |
2857 GRC_LCLCTRL_GPIO_OE2 |
2858 GRC_LCLCTRL_GPIO_OUTPUT0 |
2859 GRC_LCLCTRL_GPIO_OUTPUT1 |
2860 tp->grc_local_ctrl;
2861 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2862 TG3_GRC_LCLCTL_PWRSW_DELAY);
2863
2864 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2865 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2866 TG3_GRC_LCLCTL_PWRSW_DELAY);
2867
2868 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2869 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2870 TG3_GRC_LCLCTL_PWRSW_DELAY);
2871 } else {
2872 u32 no_gpio2;
2873 u32 grc_local_ctrl = 0;
2874
2875 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002876 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002877 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2878 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2879 grc_local_ctrl,
2880 TG3_GRC_LCLCTL_PWRSW_DELAY);
2881 }
2882
2883 /* On 5753 and variants, GPIO2 cannot be used. */
2884 no_gpio2 = tp->nic_sram_data_cfg &
2885 NIC_SRAM_DATA_CFG_NO_GPIO2;
2886
2887 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2888 GRC_LCLCTRL_GPIO_OE1 |
2889 GRC_LCLCTRL_GPIO_OE2 |
2890 GRC_LCLCTRL_GPIO_OUTPUT1 |
2891 GRC_LCLCTRL_GPIO_OUTPUT2;
2892 if (no_gpio2) {
2893 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2894 GRC_LCLCTRL_GPIO_OUTPUT2);
2895 }
2896 tw32_wait_f(GRC_LOCAL_CTRL,
2897 tp->grc_local_ctrl | grc_local_ctrl,
2898 TG3_GRC_LCLCTL_PWRSW_DELAY);
2899
2900 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2901
2902 tw32_wait_f(GRC_LOCAL_CTRL,
2903 tp->grc_local_ctrl | grc_local_ctrl,
2904 TG3_GRC_LCLCTL_PWRSW_DELAY);
2905
2906 if (!no_gpio2) {
2907 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2908 tw32_wait_f(GRC_LOCAL_CTRL,
2909 tp->grc_local_ctrl | grc_local_ctrl,
2910 TG3_GRC_LCLCTL_PWRSW_DELAY);
2911 }
2912 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002913}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002914
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002915static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002916{
2917 u32 msg = 0;
2918
2919 /* Serialize power state transitions */
2920 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2921 return;
2922
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002923 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002924 msg = TG3_GPIO_MSG_NEED_VAUX;
2925
2926 msg = tg3_set_function_status(tp, msg);
2927
2928 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2929 goto done;
2930
2931 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2932 tg3_pwrsrc_switch_to_vaux(tp);
2933 else
2934 tg3_pwrsrc_die_with_vmain(tp);
2935
2936done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002937 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002938}
2939
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002940static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Matt Carlson683644b2011-03-09 16:58:23 +00002942 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Matt Carlson334355a2010-01-20 16:58:10 +00002944 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002945 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return;
2947
Joe Perches41535772013-02-16 11:20:04 +00002948 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2949 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2950 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002951 tg3_frob_aux_power_5717(tp, include_wol ?
2952 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002953 return;
2954 }
2955
2956 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002957 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002959 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002960
Michael Chanbc1c7562006-03-20 17:48:03 -08002961 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002962 if (dev_peer) {
2963 struct tg3 *tp_peer = netdev_priv(dev_peer);
2964
Joe Perches63c3a662011-04-26 08:12:10 +00002965 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002966 return;
2967
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002968 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002969 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002970 need_vaux = true;
2971 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002974 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2975 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002976 need_vaux = true;
2977
Matt Carlson520b2752011-06-13 13:39:02 +00002978 if (need_vaux)
2979 tg3_pwrsrc_switch_to_vaux(tp);
2980 else
2981 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002984static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2985{
2986 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2987 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002988 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002989 if (speed != SPEED_10)
2990 return 1;
2991 } else if (speed == SPEED_10)
2992 return 1;
2993
2994 return 0;
2995}
2996
Matt Carlson0a459aa2008-11-03 16:54:15 -08002997static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002998{
Matt Carlsonce057f02007-11-12 21:08:03 -08002999 u32 val;
3000
Nithin Sujir942d1af2013-04-09 08:48:07 +00003001 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3002 return;
3003
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003004 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003005 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003006 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3007 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3008
3009 sg_dig_ctrl |=
3010 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3011 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3012 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3013 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003014 return;
Michael Chan51297242007-02-13 12:17:57 -08003015 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003016
Joe Perches41535772013-02-16 11:20:04 +00003017 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003018 tg3_bmcr_reset(tp);
3019 val = tr32(GRC_MISC_CFG);
3020 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3021 udelay(40);
3022 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003023 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003024 u32 phytest;
3025 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3026 u32 phy;
3027
3028 tg3_writephy(tp, MII_ADVERTISE, 0);
3029 tg3_writephy(tp, MII_BMCR,
3030 BMCR_ANENABLE | BMCR_ANRESTART);
3031
3032 tg3_writephy(tp, MII_TG3_FET_TEST,
3033 phytest | MII_TG3_FET_SHADOW_EN);
3034 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3035 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3036 tg3_writephy(tp,
3037 MII_TG3_FET_SHDW_AUXMODE4,
3038 phy);
3039 }
3040 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3041 }
3042 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003043 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07003044 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3045 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003046
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003047 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3048 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3049 MII_TG3_AUXCTL_PCTL_VREG_11V;
3050 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003051 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003052
Michael Chan15c3b692006-03-22 01:06:52 -08003053 /* The PHY should not be powered down on some chips because
3054 * of bugs.
3055 */
Joe Perches41535772013-02-16 11:20:04 +00003056 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3057 tg3_asic_rev(tp) == ASIC_REV_5704 ||
3058 (tg3_asic_rev(tp) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00003059 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
Joe Perches41535772013-02-16 11:20:04 +00003060 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00003061 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08003062 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003063
Joe Perches41535772013-02-16 11:20:04 +00003064 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3065 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003066 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3067 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3068 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3069 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3070 }
3071
Michael Chan15c3b692006-03-22 01:06:52 -08003072 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3073}
3074
Matt Carlson3f007892008-11-03 16:51:36 -08003075/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003076static int tg3_nvram_lock(struct tg3 *tp)
3077{
Joe Perches63c3a662011-04-26 08:12:10 +00003078 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003079 int i;
3080
3081 if (tp->nvram_lock_cnt == 0) {
3082 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3083 for (i = 0; i < 8000; i++) {
3084 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3085 break;
3086 udelay(20);
3087 }
3088 if (i == 8000) {
3089 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3090 return -ENODEV;
3091 }
3092 }
3093 tp->nvram_lock_cnt++;
3094 }
3095 return 0;
3096}
3097
3098/* tp->lock is held. */
3099static void tg3_nvram_unlock(struct tg3 *tp)
3100{
Joe Perches63c3a662011-04-26 08:12:10 +00003101 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003102 if (tp->nvram_lock_cnt > 0)
3103 tp->nvram_lock_cnt--;
3104 if (tp->nvram_lock_cnt == 0)
3105 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3106 }
3107}
3108
3109/* tp->lock is held. */
3110static void tg3_enable_nvram_access(struct tg3 *tp)
3111{
Joe Perches63c3a662011-04-26 08:12:10 +00003112 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003113 u32 nvaccess = tr32(NVRAM_ACCESS);
3114
3115 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3116 }
3117}
3118
3119/* tp->lock is held. */
3120static void tg3_disable_nvram_access(struct tg3 *tp)
3121{
Joe Perches63c3a662011-04-26 08:12:10 +00003122 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003123 u32 nvaccess = tr32(NVRAM_ACCESS);
3124
3125 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3126 }
3127}
3128
3129static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3130 u32 offset, u32 *val)
3131{
3132 u32 tmp;
3133 int i;
3134
3135 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3136 return -EINVAL;
3137
3138 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3139 EEPROM_ADDR_DEVID_MASK |
3140 EEPROM_ADDR_READ);
3141 tw32(GRC_EEPROM_ADDR,
3142 tmp |
3143 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3144 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3145 EEPROM_ADDR_ADDR_MASK) |
3146 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3147
3148 for (i = 0; i < 1000; i++) {
3149 tmp = tr32(GRC_EEPROM_ADDR);
3150
3151 if (tmp & EEPROM_ADDR_COMPLETE)
3152 break;
3153 msleep(1);
3154 }
3155 if (!(tmp & EEPROM_ADDR_COMPLETE))
3156 return -EBUSY;
3157
Matt Carlson62cedd12009-04-20 14:52:29 -07003158 tmp = tr32(GRC_EEPROM_DATA);
3159
3160 /*
3161 * The data will always be opposite the native endian
3162 * format. Perform a blind byteswap to compensate.
3163 */
3164 *val = swab32(tmp);
3165
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003166 return 0;
3167}
3168
3169#define NVRAM_CMD_TIMEOUT 10000
3170
3171static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3172{
3173 int i;
3174
3175 tw32(NVRAM_CMD, nvram_cmd);
3176 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3177 udelay(10);
3178 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3179 udelay(10);
3180 break;
3181 }
3182 }
3183
3184 if (i == NVRAM_CMD_TIMEOUT)
3185 return -EBUSY;
3186
3187 return 0;
3188}
3189
3190static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3191{
Joe Perches63c3a662011-04-26 08:12:10 +00003192 if (tg3_flag(tp, NVRAM) &&
3193 tg3_flag(tp, NVRAM_BUFFERED) &&
3194 tg3_flag(tp, FLASH) &&
3195 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003196 (tp->nvram_jedecnum == JEDEC_ATMEL))
3197
3198 addr = ((addr / tp->nvram_pagesize) <<
3199 ATMEL_AT45DB0X1B_PAGE_POS) +
3200 (addr % tp->nvram_pagesize);
3201
3202 return addr;
3203}
3204
3205static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3206{
Joe Perches63c3a662011-04-26 08:12:10 +00003207 if (tg3_flag(tp, NVRAM) &&
3208 tg3_flag(tp, NVRAM_BUFFERED) &&
3209 tg3_flag(tp, FLASH) &&
3210 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003211 (tp->nvram_jedecnum == JEDEC_ATMEL))
3212
3213 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3214 tp->nvram_pagesize) +
3215 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3216
3217 return addr;
3218}
3219
Matt Carlsone4f34112009-02-25 14:25:00 +00003220/* NOTE: Data read in from NVRAM is byteswapped according to
3221 * the byteswapping settings for all other register accesses.
3222 * tg3 devices are BE devices, so on a BE machine, the data
3223 * returned will be exactly as it is seen in NVRAM. On a LE
3224 * machine, the 32-bit value will be byteswapped.
3225 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003226static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3227{
3228 int ret;
3229
Joe Perches63c3a662011-04-26 08:12:10 +00003230 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003231 return tg3_nvram_read_using_eeprom(tp, offset, val);
3232
3233 offset = tg3_nvram_phys_addr(tp, offset);
3234
3235 if (offset > NVRAM_ADDR_MSK)
3236 return -EINVAL;
3237
3238 ret = tg3_nvram_lock(tp);
3239 if (ret)
3240 return ret;
3241
3242 tg3_enable_nvram_access(tp);
3243
3244 tw32(NVRAM_ADDR, offset);
3245 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3246 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3247
3248 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003249 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003250
3251 tg3_disable_nvram_access(tp);
3252
3253 tg3_nvram_unlock(tp);
3254
3255 return ret;
3256}
3257
Matt Carlsona9dc5292009-02-25 14:25:30 +00003258/* Ensures NVRAM data is in bytestream format. */
3259static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003260{
3261 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003262 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003263 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003264 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003265 return res;
3266}
3267
Matt Carlsondbe9b922012-02-13 10:20:09 +00003268static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3269 u32 offset, u32 len, u8 *buf)
3270{
3271 int i, j, rc = 0;
3272 u32 val;
3273
3274 for (i = 0; i < len; i += 4) {
3275 u32 addr;
3276 __be32 data;
3277
3278 addr = offset + i;
3279
3280 memcpy(&data, buf + i, 4);
3281
3282 /*
3283 * The SEEPROM interface expects the data to always be opposite
3284 * the native endian format. We accomplish this by reversing
3285 * all the operations that would have been performed on the
3286 * data from a call to tg3_nvram_read_be32().
3287 */
3288 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3289
3290 val = tr32(GRC_EEPROM_ADDR);
3291 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3292
3293 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3294 EEPROM_ADDR_READ);
3295 tw32(GRC_EEPROM_ADDR, val |
3296 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3297 (addr & EEPROM_ADDR_ADDR_MASK) |
3298 EEPROM_ADDR_START |
3299 EEPROM_ADDR_WRITE);
3300
3301 for (j = 0; j < 1000; j++) {
3302 val = tr32(GRC_EEPROM_ADDR);
3303
3304 if (val & EEPROM_ADDR_COMPLETE)
3305 break;
3306 msleep(1);
3307 }
3308 if (!(val & EEPROM_ADDR_COMPLETE)) {
3309 rc = -EBUSY;
3310 break;
3311 }
3312 }
3313
3314 return rc;
3315}
3316
3317/* offset and length are dword aligned */
3318static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3319 u8 *buf)
3320{
3321 int ret = 0;
3322 u32 pagesize = tp->nvram_pagesize;
3323 u32 pagemask = pagesize - 1;
3324 u32 nvram_cmd;
3325 u8 *tmp;
3326
3327 tmp = kmalloc(pagesize, GFP_KERNEL);
3328 if (tmp == NULL)
3329 return -ENOMEM;
3330
3331 while (len) {
3332 int j;
3333 u32 phy_addr, page_off, size;
3334
3335 phy_addr = offset & ~pagemask;
3336
3337 for (j = 0; j < pagesize; j += 4) {
3338 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3339 (__be32 *) (tmp + j));
3340 if (ret)
3341 break;
3342 }
3343 if (ret)
3344 break;
3345
3346 page_off = offset & pagemask;
3347 size = pagesize;
3348 if (len < size)
3349 size = len;
3350
3351 len -= size;
3352
3353 memcpy(tmp + page_off, buf, size);
3354
3355 offset = offset + (pagesize - page_off);
3356
3357 tg3_enable_nvram_access(tp);
3358
3359 /*
3360 * Before we can erase the flash page, we need
3361 * to issue a special "write enable" command.
3362 */
3363 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3364
3365 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3366 break;
3367
3368 /* Erase the target page */
3369 tw32(NVRAM_ADDR, phy_addr);
3370
3371 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3372 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3373
3374 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3375 break;
3376
3377 /* Issue another write enable to start the write. */
3378 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3379
3380 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3381 break;
3382
3383 for (j = 0; j < pagesize; j += 4) {
3384 __be32 data;
3385
3386 data = *((__be32 *) (tmp + j));
3387
3388 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3389
3390 tw32(NVRAM_ADDR, phy_addr + j);
3391
3392 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3393 NVRAM_CMD_WR;
3394
3395 if (j == 0)
3396 nvram_cmd |= NVRAM_CMD_FIRST;
3397 else if (j == (pagesize - 4))
3398 nvram_cmd |= NVRAM_CMD_LAST;
3399
3400 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3401 if (ret)
3402 break;
3403 }
3404 if (ret)
3405 break;
3406 }
3407
3408 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3409 tg3_nvram_exec_cmd(tp, nvram_cmd);
3410
3411 kfree(tmp);
3412
3413 return ret;
3414}
3415
3416/* offset and length are dword aligned */
3417static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3418 u8 *buf)
3419{
3420 int i, ret = 0;
3421
3422 for (i = 0; i < len; i += 4, offset += 4) {
3423 u32 page_off, phy_addr, nvram_cmd;
3424 __be32 data;
3425
3426 memcpy(&data, buf + i, 4);
3427 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3428
3429 page_off = offset % tp->nvram_pagesize;
3430
3431 phy_addr = tg3_nvram_phys_addr(tp, offset);
3432
Matt Carlsondbe9b922012-02-13 10:20:09 +00003433 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3434
3435 if (page_off == 0 || i == 0)
3436 nvram_cmd |= NVRAM_CMD_FIRST;
3437 if (page_off == (tp->nvram_pagesize - 4))
3438 nvram_cmd |= NVRAM_CMD_LAST;
3439
3440 if (i == (len - 4))
3441 nvram_cmd |= NVRAM_CMD_LAST;
3442
Matt Carlson42278222012-02-13 15:20:11 +00003443 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3444 !tg3_flag(tp, FLASH) ||
3445 !tg3_flag(tp, 57765_PLUS))
3446 tw32(NVRAM_ADDR, phy_addr);
3447
Joe Perches41535772013-02-16 11:20:04 +00003448 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003449 !tg3_flag(tp, 5755_PLUS) &&
3450 (tp->nvram_jedecnum == JEDEC_ST) &&
3451 (nvram_cmd & NVRAM_CMD_FIRST)) {
3452 u32 cmd;
3453
3454 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3455 ret = tg3_nvram_exec_cmd(tp, cmd);
3456 if (ret)
3457 break;
3458 }
3459 if (!tg3_flag(tp, FLASH)) {
3460 /* We always do complete word writes to eeprom. */
3461 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3462 }
3463
3464 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3465 if (ret)
3466 break;
3467 }
3468 return ret;
3469}
3470
3471/* offset and length are dword aligned */
3472static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3473{
3474 int ret;
3475
3476 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3477 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3478 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3479 udelay(40);
3480 }
3481
3482 if (!tg3_flag(tp, NVRAM)) {
3483 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3484 } else {
3485 u32 grc_mode;
3486
3487 ret = tg3_nvram_lock(tp);
3488 if (ret)
3489 return ret;
3490
3491 tg3_enable_nvram_access(tp);
3492 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3493 tw32(NVRAM_WRITE1, 0x406);
3494
3495 grc_mode = tr32(GRC_MODE);
3496 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3497
3498 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3499 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3500 buf);
3501 } else {
3502 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3503 buf);
3504 }
3505
3506 grc_mode = tr32(GRC_MODE);
3507 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3508
3509 tg3_disable_nvram_access(tp);
3510 tg3_nvram_unlock(tp);
3511 }
3512
3513 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3514 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3515 udelay(40);
3516 }
3517
3518 return ret;
3519}
3520
Matt Carlson997b4f12011-08-31 11:44:53 +00003521#define RX_CPU_SCRATCH_BASE 0x30000
3522#define RX_CPU_SCRATCH_SIZE 0x04000
3523#define TX_CPU_SCRATCH_BASE 0x34000
3524#define TX_CPU_SCRATCH_SIZE 0x04000
3525
3526/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003527static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003528{
3529 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003530 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003531
Nithin Sujir837c45b2013-03-06 17:02:30 +00003532 for (i = 0; i < iters; i++) {
3533 tw32(cpu_base + CPU_STATE, 0xffffffff);
3534 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3535 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3536 break;
3537 }
3538
3539 return (i == iters) ? -EBUSY : 0;
3540}
3541
3542/* tp->lock is held. */
3543static int tg3_rxcpu_pause(struct tg3 *tp)
3544{
3545 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3546
3547 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3548 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3549 udelay(10);
3550
3551 return rc;
3552}
3553
3554/* tp->lock is held. */
3555static int tg3_txcpu_pause(struct tg3 *tp)
3556{
3557 return tg3_pause_cpu(tp, TX_CPU_BASE);
3558}
3559
3560/* tp->lock is held. */
3561static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3562{
3563 tw32(cpu_base + CPU_STATE, 0xffffffff);
3564 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3565}
3566
3567/* tp->lock is held. */
3568static void tg3_rxcpu_resume(struct tg3 *tp)
3569{
3570 tg3_resume_cpu(tp, RX_CPU_BASE);
3571}
3572
3573/* tp->lock is held. */
3574static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3575{
3576 int rc;
3577
3578 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003579
Joe Perches41535772013-02-16 11:20:04 +00003580 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003581 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3582
3583 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3584 return 0;
3585 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003586 if (cpu_base == RX_CPU_BASE) {
3587 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003588 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003589 /*
3590 * There is only an Rx CPU for the 5750 derivative in the
3591 * BCM4785.
3592 */
3593 if (tg3_flag(tp, IS_SSB_CORE))
3594 return 0;
3595
Nithin Sujir837c45b2013-03-06 17:02:30 +00003596 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003597 }
3598
Nithin Sujir837c45b2013-03-06 17:02:30 +00003599 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003600 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003601 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003602 return -ENODEV;
3603 }
3604
3605 /* Clear firmware's nvram arbitration. */
3606 if (tg3_flag(tp, NVRAM))
3607 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3608 return 0;
3609}
3610
Nithin Sujir31f11a92013-03-06 17:02:33 +00003611static int tg3_fw_data_len(struct tg3 *tp,
3612 const struct tg3_firmware_hdr *fw_hdr)
3613{
3614 int fw_len;
3615
3616 /* Non fragmented firmware have one firmware header followed by a
3617 * contiguous chunk of data to be written. The length field in that
3618 * header is not the length of data to be written but the complete
3619 * length of the bss. The data length is determined based on
3620 * tp->fw->size minus headers.
3621 *
3622 * Fragmented firmware have a main header followed by multiple
3623 * fragments. Each fragment is identical to non fragmented firmware
3624 * with a firmware header followed by a contiguous chunk of data. In
3625 * the main header, the length field is unused and set to 0xffffffff.
3626 * In each fragment header the length is the entire size of that
3627 * fragment i.e. fragment data + header length. Data length is
3628 * therefore length field in the header minus TG3_FW_HDR_LEN.
3629 */
3630 if (tp->fw_len == 0xffffffff)
3631 fw_len = be32_to_cpu(fw_hdr->len);
3632 else
3633 fw_len = tp->fw->size;
3634
3635 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3636}
3637
Matt Carlson997b4f12011-08-31 11:44:53 +00003638/* tp->lock is held. */
3639static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3640 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003641 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003642{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003643 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003644 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003645 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003646
3647 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3648 netdev_err(tp->dev,
3649 "%s: Trying to load TX cpu firmware which is 5705\n",
3650 __func__);
3651 return -EINVAL;
3652 }
3653
Nithin Sujirc4dab502013-03-06 17:02:34 +00003654 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003655 write_op = tg3_write_mem;
3656 else
3657 write_op = tg3_write_indirect_reg32;
3658
Nithin Sujirc4dab502013-03-06 17:02:34 +00003659 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3660 /* It is possible that bootcode is still loading at this point.
3661 * Get the nvram lock first before halting the cpu.
3662 */
3663 int lock_err = tg3_nvram_lock(tp);
3664 err = tg3_halt_cpu(tp, cpu_base);
3665 if (!lock_err)
3666 tg3_nvram_unlock(tp);
3667 if (err)
3668 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003669
Nithin Sujirc4dab502013-03-06 17:02:34 +00003670 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3671 write_op(tp, cpu_scratch_base + i, 0);
3672 tw32(cpu_base + CPU_STATE, 0xffffffff);
3673 tw32(cpu_base + CPU_MODE,
3674 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3675 } else {
3676 /* Subtract additional main header for fragmented firmware and
3677 * advance to the first fragment
3678 */
3679 total_len -= TG3_FW_HDR_LEN;
3680 fw_hdr++;
3681 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003682
Nithin Sujir31f11a92013-03-06 17:02:33 +00003683 do {
3684 u32 *fw_data = (u32 *)(fw_hdr + 1);
3685 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3686 write_op(tp, cpu_scratch_base +
3687 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3688 (i * sizeof(u32)),
3689 be32_to_cpu(fw_data[i]));
3690
3691 total_len -= be32_to_cpu(fw_hdr->len);
3692
3693 /* Advance to next fragment */
3694 fw_hdr = (struct tg3_firmware_hdr *)
3695 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3696 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003697
3698 err = 0;
3699
3700out:
3701 return err;
3702}
3703
3704/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003705static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3706{
3707 int i;
3708 const int iters = 5;
3709
3710 tw32(cpu_base + CPU_STATE, 0xffffffff);
3711 tw32_f(cpu_base + CPU_PC, pc);
3712
3713 for (i = 0; i < iters; i++) {
3714 if (tr32(cpu_base + CPU_PC) == pc)
3715 break;
3716 tw32(cpu_base + CPU_STATE, 0xffffffff);
3717 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3718 tw32_f(cpu_base + CPU_PC, pc);
3719 udelay(1000);
3720 }
3721
3722 return (i == iters) ? -EBUSY : 0;
3723}
3724
3725/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003726static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3727{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003728 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003729 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003730
Nithin Sujir77997ea2013-03-06 17:02:32 +00003731 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003732
3733 /* Firmware blob starts with version numbers, followed by
3734 start address and length. We are setting complete length.
3735 length = end_address_of_bss - start_address_of_text.
3736 Remainder is the blob to be loaded contiguously
3737 from start address. */
3738
Matt Carlson997b4f12011-08-31 11:44:53 +00003739 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3740 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003741 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003742 if (err)
3743 return err;
3744
3745 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3746 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003747 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003748 if (err)
3749 return err;
3750
3751 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003752 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3753 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003754 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003755 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3756 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003757 tr32(RX_CPU_BASE + CPU_PC),
3758 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003759 return -ENODEV;
3760 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003761
3762 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003763
3764 return 0;
3765}
3766
Nithin Sujirc4dab502013-03-06 17:02:34 +00003767static int tg3_validate_rxcpu_state(struct tg3 *tp)
3768{
3769 const int iters = 1000;
3770 int i;
3771 u32 val;
3772
3773 /* Wait for boot code to complete initialization and enter service
3774 * loop. It is then safe to download service patches
3775 */
3776 for (i = 0; i < iters; i++) {
3777 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3778 break;
3779
3780 udelay(10);
3781 }
3782
3783 if (i == iters) {
3784 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3785 return -EBUSY;
3786 }
3787
3788 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3789 if (val & 0xff) {
3790 netdev_warn(tp->dev,
3791 "Other patches exist. Not downloading EEE patch\n");
3792 return -EEXIST;
3793 }
3794
3795 return 0;
3796}
3797
3798/* tp->lock is held. */
3799static void tg3_load_57766_firmware(struct tg3 *tp)
3800{
3801 struct tg3_firmware_hdr *fw_hdr;
3802
3803 if (!tg3_flag(tp, NO_NVRAM))
3804 return;
3805
3806 if (tg3_validate_rxcpu_state(tp))
3807 return;
3808
3809 if (!tp->fw)
3810 return;
3811
3812 /* This firmware blob has a different format than older firmware
3813 * releases as given below. The main difference is we have fragmented
3814 * data to be written to non-contiguous locations.
3815 *
3816 * In the beginning we have a firmware header identical to other
3817 * firmware which consists of version, base addr and length. The length
3818 * here is unused and set to 0xffffffff.
3819 *
3820 * This is followed by a series of firmware fragments which are
3821 * individually identical to previous firmware. i.e. they have the
3822 * firmware header and followed by data for that fragment. The version
3823 * field of the individual fragment header is unused.
3824 */
3825
3826 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3827 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3828 return;
3829
3830 if (tg3_rxcpu_pause(tp))
3831 return;
3832
3833 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3834 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3835
3836 tg3_rxcpu_resume(tp);
3837}
3838
Matt Carlson997b4f12011-08-31 11:44:53 +00003839/* tp->lock is held. */
3840static int tg3_load_tso_firmware(struct tg3 *tp)
3841{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003842 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003843 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003844 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003845
Matt Carlson1caf13e2013-03-06 17:02:29 +00003846 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003847 return 0;
3848
Nithin Sujir77997ea2013-03-06 17:02:32 +00003849 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003850
3851 /* Firmware blob starts with version numbers, followed by
3852 start address and length. We are setting complete length.
3853 length = end_address_of_bss - start_address_of_text.
3854 Remainder is the blob to be loaded contiguously
3855 from start address. */
3856
Matt Carlson997b4f12011-08-31 11:44:53 +00003857 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003858
Joe Perches41535772013-02-16 11:20:04 +00003859 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003860 cpu_base = RX_CPU_BASE;
3861 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3862 } else {
3863 cpu_base = TX_CPU_BASE;
3864 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3865 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3866 }
3867
3868 err = tg3_load_firmware_cpu(tp, cpu_base,
3869 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003870 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003871 if (err)
3872 return err;
3873
3874 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003875 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3876 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003877 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003878 netdev_err(tp->dev,
3879 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003880 __func__, tr32(cpu_base + CPU_PC),
3881 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003882 return -ENODEV;
3883 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003884
3885 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003886 return 0;
3887}
3888
3889
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003890/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003891static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003892{
3893 u32 addr_high, addr_low;
3894 int i;
3895
3896 addr_high = ((tp->dev->dev_addr[0] << 8) |
3897 tp->dev->dev_addr[1]);
3898 addr_low = ((tp->dev->dev_addr[2] << 24) |
3899 (tp->dev->dev_addr[3] << 16) |
3900 (tp->dev->dev_addr[4] << 8) |
3901 (tp->dev->dev_addr[5] << 0));
3902 for (i = 0; i < 4; i++) {
3903 if (i == 1 && skip_mac_1)
3904 continue;
3905 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3906 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3907 }
3908
Joe Perches41535772013-02-16 11:20:04 +00003909 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3910 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003911 for (i = 0; i < 12; i++) {
3912 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3913 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3914 }
3915 }
3916
3917 addr_high = (tp->dev->dev_addr[0] +
3918 tp->dev->dev_addr[1] +
3919 tp->dev->dev_addr[2] +
3920 tp->dev->dev_addr[3] +
3921 tp->dev->dev_addr[4] +
3922 tp->dev->dev_addr[5]) &
3923 TX_BACKOFF_SEED_MASK;
3924 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3925}
3926
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003927static void tg3_enable_register_access(struct tg3 *tp)
3928{
3929 /*
3930 * Make sure register accesses (indirect or otherwise) will function
3931 * correctly.
3932 */
3933 pci_write_config_dword(tp->pdev,
3934 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3935}
3936
3937static int tg3_power_up(struct tg3 *tp)
3938{
Matt Carlsonbed98292011-07-13 09:27:29 +00003939 int err;
3940
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003941 tg3_enable_register_access(tp);
3942
Matt Carlsonbed98292011-07-13 09:27:29 +00003943 err = pci_set_power_state(tp->pdev, PCI_D0);
3944 if (!err) {
3945 /* Switch out of Vaux if it is a NIC */
3946 tg3_pwrsrc_switch_to_vmain(tp);
3947 } else {
3948 netdev_err(tp->dev, "Transition to D0 failed\n");
3949 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003950
Matt Carlsonbed98292011-07-13 09:27:29 +00003951 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003952}
3953
Joe Perches953c96e2013-04-09 10:18:14 +00003954static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00003955
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003956static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
3958 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003959 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003961 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003962
3963 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003964 if (tg3_flag(tp, CLKREQ_BUG))
3965 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3966 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3969 tw32(TG3PCI_MISC_HOST_CTRL,
3970 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3971
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003972 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003973 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003974
Joe Perches63c3a662011-04-26 08:12:10 +00003975 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003976 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003977 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003978 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003979 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003980 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003981
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003982 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003983
Matt Carlson80096062010-08-02 11:26:06 +00003984 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003985
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003986 tp->link_config.speed = phydev->speed;
3987 tp->link_config.duplex = phydev->duplex;
3988 tp->link_config.autoneg = phydev->autoneg;
3989 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003990
3991 advertising = ADVERTISED_TP |
3992 ADVERTISED_Pause |
3993 ADVERTISED_Autoneg |
3994 ADVERTISED_10baseT_Half;
3995
Joe Perches63c3a662011-04-26 08:12:10 +00003996 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3997 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003998 advertising |=
3999 ADVERTISED_100baseT_Half |
4000 ADVERTISED_100baseT_Full |
4001 ADVERTISED_10baseT_Full;
4002 else
4003 advertising |= ADVERTISED_10baseT_Full;
4004 }
4005
4006 phydev->advertising = advertising;
4007
4008 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004009
4010 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004011 if (phyid != PHY_ID_BCMAC131) {
4012 phyid &= PHY_BCM_OUI_MASK;
4013 if (phyid == PHY_BCM_OUI_1 ||
4014 phyid == PHY_BCM_OUI_2 ||
4015 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004016 do_low_power = true;
4017 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004018 }
Matt Carlsondd477002008-05-25 23:45:58 -07004019 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004020 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004021
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004022 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004023 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Matt Carlson2855b9f2012-02-13 15:20:14 +00004025 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004026 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028
Joe Perches41535772013-02-16 11:20:04 +00004029 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004030 u32 val;
4031
4032 val = tr32(GRC_VCPU_EXT_CTRL);
4033 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004034 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004035 int i;
4036 u32 val;
4037
4038 for (i = 0; i < 200; i++) {
4039 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4040 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4041 break;
4042 msleep(1);
4043 }
4044 }
Joe Perches63c3a662011-04-26 08:12:10 +00004045 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004046 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4047 WOL_DRV_STATE_SHUTDOWN |
4048 WOL_DRV_WOL |
4049 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004050
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004051 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 u32 mac_mode;
4053
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004054 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004055 if (do_low_power &&
4056 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4057 tg3_phy_auxctl_write(tp,
4058 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4059 MII_TG3_AUXCTL_PCTL_WOL_EN |
4060 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4061 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004062 udelay(40);
4063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004065 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004066 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004067 else if (tp->phy_flags &
4068 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4069 if (tp->link_config.active_speed == SPEED_1000)
4070 mac_mode = MAC_MODE_PORT_MODE_GMII;
4071 else
4072 mac_mode = MAC_MODE_PORT_MODE_MII;
4073 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004074 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004076 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004077 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004078 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004079 SPEED_100 : SPEED_10;
4080 if (tg3_5700_link_polarity(tp, speed))
4081 mac_mode |= MAC_MODE_LINK_POLARITY;
4082 else
4083 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 } else {
4086 mac_mode = MAC_MODE_PORT_MODE_TBI;
4087 }
4088
Joe Perches63c3a662011-04-26 08:12:10 +00004089 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 tw32(MAC_LED_CTRL, tp->led_ctrl);
4091
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004092 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004093 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4094 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004095 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Joe Perches63c3a662011-04-26 08:12:10 +00004097 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004098 mac_mode |= MAC_MODE_APE_TX_EN |
4099 MAC_MODE_APE_RX_EN |
4100 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004101
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 tw32_f(MAC_MODE, mac_mode);
4103 udelay(100);
4104
4105 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4106 udelay(10);
4107 }
4108
Joe Perches63c3a662011-04-26 08:12:10 +00004109 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004110 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4111 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 u32 base_val;
4113
4114 base_val = tp->pci_clock_ctrl;
4115 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4116 CLOCK_CTRL_TXCLK_DISABLE);
4117
Michael Chanb401e9e2005-12-19 16:27:04 -08004118 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4119 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004120 } else if (tg3_flag(tp, 5780_CLASS) ||
4121 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004122 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004123 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004124 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 u32 newbits1, newbits2;
4126
Joe Perches41535772013-02-16 11:20:04 +00004127 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4128 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4130 CLOCK_CTRL_TXCLK_DISABLE |
4131 CLOCK_CTRL_ALTCLK);
4132 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004133 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 newbits1 = CLOCK_CTRL_625_CORE;
4135 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4136 } else {
4137 newbits1 = CLOCK_CTRL_ALTCLK;
4138 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4139 }
4140
Michael Chanb401e9e2005-12-19 16:27:04 -08004141 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4142 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Michael Chanb401e9e2005-12-19 16:27:04 -08004144 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4145 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Joe Perches63c3a662011-04-26 08:12:10 +00004147 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 u32 newbits3;
4149
Joe Perches41535772013-02-16 11:20:04 +00004150 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4151 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4153 CLOCK_CTRL_TXCLK_DISABLE |
4154 CLOCK_CTRL_44MHZ_CORE);
4155 } else {
4156 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4157 }
4158
Michael Chanb401e9e2005-12-19 16:27:04 -08004159 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4160 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 }
4162 }
4163
Joe Perches63c3a662011-04-26 08:12:10 +00004164 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004165 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004166
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004167 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004170 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004171 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4172 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 u32 val = tr32(0x7d00);
4174
4175 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4176 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004177 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004178 int err;
4179
4180 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004182 if (!err)
4183 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
4186
Michael Chanbbadf502006-04-06 21:46:34 -07004187 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return 0;
4190}
4191
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004192static void tg3_power_down(struct tg3 *tp)
4193{
4194 tg3_power_down_prepare(tp);
4195
Joe Perches63c3a662011-04-26 08:12:10 +00004196 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004197 pci_set_power_state(tp->pdev, PCI_D3hot);
4198}
4199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4201{
4202 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4203 case MII_TG3_AUX_STAT_10HALF:
4204 *speed = SPEED_10;
4205 *duplex = DUPLEX_HALF;
4206 break;
4207
4208 case MII_TG3_AUX_STAT_10FULL:
4209 *speed = SPEED_10;
4210 *duplex = DUPLEX_FULL;
4211 break;
4212
4213 case MII_TG3_AUX_STAT_100HALF:
4214 *speed = SPEED_100;
4215 *duplex = DUPLEX_HALF;
4216 break;
4217
4218 case MII_TG3_AUX_STAT_100FULL:
4219 *speed = SPEED_100;
4220 *duplex = DUPLEX_FULL;
4221 break;
4222
4223 case MII_TG3_AUX_STAT_1000HALF:
4224 *speed = SPEED_1000;
4225 *duplex = DUPLEX_HALF;
4226 break;
4227
4228 case MII_TG3_AUX_STAT_1000FULL:
4229 *speed = SPEED_1000;
4230 *duplex = DUPLEX_FULL;
4231 break;
4232
4233 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004234 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004235 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4236 SPEED_10;
4237 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4238 DUPLEX_HALF;
4239 break;
4240 }
Matt Carlsone7405222012-02-13 15:20:16 +00004241 *speed = SPEED_UNKNOWN;
4242 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
4246
Matt Carlson42b64a42011-05-19 12:12:49 +00004247static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
Matt Carlson42b64a42011-05-19 12:12:49 +00004249 int err = 0;
4250 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Matt Carlson42b64a42011-05-19 12:12:49 +00004252 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004253 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004254 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Matt Carlson42b64a42011-05-19 12:12:49 +00004256 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4257 if (err)
4258 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Matt Carlson4f272092011-12-14 11:09:57 +00004260 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4261 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004262
Joe Perches41535772013-02-16 11:20:04 +00004263 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4264 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004265 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004266
Matt Carlson4f272092011-12-14 11:09:57 +00004267 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4268 if (err)
4269 goto done;
4270 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004271
Matt Carlson42b64a42011-05-19 12:12:49 +00004272 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4273 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Matt Carlson42b64a42011-05-19 12:12:49 +00004275 tw32(TG3_CPMU_EEE_MODE,
4276 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004277
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004278 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004279 if (!err) {
4280 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004281
Matt Carlsona6b68da2010-12-06 08:28:52 +00004282 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004283 /* Advertise 100-BaseTX EEE ability */
4284 if (advertise & ADVERTISED_100baseT_Full)
4285 val |= MDIO_AN_EEE_ADV_100TX;
4286 /* Advertise 1000-BaseT EEE ability */
4287 if (advertise & ADVERTISED_1000baseT_Full)
4288 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004289
4290 if (!tp->eee.eee_enabled) {
4291 val = 0;
4292 tp->eee.advertised = 0;
4293 } else {
4294 tp->eee.advertised = advertise &
4295 (ADVERTISED_100baseT_Full |
4296 ADVERTISED_1000baseT_Full);
4297 }
4298
Matt Carlson42b64a42011-05-19 12:12:49 +00004299 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004300 if (err)
4301 val = 0;
4302
Joe Perches41535772013-02-16 11:20:04 +00004303 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004304 case ASIC_REV_5717:
4305 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004306 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004307 case ASIC_REV_5719:
4308 /* If we advertised any eee advertisements above... */
4309 if (val)
4310 val = MII_TG3_DSP_TAP26_ALNOKO |
4311 MII_TG3_DSP_TAP26_RMRXSTO |
4312 MII_TG3_DSP_TAP26_OPCSINPT;
4313 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4314 /* Fall through */
4315 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004316 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004317 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4318 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4319 MII_TG3_DSP_CH34TP2_HIBW01);
4320 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004321
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004322 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004323 if (!err)
4324 err = err2;
4325 }
4326
4327done:
4328 return err;
4329}
4330
4331static void tg3_phy_copper_begin(struct tg3 *tp)
4332{
Matt Carlsond13ba512012-02-22 12:35:19 +00004333 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4334 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4335 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004336
Nithin Sujir942d1af2013-04-09 08:48:07 +00004337 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4338 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004339 adv = ADVERTISED_10baseT_Half |
4340 ADVERTISED_10baseT_Full;
4341 if (tg3_flag(tp, WOL_SPEED_100MB))
4342 adv |= ADVERTISED_100baseT_Half |
4343 ADVERTISED_100baseT_Full;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004344 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK)
4345 adv |= ADVERTISED_1000baseT_Half |
4346 ADVERTISED_1000baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004347
Matt Carlsond13ba512012-02-22 12:35:19 +00004348 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004349 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004350 adv = tp->link_config.advertising;
4351 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4352 adv &= ~(ADVERTISED_1000baseT_Half |
4353 ADVERTISED_1000baseT_Full);
4354
4355 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004356 }
4357
Matt Carlsond13ba512012-02-22 12:35:19 +00004358 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004359
Nithin Sujir942d1af2013-04-09 08:48:07 +00004360 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4361 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4362 /* Normally during power down we want to autonegotiate
4363 * the lowest possible speed for WOL. However, to avoid
4364 * link flap, we leave it untouched.
4365 */
4366 return;
4367 }
4368
Matt Carlsond13ba512012-02-22 12:35:19 +00004369 tg3_writephy(tp, MII_BMCR,
4370 BMCR_ANENABLE | BMCR_ANRESTART);
4371 } else {
4372 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 u32 bmcr, orig_bmcr;
4374
4375 tp->link_config.active_speed = tp->link_config.speed;
4376 tp->link_config.active_duplex = tp->link_config.duplex;
4377
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004378 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4379 /* With autoneg disabled, 5715 only links up when the
4380 * advertisement register has the configured speed
4381 * enabled.
4382 */
4383 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4384 }
4385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 bmcr = 0;
4387 switch (tp->link_config.speed) {
4388 default:
4389 case SPEED_10:
4390 break;
4391
4392 case SPEED_100:
4393 bmcr |= BMCR_SPEED100;
4394 break;
4395
4396 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004397 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 if (tp->link_config.duplex == DUPLEX_FULL)
4402 bmcr |= BMCR_FULLDPLX;
4403
4404 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4405 (bmcr != orig_bmcr)) {
4406 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4407 for (i = 0; i < 1500; i++) {
4408 u32 tmp;
4409
4410 udelay(10);
4411 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4412 tg3_readphy(tp, MII_BMSR, &tmp))
4413 continue;
4414 if (!(tmp & BMSR_LSTATUS)) {
4415 udelay(40);
4416 break;
4417 }
4418 }
4419 tg3_writephy(tp, MII_BMCR, bmcr);
4420 udelay(40);
4421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 }
4423}
4424
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004425static int tg3_phy_pull_config(struct tg3 *tp)
4426{
4427 int err;
4428 u32 val;
4429
4430 err = tg3_readphy(tp, MII_BMCR, &val);
4431 if (err)
4432 goto done;
4433
4434 if (!(val & BMCR_ANENABLE)) {
4435 tp->link_config.autoneg = AUTONEG_DISABLE;
4436 tp->link_config.advertising = 0;
4437 tg3_flag_clear(tp, PAUSE_AUTONEG);
4438
4439 err = -EIO;
4440
4441 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4442 case 0:
4443 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4444 goto done;
4445
4446 tp->link_config.speed = SPEED_10;
4447 break;
4448 case BMCR_SPEED100:
4449 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4450 goto done;
4451
4452 tp->link_config.speed = SPEED_100;
4453 break;
4454 case BMCR_SPEED1000:
4455 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4456 tp->link_config.speed = SPEED_1000;
4457 break;
4458 }
4459 /* Fall through */
4460 default:
4461 goto done;
4462 }
4463
4464 if (val & BMCR_FULLDPLX)
4465 tp->link_config.duplex = DUPLEX_FULL;
4466 else
4467 tp->link_config.duplex = DUPLEX_HALF;
4468
4469 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4470
4471 err = 0;
4472 goto done;
4473 }
4474
4475 tp->link_config.autoneg = AUTONEG_ENABLE;
4476 tp->link_config.advertising = ADVERTISED_Autoneg;
4477 tg3_flag_set(tp, PAUSE_AUTONEG);
4478
4479 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4480 u32 adv;
4481
4482 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4483 if (err)
4484 goto done;
4485
4486 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4487 tp->link_config.advertising |= adv | ADVERTISED_TP;
4488
4489 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4490 } else {
4491 tp->link_config.advertising |= ADVERTISED_FIBRE;
4492 }
4493
4494 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4495 u32 adv;
4496
4497 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4498 err = tg3_readphy(tp, MII_CTRL1000, &val);
4499 if (err)
4500 goto done;
4501
4502 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4503 } else {
4504 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4505 if (err)
4506 goto done;
4507
4508 adv = tg3_decode_flowctrl_1000X(val);
4509 tp->link_config.flowctrl = adv;
4510
4511 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4512 adv = mii_adv_to_ethtool_adv_x(val);
4513 }
4514
4515 tp->link_config.advertising |= adv;
4516 }
4517
4518done:
4519 return err;
4520}
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522static int tg3_init_5401phy_dsp(struct tg3 *tp)
4523{
4524 int err;
4525
4526 /* Turn off tap power management. */
4527 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004528 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004530 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4531 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4532 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4533 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4534 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536 udelay(40);
4537
4538 return err;
4539}
4540
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004541static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4542{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004543 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004544
4545 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4546 return true;
4547
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004548 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004549
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004550 if (tp->eee.eee_enabled) {
4551 if (tp->eee.advertised != eee.advertised ||
4552 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4553 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4554 return false;
4555 } else {
4556 /* EEE is disabled but we're advertising */
4557 if (eee.advertised)
4558 return false;
4559 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004560
4561 return true;
4562}
4563
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004564static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004566 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004567
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004568 advertising = tp->link_config.advertising;
4569 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004571 advmsk = ADVERTISE_ALL;
4572 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004573 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004574 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004577 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4578 return false;
4579
4580 if ((*lcladv & advmsk) != tgtadv)
4581 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004582
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004583 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 u32 tg3_ctrl;
4585
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004586 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004587
Matt Carlson221c5632011-06-13 13:39:01 +00004588 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004589 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Matt Carlson3198e072012-02-13 15:20:10 +00004591 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004592 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4593 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004594 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4595 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4596 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4597 } else {
4598 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4599 }
4600
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004601 if (tg3_ctrl != tgtadv)
4602 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004604
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004605 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004606}
4607
Matt Carlson859edb22011-12-08 14:40:16 +00004608static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4609{
4610 u32 lpeth = 0;
4611
4612 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4613 u32 val;
4614
4615 if (tg3_readphy(tp, MII_STAT1000, &val))
4616 return false;
4617
4618 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4619 }
4620
4621 if (tg3_readphy(tp, MII_LPA, rmtadv))
4622 return false;
4623
4624 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4625 tp->link_config.rmt_adv = lpeth;
4626
4627 return true;
4628}
4629
Joe Perches953c96e2013-04-09 10:18:14 +00004630static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004631{
4632 if (curr_link_up != tp->link_up) {
4633 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004634 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004635 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004636 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004637 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4638 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4639 }
4640
4641 tg3_link_report(tp);
4642 return true;
4643 }
4644
4645 return false;
4646}
4647
Michael Chan3310e242013-04-09 08:48:05 +00004648static void tg3_clear_mac_status(struct tg3 *tp)
4649{
4650 tw32(MAC_EVENT, 0);
4651
4652 tw32_f(MAC_STATUS,
4653 MAC_STATUS_SYNC_CHANGED |
4654 MAC_STATUS_CFG_CHANGED |
4655 MAC_STATUS_MI_COMPLETION |
4656 MAC_STATUS_LNKSTATE_CHANGED);
4657 udelay(40);
4658}
4659
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004660static void tg3_setup_eee(struct tg3 *tp)
4661{
4662 u32 val;
4663
4664 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4665 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4666 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4667 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4668
4669 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4670
4671 tw32_f(TG3_CPMU_EEE_CTRL,
4672 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4673
4674 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4675 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4676 TG3_CPMU_EEEMD_LPI_IN_RX |
4677 TG3_CPMU_EEEMD_EEE_ENABLE;
4678
4679 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4680 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4681
4682 if (tg3_flag(tp, ENABLE_APE))
4683 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4684
4685 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4686
4687 tw32_f(TG3_CPMU_EEE_DBTMR1,
4688 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4689 (tp->eee.tx_lpi_timer & 0xffff));
4690
4691 tw32_f(TG3_CPMU_EEE_DBTMR2,
4692 TG3_CPMU_DBTMR2_APE_TX_2047US |
4693 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4694}
4695
Joe Perches953c96e2013-04-09 10:18:14 +00004696static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Joe Perches953c96e2013-04-09 10:18:14 +00004698 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004699 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004700 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 u16 current_speed;
4702 u8 current_duplex;
4703 int i, err;
4704
Michael Chan3310e242013-04-09 08:48:05 +00004705 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Matt Carlson8ef21422008-05-02 16:47:53 -07004707 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4708 tw32_f(MAC_MI_MODE,
4709 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4710 udelay(80);
4711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004713 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 /* Some third-party PHYs need to be reset on link going
4716 * down.
4717 */
Joe Perches41535772013-02-16 11:20:04 +00004718 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4719 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4720 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004721 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 tg3_readphy(tp, MII_BMSR, &bmsr);
4723 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4724 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004725 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 }
4727 if (force_reset)
4728 tg3_phy_reset(tp);
4729
Matt Carlson79eb6902010-02-17 15:17:03 +00004730 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 tg3_readphy(tp, MII_BMSR, &bmsr);
4732 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004733 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 bmsr = 0;
4735
4736 if (!(bmsr & BMSR_LSTATUS)) {
4737 err = tg3_init_5401phy_dsp(tp);
4738 if (err)
4739 return err;
4740
4741 tg3_readphy(tp, MII_BMSR, &bmsr);
4742 for (i = 0; i < 1000; i++) {
4743 udelay(10);
4744 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4745 (bmsr & BMSR_LSTATUS)) {
4746 udelay(40);
4747 break;
4748 }
4749 }
4750
Matt Carlson79eb6902010-02-17 15:17:03 +00004751 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4752 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 !(bmsr & BMSR_LSTATUS) &&
4754 tp->link_config.active_speed == SPEED_1000) {
4755 err = tg3_phy_reset(tp);
4756 if (!err)
4757 err = tg3_init_5401phy_dsp(tp);
4758 if (err)
4759 return err;
4760 }
4761 }
Joe Perches41535772013-02-16 11:20:04 +00004762 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4763 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* 5701 {A0,B0} CRC bug workaround */
4765 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004766 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4767 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4768 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 }
4770
4771 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004772 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4773 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004775 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004777 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4779
Joe Perches41535772013-02-16 11:20:04 +00004780 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4781 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4783 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4784 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4785 else
4786 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4787 }
4788
Joe Perches953c96e2013-04-09 10:18:14 +00004789 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004790 current_speed = SPEED_UNKNOWN;
4791 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004792 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004793 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004795 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004796 err = tg3_phy_auxctl_read(tp,
4797 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4798 &val);
4799 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004800 tg3_phy_auxctl_write(tp,
4801 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4802 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 goto relink;
4804 }
4805 }
4806
4807 bmsr = 0;
4808 for (i = 0; i < 100; i++) {
4809 tg3_readphy(tp, MII_BMSR, &bmsr);
4810 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4811 (bmsr & BMSR_LSTATUS))
4812 break;
4813 udelay(40);
4814 }
4815
4816 if (bmsr & BMSR_LSTATUS) {
4817 u32 aux_stat, bmcr;
4818
4819 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4820 for (i = 0; i < 2000; i++) {
4821 udelay(10);
4822 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4823 aux_stat)
4824 break;
4825 }
4826
4827 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4828 &current_speed,
4829 &current_duplex);
4830
4831 bmcr = 0;
4832 for (i = 0; i < 200; i++) {
4833 tg3_readphy(tp, MII_BMCR, &bmcr);
4834 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4835 continue;
4836 if (bmcr && bmcr != 0x7fff)
4837 break;
4838 udelay(10);
4839 }
4840
Matt Carlsonef167e22007-12-20 20:10:01 -08004841 lcl_adv = 0;
4842 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Matt Carlsonef167e22007-12-20 20:10:01 -08004844 tp->link_config.active_speed = current_speed;
4845 tp->link_config.active_duplex = current_duplex;
4846
4847 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004848 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4849
Matt Carlsonef167e22007-12-20 20:10:01 -08004850 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004851 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004852 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004853 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004854 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004855
4856 /* EEE settings changes take effect only after a phy
4857 * reset. If we have skipped a reset due to Link Flap
4858 * Avoidance being enabled, do it now.
4859 */
4860 if (!eee_config_ok &&
4861 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004862 !force_reset) {
4863 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004864 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 } else {
4867 if (!(bmcr & BMCR_ANENABLE) &&
4868 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004869 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004870 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 }
4872 }
4873
Joe Perches953c96e2013-04-09 10:18:14 +00004874 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004875 tp->link_config.active_duplex == DUPLEX_FULL) {
4876 u32 reg, bit;
4877
4878 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4879 reg = MII_TG3_FET_GEN_STAT;
4880 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4881 } else {
4882 reg = MII_TG3_EXT_STAT;
4883 bit = MII_TG3_EXT_STAT_MDIX;
4884 }
4885
4886 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4887 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4888
Matt Carlsonef167e22007-12-20 20:10:01 -08004889 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004894 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 tg3_phy_copper_begin(tp);
4896
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004897 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004898 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004899 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4900 current_speed = SPEED_1000;
4901 current_duplex = DUPLEX_FULL;
4902 tp->link_config.active_speed = current_speed;
4903 tp->link_config.active_duplex = current_duplex;
4904 }
4905
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004906 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004907 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4908 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004909 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 }
4911
4912 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004913 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 if (tp->link_config.active_speed == SPEED_100 ||
4915 tp->link_config.active_speed == SPEED_10)
4916 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4917 else
4918 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004919 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004920 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4921 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4923
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004924 /* In order for the 5750 core in BCM4785 chip to work properly
4925 * in RGMII mode, the Led Control Register must be set up.
4926 */
4927 if (tg3_flag(tp, RGMII_MODE)) {
4928 u32 led_ctrl = tr32(MAC_LED_CTRL);
4929 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4930
4931 if (tp->link_config.active_speed == SPEED_10)
4932 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4933 else if (tp->link_config.active_speed == SPEED_100)
4934 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4935 LED_CTRL_100MBPS_ON);
4936 else if (tp->link_config.active_speed == SPEED_1000)
4937 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4938 LED_CTRL_1000MBPS_ON);
4939
4940 tw32(MAC_LED_CTRL, led_ctrl);
4941 udelay(40);
4942 }
4943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4945 if (tp->link_config.active_duplex == DUPLEX_HALF)
4946 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4947
Joe Perches41535772013-02-16 11:20:04 +00004948 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00004949 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004950 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004952 else
4953 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
4955
4956 /* ??? Without this setting Netgear GA302T PHY does not
4957 * ??? send/receive packets...
4958 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004959 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004960 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4962 tw32_f(MAC_MI_MODE, tp->mi_mode);
4963 udelay(80);
4964 }
4965
4966 tw32_f(MAC_MODE, tp->mac_mode);
4967 udelay(40);
4968
Matt Carlson52b02d02010-10-14 10:37:41 +00004969 tg3_phy_eee_adjust(tp, current_link_up);
4970
Joe Perches63c3a662011-04-26 08:12:10 +00004971 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 /* Polled via timer. */
4973 tw32_f(MAC_EVENT, 0);
4974 } else {
4975 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4976 }
4977 udelay(40);
4978
Joe Perches41535772013-02-16 11:20:04 +00004979 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00004980 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004982 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 udelay(120);
4984 tw32_f(MAC_STATUS,
4985 (MAC_STATUS_SYNC_CHANGED |
4986 MAC_STATUS_CFG_CHANGED));
4987 udelay(40);
4988 tg3_write_mem(tp,
4989 NIC_SRAM_FIRMWARE_MBOX,
4990 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4991 }
4992
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004993 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004994 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004995 if (tp->link_config.active_speed == SPEED_100 ||
4996 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004997 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4998 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004999 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005000 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5001 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005002 }
5003
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005004 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
5006 return 0;
5007}
5008
5009struct tg3_fiber_aneginfo {
5010 int state;
5011#define ANEG_STATE_UNKNOWN 0
5012#define ANEG_STATE_AN_ENABLE 1
5013#define ANEG_STATE_RESTART_INIT 2
5014#define ANEG_STATE_RESTART 3
5015#define ANEG_STATE_DISABLE_LINK_OK 4
5016#define ANEG_STATE_ABILITY_DETECT_INIT 5
5017#define ANEG_STATE_ABILITY_DETECT 6
5018#define ANEG_STATE_ACK_DETECT_INIT 7
5019#define ANEG_STATE_ACK_DETECT 8
5020#define ANEG_STATE_COMPLETE_ACK_INIT 9
5021#define ANEG_STATE_COMPLETE_ACK 10
5022#define ANEG_STATE_IDLE_DETECT_INIT 11
5023#define ANEG_STATE_IDLE_DETECT 12
5024#define ANEG_STATE_LINK_OK 13
5025#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5026#define ANEG_STATE_NEXT_PAGE_WAIT 15
5027
5028 u32 flags;
5029#define MR_AN_ENABLE 0x00000001
5030#define MR_RESTART_AN 0x00000002
5031#define MR_AN_COMPLETE 0x00000004
5032#define MR_PAGE_RX 0x00000008
5033#define MR_NP_LOADED 0x00000010
5034#define MR_TOGGLE_TX 0x00000020
5035#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5036#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5037#define MR_LP_ADV_SYM_PAUSE 0x00000100
5038#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5039#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5040#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5041#define MR_LP_ADV_NEXT_PAGE 0x00001000
5042#define MR_TOGGLE_RX 0x00002000
5043#define MR_NP_RX 0x00004000
5044
5045#define MR_LINK_OK 0x80000000
5046
5047 unsigned long link_time, cur_time;
5048
5049 u32 ability_match_cfg;
5050 int ability_match_count;
5051
5052 char ability_match, idle_match, ack_match;
5053
5054 u32 txconfig, rxconfig;
5055#define ANEG_CFG_NP 0x00000080
5056#define ANEG_CFG_ACK 0x00000040
5057#define ANEG_CFG_RF2 0x00000020
5058#define ANEG_CFG_RF1 0x00000010
5059#define ANEG_CFG_PS2 0x00000001
5060#define ANEG_CFG_PS1 0x00008000
5061#define ANEG_CFG_HD 0x00004000
5062#define ANEG_CFG_FD 0x00002000
5063#define ANEG_CFG_INVAL 0x00001f06
5064
5065};
5066#define ANEG_OK 0
5067#define ANEG_DONE 1
5068#define ANEG_TIMER_ENAB 2
5069#define ANEG_FAILED -1
5070
5071#define ANEG_STATE_SETTLE_TIME 10000
5072
5073static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5074 struct tg3_fiber_aneginfo *ap)
5075{
Matt Carlson5be73b42007-12-20 20:09:29 -08005076 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 unsigned long delta;
5078 u32 rx_cfg_reg;
5079 int ret;
5080
5081 if (ap->state == ANEG_STATE_UNKNOWN) {
5082 ap->rxconfig = 0;
5083 ap->link_time = 0;
5084 ap->cur_time = 0;
5085 ap->ability_match_cfg = 0;
5086 ap->ability_match_count = 0;
5087 ap->ability_match = 0;
5088 ap->idle_match = 0;
5089 ap->ack_match = 0;
5090 }
5091 ap->cur_time++;
5092
5093 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5094 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5095
5096 if (rx_cfg_reg != ap->ability_match_cfg) {
5097 ap->ability_match_cfg = rx_cfg_reg;
5098 ap->ability_match = 0;
5099 ap->ability_match_count = 0;
5100 } else {
5101 if (++ap->ability_match_count > 1) {
5102 ap->ability_match = 1;
5103 ap->ability_match_cfg = rx_cfg_reg;
5104 }
5105 }
5106 if (rx_cfg_reg & ANEG_CFG_ACK)
5107 ap->ack_match = 1;
5108 else
5109 ap->ack_match = 0;
5110
5111 ap->idle_match = 0;
5112 } else {
5113 ap->idle_match = 1;
5114 ap->ability_match_cfg = 0;
5115 ap->ability_match_count = 0;
5116 ap->ability_match = 0;
5117 ap->ack_match = 0;
5118
5119 rx_cfg_reg = 0;
5120 }
5121
5122 ap->rxconfig = rx_cfg_reg;
5123 ret = ANEG_OK;
5124
Matt Carlson33f401a2010-04-05 10:19:27 +00005125 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 case ANEG_STATE_UNKNOWN:
5127 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5128 ap->state = ANEG_STATE_AN_ENABLE;
5129
5130 /* fallthru */
5131 case ANEG_STATE_AN_ENABLE:
5132 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5133 if (ap->flags & MR_AN_ENABLE) {
5134 ap->link_time = 0;
5135 ap->cur_time = 0;
5136 ap->ability_match_cfg = 0;
5137 ap->ability_match_count = 0;
5138 ap->ability_match = 0;
5139 ap->idle_match = 0;
5140 ap->ack_match = 0;
5141
5142 ap->state = ANEG_STATE_RESTART_INIT;
5143 } else {
5144 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5145 }
5146 break;
5147
5148 case ANEG_STATE_RESTART_INIT:
5149 ap->link_time = ap->cur_time;
5150 ap->flags &= ~(MR_NP_LOADED);
5151 ap->txconfig = 0;
5152 tw32(MAC_TX_AUTO_NEG, 0);
5153 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5154 tw32_f(MAC_MODE, tp->mac_mode);
5155 udelay(40);
5156
5157 ret = ANEG_TIMER_ENAB;
5158 ap->state = ANEG_STATE_RESTART;
5159
5160 /* fallthru */
5161 case ANEG_STATE_RESTART:
5162 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005163 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005165 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 break;
5168
5169 case ANEG_STATE_DISABLE_LINK_OK:
5170 ret = ANEG_DONE;
5171 break;
5172
5173 case ANEG_STATE_ABILITY_DETECT_INIT:
5174 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005175 ap->txconfig = ANEG_CFG_FD;
5176 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5177 if (flowctrl & ADVERTISE_1000XPAUSE)
5178 ap->txconfig |= ANEG_CFG_PS1;
5179 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5180 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5182 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5183 tw32_f(MAC_MODE, tp->mac_mode);
5184 udelay(40);
5185
5186 ap->state = ANEG_STATE_ABILITY_DETECT;
5187 break;
5188
5189 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005190 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 break;
5193
5194 case ANEG_STATE_ACK_DETECT_INIT:
5195 ap->txconfig |= ANEG_CFG_ACK;
5196 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5197 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5198 tw32_f(MAC_MODE, tp->mac_mode);
5199 udelay(40);
5200
5201 ap->state = ANEG_STATE_ACK_DETECT;
5202
5203 /* fallthru */
5204 case ANEG_STATE_ACK_DETECT:
5205 if (ap->ack_match != 0) {
5206 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5207 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5208 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5209 } else {
5210 ap->state = ANEG_STATE_AN_ENABLE;
5211 }
5212 } else if (ap->ability_match != 0 &&
5213 ap->rxconfig == 0) {
5214 ap->state = ANEG_STATE_AN_ENABLE;
5215 }
5216 break;
5217
5218 case ANEG_STATE_COMPLETE_ACK_INIT:
5219 if (ap->rxconfig & ANEG_CFG_INVAL) {
5220 ret = ANEG_FAILED;
5221 break;
5222 }
5223 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5224 MR_LP_ADV_HALF_DUPLEX |
5225 MR_LP_ADV_SYM_PAUSE |
5226 MR_LP_ADV_ASYM_PAUSE |
5227 MR_LP_ADV_REMOTE_FAULT1 |
5228 MR_LP_ADV_REMOTE_FAULT2 |
5229 MR_LP_ADV_NEXT_PAGE |
5230 MR_TOGGLE_RX |
5231 MR_NP_RX);
5232 if (ap->rxconfig & ANEG_CFG_FD)
5233 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5234 if (ap->rxconfig & ANEG_CFG_HD)
5235 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5236 if (ap->rxconfig & ANEG_CFG_PS1)
5237 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5238 if (ap->rxconfig & ANEG_CFG_PS2)
5239 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5240 if (ap->rxconfig & ANEG_CFG_RF1)
5241 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5242 if (ap->rxconfig & ANEG_CFG_RF2)
5243 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5244 if (ap->rxconfig & ANEG_CFG_NP)
5245 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5246
5247 ap->link_time = ap->cur_time;
5248
5249 ap->flags ^= (MR_TOGGLE_TX);
5250 if (ap->rxconfig & 0x0008)
5251 ap->flags |= MR_TOGGLE_RX;
5252 if (ap->rxconfig & ANEG_CFG_NP)
5253 ap->flags |= MR_NP_RX;
5254 ap->flags |= MR_PAGE_RX;
5255
5256 ap->state = ANEG_STATE_COMPLETE_ACK;
5257 ret = ANEG_TIMER_ENAB;
5258 break;
5259
5260 case ANEG_STATE_COMPLETE_ACK:
5261 if (ap->ability_match != 0 &&
5262 ap->rxconfig == 0) {
5263 ap->state = ANEG_STATE_AN_ENABLE;
5264 break;
5265 }
5266 delta = ap->cur_time - ap->link_time;
5267 if (delta > ANEG_STATE_SETTLE_TIME) {
5268 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5269 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5270 } else {
5271 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5272 !(ap->flags & MR_NP_RX)) {
5273 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5274 } else {
5275 ret = ANEG_FAILED;
5276 }
5277 }
5278 }
5279 break;
5280
5281 case ANEG_STATE_IDLE_DETECT_INIT:
5282 ap->link_time = ap->cur_time;
5283 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5284 tw32_f(MAC_MODE, tp->mac_mode);
5285 udelay(40);
5286
5287 ap->state = ANEG_STATE_IDLE_DETECT;
5288 ret = ANEG_TIMER_ENAB;
5289 break;
5290
5291 case ANEG_STATE_IDLE_DETECT:
5292 if (ap->ability_match != 0 &&
5293 ap->rxconfig == 0) {
5294 ap->state = ANEG_STATE_AN_ENABLE;
5295 break;
5296 }
5297 delta = ap->cur_time - ap->link_time;
5298 if (delta > ANEG_STATE_SETTLE_TIME) {
5299 /* XXX another gem from the Broadcom driver :( */
5300 ap->state = ANEG_STATE_LINK_OK;
5301 }
5302 break;
5303
5304 case ANEG_STATE_LINK_OK:
5305 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5306 ret = ANEG_DONE;
5307 break;
5308
5309 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5310 /* ??? unimplemented */
5311 break;
5312
5313 case ANEG_STATE_NEXT_PAGE_WAIT:
5314 /* ??? unimplemented */
5315 break;
5316
5317 default:
5318 ret = ANEG_FAILED;
5319 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
5322 return ret;
5323}
5324
Matt Carlson5be73b42007-12-20 20:09:29 -08005325static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326{
5327 int res = 0;
5328 struct tg3_fiber_aneginfo aninfo;
5329 int status = ANEG_FAILED;
5330 unsigned int tick;
5331 u32 tmp;
5332
5333 tw32_f(MAC_TX_AUTO_NEG, 0);
5334
5335 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5336 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5337 udelay(40);
5338
5339 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5340 udelay(40);
5341
5342 memset(&aninfo, 0, sizeof(aninfo));
5343 aninfo.flags |= MR_AN_ENABLE;
5344 aninfo.state = ANEG_STATE_UNKNOWN;
5345 aninfo.cur_time = 0;
5346 tick = 0;
5347 while (++tick < 195000) {
5348 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5349 if (status == ANEG_DONE || status == ANEG_FAILED)
5350 break;
5351
5352 udelay(1);
5353 }
5354
5355 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5356 tw32_f(MAC_MODE, tp->mac_mode);
5357 udelay(40);
5358
Matt Carlson5be73b42007-12-20 20:09:29 -08005359 *txflags = aninfo.txconfig;
5360 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
5362 if (status == ANEG_DONE &&
5363 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5364 MR_LP_ADV_FULL_DUPLEX)))
5365 res = 1;
5366
5367 return res;
5368}
5369
5370static void tg3_init_bcm8002(struct tg3 *tp)
5371{
5372 u32 mac_status = tr32(MAC_STATUS);
5373 int i;
5374
5375 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005376 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 !(mac_status & MAC_STATUS_PCS_SYNCED))
5378 return;
5379
5380 /* Set PLL lock range. */
5381 tg3_writephy(tp, 0x16, 0x8007);
5382
5383 /* SW reset */
5384 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5385
5386 /* Wait for reset to complete. */
5387 /* XXX schedule_timeout() ... */
5388 for (i = 0; i < 500; i++)
5389 udelay(10);
5390
5391 /* Config mode; select PMA/Ch 1 regs. */
5392 tg3_writephy(tp, 0x10, 0x8411);
5393
5394 /* Enable auto-lock and comdet, select txclk for tx. */
5395 tg3_writephy(tp, 0x11, 0x0a10);
5396
5397 tg3_writephy(tp, 0x18, 0x00a0);
5398 tg3_writephy(tp, 0x16, 0x41ff);
5399
5400 /* Assert and deassert POR. */
5401 tg3_writephy(tp, 0x13, 0x0400);
5402 udelay(40);
5403 tg3_writephy(tp, 0x13, 0x0000);
5404
5405 tg3_writephy(tp, 0x11, 0x0a50);
5406 udelay(40);
5407 tg3_writephy(tp, 0x11, 0x0a10);
5408
5409 /* Wait for signal to stabilize */
5410 /* XXX schedule_timeout() ... */
5411 for (i = 0; i < 15000; i++)
5412 udelay(10);
5413
5414 /* Deselect the channel register so we can read the PHYID
5415 * later.
5416 */
5417 tg3_writephy(tp, 0x10, 0x8011);
5418}
5419
Joe Perches953c96e2013-04-09 10:18:14 +00005420static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005422 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005423 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 u32 sg_dig_ctrl, sg_dig_status;
5425 u32 serdes_cfg, expected_sg_dig_ctrl;
5426 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
5428 serdes_cfg = 0;
5429 expected_sg_dig_ctrl = 0;
5430 workaround = 0;
5431 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005432 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
Joe Perches41535772013-02-16 11:20:04 +00005434 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5435 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 workaround = 1;
5437 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5438 port_a = 0;
5439
5440 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5441 /* preserve bits 20-23 for voltage regulator */
5442 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5443 }
5444
5445 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5446
5447 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005448 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 if (workaround) {
5450 u32 val = serdes_cfg;
5451
5452 if (port_a)
5453 val |= 0xc010000;
5454 else
5455 val |= 0x4010000;
5456 tw32_f(MAC_SERDES_CFG, val);
5457 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005458
5459 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 }
5461 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5462 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005463 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
5465 goto out;
5466 }
5467
5468 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005469 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
Matt Carlson82cd3d12007-12-20 20:09:00 -08005471 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5472 if (flowctrl & ADVERTISE_1000XPAUSE)
5473 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5474 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5475 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
5477 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005478 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005479 tp->serdes_counter &&
5480 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5481 MAC_STATUS_RCVD_CFG)) ==
5482 MAC_STATUS_PCS_SYNCED)) {
5483 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005484 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005485 goto out;
5486 }
5487restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 if (workaround)
5489 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005490 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 udelay(5);
5492 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5493
Michael Chan3d3ebe72006-09-27 15:59:15 -07005494 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005495 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5497 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005498 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 mac_status = tr32(MAC_STATUS);
5500
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005501 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005503 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Matt Carlson82cd3d12007-12-20 20:09:00 -08005505 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5506 local_adv |= ADVERTISE_1000XPAUSE;
5507 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5508 local_adv |= ADVERTISE_1000XPSE_ASYM;
5509
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005510 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005511 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005512 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005513 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Matt Carlson859edb22011-12-08 14:40:16 +00005515 tp->link_config.rmt_adv =
5516 mii_adv_to_ethtool_adv_x(remote_adv);
5517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005519 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005520 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005521 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005522 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005523 if (tp->serdes_counter)
5524 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 else {
5526 if (workaround) {
5527 u32 val = serdes_cfg;
5528
5529 if (port_a)
5530 val |= 0xc010000;
5531 else
5532 val |= 0x4010000;
5533
5534 tw32_f(MAC_SERDES_CFG, val);
5535 }
5536
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005537 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 udelay(40);
5539
5540 /* Link parallel detection - link is up */
5541 /* only if we have PCS_SYNC and not */
5542 /* receiving config code words */
5543 mac_status = tr32(MAC_STATUS);
5544 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5545 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5546 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005547 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005548 tp->phy_flags |=
5549 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005550 tp->serdes_counter =
5551 SERDES_PARALLEL_DET_TIMEOUT;
5552 } else
5553 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 }
5555 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005556 } else {
5557 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005558 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 }
5560
5561out:
5562 return current_link_up;
5563}
5564
Joe Perches953c96e2013-04-09 10:18:14 +00005565static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566{
Joe Perches953c96e2013-04-09 10:18:14 +00005567 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005569 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
5572 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005573 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005575
Matt Carlson5be73b42007-12-20 20:09:29 -08005576 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5577 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Matt Carlson5be73b42007-12-20 20:09:29 -08005579 if (txflags & ANEG_CFG_PS1)
5580 local_adv |= ADVERTISE_1000XPAUSE;
5581 if (txflags & ANEG_CFG_PS2)
5582 local_adv |= ADVERTISE_1000XPSE_ASYM;
5583
5584 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5585 remote_adv |= LPA_1000XPAUSE;
5586 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5587 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Matt Carlson859edb22011-12-08 14:40:16 +00005589 tp->link_config.rmt_adv =
5590 mii_adv_to_ethtool_adv_x(remote_adv);
5591
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 tg3_setup_flow_control(tp, local_adv, remote_adv);
5593
Joe Perches953c96e2013-04-09 10:18:14 +00005594 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 }
5596 for (i = 0; i < 30; i++) {
5597 udelay(20);
5598 tw32_f(MAC_STATUS,
5599 (MAC_STATUS_SYNC_CHANGED |
5600 MAC_STATUS_CFG_CHANGED));
5601 udelay(40);
5602 if ((tr32(MAC_STATUS) &
5603 (MAC_STATUS_SYNC_CHANGED |
5604 MAC_STATUS_CFG_CHANGED)) == 0)
5605 break;
5606 }
5607
5608 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005609 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5611 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005612 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005614 tg3_setup_flow_control(tp, 0, 0);
5615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005617 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
5619 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5620 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005621
5622 tw32_f(MAC_MODE, tp->mac_mode);
5623 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 }
5625
5626out:
5627 return current_link_up;
5628}
5629
Joe Perches953c96e2013-04-09 10:18:14 +00005630static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631{
5632 u32 orig_pause_cfg;
5633 u16 orig_active_speed;
5634 u8 orig_active_duplex;
5635 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005636 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 int i;
5638
Matt Carlson8d018622007-12-20 20:05:44 -08005639 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 orig_active_speed = tp->link_config.active_speed;
5641 orig_active_duplex = tp->link_config.active_duplex;
5642
Joe Perches63c3a662011-04-26 08:12:10 +00005643 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005644 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005645 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 mac_status = tr32(MAC_STATUS);
5647 mac_status &= (MAC_STATUS_PCS_SYNCED |
5648 MAC_STATUS_SIGNAL_DET |
5649 MAC_STATUS_CFG_CHANGED |
5650 MAC_STATUS_RCVD_CFG);
5651 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5652 MAC_STATUS_SIGNAL_DET)) {
5653 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5654 MAC_STATUS_CFG_CHANGED));
5655 return 0;
5656 }
5657 }
5658
5659 tw32_f(MAC_TX_AUTO_NEG, 0);
5660
5661 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5662 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5663 tw32_f(MAC_MODE, tp->mac_mode);
5664 udelay(40);
5665
Matt Carlson79eb6902010-02-17 15:17:03 +00005666 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 tg3_init_bcm8002(tp);
5668
5669 /* Enable link change event even when serdes polling. */
5670 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5671 udelay(40);
5672
Joe Perches953c96e2013-04-09 10:18:14 +00005673 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005674 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 mac_status = tr32(MAC_STATUS);
5676
Joe Perches63c3a662011-04-26 08:12:10 +00005677 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5679 else
5680 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5681
Matt Carlson898a56f2009-08-28 14:02:40 +00005682 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005684 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
5686 for (i = 0; i < 100; i++) {
5687 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5688 MAC_STATUS_CFG_CHANGED));
5689 udelay(5);
5690 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005691 MAC_STATUS_CFG_CHANGED |
5692 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 break;
5694 }
5695
5696 mac_status = tr32(MAC_STATUS);
5697 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005698 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005699 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5700 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 tw32_f(MAC_MODE, (tp->mac_mode |
5702 MAC_MODE_SEND_CONFIGS));
5703 udelay(1);
5704 tw32_f(MAC_MODE, tp->mac_mode);
5705 }
5706 }
5707
Joe Perches953c96e2013-04-09 10:18:14 +00005708 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 tp->link_config.active_speed = SPEED_1000;
5710 tp->link_config.active_duplex = DUPLEX_FULL;
5711 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5712 LED_CTRL_LNKLED_OVERRIDE |
5713 LED_CTRL_1000MBPS_ON));
5714 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005715 tp->link_config.active_speed = SPEED_UNKNOWN;
5716 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5718 LED_CTRL_LNKLED_OVERRIDE |
5719 LED_CTRL_TRAFFIC_OVERRIDE));
5720 }
5721
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005722 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005723 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 if (orig_pause_cfg != now_pause_cfg ||
5725 orig_active_speed != tp->link_config.active_speed ||
5726 orig_active_duplex != tp->link_config.active_duplex)
5727 tg3_link_report(tp);
5728 }
5729
5730 return 0;
5731}
5732
Joe Perches953c96e2013-04-09 10:18:14 +00005733static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005734{
Joe Perches953c96e2013-04-09 10:18:14 +00005735 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005736 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005737 u16 current_speed = SPEED_UNKNOWN;
5738 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005739 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005740 u32 local_adv, remote_adv, sgsr;
5741
5742 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5743 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5744 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5745 (sgsr & SERDES_TG3_SGMII_MODE)) {
5746
5747 if (force_reset)
5748 tg3_phy_reset(tp);
5749
5750 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5751
5752 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5753 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5754 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005755 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005756 if (sgsr & SERDES_TG3_SPEED_1000) {
5757 current_speed = SPEED_1000;
5758 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5759 } else if (sgsr & SERDES_TG3_SPEED_100) {
5760 current_speed = SPEED_100;
5761 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5762 } else {
5763 current_speed = SPEED_10;
5764 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5765 }
5766
5767 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5768 current_duplex = DUPLEX_FULL;
5769 else
5770 current_duplex = DUPLEX_HALF;
5771 }
5772
5773 tw32_f(MAC_MODE, tp->mac_mode);
5774 udelay(40);
5775
5776 tg3_clear_mac_status(tp);
5777
5778 goto fiber_setup_done;
5779 }
Michael Chan747e8f82005-07-25 12:33:22 -07005780
5781 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5782 tw32_f(MAC_MODE, tp->mac_mode);
5783 udelay(40);
5784
Michael Chan3310e242013-04-09 08:48:05 +00005785 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005786
5787 if (force_reset)
5788 tg3_phy_reset(tp);
5789
Matt Carlson859edb22011-12-08 14:40:16 +00005790 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005791
5792 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5793 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005794 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005795 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5796 bmsr |= BMSR_LSTATUS;
5797 else
5798 bmsr &= ~BMSR_LSTATUS;
5799 }
Michael Chan747e8f82005-07-25 12:33:22 -07005800
5801 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5802
5803 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005804 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005805 /* do nothing, just check for link up at the end */
5806 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005807 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005808
5809 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005810 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5811 ADVERTISE_1000XPAUSE |
5812 ADVERTISE_1000XPSE_ASYM |
5813 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005814
Matt Carlson28011cf2011-11-16 18:36:59 -05005815 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005816 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005817
Matt Carlson28011cf2011-11-16 18:36:59 -05005818 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5819 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005820 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5821 tg3_writephy(tp, MII_BMCR, bmcr);
5822
5823 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005824 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005825 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005826
5827 return err;
5828 }
5829 } else {
5830 u32 new_bmcr;
5831
5832 bmcr &= ~BMCR_SPEED1000;
5833 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5834
5835 if (tp->link_config.duplex == DUPLEX_FULL)
5836 new_bmcr |= BMCR_FULLDPLX;
5837
5838 if (new_bmcr != bmcr) {
5839 /* BMCR_SPEED1000 is a reserved bit that needs
5840 * to be set on write.
5841 */
5842 new_bmcr |= BMCR_SPEED1000;
5843
5844 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005845 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005846 u32 adv;
5847
5848 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5849 adv &= ~(ADVERTISE_1000XFULL |
5850 ADVERTISE_1000XHALF |
5851 ADVERTISE_SLCT);
5852 tg3_writephy(tp, MII_ADVERTISE, adv);
5853 tg3_writephy(tp, MII_BMCR, bmcr |
5854 BMCR_ANRESTART |
5855 BMCR_ANENABLE);
5856 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005857 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005858 }
5859 tg3_writephy(tp, MII_BMCR, new_bmcr);
5860 bmcr = new_bmcr;
5861 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5862 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005863 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005864 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5865 bmsr |= BMSR_LSTATUS;
5866 else
5867 bmsr &= ~BMSR_LSTATUS;
5868 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005869 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005870 }
5871 }
5872
5873 if (bmsr & BMSR_LSTATUS) {
5874 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005875 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005876 if (bmcr & BMCR_FULLDPLX)
5877 current_duplex = DUPLEX_FULL;
5878 else
5879 current_duplex = DUPLEX_HALF;
5880
Matt Carlsonef167e22007-12-20 20:10:01 -08005881 local_adv = 0;
5882 remote_adv = 0;
5883
Michael Chan747e8f82005-07-25 12:33:22 -07005884 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005885 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005886
5887 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5888 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5889 common = local_adv & remote_adv;
5890 if (common & (ADVERTISE_1000XHALF |
5891 ADVERTISE_1000XFULL)) {
5892 if (common & ADVERTISE_1000XFULL)
5893 current_duplex = DUPLEX_FULL;
5894 else
5895 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005896
5897 tp->link_config.rmt_adv =
5898 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005899 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005900 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005901 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005902 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005903 }
Michael Chan747e8f82005-07-25 12:33:22 -07005904 }
5905 }
5906
Michael Chan85730a62013-04-09 08:48:06 +00005907fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005908 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005909 tg3_setup_flow_control(tp, local_adv, remote_adv);
5910
Michael Chan747e8f82005-07-25 12:33:22 -07005911 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5912 if (tp->link_config.active_duplex == DUPLEX_HALF)
5913 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5914
5915 tw32_f(MAC_MODE, tp->mac_mode);
5916 udelay(40);
5917
5918 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5919
5920 tp->link_config.active_speed = current_speed;
5921 tp->link_config.active_duplex = current_duplex;
5922
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005923 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005924 return err;
5925}
5926
5927static void tg3_serdes_parallel_detect(struct tg3 *tp)
5928{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005929 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005930 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005931 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005932 return;
5933 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005934
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005935 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005936 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5937 u32 bmcr;
5938
5939 tg3_readphy(tp, MII_BMCR, &bmcr);
5940 if (bmcr & BMCR_ANENABLE) {
5941 u32 phy1, phy2;
5942
5943 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005944 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5945 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005946
5947 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005948 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5949 MII_TG3_DSP_EXP1_INT_STAT);
5950 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5951 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005952
5953 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5954 /* We have signal detect and not receiving
5955 * config code words, link is up by parallel
5956 * detection.
5957 */
5958
5959 bmcr &= ~BMCR_ANENABLE;
5960 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5961 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005962 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005963 }
5964 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005965 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005966 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005967 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005968 u32 phy2;
5969
5970 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005971 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5972 MII_TG3_DSP_EXP1_INT_STAT);
5973 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005974 if (phy2 & 0x20) {
5975 u32 bmcr;
5976
5977 /* Config code words received, turn on autoneg. */
5978 tg3_readphy(tp, MII_BMCR, &bmcr);
5979 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5980
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005981 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005982
5983 }
5984 }
5985}
5986
Joe Perches953c96e2013-04-09 10:18:14 +00005987static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005989 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 int err;
5991
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005992 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005994 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005995 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005996 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
Joe Perches41535772013-02-16 11:20:04 +00005999 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006000 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006001
6002 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6003 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6004 scale = 65;
6005 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6006 scale = 6;
6007 else
6008 scale = 12;
6009
6010 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6011 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6012 tw32(GRC_MISC_CFG, val);
6013 }
6014
Matt Carlsonf2096f92011-04-05 14:22:48 +00006015 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6016 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006017 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6018 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006019 val |= tr32(MAC_TX_LENGTHS) &
6020 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6021 TX_LENGTHS_CNT_DWN_VAL_MSK);
6022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 if (tp->link_config.active_speed == SPEED_1000 &&
6024 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006025 tw32(MAC_TX_LENGTHS, val |
6026 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006028 tw32(MAC_TX_LENGTHS, val |
6029 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Joe Perches63c3a662011-04-26 08:12:10 +00006031 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006032 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006034 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 } else {
6036 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6037 }
6038 }
6039
Joe Perches63c3a662011-04-26 08:12:10 +00006040 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006041 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006042 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006043 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6044 tp->pwrmgmt_thresh;
6045 else
6046 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6047 tw32(PCIE_PWR_MGMT_THRESH, val);
6048 }
6049
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 return err;
6051}
6052
Matt Carlsonbe947302012-12-03 19:36:57 +00006053/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006054static u64 tg3_refclk_read(struct tg3 *tp)
6055{
6056 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6057 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6058}
6059
6060/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006061static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6062{
6063 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
6064 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6065 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
6066 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
6067}
6068
Matt Carlson7d41e492012-12-03 19:36:58 +00006069static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6070static inline void tg3_full_unlock(struct tg3 *tp);
6071static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6072{
6073 struct tg3 *tp = netdev_priv(dev);
6074
6075 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6076 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006077 SOF_TIMESTAMPING_SOFTWARE;
6078
6079 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006080 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006081 SOF_TIMESTAMPING_RX_HARDWARE |
6082 SOF_TIMESTAMPING_RAW_HARDWARE;
6083 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006084
6085 if (tp->ptp_clock)
6086 info->phc_index = ptp_clock_index(tp->ptp_clock);
6087 else
6088 info->phc_index = -1;
6089
6090 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6091
6092 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6093 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6094 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6095 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6096 return 0;
6097}
6098
6099static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6100{
6101 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6102 bool neg_adj = false;
6103 u32 correction = 0;
6104
6105 if (ppb < 0) {
6106 neg_adj = true;
6107 ppb = -ppb;
6108 }
6109
6110 /* Frequency adjustment is performed using hardware with a 24 bit
6111 * accumulator and a programmable correction value. On each clk, the
6112 * correction value gets added to the accumulator and when it
6113 * overflows, the time counter is incremented/decremented.
6114 *
6115 * So conversion from ppb to correction value is
6116 * ppb * (1 << 24) / 1000000000
6117 */
6118 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6119 TG3_EAV_REF_CLK_CORRECT_MASK;
6120
6121 tg3_full_lock(tp, 0);
6122
6123 if (correction)
6124 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6125 TG3_EAV_REF_CLK_CORRECT_EN |
6126 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6127 else
6128 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6129
6130 tg3_full_unlock(tp);
6131
6132 return 0;
6133}
6134
6135static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6136{
6137 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6138
6139 tg3_full_lock(tp, 0);
6140 tp->ptp_adjust += delta;
6141 tg3_full_unlock(tp);
6142
6143 return 0;
6144}
6145
6146static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6147{
6148 u64 ns;
6149 u32 remainder;
6150 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6151
6152 tg3_full_lock(tp, 0);
6153 ns = tg3_refclk_read(tp);
6154 ns += tp->ptp_adjust;
6155 tg3_full_unlock(tp);
6156
6157 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6158 ts->tv_nsec = remainder;
6159
6160 return 0;
6161}
6162
6163static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6164 const struct timespec *ts)
6165{
6166 u64 ns;
6167 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6168
6169 ns = timespec_to_ns(ts);
6170
6171 tg3_full_lock(tp, 0);
6172 tg3_refclk_write(tp, ns);
6173 tp->ptp_adjust = 0;
6174 tg3_full_unlock(tp);
6175
6176 return 0;
6177}
6178
6179static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6180 struct ptp_clock_request *rq, int on)
6181{
6182 return -EOPNOTSUPP;
6183}
6184
6185static const struct ptp_clock_info tg3_ptp_caps = {
6186 .owner = THIS_MODULE,
6187 .name = "tg3 clock",
6188 .max_adj = 250000000,
6189 .n_alarm = 0,
6190 .n_ext_ts = 0,
6191 .n_per_out = 0,
6192 .pps = 0,
6193 .adjfreq = tg3_ptp_adjfreq,
6194 .adjtime = tg3_ptp_adjtime,
6195 .gettime = tg3_ptp_gettime,
6196 .settime = tg3_ptp_settime,
6197 .enable = tg3_ptp_enable,
6198};
6199
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006200static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6201 struct skb_shared_hwtstamps *timestamp)
6202{
6203 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6204 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6205 tp->ptp_adjust);
6206}
6207
Matt Carlsonbe947302012-12-03 19:36:57 +00006208/* tp->lock must be held */
6209static void tg3_ptp_init(struct tg3 *tp)
6210{
6211 if (!tg3_flag(tp, PTP_CAPABLE))
6212 return;
6213
6214 /* Initialize the hardware clock to the system time. */
6215 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6216 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006217 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006218}
6219
6220/* tp->lock must be held */
6221static void tg3_ptp_resume(struct tg3 *tp)
6222{
6223 if (!tg3_flag(tp, PTP_CAPABLE))
6224 return;
6225
6226 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6227 tp->ptp_adjust = 0;
6228}
6229
6230static void tg3_ptp_fini(struct tg3 *tp)
6231{
6232 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6233 return;
6234
Matt Carlson7d41e492012-12-03 19:36:58 +00006235 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006236 tp->ptp_clock = NULL;
6237 tp->ptp_adjust = 0;
6238}
6239
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006240static inline int tg3_irq_sync(struct tg3 *tp)
6241{
6242 return tp->irq_sync;
6243}
6244
Matt Carlson97bd8e42011-04-13 11:05:04 +00006245static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6246{
6247 int i;
6248
6249 dst = (u32 *)((u8 *)dst + off);
6250 for (i = 0; i < len; i += sizeof(u32))
6251 *dst++ = tr32(off + i);
6252}
6253
6254static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6255{
6256 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6257 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6258 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6259 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6260 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6261 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6262 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6263 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6264 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6265 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6266 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6267 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6268 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6269 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6270 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6271 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6272 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6273 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6274 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6275
Joe Perches63c3a662011-04-26 08:12:10 +00006276 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006277 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6278
6279 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6280 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6281 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6282 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6283 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6284 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6285 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6286 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6287
Joe Perches63c3a662011-04-26 08:12:10 +00006288 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006289 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6290 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6291 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6292 }
6293
6294 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6295 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6296 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6297 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6298 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6299
Joe Perches63c3a662011-04-26 08:12:10 +00006300 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006301 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6302}
6303
6304static void tg3_dump_state(struct tg3 *tp)
6305{
6306 int i;
6307 u32 *regs;
6308
6309 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006310 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006311 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006312
Joe Perches63c3a662011-04-26 08:12:10 +00006313 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006314 /* Read up to but not including private PCI registers */
6315 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6316 regs[i / sizeof(u32)] = tr32(i);
6317 } else
6318 tg3_dump_legacy_regs(tp, regs);
6319
6320 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6321 if (!regs[i + 0] && !regs[i + 1] &&
6322 !regs[i + 2] && !regs[i + 3])
6323 continue;
6324
6325 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6326 i * 4,
6327 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6328 }
6329
6330 kfree(regs);
6331
6332 for (i = 0; i < tp->irq_cnt; i++) {
6333 struct tg3_napi *tnapi = &tp->napi[i];
6334
6335 /* SW status block */
6336 netdev_err(tp->dev,
6337 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6338 i,
6339 tnapi->hw_status->status,
6340 tnapi->hw_status->status_tag,
6341 tnapi->hw_status->rx_jumbo_consumer,
6342 tnapi->hw_status->rx_consumer,
6343 tnapi->hw_status->rx_mini_consumer,
6344 tnapi->hw_status->idx[0].rx_producer,
6345 tnapi->hw_status->idx[0].tx_consumer);
6346
6347 netdev_err(tp->dev,
6348 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6349 i,
6350 tnapi->last_tag, tnapi->last_irq_tag,
6351 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6352 tnapi->rx_rcb_ptr,
6353 tnapi->prodring.rx_std_prod_idx,
6354 tnapi->prodring.rx_std_cons_idx,
6355 tnapi->prodring.rx_jmb_prod_idx,
6356 tnapi->prodring.rx_jmb_cons_idx);
6357 }
6358}
6359
Michael Chandf3e6542006-05-26 17:48:07 -07006360/* This is called whenever we suspect that the system chipset is re-
6361 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6362 * is bogus tx completions. We try to recover by setting the
6363 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6364 * in the workqueue.
6365 */
6366static void tg3_tx_recover(struct tg3 *tp)
6367{
Joe Perches63c3a662011-04-26 08:12:10 +00006368 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006369 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6370
Matt Carlson5129c3a2010-04-05 10:19:23 +00006371 netdev_warn(tp->dev,
6372 "The system may be re-ordering memory-mapped I/O "
6373 "cycles to the network device, attempting to recover. "
6374 "Please report the problem to the driver maintainer "
6375 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006376
6377 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006378 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006379 spin_unlock(&tp->lock);
6380}
6381
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006382static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006383{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006384 /* Tell compiler to fetch tx indices from memory. */
6385 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006386 return tnapi->tx_pending -
6387 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006388}
6389
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390/* Tigon3 never reports partial packet sends. So we do not
6391 * need special logic to handle SKBs that have not had all
6392 * of their frags sent yet, like SunGEM does.
6393 */
Matt Carlson17375d22009-08-28 14:02:18 +00006394static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395{
Matt Carlson17375d22009-08-28 14:02:18 +00006396 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006397 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006398 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006399 struct netdev_queue *txq;
6400 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006401 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006402
Joe Perches63c3a662011-04-26 08:12:10 +00006403 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006404 index--;
6405
6406 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
6408 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006409 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006411 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412
Michael Chandf3e6542006-05-26 17:48:07 -07006413 if (unlikely(skb == NULL)) {
6414 tg3_tx_recover(tp);
6415 return;
6416 }
6417
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006418 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6419 struct skb_shared_hwtstamps timestamp;
6420 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6421 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6422
6423 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6424
6425 skb_tstamp_tx(skb, &timestamp);
6426 }
6427
Alexander Duyckf4188d82009-12-02 16:48:38 +00006428 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006429 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006430 skb_headlen(skb),
6431 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
6433 ri->skb = NULL;
6434
Matt Carlsone01ee142011-07-27 14:20:50 +00006435 while (ri->fragmented) {
6436 ri->fragmented = false;
6437 sw_idx = NEXT_TX(sw_idx);
6438 ri = &tnapi->tx_buffers[sw_idx];
6439 }
6440
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 sw_idx = NEXT_TX(sw_idx);
6442
6443 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006444 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006445 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6446 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006447
6448 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006449 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006450 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006451 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006452
6453 while (ri->fragmented) {
6454 ri->fragmented = false;
6455 sw_idx = NEXT_TX(sw_idx);
6456 ri = &tnapi->tx_buffers[sw_idx];
6457 }
6458
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 sw_idx = NEXT_TX(sw_idx);
6460 }
6461
Tom Herbert298376d2011-11-28 16:33:30 +00006462 pkts_compl++;
6463 bytes_compl += skb->len;
6464
David S. Millerf47c11e2005-06-24 20:18:35 -07006465 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006466
6467 if (unlikely(tx_bug)) {
6468 tg3_tx_recover(tp);
6469 return;
6470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 }
6472
Tom Herbert5cb917b2012-03-05 19:53:50 +00006473 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006474
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006475 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476
Michael Chan1b2a7202006-08-07 21:46:02 -07006477 /* Need to make the tx_cons update visible to tg3_start_xmit()
6478 * before checking for netif_queue_stopped(). Without the
6479 * memory barrier, there is a small possibility that tg3_start_xmit()
6480 * will miss it and cause the queue to be stopped forever.
6481 */
6482 smp_mb();
6483
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006484 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006485 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006486 __netif_tx_lock(txq, smp_processor_id());
6487 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006488 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006489 netif_tx_wake_queue(txq);
6490 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492}
6493
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006494static void tg3_frag_free(bool is_frag, void *data)
6495{
6496 if (is_frag)
6497 put_page(virt_to_head_page(data));
6498 else
6499 kfree(data);
6500}
6501
Eric Dumazet9205fd92011-11-18 06:47:01 +00006502static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006503{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006504 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6505 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6506
Eric Dumazet9205fd92011-11-18 06:47:01 +00006507 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006508 return;
6509
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006510 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006511 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006512 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006513 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006514}
6515
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006516
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517/* Returns size of skb allocated or < 0 on error.
6518 *
6519 * We only need to fill in the address because the other members
6520 * of the RX descriptor are invariant, see tg3_init_rings.
6521 *
6522 * Note the purposeful assymetry of cpu vs. chip accesses. For
6523 * posting buffers we only dirty the first cache line of the RX
6524 * descriptor (containing the address). Whereas for the RX status
6525 * buffers the cpu only reads the last cacheline of the RX descriptor
6526 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6527 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006528static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006529 u32 opaque_key, u32 dest_idx_unmasked,
6530 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531{
6532 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006533 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006534 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006536 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 switch (opaque_key) {
6539 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006540 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006541 desc = &tpr->rx_std[dest_idx];
6542 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006543 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 break;
6545
6546 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006547 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006548 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006549 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006550 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 break;
6552
6553 default:
6554 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
6557 /* Do not overwrite any of the map or rp information
6558 * until we are sure we can commit to a new buffer.
6559 *
6560 * Callers depend upon this behavior and assume that
6561 * we leave everything unchanged if we fail.
6562 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006563 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6564 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006565 if (skb_size <= PAGE_SIZE) {
6566 data = netdev_alloc_frag(skb_size);
6567 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006568 } else {
6569 data = kmalloc(skb_size, GFP_ATOMIC);
6570 *frag_size = 0;
6571 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006572 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 return -ENOMEM;
6574
Eric Dumazet9205fd92011-11-18 06:47:01 +00006575 mapping = pci_map_single(tp->pdev,
6576 data + TG3_RX_OFFSET(tp),
6577 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006579 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006580 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006581 return -EIO;
6582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
Eric Dumazet9205fd92011-11-18 06:47:01 +00006584 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006585 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 desc->addr_hi = ((u64)mapping >> 32);
6588 desc->addr_lo = ((u64)mapping & 0xffffffff);
6589
Eric Dumazet9205fd92011-11-18 06:47:01 +00006590 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591}
6592
6593/* We only need to move over in the address because the other
6594 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006595 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 */
Matt Carlsona3896162009-11-13 13:03:44 +00006597static void tg3_recycle_rx(struct tg3_napi *tnapi,
6598 struct tg3_rx_prodring_set *dpr,
6599 u32 opaque_key, int src_idx,
6600 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601{
Matt Carlson17375d22009-08-28 14:02:18 +00006602 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6604 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006605 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006606 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607
6608 switch (opaque_key) {
6609 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006610 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006611 dest_desc = &dpr->rx_std[dest_idx];
6612 dest_map = &dpr->rx_std_buffers[dest_idx];
6613 src_desc = &spr->rx_std[src_idx];
6614 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 break;
6616
6617 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006618 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006619 dest_desc = &dpr->rx_jmb[dest_idx].std;
6620 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6621 src_desc = &spr->rx_jmb[src_idx].std;
6622 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 break;
6624
6625 default:
6626 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628
Eric Dumazet9205fd92011-11-18 06:47:01 +00006629 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006630 dma_unmap_addr_set(dest_map, mapping,
6631 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 dest_desc->addr_hi = src_desc->addr_hi;
6633 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006634
6635 /* Ensure that the update to the skb happens after the physical
6636 * addresses have been transferred to the new BD location.
6637 */
6638 smp_wmb();
6639
Eric Dumazet9205fd92011-11-18 06:47:01 +00006640 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641}
6642
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643/* The RX ring scheme is composed of multiple rings which post fresh
6644 * buffers to the chip, and one special ring the chip uses to report
6645 * status back to the host.
6646 *
6647 * The special ring reports the status of received packets to the
6648 * host. The chip does not write into the original descriptor the
6649 * RX buffer was obtained from. The chip simply takes the original
6650 * descriptor as provided by the host, updates the status and length
6651 * field, then writes this into the next status ring entry.
6652 *
6653 * Each ring the host uses to post buffers to the chip is described
6654 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6655 * it is first placed into the on-chip ram. When the packet's length
6656 * is known, it walks down the TG3_BDINFO entries to select the ring.
6657 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6658 * which is within the range of the new packet's length is chosen.
6659 *
6660 * The "separate ring for rx status" scheme may sound queer, but it makes
6661 * sense from a cache coherency perspective. If only the host writes
6662 * to the buffer post rings, and only the chip writes to the rx status
6663 * rings, then cache lines never move beyond shared-modified state.
6664 * If both the host and chip were to write into the same ring, cache line
6665 * eviction could occur since both entities want it in an exclusive state.
6666 */
Matt Carlson17375d22009-08-28 14:02:18 +00006667static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668{
Matt Carlson17375d22009-08-28 14:02:18 +00006669 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006670 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006671 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006672 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006673 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006675 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006677 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 /*
6679 * We need to order the read of hw_idx and the read of
6680 * the opaque cookie.
6681 */
6682 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 work_mask = 0;
6684 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006685 std_prod_idx = tpr->rx_std_prod_idx;
6686 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006688 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006689 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 unsigned int len;
6691 struct sk_buff *skb;
6692 dma_addr_t dma_addr;
6693 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006694 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006695 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696
6697 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6698 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6699 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006700 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006701 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006702 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006703 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006704 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006706 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006707 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006708 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006709 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006710 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
6713 work_mask |= opaque_key;
6714
6715 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6716 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6717 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006718 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 desc_idx, *post_ptr);
6720 drop_it_no_recycle:
6721 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006722 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 goto next_pkt;
6724 }
6725
Eric Dumazet9205fd92011-11-18 06:47:01 +00006726 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006727 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6728 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006730 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6731 RXD_FLAG_PTPSTAT_PTPV1 ||
6732 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6733 RXD_FLAG_PTPSTAT_PTPV2) {
6734 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6735 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6736 }
6737
Matt Carlsond2757fc2010-04-12 06:58:27 +00006738 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006740 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
Eric Dumazet9205fd92011-11-18 06:47:01 +00006742 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006743 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 if (skb_size < 0)
6745 goto drop_it;
6746
Matt Carlson287be122009-08-28 13:58:46 +00006747 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 PCI_DMA_FROMDEVICE);
6749
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006750 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006751 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006752 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006753 goto drop_it_no_recycle;
6754 }
6755 skb_reserve(skb, TG3_RX_OFFSET(tp));
6756 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006757 * after the usage of the old DMA mapping.
6758 */
6759 smp_wmb();
6760
Eric Dumazet9205fd92011-11-18 06:47:01 +00006761 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006762
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006764 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 desc_idx, *post_ptr);
6766
Eric Dumazet9205fd92011-11-18 06:47:01 +00006767 skb = netdev_alloc_skb(tp->dev,
6768 len + TG3_RAW_IP_ALIGN);
6769 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 goto drop_it_no_recycle;
6771
Eric Dumazet9205fd92011-11-18 06:47:01 +00006772 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006774 memcpy(skb->data,
6775 data + TG3_RX_OFFSET(tp),
6776 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 }
6779
Eric Dumazet9205fd92011-11-18 06:47:01 +00006780 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006781 if (tstamp)
6782 tg3_hwclock_to_timestamp(tp, tstamp,
6783 skb_hwtstamps(skb));
6784
Michał Mirosławdc668912011-04-07 03:35:07 +00006785 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6787 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6788 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6789 skb->ip_summed = CHECKSUM_UNNECESSARY;
6790 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006791 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
6793 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006794
6795 if (len > (tp->dev->mtu + ETH_HLEN) &&
6796 skb->protocol != htons(ETH_P_8021Q)) {
6797 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006798 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006799 }
6800
Matt Carlson9dc7a112010-04-12 06:58:28 +00006801 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006802 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006803 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006804 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006805
Matt Carlsonbf933c82011-01-25 15:58:49 +00006806 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 received++;
6809 budget--;
6810
6811next_pkt:
6812 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006813
6814 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006815 tpr->rx_std_prod_idx = std_prod_idx &
6816 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006817 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6818 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006819 work_mask &= ~RXD_OPAQUE_RING_STD;
6820 rx_std_posted = 0;
6821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006823 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006824 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006825
6826 /* Refresh hw_idx to see if there is new work */
6827 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006828 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006829 rmb();
6830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 }
6832
6833 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006834 tnapi->rx_rcb_ptr = sw_idx;
6835 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
6837 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006838 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006839 /* Sync BD data before updating mailbox */
6840 wmb();
6841
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006842 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006843 tpr->rx_std_prod_idx = std_prod_idx &
6844 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006845 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6846 tpr->rx_std_prod_idx);
6847 }
6848 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006849 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6850 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006851 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6852 tpr->rx_jmb_prod_idx);
6853 }
6854 mmiowb();
6855 } else if (work_mask) {
6856 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6857 * updated before the producer indices can be updated.
6858 */
6859 smp_wmb();
6860
Matt Carlson2c49a442010-09-30 10:34:35 +00006861 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6862 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006863
Michael Chan7ae52892012-03-21 15:38:33 +00006864 if (tnapi != &tp->napi[1]) {
6865 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006866 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
6870 return received;
6871}
6872
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006873static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006876 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006877 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6878
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 if (sblk->status & SD_STATUS_LINK_CHG) {
6880 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006881 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006882 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006883 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006884 tw32_f(MAC_STATUS,
6885 (MAC_STATUS_SYNC_CHANGED |
6886 MAC_STATUS_CFG_CHANGED |
6887 MAC_STATUS_MI_COMPLETION |
6888 MAC_STATUS_LNKSTATE_CHANGED));
6889 udelay(40);
6890 } else
Joe Perches953c96e2013-04-09 10:18:14 +00006891 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07006892 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 }
6894 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006895}
6896
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006897static int tg3_rx_prodring_xfer(struct tg3 *tp,
6898 struct tg3_rx_prodring_set *dpr,
6899 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006900{
6901 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006902 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006903
6904 while (1) {
6905 src_prod_idx = spr->rx_std_prod_idx;
6906
6907 /* Make sure updates to the rx_std_buffers[] entries and the
6908 * standard producer index are seen in the correct order.
6909 */
6910 smp_rmb();
6911
6912 if (spr->rx_std_cons_idx == src_prod_idx)
6913 break;
6914
6915 if (spr->rx_std_cons_idx < src_prod_idx)
6916 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6917 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006918 cpycnt = tp->rx_std_ring_mask + 1 -
6919 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006920
Matt Carlson2c49a442010-09-30 10:34:35 +00006921 cpycnt = min(cpycnt,
6922 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006923
6924 si = spr->rx_std_cons_idx;
6925 di = dpr->rx_std_prod_idx;
6926
Matt Carlsone92967b2010-02-12 14:47:06 +00006927 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006928 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006929 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006930 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006931 break;
6932 }
6933 }
6934
6935 if (!cpycnt)
6936 break;
6937
6938 /* Ensure that updates to the rx_std_buffers ring and the
6939 * shadowed hardware producer ring from tg3_recycle_skb() are
6940 * ordered correctly WRT the skb check above.
6941 */
6942 smp_rmb();
6943
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006944 memcpy(&dpr->rx_std_buffers[di],
6945 &spr->rx_std_buffers[si],
6946 cpycnt * sizeof(struct ring_info));
6947
6948 for (i = 0; i < cpycnt; i++, di++, si++) {
6949 struct tg3_rx_buffer_desc *sbd, *dbd;
6950 sbd = &spr->rx_std[si];
6951 dbd = &dpr->rx_std[di];
6952 dbd->addr_hi = sbd->addr_hi;
6953 dbd->addr_lo = sbd->addr_lo;
6954 }
6955
Matt Carlson2c49a442010-09-30 10:34:35 +00006956 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6957 tp->rx_std_ring_mask;
6958 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6959 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006960 }
6961
6962 while (1) {
6963 src_prod_idx = spr->rx_jmb_prod_idx;
6964
6965 /* Make sure updates to the rx_jmb_buffers[] entries and
6966 * the jumbo producer index are seen in the correct order.
6967 */
6968 smp_rmb();
6969
6970 if (spr->rx_jmb_cons_idx == src_prod_idx)
6971 break;
6972
6973 if (spr->rx_jmb_cons_idx < src_prod_idx)
6974 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6975 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006976 cpycnt = tp->rx_jmb_ring_mask + 1 -
6977 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006978
6979 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006980 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006981
6982 si = spr->rx_jmb_cons_idx;
6983 di = dpr->rx_jmb_prod_idx;
6984
Matt Carlsone92967b2010-02-12 14:47:06 +00006985 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006986 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006987 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006988 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006989 break;
6990 }
6991 }
6992
6993 if (!cpycnt)
6994 break;
6995
6996 /* Ensure that updates to the rx_jmb_buffers ring and the
6997 * shadowed hardware producer ring from tg3_recycle_skb() are
6998 * ordered correctly WRT the skb check above.
6999 */
7000 smp_rmb();
7001
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007002 memcpy(&dpr->rx_jmb_buffers[di],
7003 &spr->rx_jmb_buffers[si],
7004 cpycnt * sizeof(struct ring_info));
7005
7006 for (i = 0; i < cpycnt; i++, di++, si++) {
7007 struct tg3_rx_buffer_desc *sbd, *dbd;
7008 sbd = &spr->rx_jmb[si].std;
7009 dbd = &dpr->rx_jmb[di].std;
7010 dbd->addr_hi = sbd->addr_hi;
7011 dbd->addr_lo = sbd->addr_lo;
7012 }
7013
Matt Carlson2c49a442010-09-30 10:34:35 +00007014 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7015 tp->rx_jmb_ring_mask;
7016 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7017 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007018 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007019
7020 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007021}
7022
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007023static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7024{
7025 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
7027 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007028 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007029 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007030 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007031 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 }
7033
Matt Carlsonf891ea12012-04-24 13:37:01 +00007034 if (!tnapi->rx_rcb_prod_idx)
7035 return work_done;
7036
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 /* run RX thread, within the bounds set by NAPI.
7038 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007039 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007041 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007042 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
Joe Perches63c3a662011-04-26 08:12:10 +00007044 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007045 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007046 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007047 u32 std_prod_idx = dpr->rx_std_prod_idx;
7048 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007049
Michael Chan7ae52892012-03-21 15:38:33 +00007050 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007051 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007052 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007053 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007054
7055 wmb();
7056
Matt Carlsone4af1af2010-02-12 14:47:05 +00007057 if (std_prod_idx != dpr->rx_std_prod_idx)
7058 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7059 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007060
Matt Carlsone4af1af2010-02-12 14:47:05 +00007061 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7062 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7063 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007064
7065 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007066
7067 if (err)
7068 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007069 }
7070
David S. Miller6f535762007-10-11 18:08:29 -07007071 return work_done;
7072}
David S. Millerf7383c22005-05-18 22:50:53 -07007073
Matt Carlsondb219972011-11-04 09:15:03 +00007074static inline void tg3_reset_task_schedule(struct tg3 *tp)
7075{
7076 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7077 schedule_work(&tp->reset_task);
7078}
7079
7080static inline void tg3_reset_task_cancel(struct tg3 *tp)
7081{
7082 cancel_work_sync(&tp->reset_task);
7083 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007084 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007085}
7086
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007087static int tg3_poll_msix(struct napi_struct *napi, int budget)
7088{
7089 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7090 struct tg3 *tp = tnapi->tp;
7091 int work_done = 0;
7092 struct tg3_hw_status *sblk = tnapi->hw_status;
7093
7094 while (1) {
7095 work_done = tg3_poll_work(tnapi, work_done, budget);
7096
Joe Perches63c3a662011-04-26 08:12:10 +00007097 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007098 goto tx_recovery;
7099
7100 if (unlikely(work_done >= budget))
7101 break;
7102
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007103 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007104 * to tell the hw how much work has been processed,
7105 * so we must read it before checking for more work.
7106 */
7107 tnapi->last_tag = sblk->status_tag;
7108 tnapi->last_irq_tag = tnapi->last_tag;
7109 rmb();
7110
7111 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007112 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7113 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007114
7115 /* This test here is not race free, but will reduce
7116 * the number of interrupts by looping again.
7117 */
7118 if (tnapi == &tp->napi[1] && tp->rx_refill)
7119 continue;
7120
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007121 napi_complete(napi);
7122 /* Reenable interrupts. */
7123 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007124
7125 /* This test here is synchronized by napi_schedule()
7126 * and napi_complete() to close the race condition.
7127 */
7128 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7129 tw32(HOSTCC_MODE, tp->coalesce_mode |
7130 HOSTCC_MODE_ENABLE |
7131 tnapi->coal_now);
7132 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007133 mmiowb();
7134 break;
7135 }
7136 }
7137
7138 return work_done;
7139
7140tx_recovery:
7141 /* work_done is guaranteed to be less than budget. */
7142 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007143 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007144 return work_done;
7145}
7146
Matt Carlsone64de4e2011-04-13 11:05:05 +00007147static void tg3_process_error(struct tg3 *tp)
7148{
7149 u32 val;
7150 bool real_error = false;
7151
Joe Perches63c3a662011-04-26 08:12:10 +00007152 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007153 return;
7154
7155 /* Check Flow Attention register */
7156 val = tr32(HOSTCC_FLOW_ATTN);
7157 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7158 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7159 real_error = true;
7160 }
7161
7162 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7163 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7164 real_error = true;
7165 }
7166
7167 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7168 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7169 real_error = true;
7170 }
7171
7172 if (!real_error)
7173 return;
7174
7175 tg3_dump_state(tp);
7176
Joe Perches63c3a662011-04-26 08:12:10 +00007177 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007178 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007179}
7180
David S. Miller6f535762007-10-11 18:08:29 -07007181static int tg3_poll(struct napi_struct *napi, int budget)
7182{
Matt Carlson8ef04422009-08-28 14:01:37 +00007183 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7184 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007185 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007186 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007187
7188 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007189 if (sblk->status & SD_STATUS_ERROR)
7190 tg3_process_error(tp);
7191
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007192 tg3_poll_link(tp);
7193
Matt Carlson17375d22009-08-28 14:02:18 +00007194 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007195
Joe Perches63c3a662011-04-26 08:12:10 +00007196 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007197 goto tx_recovery;
7198
7199 if (unlikely(work_done >= budget))
7200 break;
7201
Joe Perches63c3a662011-04-26 08:12:10 +00007202 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007203 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007204 * to tell the hw how much work has been processed,
7205 * so we must read it before checking for more work.
7206 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007207 tnapi->last_tag = sblk->status_tag;
7208 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007209 rmb();
7210 } else
7211 sblk->status &= ~SD_STATUS_UPDATED;
7212
Matt Carlson17375d22009-08-28 14:02:18 +00007213 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007214 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007215 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007216 break;
7217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 }
7219
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007220 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007221
7222tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007223 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007224 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007225 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007226 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227}
7228
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007229static void tg3_napi_disable(struct tg3 *tp)
7230{
7231 int i;
7232
7233 for (i = tp->irq_cnt - 1; i >= 0; i--)
7234 napi_disable(&tp->napi[i].napi);
7235}
7236
7237static void tg3_napi_enable(struct tg3 *tp)
7238{
7239 int i;
7240
7241 for (i = 0; i < tp->irq_cnt; i++)
7242 napi_enable(&tp->napi[i].napi);
7243}
7244
7245static void tg3_napi_init(struct tg3 *tp)
7246{
7247 int i;
7248
7249 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7250 for (i = 1; i < tp->irq_cnt; i++)
7251 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7252}
7253
7254static void tg3_napi_fini(struct tg3 *tp)
7255{
7256 int i;
7257
7258 for (i = 0; i < tp->irq_cnt; i++)
7259 netif_napi_del(&tp->napi[i].napi);
7260}
7261
7262static inline void tg3_netif_stop(struct tg3 *tp)
7263{
7264 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7265 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007266 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007267 netif_tx_disable(tp->dev);
7268}
7269
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007270/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007271static inline void tg3_netif_start(struct tg3 *tp)
7272{
Matt Carlsonbe947302012-12-03 19:36:57 +00007273 tg3_ptp_resume(tp);
7274
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007275 /* NOTE: unconditional netif_tx_wake_all_queues is only
7276 * appropriate so long as all callers are assured to
7277 * have free tx slots (such as after tg3_init_hw)
7278 */
7279 netif_tx_wake_all_queues(tp->dev);
7280
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007281 if (tp->link_up)
7282 netif_carrier_on(tp->dev);
7283
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007284 tg3_napi_enable(tp);
7285 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7286 tg3_enable_ints(tp);
7287}
7288
David S. Millerf47c11e2005-06-24 20:18:35 -07007289static void tg3_irq_quiesce(struct tg3 *tp)
7290{
Matt Carlson4f125f42009-09-01 12:55:02 +00007291 int i;
7292
David S. Millerf47c11e2005-06-24 20:18:35 -07007293 BUG_ON(tp->irq_sync);
7294
7295 tp->irq_sync = 1;
7296 smp_mb();
7297
Matt Carlson4f125f42009-09-01 12:55:02 +00007298 for (i = 0; i < tp->irq_cnt; i++)
7299 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007300}
7301
David S. Millerf47c11e2005-06-24 20:18:35 -07007302/* Fully shutdown all tg3 driver activity elsewhere in the system.
7303 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7304 * with as well. Most of the time, this is not necessary except when
7305 * shutting down the device.
7306 */
7307static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7308{
Michael Chan46966542007-07-11 19:47:19 -07007309 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007310 if (irq_sync)
7311 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007312}
7313
7314static inline void tg3_full_unlock(struct tg3 *tp)
7315{
David S. Millerf47c11e2005-06-24 20:18:35 -07007316 spin_unlock_bh(&tp->lock);
7317}
7318
Michael Chanfcfa0a32006-03-20 22:28:41 -08007319/* One-shot MSI handler - Chip automatically disables interrupt
7320 * after sending MSI so driver doesn't have to do it.
7321 */
David Howells7d12e782006-10-05 14:55:46 +01007322static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007323{
Matt Carlson09943a12009-08-28 14:01:57 +00007324 struct tg3_napi *tnapi = dev_id;
7325 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007326
Matt Carlson898a56f2009-08-28 14:02:40 +00007327 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007328 if (tnapi->rx_rcb)
7329 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007330
7331 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007332 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007333
7334 return IRQ_HANDLED;
7335}
7336
Michael Chan88b06bc22005-04-21 17:13:25 -07007337/* MSI ISR - No need to check for interrupt sharing and no need to
7338 * flush status block and interrupt mailbox. PCI ordering rules
7339 * guarantee that MSI will arrive after the status block.
7340 */
David Howells7d12e782006-10-05 14:55:46 +01007341static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007342{
Matt Carlson09943a12009-08-28 14:01:57 +00007343 struct tg3_napi *tnapi = dev_id;
7344 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007345
Matt Carlson898a56f2009-08-28 14:02:40 +00007346 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007347 if (tnapi->rx_rcb)
7348 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007349 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007350 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007351 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007352 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007353 * NIC to stop sending us irqs, engaging "in-intr-handler"
7354 * event coalescing.
7355 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007356 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007357 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007358 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007359
Michael Chan88b06bc22005-04-21 17:13:25 -07007360 return IRQ_RETVAL(1);
7361}
7362
David Howells7d12e782006-10-05 14:55:46 +01007363static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364{
Matt Carlson09943a12009-08-28 14:01:57 +00007365 struct tg3_napi *tnapi = dev_id;
7366 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007367 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 unsigned int handled = 1;
7369
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 /* In INTx mode, it is possible for the interrupt to arrive at
7371 * the CPU before the status block posted prior to the interrupt.
7372 * Reading the PCI State register will confirm whether the
7373 * interrupt is ours and will flush the status block.
7374 */
Michael Chand18edcb2007-03-24 20:57:11 -07007375 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007376 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007377 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7378 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007379 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007380 }
Michael Chand18edcb2007-03-24 20:57:11 -07007381 }
7382
7383 /*
7384 * Writing any value to intr-mbox-0 clears PCI INTA# and
7385 * chip-internal interrupt pending events.
7386 * Writing non-zero to intr-mbox-0 additional tells the
7387 * NIC to stop sending us irqs, engaging "in-intr-handler"
7388 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007389 *
7390 * Flush the mailbox to de-assert the IRQ immediately to prevent
7391 * spurious interrupts. The flush impacts performance but
7392 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007393 */
Michael Chanc04cb342007-05-07 00:26:15 -07007394 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007395 if (tg3_irq_sync(tp))
7396 goto out;
7397 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007398 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007399 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007400 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007401 } else {
7402 /* No work, shared interrupt perhaps? re-enable
7403 * interrupts, and flush that PCI write
7404 */
7405 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7406 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007407 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007408out:
David S. Millerfac9b832005-05-18 22:46:34 -07007409 return IRQ_RETVAL(handled);
7410}
7411
David Howells7d12e782006-10-05 14:55:46 +01007412static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007413{
Matt Carlson09943a12009-08-28 14:01:57 +00007414 struct tg3_napi *tnapi = dev_id;
7415 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007416 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007417 unsigned int handled = 1;
7418
David S. Millerfac9b832005-05-18 22:46:34 -07007419 /* In INTx mode, it is possible for the interrupt to arrive at
7420 * the CPU before the status block posted prior to the interrupt.
7421 * Reading the PCI State register will confirm whether the
7422 * interrupt is ours and will flush the status block.
7423 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007424 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007425 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007426 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7427 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 }
Michael Chand18edcb2007-03-24 20:57:11 -07007430 }
7431
7432 /*
7433 * writing any value to intr-mbox-0 clears PCI INTA# and
7434 * chip-internal interrupt pending events.
7435 * writing non-zero to intr-mbox-0 additional tells the
7436 * NIC to stop sending us irqs, engaging "in-intr-handler"
7437 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007438 *
7439 * Flush the mailbox to de-assert the IRQ immediately to prevent
7440 * spurious interrupts. The flush impacts performance but
7441 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007442 */
Michael Chanc04cb342007-05-07 00:26:15 -07007443 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007444
7445 /*
7446 * In a shared interrupt configuration, sometimes other devices'
7447 * interrupts will scream. We record the current status tag here
7448 * so that the above check can report that the screaming interrupts
7449 * are unhandled. Eventually they will be silenced.
7450 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007451 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007452
Michael Chand18edcb2007-03-24 20:57:11 -07007453 if (tg3_irq_sync(tp))
7454 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007455
Matt Carlson72334482009-08-28 14:03:01 +00007456 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007457
Matt Carlson09943a12009-08-28 14:01:57 +00007458 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007459
David S. Millerf47c11e2005-06-24 20:18:35 -07007460out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 return IRQ_RETVAL(handled);
7462}
7463
Michael Chan79381092005-04-21 17:13:59 -07007464/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007465static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007466{
Matt Carlson09943a12009-08-28 14:01:57 +00007467 struct tg3_napi *tnapi = dev_id;
7468 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007469 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007470
Michael Chanf9804dd2005-09-27 12:13:10 -07007471 if ((sblk->status & SD_STATUS_UPDATED) ||
7472 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007473 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007474 return IRQ_RETVAL(1);
7475 }
7476 return IRQ_RETVAL(0);
7477}
7478
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479#ifdef CONFIG_NET_POLL_CONTROLLER
7480static void tg3_poll_controller(struct net_device *dev)
7481{
Matt Carlson4f125f42009-09-01 12:55:02 +00007482 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007483 struct tg3 *tp = netdev_priv(dev);
7484
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007485 if (tg3_irq_sync(tp))
7486 return;
7487
Matt Carlson4f125f42009-09-01 12:55:02 +00007488 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007489 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490}
7491#endif
7492
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493static void tg3_tx_timeout(struct net_device *dev)
7494{
7495 struct tg3 *tp = netdev_priv(dev);
7496
Michael Chanb0408752007-02-13 12:18:30 -08007497 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007498 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007499 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501
Matt Carlsondb219972011-11-04 09:15:03 +00007502 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503}
7504
Michael Chanc58ec932005-09-17 00:46:27 -07007505/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7506static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7507{
7508 u32 base = (u32) mapping & 0xffffffff;
7509
Eric Dumazet807540b2010-09-23 05:40:09 +00007510 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007511}
7512
Michael Chan72f2afb2006-03-06 19:28:35 -08007513/* Test for DMA addresses > 40-bit */
7514static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7515 int len)
7516{
7517#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007518 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007519 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007520 return 0;
7521#else
7522 return 0;
7523#endif
7524}
7525
Matt Carlsond1a3b732011-07-27 14:20:51 +00007526static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007527 dma_addr_t mapping, u32 len, u32 flags,
7528 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007529{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007530 txbd->addr_hi = ((u64) mapping >> 32);
7531 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7532 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7533 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007534}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535
Matt Carlson84b67b22011-07-27 14:20:52 +00007536static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007537 dma_addr_t map, u32 len, u32 flags,
7538 u32 mss, u32 vlan)
7539{
7540 struct tg3 *tp = tnapi->tp;
7541 bool hwbug = false;
7542
7543 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007544 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007545
7546 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007547 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007548
7549 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007550 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007551
Matt Carlsona4cb4282011-12-14 11:09:58 +00007552 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007553 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007554 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007555 while (len > tp->dma_limit && *budget) {
7556 u32 frag_len = tp->dma_limit;
7557 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007558
Matt Carlsonb9e45482011-11-04 09:14:59 +00007559 /* Avoid the 8byte DMA problem */
7560 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007561 len += tp->dma_limit / 2;
7562 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007563 }
7564
Matt Carlsonb9e45482011-11-04 09:14:59 +00007565 tnapi->tx_buffers[*entry].fragmented = true;
7566
7567 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7568 frag_len, tmp_flag, mss, vlan);
7569 *budget -= 1;
7570 prvidx = *entry;
7571 *entry = NEXT_TX(*entry);
7572
Matt Carlsone31aa982011-07-27 14:20:53 +00007573 map += frag_len;
7574 }
7575
7576 if (len) {
7577 if (*budget) {
7578 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7579 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007580 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007581 *entry = NEXT_TX(*entry);
7582 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007583 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007584 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007585 }
7586 }
7587 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007588 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7589 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007590 *entry = NEXT_TX(*entry);
7591 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007592
7593 return hwbug;
7594}
7595
Matt Carlson0d681b22011-07-27 14:20:49 +00007596static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007597{
7598 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007599 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007600 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007601
Matt Carlson0d681b22011-07-27 14:20:49 +00007602 skb = txb->skb;
7603 txb->skb = NULL;
7604
Matt Carlson432aa7e2011-05-19 12:12:45 +00007605 pci_unmap_single(tnapi->tp->pdev,
7606 dma_unmap_addr(txb, mapping),
7607 skb_headlen(skb),
7608 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007609
7610 while (txb->fragmented) {
7611 txb->fragmented = false;
7612 entry = NEXT_TX(entry);
7613 txb = &tnapi->tx_buffers[entry];
7614 }
7615
Matt Carlsonba1142e2011-11-04 09:15:00 +00007616 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007617 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007618
7619 entry = NEXT_TX(entry);
7620 txb = &tnapi->tx_buffers[entry];
7621
7622 pci_unmap_page(tnapi->tp->pdev,
7623 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007624 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007625
7626 while (txb->fragmented) {
7627 txb->fragmented = false;
7628 entry = NEXT_TX(entry);
7629 txb = &tnapi->tx_buffers[entry];
7630 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007631 }
7632}
7633
Michael Chan72f2afb2006-03-06 19:28:35 -08007634/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007635static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007636 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007637 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007638 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007640 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007641 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007642 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007643 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644
Joe Perches41535772013-02-16 11:20:04 +00007645 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007646 new_skb = skb_copy(skb, GFP_ATOMIC);
7647 else {
7648 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7649
7650 new_skb = skb_copy_expand(skb,
7651 skb_headroom(skb) + more_headroom,
7652 skb_tailroom(skb), GFP_ATOMIC);
7653 }
7654
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007656 ret = -1;
7657 } else {
7658 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007659 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7660 PCI_DMA_TODEVICE);
7661 /* Make sure the mapping succeeded */
7662 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007663 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007664 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007665 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007666 u32 save_entry = *entry;
7667
Matt Carlson92cd3a12011-07-27 14:20:47 +00007668 base_flags |= TXD_FLAG_END;
7669
Matt Carlson84b67b22011-07-27 14:20:52 +00007670 tnapi->tx_buffers[*entry].skb = new_skb;
7671 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007672 mapping, new_addr);
7673
Matt Carlson84b67b22011-07-27 14:20:52 +00007674 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007675 new_skb->len, base_flags,
7676 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007677 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007678 dev_kfree_skb(new_skb);
7679 ret = -1;
7680 }
Michael Chanc58ec932005-09-17 00:46:27 -07007681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682 }
7683
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007685 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007686 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687}
7688
Matt Carlson2ffcc982011-05-19 12:12:44 +00007689static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007690
7691/* Use GSO to workaround a rare TSO bug that may be triggered when the
7692 * TSO header is greater than 80 bytes.
7693 */
7694static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7695{
7696 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007697 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007698
7699 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007700 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007701 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007702
7703 /* netif_tx_stop_queue() must be done before checking
7704 * checking tx index in tg3_tx_avail() below, because in
7705 * tg3_tx(), we update tx index before checking for
7706 * netif_tx_queue_stopped().
7707 */
7708 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007709 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007710 return NETDEV_TX_BUSY;
7711
7712 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007713 }
7714
7715 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007716 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007717 goto tg3_tso_bug_end;
7718
7719 do {
7720 nskb = segs;
7721 segs = segs->next;
7722 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007723 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007724 } while (segs);
7725
7726tg3_tso_bug_end:
7727 dev_kfree_skb(skb);
7728
7729 return NETDEV_TX_OK;
7730}
Michael Chan52c0fd82006-06-29 20:15:54 -07007731
Michael Chan5a6f3072006-03-20 22:28:05 -08007732/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007733 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007734 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007735static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007736{
7737 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007738 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007739 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007740 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007741 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007742 struct tg3_napi *tnapi;
7743 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007744 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007745
Matt Carlson24f4efd2009-11-13 13:03:35 +00007746 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7747 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007748 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007749 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750
Matt Carlson84b67b22011-07-27 14:20:52 +00007751 budget = tg3_tx_avail(tnapi);
7752
Michael Chan00b70502006-06-17 21:58:45 -07007753 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007754 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007755 * interrupt. Furthermore, IRQ processing runs lockless so we have
7756 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007758 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007759 if (!netif_tx_queue_stopped(txq)) {
7760 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007761
7762 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007763 netdev_err(dev,
7764 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 return NETDEV_TX_BUSY;
7767 }
7768
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007769 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007771 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007773
Matt Carlsonbe98da62010-07-11 09:31:46 +00007774 mss = skb_shinfo(skb)->gso_size;
7775 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007776 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007777 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778
7779 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007780 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7781 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782
Matt Carlson34195c32010-07-11 09:31:42 +00007783 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007784 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785
Eric Dumazeta5a11952012-01-23 01:22:09 +00007786 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007787
Eric Dumazeta5a11952012-01-23 01:22:09 +00007788 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007789 iph->check = 0;
7790 iph->tot_len = htons(mss + hdr_len);
7791 }
7792
Michael Chan52c0fd82006-06-29 20:15:54 -07007793 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007794 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007795 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007796
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7798 TXD_FLAG_CPU_POST_DMA);
7799
Joe Perches63c3a662011-04-26 08:12:10 +00007800 if (tg3_flag(tp, HW_TSO_1) ||
7801 tg3_flag(tp, HW_TSO_2) ||
7802 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007803 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007805 } else
7806 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7807 iph->daddr, 0,
7808 IPPROTO_TCP,
7809 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810
Joe Perches63c3a662011-04-26 08:12:10 +00007811 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007812 mss |= (hdr_len & 0xc) << 12;
7813 if (hdr_len & 0x10)
7814 base_flags |= 0x00000010;
7815 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007816 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007817 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007818 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007819 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007820 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 int tsflags;
7822
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007823 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 mss |= (tsflags << 11);
7825 }
7826 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007827 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828 int tsflags;
7829
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007830 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831 base_flags |= tsflags << 12;
7832 }
7833 }
7834 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007835
Matt Carlson93a700a2011-08-31 11:44:54 +00007836 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7837 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7838 base_flags |= TXD_FLAG_JMB_PKT;
7839
Matt Carlson92cd3a12011-07-27 14:20:47 +00007840 if (vlan_tx_tag_present(skb)) {
7841 base_flags |= TXD_FLAG_VLAN;
7842 vlan = vlan_tx_tag_get(skb);
7843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007845 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7846 tg3_flag(tp, TX_TSTAMP_EN)) {
7847 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7848 base_flags |= TXD_FLAG_HWTSTAMP;
7849 }
7850
Alexander Duyckf4188d82009-12-02 16:48:38 +00007851 len = skb_headlen(skb);
7852
7853 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007854 if (pci_dma_mapping_error(tp->pdev, mapping))
7855 goto drop;
7856
David S. Miller90079ce2008-09-11 04:52:51 -07007857
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007858 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007859 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
7861 would_hit_hwbug = 0;
7862
Joe Perches63c3a662011-04-26 08:12:10 +00007863 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007864 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865
Matt Carlson84b67b22011-07-27 14:20:52 +00007866 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007867 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007868 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007869 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007870 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007871 u32 tmp_mss = mss;
7872
7873 if (!tg3_flag(tp, HW_TSO_1) &&
7874 !tg3_flag(tp, HW_TSO_2) &&
7875 !tg3_flag(tp, HW_TSO_3))
7876 tmp_mss = 0;
7877
Matt Carlsonc5665a52012-02-13 10:20:12 +00007878 /* Now loop through additional data
7879 * fragments, and queue them.
7880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 last = skb_shinfo(skb)->nr_frags - 1;
7882 for (i = 0; i <= last; i++) {
7883 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7884
Eric Dumazet9e903e02011-10-18 21:00:24 +00007885 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007886 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007887 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007889 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007890 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007891 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007892 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007893 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
Matt Carlsonb9e45482011-11-04 09:14:59 +00007895 if (!budget ||
7896 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007897 len, base_flags |
7898 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007899 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007900 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007901 break;
7902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 }
7904 }
7905
7906 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007907 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
7909 /* If the workaround fails due to memory/mapping
7910 * failure, silently drop this packet.
7911 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007912 entry = tnapi->tx_prod;
7913 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007914 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007915 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007916 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 }
7918
Richard Cochrand515b452011-06-19 03:31:41 +00007919 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007920 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007921
Michael Chan6541b802012-03-04 14:48:14 +00007922 /* Sync BD data before updating mailbox */
7923 wmb();
7924
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007926 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007928 tnapi->tx_prod = entry;
7929 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007930 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007931
7932 /* netif_tx_stop_queue() must be done before checking
7933 * checking tx index in tg3_tx_avail() below, because in
7934 * tg3_tx(), we update tx index before checking for
7935 * netif_tx_queue_stopped().
7936 */
7937 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007938 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007939 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007942 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007944
7945dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007946 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007947 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007948drop:
7949 dev_kfree_skb(skb);
7950drop_nofree:
7951 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007952 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953}
7954
Matt Carlson6e01b202011-08-19 13:58:20 +00007955static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7956{
7957 if (enable) {
7958 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7959 MAC_MODE_PORT_MODE_MASK);
7960
7961 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7962
7963 if (!tg3_flag(tp, 5705_PLUS))
7964 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7965
7966 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7967 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7968 else
7969 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7970 } else {
7971 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7972
7973 if (tg3_flag(tp, 5705_PLUS) ||
7974 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00007975 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00007976 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7977 }
7978
7979 tw32(MAC_MODE, tp->mac_mode);
7980 udelay(40);
7981}
7982
Matt Carlson941ec902011-08-19 13:58:23 +00007983static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007984{
Matt Carlson941ec902011-08-19 13:58:23 +00007985 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007986
7987 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00007988 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007989
Matt Carlson941ec902011-08-19 13:58:23 +00007990 if (extlpbk && tg3_phy_set_extloopbk(tp))
7991 return -EIO;
7992
7993 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007994 switch (speed) {
7995 case SPEED_10:
7996 break;
7997 case SPEED_100:
7998 bmcr |= BMCR_SPEED100;
7999 break;
8000 case SPEED_1000:
8001 default:
8002 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8003 speed = SPEED_100;
8004 bmcr |= BMCR_SPEED100;
8005 } else {
8006 speed = SPEED_1000;
8007 bmcr |= BMCR_SPEED1000;
8008 }
8009 }
8010
Matt Carlson941ec902011-08-19 13:58:23 +00008011 if (extlpbk) {
8012 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8013 tg3_readphy(tp, MII_CTRL1000, &val);
8014 val |= CTL1000_AS_MASTER |
8015 CTL1000_ENABLE_MASTER;
8016 tg3_writephy(tp, MII_CTRL1000, val);
8017 } else {
8018 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8019 MII_TG3_FET_PTEST_TRIM_2;
8020 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8021 }
8022 } else
8023 bmcr |= BMCR_LOOPBACK;
8024
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008025 tg3_writephy(tp, MII_BMCR, bmcr);
8026
8027 /* The write needs to be flushed for the FETs */
8028 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8029 tg3_readphy(tp, MII_BMCR, &bmcr);
8030
8031 udelay(40);
8032
8033 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008034 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008035 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008036 MII_TG3_FET_PTEST_FRC_TX_LINK |
8037 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8038
8039 /* The write needs to be flushed for the AC131 */
8040 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8041 }
8042
8043 /* Reset to prevent losing 1st rx packet intermittently */
8044 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8045 tg3_flag(tp, 5780_CLASS)) {
8046 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8047 udelay(10);
8048 tw32_f(MAC_RX_MODE, tp->rx_mode);
8049 }
8050
8051 mac_mode = tp->mac_mode &
8052 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8053 if (speed == SPEED_1000)
8054 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8055 else
8056 mac_mode |= MAC_MODE_PORT_MODE_MII;
8057
Joe Perches41535772013-02-16 11:20:04 +00008058 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008059 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8060
8061 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8062 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8063 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8064 mac_mode |= MAC_MODE_LINK_POLARITY;
8065
8066 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8067 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8068 }
8069
8070 tw32(MAC_MODE, mac_mode);
8071 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008072
8073 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008074}
8075
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008076static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008077{
8078 struct tg3 *tp = netdev_priv(dev);
8079
8080 if (features & NETIF_F_LOOPBACK) {
8081 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8082 return;
8083
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008084 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008085 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008086 netif_carrier_on(tp->dev);
8087 spin_unlock_bh(&tp->lock);
8088 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8089 } else {
8090 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8091 return;
8092
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008093 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008094 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008095 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008096 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008097 spin_unlock_bh(&tp->lock);
8098 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8099 }
8100}
8101
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008102static netdev_features_t tg3_fix_features(struct net_device *dev,
8103 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008104{
8105 struct tg3 *tp = netdev_priv(dev);
8106
Joe Perches63c3a662011-04-26 08:12:10 +00008107 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008108 features &= ~NETIF_F_ALL_TSO;
8109
8110 return features;
8111}
8112
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008113static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008114{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008115 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008116
8117 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8118 tg3_set_loopback(dev, features);
8119
8120 return 0;
8121}
8122
Matt Carlson21f581a2009-08-28 14:00:25 +00008123static void tg3_rx_prodring_free(struct tg3 *tp,
8124 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 int i;
8127
Matt Carlson8fea32b2010-09-15 08:59:58 +00008128 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008129 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008130 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008131 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008132 tp->rx_pkt_map_sz);
8133
Joe Perches63c3a662011-04-26 08:12:10 +00008134 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008135 for (i = tpr->rx_jmb_cons_idx;
8136 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008137 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008138 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008139 TG3_RX_JMB_MAP_SZ);
8140 }
8141 }
8142
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008143 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
Matt Carlson2c49a442010-09-30 10:34:35 +00008146 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008147 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008148 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149
Joe Perches63c3a662011-04-26 08:12:10 +00008150 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008151 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008152 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008153 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 }
8155}
8156
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008157/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 *
8159 * The chip has been shut down and the driver detached from
8160 * the networking, so no interrupts or new tx packets will
8161 * end up in the driver. tp->{tx,}lock are held and thus
8162 * we may not sleep.
8163 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008164static int tg3_rx_prodring_alloc(struct tg3 *tp,
8165 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166{
Matt Carlson287be122009-08-28 13:58:46 +00008167 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008169 tpr->rx_std_cons_idx = 0;
8170 tpr->rx_std_prod_idx = 0;
8171 tpr->rx_jmb_cons_idx = 0;
8172 tpr->rx_jmb_prod_idx = 0;
8173
Matt Carlson8fea32b2010-09-15 08:59:58 +00008174 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008175 memset(&tpr->rx_std_buffers[0], 0,
8176 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008177 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008178 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008179 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008180 goto done;
8181 }
8182
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008184 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185
Matt Carlson287be122009-08-28 13:58:46 +00008186 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008187 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008188 tp->dev->mtu > ETH_DATA_LEN)
8189 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8190 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008191
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 /* Initialize invariants of the rings, we only set this
8193 * stuff once. This works because the card does not
8194 * write into the rx buffer posting rings.
8195 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008196 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 struct tg3_rx_buffer_desc *rxd;
8198
Matt Carlson21f581a2009-08-28 14:00:25 +00008199 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008200 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8202 rxd->opaque = (RXD_OPAQUE_RING_STD |
8203 (i << RXD_OPAQUE_INDEX_SHIFT));
8204 }
8205
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008206 /* Now allocate fresh SKBs for each rx ring. */
8207 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008208 unsigned int frag_size;
8209
8210 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8211 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008212 netdev_warn(tp->dev,
8213 "Using a smaller RX standard ring. Only "
8214 "%d out of %d buffers were allocated "
8215 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008216 if (i == 0)
8217 goto initfail;
8218 tp->rx_pending = i;
8219 break;
8220 }
8221 }
8222
Joe Perches63c3a662011-04-26 08:12:10 +00008223 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008224 goto done;
8225
Matt Carlson2c49a442010-09-30 10:34:35 +00008226 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008227
Joe Perches63c3a662011-04-26 08:12:10 +00008228 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008229 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230
Matt Carlson2c49a442010-09-30 10:34:35 +00008231 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008232 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233
Matt Carlson0d86df82010-02-17 15:17:00 +00008234 rxd = &tpr->rx_jmb[i].std;
8235 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8236 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8237 RXD_FLAG_JUMBO;
8238 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8239 (i << RXD_OPAQUE_INDEX_SHIFT));
8240 }
8241
8242 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008243 unsigned int frag_size;
8244
8245 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8246 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008247 netdev_warn(tp->dev,
8248 "Using a smaller RX jumbo ring. Only %d "
8249 "out of %d buffers were allocated "
8250 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008251 if (i == 0)
8252 goto initfail;
8253 tp->rx_jumbo_pending = i;
8254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255 }
8256 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008257
8258done:
Michael Chan32d8c572006-07-25 16:38:29 -07008259 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008260
8261initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008262 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008263 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264}
8265
Matt Carlson21f581a2009-08-28 14:00:25 +00008266static void tg3_rx_prodring_fini(struct tg3 *tp,
8267 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268{
Matt Carlson21f581a2009-08-28 14:00:25 +00008269 kfree(tpr->rx_std_buffers);
8270 tpr->rx_std_buffers = NULL;
8271 kfree(tpr->rx_jmb_buffers);
8272 tpr->rx_jmb_buffers = NULL;
8273 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008274 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8275 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008276 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008278 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008279 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8280 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008281 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008283}
8284
Matt Carlson21f581a2009-08-28 14:00:25 +00008285static int tg3_rx_prodring_init(struct tg3 *tp,
8286 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008287{
Matt Carlson2c49a442010-09-30 10:34:35 +00008288 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8289 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008290 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008291 return -ENOMEM;
8292
Matt Carlson4bae65c2010-11-24 08:31:52 +00008293 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8294 TG3_RX_STD_RING_BYTES(tp),
8295 &tpr->rx_std_mapping,
8296 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008297 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008298 goto err_out;
8299
Joe Perches63c3a662011-04-26 08:12:10 +00008300 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008301 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008302 GFP_KERNEL);
8303 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008304 goto err_out;
8305
Matt Carlson4bae65c2010-11-24 08:31:52 +00008306 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8307 TG3_RX_JMB_RING_BYTES(tp),
8308 &tpr->rx_jmb_mapping,
8309 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008310 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008311 goto err_out;
8312 }
8313
8314 return 0;
8315
8316err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008317 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008318 return -ENOMEM;
8319}
8320
8321/* Free up pending packets in all rx/tx rings.
8322 *
8323 * The chip has been shut down and the driver detached from
8324 * the networking, so no interrupts or new tx packets will
8325 * end up in the driver. tp->{tx,}lock is not held and we are not
8326 * in an interrupt context and thus may sleep.
8327 */
8328static void tg3_free_rings(struct tg3 *tp)
8329{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008330 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008331
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008332 for (j = 0; j < tp->irq_cnt; j++) {
8333 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008334
Matt Carlson8fea32b2010-09-15 08:59:58 +00008335 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008336
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008337 if (!tnapi->tx_buffers)
8338 continue;
8339
Matt Carlson0d681b22011-07-27 14:20:49 +00008340 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8341 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008342
Matt Carlson0d681b22011-07-27 14:20:49 +00008343 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008344 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008345
Matt Carlsonba1142e2011-11-04 09:15:00 +00008346 tg3_tx_skb_unmap(tnapi, i,
8347 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008348
8349 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008350 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008351 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008352 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008353}
8354
8355/* Initialize tx/rx rings for packet processing.
8356 *
8357 * The chip has been shut down and the driver detached from
8358 * the networking, so no interrupts or new tx packets will
8359 * end up in the driver. tp->{tx,}lock are held and thus
8360 * we may not sleep.
8361 */
8362static int tg3_init_rings(struct tg3 *tp)
8363{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008364 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008365
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008366 /* Free up all the SKBs. */
8367 tg3_free_rings(tp);
8368
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008369 for (i = 0; i < tp->irq_cnt; i++) {
8370 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008371
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008372 tnapi->last_tag = 0;
8373 tnapi->last_irq_tag = 0;
8374 tnapi->hw_status->status = 0;
8375 tnapi->hw_status->status_tag = 0;
8376 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8377
8378 tnapi->tx_prod = 0;
8379 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008380 if (tnapi->tx_ring)
8381 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008382
8383 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008384 if (tnapi->rx_rcb)
8385 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008386
Matt Carlson8fea32b2010-09-15 08:59:58 +00008387 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008388 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008389 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008390 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008391 }
Matt Carlson72334482009-08-28 14:03:01 +00008392
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008393 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008394}
8395
Michael Chan49a359e2012-09-28 07:12:37 +00008396static void tg3_mem_tx_release(struct tg3 *tp)
8397{
8398 int i;
8399
8400 for (i = 0; i < tp->irq_max; i++) {
8401 struct tg3_napi *tnapi = &tp->napi[i];
8402
8403 if (tnapi->tx_ring) {
8404 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8405 tnapi->tx_ring, tnapi->tx_desc_mapping);
8406 tnapi->tx_ring = NULL;
8407 }
8408
8409 kfree(tnapi->tx_buffers);
8410 tnapi->tx_buffers = NULL;
8411 }
8412}
8413
8414static int tg3_mem_tx_acquire(struct tg3 *tp)
8415{
8416 int i;
8417 struct tg3_napi *tnapi = &tp->napi[0];
8418
8419 /* If multivector TSS is enabled, vector 0 does not handle
8420 * tx interrupts. Don't allocate any resources for it.
8421 */
8422 if (tg3_flag(tp, ENABLE_TSS))
8423 tnapi++;
8424
8425 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8426 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8427 TG3_TX_RING_SIZE, GFP_KERNEL);
8428 if (!tnapi->tx_buffers)
8429 goto err_out;
8430
8431 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8432 TG3_TX_RING_BYTES,
8433 &tnapi->tx_desc_mapping,
8434 GFP_KERNEL);
8435 if (!tnapi->tx_ring)
8436 goto err_out;
8437 }
8438
8439 return 0;
8440
8441err_out:
8442 tg3_mem_tx_release(tp);
8443 return -ENOMEM;
8444}
8445
8446static void tg3_mem_rx_release(struct tg3 *tp)
8447{
8448 int i;
8449
8450 for (i = 0; i < tp->irq_max; i++) {
8451 struct tg3_napi *tnapi = &tp->napi[i];
8452
8453 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8454
8455 if (!tnapi->rx_rcb)
8456 continue;
8457
8458 dma_free_coherent(&tp->pdev->dev,
8459 TG3_RX_RCB_RING_BYTES(tp),
8460 tnapi->rx_rcb,
8461 tnapi->rx_rcb_mapping);
8462 tnapi->rx_rcb = NULL;
8463 }
8464}
8465
8466static int tg3_mem_rx_acquire(struct tg3 *tp)
8467{
8468 unsigned int i, limit;
8469
8470 limit = tp->rxq_cnt;
8471
8472 /* If RSS is enabled, we need a (dummy) producer ring
8473 * set on vector zero. This is the true hw prodring.
8474 */
8475 if (tg3_flag(tp, ENABLE_RSS))
8476 limit++;
8477
8478 for (i = 0; i < limit; i++) {
8479 struct tg3_napi *tnapi = &tp->napi[i];
8480
8481 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8482 goto err_out;
8483
8484 /* If multivector RSS is enabled, vector 0
8485 * does not handle rx or tx interrupts.
8486 * Don't allocate any resources for it.
8487 */
8488 if (!i && tg3_flag(tp, ENABLE_RSS))
8489 continue;
8490
8491 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8492 TG3_RX_RCB_RING_BYTES(tp),
8493 &tnapi->rx_rcb_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008494 GFP_KERNEL | __GFP_ZERO);
Michael Chan49a359e2012-09-28 07:12:37 +00008495 if (!tnapi->rx_rcb)
8496 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008497 }
8498
8499 return 0;
8500
8501err_out:
8502 tg3_mem_rx_release(tp);
8503 return -ENOMEM;
8504}
8505
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008506/*
8507 * Must not be invoked with interrupt sources disabled and
8508 * the hardware shutdown down.
8509 */
8510static void tg3_free_consistent(struct tg3 *tp)
8511{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008512 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008513
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008514 for (i = 0; i < tp->irq_cnt; i++) {
8515 struct tg3_napi *tnapi = &tp->napi[i];
8516
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008517 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008518 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8519 tnapi->hw_status,
8520 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008521 tnapi->hw_status = NULL;
8522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008524
Michael Chan49a359e2012-09-28 07:12:37 +00008525 tg3_mem_rx_release(tp);
8526 tg3_mem_tx_release(tp);
8527
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008529 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8530 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 tp->hw_stats = NULL;
8532 }
8533}
8534
8535/*
8536 * Must not be invoked with interrupt sources disabled and
8537 * the hardware shutdown down. Can sleep.
8538 */
8539static int tg3_alloc_consistent(struct tg3 *tp)
8540{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008541 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008542
Matt Carlson4bae65c2010-11-24 08:31:52 +00008543 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8544 sizeof(struct tg3_hw_stats),
8545 &tp->stats_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008546 GFP_KERNEL | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 if (!tp->hw_stats)
8548 goto err_out;
8549
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008550 for (i = 0; i < tp->irq_cnt; i++) {
8551 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008552 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008553
Matt Carlson4bae65c2010-11-24 08:31:52 +00008554 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8555 TG3_HW_STATUS_SIZE,
8556 &tnapi->status_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008557 GFP_KERNEL | __GFP_ZERO);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008558 if (!tnapi->hw_status)
8559 goto err_out;
8560
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008561 sblk = tnapi->hw_status;
8562
Michael Chan49a359e2012-09-28 07:12:37 +00008563 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008564 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008565
Michael Chan49a359e2012-09-28 07:12:37 +00008566 /*
8567 * When RSS is enabled, the status block format changes
8568 * slightly. The "rx_jumbo_consumer", "reserved",
8569 * and "rx_mini_consumer" members get mapped to the
8570 * other three rx return ring producer indexes.
8571 */
8572 switch (i) {
8573 case 1:
8574 prodptr = &sblk->idx[0].rx_producer;
8575 break;
8576 case 2:
8577 prodptr = &sblk->rx_jumbo_consumer;
8578 break;
8579 case 3:
8580 prodptr = &sblk->reserved;
8581 break;
8582 case 4:
8583 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008584 break;
8585 }
Michael Chan49a359e2012-09-28 07:12:37 +00008586 tnapi->rx_rcb_prod_idx = prodptr;
8587 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008588 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008589 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008590 }
8591
Michael Chan49a359e2012-09-28 07:12:37 +00008592 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8593 goto err_out;
8594
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595 return 0;
8596
8597err_out:
8598 tg3_free_consistent(tp);
8599 return -ENOMEM;
8600}
8601
8602#define MAX_WAIT_CNT 1000
8603
8604/* To stop a block, clear the enable bit and poll till it
8605 * clears. tp->lock is held.
8606 */
Joe Perches953c96e2013-04-09 10:18:14 +00008607static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608{
8609 unsigned int i;
8610 u32 val;
8611
Joe Perches63c3a662011-04-26 08:12:10 +00008612 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 switch (ofs) {
8614 case RCVLSC_MODE:
8615 case DMAC_MODE:
8616 case MBFREE_MODE:
8617 case BUFMGR_MODE:
8618 case MEMARB_MODE:
8619 /* We can't enable/disable these bits of the
8620 * 5705/5750, just say success.
8621 */
8622 return 0;
8623
8624 default:
8625 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627 }
8628
8629 val = tr32(ofs);
8630 val &= ~enable_bit;
8631 tw32_f(ofs, val);
8632
8633 for (i = 0; i < MAX_WAIT_CNT; i++) {
8634 udelay(100);
8635 val = tr32(ofs);
8636 if ((val & enable_bit) == 0)
8637 break;
8638 }
8639
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008640 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008641 dev_err(&tp->pdev->dev,
8642 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8643 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644 return -ENODEV;
8645 }
8646
8647 return 0;
8648}
8649
8650/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008651static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008652{
8653 int i, err;
8654
8655 tg3_disable_ints(tp);
8656
8657 tp->rx_mode &= ~RX_MODE_ENABLE;
8658 tw32_f(MAC_RX_MODE, tp->rx_mode);
8659 udelay(10);
8660
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008661 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8662 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8663 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8664 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8665 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8666 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008668 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8669 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8670 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8671 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8672 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8673 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8674 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675
8676 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8677 tw32_f(MAC_MODE, tp->mac_mode);
8678 udelay(40);
8679
8680 tp->tx_mode &= ~TX_MODE_ENABLE;
8681 tw32_f(MAC_TX_MODE, tp->tx_mode);
8682
8683 for (i = 0; i < MAX_WAIT_CNT; i++) {
8684 udelay(100);
8685 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8686 break;
8687 }
8688 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008689 dev_err(&tp->pdev->dev,
8690 "%s timed out, TX_MODE_ENABLE will not clear "
8691 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008692 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 }
8694
Michael Chane6de8ad2005-05-05 14:42:41 -07008695 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008696 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8697 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698
8699 tw32(FTQ_RESET, 0xffffffff);
8700 tw32(FTQ_RESET, 0x00000000);
8701
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008702 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8703 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008705 for (i = 0; i < tp->irq_cnt; i++) {
8706 struct tg3_napi *tnapi = &tp->napi[i];
8707 if (tnapi->hw_status)
8708 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710
Linus Torvalds1da177e2005-04-16 15:20:36 -07008711 return err;
8712}
8713
Michael Chanee6a99b2007-07-18 21:49:10 -07008714/* Save PCI command register before chip reset */
8715static void tg3_save_pci_state(struct tg3 *tp)
8716{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008717 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008718}
8719
8720/* Restore PCI state after chip reset */
8721static void tg3_restore_pci_state(struct tg3 *tp)
8722{
8723 u32 val;
8724
8725 /* Re-enable indirect register accesses. */
8726 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8727 tp->misc_host_ctrl);
8728
8729 /* Set MAX PCI retry to zero. */
8730 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008731 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008732 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008733 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008734 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008735 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008736 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008737 PCISTATE_ALLOW_APE_SHMEM_WR |
8738 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008739 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8740
Matt Carlson8a6eac92007-10-21 16:17:55 -07008741 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008742
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008743 if (!tg3_flag(tp, PCI_EXPRESS)) {
8744 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8745 tp->pci_cacheline_sz);
8746 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8747 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008748 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008749
Michael Chanee6a99b2007-07-18 21:49:10 -07008750 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008751 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008752 u16 pcix_cmd;
8753
8754 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8755 &pcix_cmd);
8756 pcix_cmd &= ~PCI_X_CMD_ERO;
8757 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8758 pcix_cmd);
8759 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008760
Joe Perches63c3a662011-04-26 08:12:10 +00008761 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008762
8763 /* Chip reset on 5780 will reset MSI enable bit,
8764 * so need to restore it.
8765 */
Joe Perches63c3a662011-04-26 08:12:10 +00008766 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008767 u16 ctrl;
8768
8769 pci_read_config_word(tp->pdev,
8770 tp->msi_cap + PCI_MSI_FLAGS,
8771 &ctrl);
8772 pci_write_config_word(tp->pdev,
8773 tp->msi_cap + PCI_MSI_FLAGS,
8774 ctrl | PCI_MSI_FLAGS_ENABLE);
8775 val = tr32(MSGINT_MODE);
8776 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8777 }
8778 }
8779}
8780
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781/* tp->lock is held. */
8782static int tg3_chip_reset(struct tg3 *tp)
8783{
8784 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008785 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008786 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008787
David S. Millerf49639e2006-06-09 11:58:36 -07008788 tg3_nvram_lock(tp);
8789
Matt Carlson77b483f2008-08-15 14:07:24 -07008790 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8791
David S. Millerf49639e2006-06-09 11:58:36 -07008792 /* No matching tg3_nvram_unlock() after this because
8793 * chip reset below will undo the nvram lock.
8794 */
8795 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796
Michael Chanee6a99b2007-07-18 21:49:10 -07008797 /* GRC_MISC_CFG core clock reset will clear the memory
8798 * enable bit in PCI register 4 and the MSI enable bit
8799 * on some chips, so we save relevant registers here.
8800 */
8801 tg3_save_pci_state(tp);
8802
Joe Perches41535772013-02-16 11:20:04 +00008803 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008804 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008805 tw32(GRC_FASTBOOT_PC, 0);
8806
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807 /*
8808 * We must avoid the readl() that normally takes place.
8809 * It locks machines, causes machine checks, and other
8810 * fun things. So, temporarily disable the 5701
8811 * hardware workaround, while we do the reset.
8812 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008813 write_op = tp->write32;
8814 if (write_op == tg3_write_flush_reg32)
8815 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008816
Michael Chand18edcb2007-03-24 20:57:11 -07008817 /* Prevent the irq handler from reading or writing PCI registers
8818 * during chip reset when the memory enable bit in the PCI command
8819 * register may be cleared. The chip does not generate interrupt
8820 * at this time, but the irq handler may still be called due to irq
8821 * sharing or irqpoll.
8822 */
Joe Perches63c3a662011-04-26 08:12:10 +00008823 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008824 for (i = 0; i < tp->irq_cnt; i++) {
8825 struct tg3_napi *tnapi = &tp->napi[i];
8826 if (tnapi->hw_status) {
8827 tnapi->hw_status->status = 0;
8828 tnapi->hw_status->status_tag = 0;
8829 }
8830 tnapi->last_tag = 0;
8831 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008832 }
Michael Chand18edcb2007-03-24 20:57:11 -07008833 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008834
8835 for (i = 0; i < tp->irq_cnt; i++)
8836 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008837
Joe Perches41535772013-02-16 11:20:04 +00008838 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008839 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8840 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8841 }
8842
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 /* do the reset */
8844 val = GRC_MISC_CFG_CORECLK_RESET;
8845
Joe Perches63c3a662011-04-26 08:12:10 +00008846 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008847 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008848 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008849 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008850 tr32(TG3_PCIE_PHY_TSTCTL) ==
8851 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8852 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8853
Joe Perches41535772013-02-16 11:20:04 +00008854 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008855 tw32(GRC_MISC_CFG, (1 << 29));
8856 val |= (1 << 29);
8857 }
8858 }
8859
Joe Perches41535772013-02-16 11:20:04 +00008860 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008861 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8862 tw32(GRC_VCPU_EXT_CTRL,
8863 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8864 }
8865
Matt Carlsonf37500d2010-08-02 11:25:59 +00008866 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008867 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008869
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 tw32(GRC_MISC_CFG, val);
8871
Michael Chan1ee582d2005-08-09 20:16:46 -07008872 /* restore 5701 hardware bug workaround write method */
8873 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874
8875 /* Unfortunately, we have to delay before the PCI read back.
8876 * Some 575X chips even will not respond to a PCI cfg access
8877 * when the reset command is given to the chip.
8878 *
8879 * How do these hardware designers expect things to work
8880 * properly if the PCI write is posted for a long period
8881 * of time? It is always necessary to have some method by
8882 * which a register read back can occur to push the write
8883 * out which does the reset.
8884 *
8885 * For most tg3 variants the trick below was working.
8886 * Ho hum...
8887 */
8888 udelay(120);
8889
8890 /* Flush PCI posted writes. The normal MMIO registers
8891 * are inaccessible at this time so this is the only
8892 * way to make this reliably (actually, this is no longer
8893 * the case, see above). I tried to use indirect
8894 * register read/write but this upset some 5701 variants.
8895 */
8896 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8897
8898 udelay(120);
8899
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008900 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008901 u16 val16;
8902
Joe Perches41535772013-02-16 11:20:04 +00008903 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008904 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008905 u32 cfg_val;
8906
8907 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008908 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 udelay(100);
8910
8911 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8912 pci_write_config_dword(tp->pdev, 0xc4,
8913 cfg_val | (1 << 15));
8914 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008915
Matt Carlsone7126992009-08-25 10:08:16 +00008916 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008917 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008918 /*
8919 * Older PCIe devices only support the 128 byte
8920 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008921 */
Joe Perches63c3a662011-04-26 08:12:10 +00008922 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008923 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8924 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008925
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008926 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008927 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008928 PCI_EXP_DEVSTA_CED |
8929 PCI_EXP_DEVSTA_NFED |
8930 PCI_EXP_DEVSTA_FED |
8931 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932 }
8933
Michael Chanee6a99b2007-07-18 21:49:10 -07008934 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935
Joe Perches63c3a662011-04-26 08:12:10 +00008936 tg3_flag_clear(tp, CHIP_RESETTING);
8937 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008938
Michael Chanee6a99b2007-07-18 21:49:10 -07008939 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008940 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008941 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008942 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943
Joe Perches41535772013-02-16 11:20:04 +00008944 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 tg3_stop_fw(tp);
8946 tw32(0x5000, 0x400);
8947 }
8948
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00008949 if (tg3_flag(tp, IS_SSB_CORE)) {
8950 /*
8951 * BCM4785: In order to avoid repercussions from using
8952 * potentially defective internal ROM, stop the Rx RISC CPU,
8953 * which is not required.
8954 */
8955 tg3_stop_fw(tp);
8956 tg3_halt_cpu(tp, RX_CPU_BASE);
8957 }
8958
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959 tw32(GRC_MODE, tp->grc_mode);
8960
Joe Perches41535772013-02-16 11:20:04 +00008961 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008962 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963
8964 tw32(0xc4, val | (1 << 15));
8965 }
8966
8967 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00008968 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00008970 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8972 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8973 }
8974
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008975 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008976 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008977 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008978 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008979 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008980 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008982 val = 0;
8983
8984 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985 udelay(40);
8986
Matt Carlson77b483f2008-08-15 14:07:24 -07008987 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8988
Michael Chan7a6f4362006-09-27 16:03:31 -07008989 err = tg3_poll_fw(tp);
8990 if (err)
8991 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992
Matt Carlson0a9140c2009-08-28 12:27:50 +00008993 tg3_mdio_start(tp);
8994
Joe Perches63c3a662011-04-26 08:12:10 +00008995 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00008996 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
8997 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008998 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008999 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000
9001 tw32(0x7c00, val | (1 << 25));
9002 }
9003
Joe Perches41535772013-02-16 11:20:04 +00009004 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00009005 val = tr32(TG3_CPMU_CLCK_ORIDE);
9006 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9007 }
9008
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009010 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009011 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9012 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9013
Joe Perches63c3a662011-04-26 08:12:10 +00009014 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9016 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9017 u32 nic_cfg;
9018
9019 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9020 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009021 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009022 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009023 if (tg3_flag(tp, 5750_PLUS))
9024 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009025
9026 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9027 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9028 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9029 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9030 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031 }
9032 }
9033
9034 return 0;
9035}
9036
Matt Carlson65ec6982012-02-28 23:33:37 +00009037static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9038static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009039
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009041static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042{
9043 int err;
9044
9045 tg3_stop_fw(tp);
9046
Michael Chan944d9802005-05-29 14:57:48 -07009047 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009048
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009049 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050 err = tg3_chip_reset(tp);
9051
Joe Perches953c96e2013-04-09 10:18:14 +00009052 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009053
Michael Chan944d9802005-05-29 14:57:48 -07009054 tg3_write_sig_legacy(tp, kind);
9055 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056
Matt Carlson92feeab2011-12-08 14:40:14 +00009057 if (tp->hw_stats) {
9058 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009059 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009060 tg3_get_estats(tp, &tp->estats_prev);
9061
9062 /* And make sure the next sample is new data */
9063 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9064 }
9065
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066 if (err)
9067 return err;
9068
9069 return 0;
9070}
9071
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072static int tg3_set_mac_addr(struct net_device *dev, void *p)
9073{
9074 struct tg3 *tp = netdev_priv(dev);
9075 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009076 int err = 0;
9077 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078
Michael Chanf9804dd2005-09-27 12:13:10 -07009079 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009080 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009081
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9083
Michael Chane75f7c92006-03-20 21:33:26 -08009084 if (!netif_running(dev))
9085 return 0;
9086
Joe Perches63c3a662011-04-26 08:12:10 +00009087 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009088 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009089
Michael Chan986e0ae2007-05-05 12:10:20 -07009090 addr0_high = tr32(MAC_ADDR_0_HIGH);
9091 addr0_low = tr32(MAC_ADDR_0_LOW);
9092 addr1_high = tr32(MAC_ADDR_1_HIGH);
9093 addr1_low = tr32(MAC_ADDR_1_LOW);
9094
9095 /* Skip MAC addr 1 if ASF is using it. */
9096 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9097 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009098 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009099 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009100 spin_lock_bh(&tp->lock);
9101 __tg3_set_mac_addr(tp, skip_mac_1);
9102 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103
Michael Chanb9ec6c12006-07-25 16:37:27 -07009104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009105}
9106
9107/* tp->lock is held. */
9108static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9109 dma_addr_t mapping, u32 maxlen_flags,
9110 u32 nic_addr)
9111{
9112 tg3_write_mem(tp,
9113 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9114 ((u64) mapping >> 32));
9115 tg3_write_mem(tp,
9116 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9117 ((u64) mapping & 0xffffffff));
9118 tg3_write_mem(tp,
9119 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9120 maxlen_flags);
9121
Joe Perches63c3a662011-04-26 08:12:10 +00009122 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123 tg3_write_mem(tp,
9124 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9125 nic_addr);
9126}
9127
Michael Chana489b6d2012-09-28 07:12:39 +00009128
9129static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009130{
Michael Chana489b6d2012-09-28 07:12:39 +00009131 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009132
Joe Perches63c3a662011-04-26 08:12:10 +00009133 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009134 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9135 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9136 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009137 } else {
9138 tw32(HOSTCC_TXCOL_TICKS, 0);
9139 tw32(HOSTCC_TXMAX_FRAMES, 0);
9140 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009141
9142 for (; i < tp->txq_cnt; i++) {
9143 u32 reg;
9144
9145 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9146 tw32(reg, ec->tx_coalesce_usecs);
9147 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9148 tw32(reg, ec->tx_max_coalesced_frames);
9149 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9150 tw32(reg, ec->tx_max_coalesced_frames_irq);
9151 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009152 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009153
Michael Chana489b6d2012-09-28 07:12:39 +00009154 for (; i < tp->irq_max - 1; i++) {
9155 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9156 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9157 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9158 }
9159}
9160
9161static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9162{
9163 int i = 0;
9164 u32 limit = tp->rxq_cnt;
9165
Joe Perches63c3a662011-04-26 08:12:10 +00009166 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009167 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9168 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9169 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009170 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009171 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009172 tw32(HOSTCC_RXCOL_TICKS, 0);
9173 tw32(HOSTCC_RXMAX_FRAMES, 0);
9174 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009175 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009176
Michael Chana489b6d2012-09-28 07:12:39 +00009177 for (; i < limit; i++) {
9178 u32 reg;
9179
9180 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9181 tw32(reg, ec->rx_coalesce_usecs);
9182 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9183 tw32(reg, ec->rx_max_coalesced_frames);
9184 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9185 tw32(reg, ec->rx_max_coalesced_frames_irq);
9186 }
9187
9188 for (; i < tp->irq_max - 1; i++) {
9189 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9190 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9191 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9192 }
9193}
9194
9195static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9196{
9197 tg3_coal_tx_init(tp, ec);
9198 tg3_coal_rx_init(tp, ec);
9199
Joe Perches63c3a662011-04-26 08:12:10 +00009200 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009201 u32 val = ec->stats_block_coalesce_usecs;
9202
Matt Carlsonb6080e12009-09-01 13:12:00 +00009203 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9204 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9205
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009206 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009207 val = 0;
9208
9209 tw32(HOSTCC_STAT_COAL_TICKS, val);
9210 }
9211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212
9213/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009214static void tg3_rings_reset(struct tg3 *tp)
9215{
9216 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009217 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009218 struct tg3_napi *tnapi = &tp->napi[0];
9219
9220 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00009221 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009222 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009223 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00009224 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00009225 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009226 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00009227 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009228 else
9229 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9230
9231 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9232 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9233 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9234 BDINFO_FLAGS_DISABLED);
9235
9236
9237 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00009238 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009239 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00009240 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009241 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches41535772013-02-16 11:20:04 +00009242 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9243 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009244 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009245 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9246 else
9247 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9248
9249 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9250 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9251 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9252 BDINFO_FLAGS_DISABLED);
9253
9254 /* Disable interrupts */
9255 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009256 tp->napi[0].chk_msi_cnt = 0;
9257 tp->napi[0].last_rx_cons = 0;
9258 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009259
9260 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009261 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009262 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009263 tp->napi[i].tx_prod = 0;
9264 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009265 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009266 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009267 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9268 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009269 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009270 tp->napi[i].last_rx_cons = 0;
9271 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009272 }
Joe Perches63c3a662011-04-26 08:12:10 +00009273 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009274 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009275 } else {
9276 tp->napi[0].tx_prod = 0;
9277 tp->napi[0].tx_cons = 0;
9278 tw32_mailbox(tp->napi[0].prodmbox, 0);
9279 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9280 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009281
9282 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009283 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009284 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9285 for (i = 0; i < 16; i++)
9286 tw32_tx_mbox(mbox + i * 8, 0);
9287 }
9288
9289 txrcb = NIC_SRAM_SEND_RCB;
9290 rxrcb = NIC_SRAM_RCV_RET_RCB;
9291
9292 /* Clear status block in ram. */
9293 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9294
9295 /* Set status block DMA address */
9296 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9297 ((u64) tnapi->status_mapping >> 32));
9298 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9299 ((u64) tnapi->status_mapping & 0xffffffff));
9300
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009301 if (tnapi->tx_ring) {
9302 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9303 (TG3_TX_RING_SIZE <<
9304 BDINFO_FLAGS_MAXLEN_SHIFT),
9305 NIC_SRAM_TX_BUFFER_DESC);
9306 txrcb += TG3_BDINFO_SIZE;
9307 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009308
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009309 if (tnapi->rx_rcb) {
9310 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009311 (tp->rx_ret_ring_mask + 1) <<
9312 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009313 rxrcb += TG3_BDINFO_SIZE;
9314 }
9315
9316 stblk = HOSTCC_STATBLCK_RING1;
9317
9318 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9319 u64 mapping = (u64)tnapi->status_mapping;
9320 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9321 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
9322
9323 /* Clear status block in ram. */
9324 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9325
Matt Carlson19cfaec2009-12-03 08:36:20 +00009326 if (tnapi->tx_ring) {
9327 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9328 (TG3_TX_RING_SIZE <<
9329 BDINFO_FLAGS_MAXLEN_SHIFT),
9330 NIC_SRAM_TX_BUFFER_DESC);
9331 txrcb += TG3_BDINFO_SIZE;
9332 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009333
9334 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009335 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009336 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
9337
9338 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009339 rxrcb += TG3_BDINFO_SIZE;
9340 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009341}
9342
Matt Carlsoneb07a942011-04-20 07:57:36 +00009343static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9344{
9345 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9346
Joe Perches63c3a662011-04-26 08:12:10 +00009347 if (!tg3_flag(tp, 5750_PLUS) ||
9348 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009349 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9350 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009351 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009352 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009353 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9354 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009355 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9356 else
9357 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9358
9359 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9360 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9361
9362 val = min(nic_rep_thresh, host_rep_thresh);
9363 tw32(RCVBDI_STD_THRESH, val);
9364
Joe Perches63c3a662011-04-26 08:12:10 +00009365 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009366 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9367
Joe Perches63c3a662011-04-26 08:12:10 +00009368 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009369 return;
9370
Matt Carlson513aa6e2011-11-21 15:01:18 +00009371 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009372
9373 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9374
9375 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9376 tw32(RCVBDI_JUMBO_THRESH, val);
9377
Joe Perches63c3a662011-04-26 08:12:10 +00009378 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009379 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9380}
9381
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009382static inline u32 calc_crc(unsigned char *buf, int len)
9383{
9384 u32 reg;
9385 u32 tmp;
9386 int j, k;
9387
9388 reg = 0xffffffff;
9389
9390 for (j = 0; j < len; j++) {
9391 reg ^= buf[j];
9392
9393 for (k = 0; k < 8; k++) {
9394 tmp = reg & 0x01;
9395
9396 reg >>= 1;
9397
9398 if (tmp)
9399 reg ^= 0xedb88320;
9400 }
9401 }
9402
9403 return ~reg;
9404}
9405
9406static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9407{
9408 /* accept or reject all multicast frames */
9409 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9410 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9411 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9412 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9413}
9414
9415static void __tg3_set_rx_mode(struct net_device *dev)
9416{
9417 struct tg3 *tp = netdev_priv(dev);
9418 u32 rx_mode;
9419
9420 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9421 RX_MODE_KEEP_VLAN_TAG);
9422
9423#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9424 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9425 * flag clear.
9426 */
9427 if (!tg3_flag(tp, ENABLE_ASF))
9428 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9429#endif
9430
9431 if (dev->flags & IFF_PROMISC) {
9432 /* Promiscuous mode. */
9433 rx_mode |= RX_MODE_PROMISC;
9434 } else if (dev->flags & IFF_ALLMULTI) {
9435 /* Accept all multicast. */
9436 tg3_set_multi(tp, 1);
9437 } else if (netdev_mc_empty(dev)) {
9438 /* Reject all multicast. */
9439 tg3_set_multi(tp, 0);
9440 } else {
9441 /* Accept one or more multicast(s). */
9442 struct netdev_hw_addr *ha;
9443 u32 mc_filter[4] = { 0, };
9444 u32 regidx;
9445 u32 bit;
9446 u32 crc;
9447
9448 netdev_for_each_mc_addr(ha, dev) {
9449 crc = calc_crc(ha->addr, ETH_ALEN);
9450 bit = ~crc & 0x7f;
9451 regidx = (bit & 0x60) >> 5;
9452 bit &= 0x1f;
9453 mc_filter[regidx] |= (1 << bit);
9454 }
9455
9456 tw32(MAC_HASH_REG_0, mc_filter[0]);
9457 tw32(MAC_HASH_REG_1, mc_filter[1]);
9458 tw32(MAC_HASH_REG_2, mc_filter[2]);
9459 tw32(MAC_HASH_REG_3, mc_filter[3]);
9460 }
9461
9462 if (rx_mode != tp->rx_mode) {
9463 tp->rx_mode = rx_mode;
9464 tw32_f(MAC_RX_MODE, rx_mode);
9465 udelay(10);
9466 }
9467}
9468
Michael Chan91024262012-09-28 07:12:38 +00009469static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009470{
9471 int i;
9472
9473 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009474 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009475}
9476
9477static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009478{
9479 int i;
9480
9481 if (!tg3_flag(tp, SUPPORT_MSIX))
9482 return;
9483
Michael Chan0b3ba052012-11-14 14:44:29 +00009484 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009485 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009486 return;
9487 }
9488
9489 /* Validate table against current IRQ count */
9490 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009491 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009492 break;
9493 }
9494
9495 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009496 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009497}
9498
Matt Carlson90415472011-12-16 13:33:23 +00009499static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009500{
9501 int i = 0;
9502 u32 reg = MAC_RSS_INDIR_TBL_0;
9503
9504 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9505 u32 val = tp->rss_ind_tbl[i];
9506 i++;
9507 for (; i % 8; i++) {
9508 val <<= 4;
9509 val |= tp->rss_ind_tbl[i];
9510 }
9511 tw32(reg, val);
9512 reg += 4;
9513 }
9514}
9515
Matt Carlson2d31eca2009-09-01 12:53:31 +00009516/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009517static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518{
9519 u32 val, rdmac_mode;
9520 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009521 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009522
9523 tg3_disable_ints(tp);
9524
9525 tg3_stop_fw(tp);
9526
9527 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9528
Joe Perches63c3a662011-04-26 08:12:10 +00009529 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009530 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009532 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9533 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9534 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009535 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009536 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9537 }
9538
Nithin Sujir400dfba2013-05-18 06:26:53 +00009539 /* Enable MAC control of LPI */
9540 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9541 tg3_setup_eee(tp);
9542
Matt Carlson603f1172010-02-12 14:47:10 +00009543 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009544 tg3_phy_reset(tp);
9545
Linus Torvalds1da177e2005-04-16 15:20:36 -07009546 err = tg3_chip_reset(tp);
9547 if (err)
9548 return err;
9549
9550 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9551
Joe Perches41535772013-02-16 11:20:04 +00009552 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009553 val = tr32(TG3_CPMU_CTRL);
9554 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9555 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009556
9557 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9558 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9559 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9560 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9561
9562 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9563 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9564 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9565 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9566
9567 val = tr32(TG3_CPMU_HST_ACC);
9568 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9569 val |= CPMU_HST_ACC_MACCLK_6_25;
9570 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009571 }
9572
Joe Perches41535772013-02-16 11:20:04 +00009573 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d932009-04-20 06:57:41 +00009574 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9575 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9576 PCIE_PWR_MGMT_L1_THRESH_4MS;
9577 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009578
9579 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9580 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9581
9582 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00009583
Matt Carlsonf40386c2009-11-02 14:24:02 +00009584 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9585 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009586 }
9587
Joe Perches63c3a662011-04-26 08:12:10 +00009588 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009589 u32 grc_mode = tr32(GRC_MODE);
9590
9591 /* Access the lower 1K of PL PCIE block registers. */
9592 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9593 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9594
9595 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9596 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9597 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9598
9599 tw32(GRC_MODE, grc_mode);
9600 }
9601
Matt Carlson55086ad2011-12-14 11:09:59 +00009602 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009603 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009604 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009605
Matt Carlson5093eed2010-11-24 08:31:45 +00009606 /* Access the lower 1K of PL PCIE block registers. */
9607 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9608 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009609
Matt Carlson5093eed2010-11-24 08:31:45 +00009610 val = tr32(TG3_PCIE_TLDLPL_PORT +
9611 TG3_PCIE_PL_LO_PHYCTL5);
9612 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9613 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009614
Matt Carlson5093eed2010-11-24 08:31:45 +00009615 tw32(GRC_MODE, grc_mode);
9616 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009617
Joe Perches41535772013-02-16 11:20:04 +00009618 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009619 u32 grc_mode;
9620
9621 /* Fix transmit hangs */
9622 val = tr32(TG3_CPMU_PADRNG_CTL);
9623 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9624 tw32(TG3_CPMU_PADRNG_CTL, val);
9625
9626 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009627
9628 /* Access the lower 1K of DL PCIE block registers. */
9629 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9630 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9631
9632 val = tr32(TG3_PCIE_TLDLPL_PORT +
9633 TG3_PCIE_DL_LO_FTSMAX);
9634 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9635 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9636 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9637
9638 tw32(GRC_MODE, grc_mode);
9639 }
9640
Matt Carlsona977dbe2010-04-12 06:58:26 +00009641 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9642 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9643 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9644 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009645 }
9646
Linus Torvalds1da177e2005-04-16 15:20:36 -07009647 /* This works around an issue with Athlon chipsets on
9648 * B3 tigon3 silicon. This bit has no effect on any
9649 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009650 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651 */
Joe Perches63c3a662011-04-26 08:12:10 +00009652 if (!tg3_flag(tp, CPMU_PRESENT)) {
9653 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009654 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9655 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009657
Joe Perches41535772013-02-16 11:20:04 +00009658 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009659 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009660 val = tr32(TG3PCI_PCISTATE);
9661 val |= PCISTATE_RETRY_SAME_DMA;
9662 tw32(TG3PCI_PCISTATE, val);
9663 }
9664
Joe Perches63c3a662011-04-26 08:12:10 +00009665 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009666 /* Allow reads and writes to the
9667 * APE register and memory space.
9668 */
9669 val = tr32(TG3PCI_PCISTATE);
9670 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009671 PCISTATE_ALLOW_APE_SHMEM_WR |
9672 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009673 tw32(TG3PCI_PCISTATE, val);
9674 }
9675
Joe Perches41535772013-02-16 11:20:04 +00009676 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009677 /* Enable some hw fixes. */
9678 val = tr32(TG3PCI_MSI_DATA);
9679 val |= (1 << 26) | (1 << 28) | (1 << 29);
9680 tw32(TG3PCI_MSI_DATA, val);
9681 }
9682
9683 /* Descriptor ring init may make accesses to the
9684 * NIC SRAM area to setup the TX descriptors, so we
9685 * can only do this after the hardware has been
9686 * successfully reset.
9687 */
Michael Chan32d8c572006-07-25 16:38:29 -07009688 err = tg3_init_rings(tp);
9689 if (err)
9690 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691
Joe Perches63c3a662011-04-26 08:12:10 +00009692 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009693 val = tr32(TG3PCI_DMA_RW_CTRL) &
9694 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009695 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009696 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009697 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009698 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9699 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009700 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009701 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009702 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9703 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009704 /* This value is determined during the probe time DMA
9705 * engine test, tg3_test_dma.
9706 */
9707 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709
9710 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9711 GRC_MODE_4X_NIC_SEND_RINGS |
9712 GRC_MODE_NO_TX_PHDR_CSUM |
9713 GRC_MODE_NO_RX_PHDR_CSUM);
9714 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009715
9716 /* Pseudo-header checksum is done by hardware logic and not
9717 * the offload processers, so make the chip do the pseudo-
9718 * header checksums on receive. For transmit it is more
9719 * convenient to do the pseudo-header checksum in software
9720 * as Linux does that on transmit for us in all cases.
9721 */
9722 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009723
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009724 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9725 if (tp->rxptpctl)
9726 tw32(TG3_RX_PTP_CTL,
9727 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9728
9729 if (tg3_flag(tp, PTP_CAPABLE))
9730 val |= GRC_MODE_TIME_SYNC_ENABLE;
9731
9732 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733
9734 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9735 val = tr32(GRC_MISC_CFG);
9736 val &= ~0xff;
9737 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9738 tw32(GRC_MISC_CFG, val);
9739
9740 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009741 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009742 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009743 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009745 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009746 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9747 else
9748 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9749 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9750 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009751 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 int fw_len;
9753
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009754 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9756 tw32(BUFMGR_MB_POOL_ADDR,
9757 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9758 tw32(BUFMGR_MB_POOL_SIZE,
9759 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761
Michael Chan0f893dc2005-07-25 12:30:38 -07009762 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009763 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9764 tp->bufmgr_config.mbuf_read_dma_low_water);
9765 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9766 tp->bufmgr_config.mbuf_mac_rx_low_water);
9767 tw32(BUFMGR_MB_HIGH_WATER,
9768 tp->bufmgr_config.mbuf_high_water);
9769 } else {
9770 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9771 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9772 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9773 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9774 tw32(BUFMGR_MB_HIGH_WATER,
9775 tp->bufmgr_config.mbuf_high_water_jumbo);
9776 }
9777 tw32(BUFMGR_DMA_LOW_WATER,
9778 tp->bufmgr_config.dma_low_water);
9779 tw32(BUFMGR_DMA_HIGH_WATER,
9780 tp->bufmgr_config.dma_high_water);
9781
Matt Carlsond309a462010-09-30 10:34:31 +00009782 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009783 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009784 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009785 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9786 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9787 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009788 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009789 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790 for (i = 0; i < 2000; i++) {
9791 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9792 break;
9793 udelay(10);
9794 }
9795 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009796 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797 return -ENODEV;
9798 }
9799
Joe Perches41535772013-02-16 11:20:04 +00009800 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009801 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009802
Matt Carlsoneb07a942011-04-20 07:57:36 +00009803 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009804
9805 /* Initialize TG3_BDINFO's at:
9806 * RCVDBDI_STD_BD: standard eth size rx ring
9807 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9808 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9809 *
9810 * like so:
9811 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9812 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9813 * ring attribute flags
9814 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9815 *
9816 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9817 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9818 *
9819 * The size of each ring is fixed in the firmware, but the location is
9820 * configurable.
9821 */
9822 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009823 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009825 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009826 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009827 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9828 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009830 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009831 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9833 BDINFO_FLAGS_DISABLED);
9834
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009835 /* Program the jumbo buffer descriptor ring control
9836 * blocks on those devices that have them.
9837 */
Joe Perches41535772013-02-16 11:20:04 +00009838 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009839 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840
Joe Perches63c3a662011-04-26 08:12:10 +00009841 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009843 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009845 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009846 val = TG3_RX_JMB_RING_SIZE(tp) <<
9847 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009849 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009850 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009851 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009852 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009853 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9854 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 } else {
9856 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9857 BDINFO_FLAGS_DISABLED);
9858 }
9859
Joe Perches63c3a662011-04-26 08:12:10 +00009860 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009861 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009862 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9863 val |= (TG3_RX_STD_DMA_SZ << 2);
9864 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009865 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009866 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009867 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009868
9869 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870
Matt Carlson411da642009-11-13 13:03:46 +00009871 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00009872 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009873
Joe Perches63c3a662011-04-26 08:12:10 +00009874 tpr->rx_jmb_prod_idx =
9875 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00009876 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009877
Matt Carlson2d31eca2009-09-01 12:53:31 +00009878 tg3_rings_reset(tp);
9879
Linus Torvalds1da177e2005-04-16 15:20:36 -07009880 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +00009881 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009882
9883 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009884 tw32(MAC_RX_MTU_SIZE,
9885 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886
9887 /* The slot time is changed by tg3_setup_phy if we
9888 * run at gigabit with half duplex.
9889 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009890 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9891 (6 << TX_LENGTHS_IPG_SHIFT) |
9892 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9893
Joe Perches41535772013-02-16 11:20:04 +00009894 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9895 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009896 val |= tr32(MAC_TX_LENGTHS) &
9897 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9898 TX_LENGTHS_CNT_DWN_VAL_MSK);
9899
9900 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901
9902 /* Receive rules. */
9903 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9904 tw32(RCVLPC_CONFIG, 0x0181);
9905
9906 /* Calculate RDMAC_MODE setting early, we need it to determine
9907 * the RCVLPC_STATE_ENABLE mask.
9908 */
9909 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9910 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9911 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9912 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9913 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009914
Joe Perches41535772013-02-16 11:20:04 +00009915 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009916 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9917
Joe Perches41535772013-02-16 11:20:04 +00009918 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
9919 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9920 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009921 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9922 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9923 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9924
Joe Perches41535772013-02-16 11:20:04 +00009925 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9926 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009927 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009928 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009929 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9930 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009931 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9933 }
9934 }
9935
Joe Perches63c3a662011-04-26 08:12:10 +00009936 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009937 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9938
Joe Perches41535772013-02-16 11:20:04 +00009939 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009940 tp->dma_limit = 0;
9941 if (tp->dev->mtu <= ETH_DATA_LEN) {
9942 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
9943 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
9944 }
9945 }
9946
Joe Perches63c3a662011-04-26 08:12:10 +00009947 if (tg3_flag(tp, HW_TSO_1) ||
9948 tg3_flag(tp, HW_TSO_2) ||
9949 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009950 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9951
Matt Carlson108a6c12011-05-19 12:12:47 +00009952 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009953 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9954 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -08009955 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009956
Joe Perches41535772013-02-16 11:20:04 +00009957 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9958 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009959 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9960
Joe Perches41535772013-02-16 11:20:04 +00009961 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
9962 tg3_asic_rev(tp) == ASIC_REV_5784 ||
9963 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9964 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009965 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009966 u32 tgtreg;
9967
Joe Perches41535772013-02-16 11:20:04 +00009968 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009969 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
9970 else
9971 tgtreg = TG3_RDMA_RSRVCTRL_REG;
9972
9973 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +00009974 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9975 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009976 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9977 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9978 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9979 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9980 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9981 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009982 }
Michael Chanc65a17f2013-01-06 12:51:07 +00009983 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009984 }
9985
Joe Perches41535772013-02-16 11:20:04 +00009986 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
9987 tg3_asic_rev(tp) == ASIC_REV_5720 ||
9988 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009989 u32 tgtreg;
9990
Joe Perches41535772013-02-16 11:20:04 +00009991 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009992 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
9993 else
9994 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
9995
9996 val = tr32(tgtreg);
9997 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +00009998 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9999 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10000 }
10001
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010003 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010004 val = tr32(RCVLPC_STATS_ENABLE);
10005 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10006 tw32(RCVLPC_STATS_ENABLE, val);
10007 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010008 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009 val = tr32(RCVLPC_STATS_ENABLE);
10010 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10011 tw32(RCVLPC_STATS_ENABLE, val);
10012 } else {
10013 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10014 }
10015 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10016 tw32(SNDDATAI_STATSENAB, 0xffffff);
10017 tw32(SNDDATAI_STATSCTRL,
10018 (SNDDATAI_SCTRL_ENABLE |
10019 SNDDATAI_SCTRL_FASTUPD));
10020
10021 /* Setup host coalescing engine. */
10022 tw32(HOSTCC_MODE, 0);
10023 for (i = 0; i < 2000; i++) {
10024 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10025 break;
10026 udelay(10);
10027 }
10028
Michael Chand244c892005-07-05 14:42:33 -070010029 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010030
Joe Perches63c3a662011-04-26 08:12:10 +000010031 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032 /* Status/statistics block address. See tg3_timer,
10033 * the tg3_periodic_fetch_stats call there, and
10034 * tg3_get_stats to see how this works for 5705/5750 chips.
10035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10037 ((u64) tp->stats_mapping >> 32));
10038 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10039 ((u64) tp->stats_mapping & 0xffffffff));
10040 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010041
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010043
10044 /* Clear statistics and status block memory areas */
10045 for (i = NIC_SRAM_STATS_BLK;
10046 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10047 i += sizeof(u32)) {
10048 tg3_write_mem(tp, i, 0);
10049 udelay(40);
10050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051 }
10052
10053 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10054
10055 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10056 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010057 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10059
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010060 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10061 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010062 /* reset to prevent losing 1st rx packet intermittently */
10063 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10064 udelay(10);
10065 }
10066
Matt Carlson3bda1252008-08-15 14:08:22 -070010067 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010068 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10069 MAC_MODE_FHDE_ENABLE;
10070 if (tg3_flag(tp, ENABLE_APE))
10071 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010072 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010073 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010074 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010075 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010076 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10077 udelay(40);
10078
Michael Chan314fba32005-04-21 17:07:04 -070010079 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010080 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010081 * register to preserve the GPIO settings for LOMs. The GPIOs,
10082 * whether used as inputs or outputs, are set by boot code after
10083 * reset.
10084 */
Joe Perches63c3a662011-04-26 08:12:10 +000010085 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010086 u32 gpio_mask;
10087
Michael Chan9d26e212006-12-07 00:21:14 -080010088 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10089 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10090 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010091
Joe Perches41535772013-02-16 11:20:04 +000010092 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010093 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10094 GRC_LCLCTRL_GPIO_OUTPUT3;
10095
Joe Perches41535772013-02-16 11:20:04 +000010096 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010097 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10098
Gary Zambranoaaf84462007-05-05 11:51:45 -070010099 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010100 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10101
10102 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010103 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010104 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10105 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10108 udelay(100);
10109
Matt Carlsonc3b50032012-01-17 15:27:23 +000010110 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010111 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010112 val |= MSGINT_MODE_ENABLE;
10113 if (tp->irq_cnt > 1)
10114 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010115 if (!tg3_flag(tp, 1SHOT_MSI))
10116 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010117 tw32(MSGINT_MODE, val);
10118 }
10119
Joe Perches63c3a662011-04-26 08:12:10 +000010120 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10122 udelay(40);
10123 }
10124
10125 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10126 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10127 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10128 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10129 WDMAC_MODE_LNGREAD_ENAB);
10130
Joe Perches41535772013-02-16 11:20:04 +000010131 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10132 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010133 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010134 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10135 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136 /* nothing */
10137 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010138 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139 val |= WDMAC_MODE_RX_ACCEL;
10140 }
10141 }
10142
Michael Chand9ab5ad12006-03-20 22:27:35 -080010143 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010144 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010145 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010146
Joe Perches41535772013-02-16 11:20:04 +000010147 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010148 val |= WDMAC_MODE_BURST_ALL_DATA;
10149
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150 tw32_f(WDMAC_MODE, val);
10151 udelay(40);
10152
Joe Perches63c3a662011-04-26 08:12:10 +000010153 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010154 u16 pcix_cmd;
10155
10156 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10157 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010158 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010159 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10160 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010161 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010162 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10163 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164 }
Matt Carlson9974a352007-10-07 23:27:28 -070010165 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10166 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167 }
10168
10169 tw32_f(RDMAC_MODE, rdmac_mode);
10170 udelay(40);
10171
Joe Perches41535772013-02-16 11:20:04 +000010172 if (tg3_asic_rev(tp) == ASIC_REV_5719) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010173 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10174 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10175 break;
10176 }
10177 if (i < TG3_NUM_RDMA_CHANNELS) {
10178 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10179 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
10180 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10181 tg3_flag_set(tp, 5719_RDMA_BUG);
10182 }
10183 }
10184
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010186 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010187 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010188
Joe Perches41535772013-02-16 11:20:04 +000010189 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010190 tw32(SNDDATAC_MODE,
10191 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10192 else
10193 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10194
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10196 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010197 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010198 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010199 val |= RCVDBDI_MODE_LRG_RING_SZ;
10200 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010201 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010202 if (tg3_flag(tp, HW_TSO_1) ||
10203 tg3_flag(tp, HW_TSO_2) ||
10204 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010205 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010206 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010207 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010208 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10209 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010210 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10211
Joe Perches41535772013-02-16 11:20:04 +000010212 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213 err = tg3_load_5701_a0_firmware_fix(tp);
10214 if (err)
10215 return err;
10216 }
10217
Nithin Sujirc4dab502013-03-06 17:02:34 +000010218 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10219 /* Ignore any errors for the firmware download. If download
10220 * fails, the device will operate with EEE disabled
10221 */
10222 tg3_load_57766_firmware(tp);
10223 }
10224
Joe Perches63c3a662011-04-26 08:12:10 +000010225 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 err = tg3_load_tso_firmware(tp);
10227 if (err)
10228 return err;
10229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230
10231 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010232
Joe Perches63c3a662011-04-26 08:12:10 +000010233 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010234 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010235 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010236
Joe Perches41535772013-02-16 11:20:04 +000010237 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10238 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010239 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10240 tp->tx_mode &= ~val;
10241 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10242 }
10243
Linus Torvalds1da177e2005-04-16 15:20:36 -070010244 tw32_f(MAC_TX_MODE, tp->tx_mode);
10245 udelay(100);
10246
Joe Perches63c3a662011-04-26 08:12:10 +000010247 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010248 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010249
10250 /* Setup the "secret" hash key. */
10251 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10252 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10253 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10254 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10255 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10256 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10257 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10258 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10259 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10260 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10261 }
10262
Linus Torvalds1da177e2005-04-16 15:20:36 -070010263 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010264 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010265 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10266
Joe Perches63c3a662011-04-26 08:12:10 +000010267 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010268 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10269 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10270 RX_MODE_RSS_IPV6_HASH_EN |
10271 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10272 RX_MODE_RSS_IPV4_HASH_EN |
10273 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10274
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275 tw32_f(MAC_RX_MODE, tp->rx_mode);
10276 udelay(10);
10277
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 tw32(MAC_LED_CTRL, tp->led_ctrl);
10279
10280 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010281 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10283 udelay(10);
10284 }
10285 tw32_f(MAC_RX_MODE, tp->rx_mode);
10286 udelay(10);
10287
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010288 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010289 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10290 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010291 /* Set drive transmission level to 1.2V */
10292 /* only if the signal pre-emphasis bit is not set */
10293 val = tr32(MAC_SERDES_CFG);
10294 val &= 0xfffff000;
10295 val |= 0x880;
10296 tw32(MAC_SERDES_CFG, val);
10297 }
Joe Perches41535772013-02-16 11:20:04 +000010298 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299 tw32(MAC_SERDES_CFG, 0x616000);
10300 }
10301
10302 /* Prevent chip from dropping frames when flow control
10303 * is enabled.
10304 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010305 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010306 val = 1;
10307 else
10308 val = 2;
10309 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310
Joe Perches41535772013-02-16 11:20:04 +000010311 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010312 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010314 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010315 }
10316
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010317 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010318 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010319 u32 tmp;
10320
10321 tmp = tr32(SERDES_RX_CTRL);
10322 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10323 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10324 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10325 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10326 }
10327
Joe Perches63c3a662011-04-26 08:12:10 +000010328 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010329 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010330 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010331
Joe Perches953c96e2013-04-09 10:18:14 +000010332 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010333 if (err)
10334 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010335
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010336 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10337 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010338 u32 tmp;
10339
10340 /* Clear CRC stats. */
10341 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10342 tg3_writephy(tp, MII_TG3_TEST1,
10343 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010344 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010346 }
10347 }
10348
10349 __tg3_set_rx_mode(tp->dev);
10350
10351 /* Initialize receive rules. */
10352 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10353 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10354 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10355 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10356
Joe Perches63c3a662011-04-26 08:12:10 +000010357 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010358 limit = 8;
10359 else
10360 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010361 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010362 limit -= 4;
10363 switch (limit) {
10364 case 16:
10365 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10366 case 15:
10367 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10368 case 14:
10369 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10370 case 13:
10371 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10372 case 12:
10373 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10374 case 11:
10375 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10376 case 10:
10377 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10378 case 9:
10379 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10380 case 8:
10381 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10382 case 7:
10383 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10384 case 6:
10385 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10386 case 5:
10387 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10388 case 4:
10389 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10390 case 3:
10391 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10392 case 2:
10393 case 1:
10394
10395 default:
10396 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010398
Joe Perches63c3a662011-04-26 08:12:10 +000010399 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010400 /* Write our heartbeat update interval to APE. */
10401 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10402 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010403
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10405
Linus Torvalds1da177e2005-04-16 15:20:36 -070010406 return 0;
10407}
10408
10409/* Called at device open time to get the chip ready for
10410 * packet processing. Invoked with tp->lock held.
10411 */
Joe Perches953c96e2013-04-09 10:18:14 +000010412static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010414 tg3_switch_clocks(tp);
10415
10416 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10417
Matt Carlson2f751b62008-08-04 23:17:34 -070010418 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419}
10420
Michael Chanaed93e02012-07-16 16:24:02 +000010421static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10422{
10423 int i;
10424
10425 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10426 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10427
10428 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10429 off += len;
10430
10431 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10432 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10433 memset(ocir, 0, TG3_OCIR_LEN);
10434 }
10435}
10436
10437/* sysfs attributes for hwmon */
10438static ssize_t tg3_show_temp(struct device *dev,
10439 struct device_attribute *devattr, char *buf)
10440{
10441 struct pci_dev *pdev = to_pci_dev(dev);
10442 struct net_device *netdev = pci_get_drvdata(pdev);
10443 struct tg3 *tp = netdev_priv(netdev);
10444 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10445 u32 temperature;
10446
10447 spin_lock_bh(&tp->lock);
10448 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10449 sizeof(temperature));
10450 spin_unlock_bh(&tp->lock);
10451 return sprintf(buf, "%u\n", temperature);
10452}
10453
10454
10455static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10456 TG3_TEMP_SENSOR_OFFSET);
10457static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10458 TG3_TEMP_CAUTION_OFFSET);
10459static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10460 TG3_TEMP_MAX_OFFSET);
10461
10462static struct attribute *tg3_attributes[] = {
10463 &sensor_dev_attr_temp1_input.dev_attr.attr,
10464 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10465 &sensor_dev_attr_temp1_max.dev_attr.attr,
10466 NULL
10467};
10468
10469static const struct attribute_group tg3_group = {
10470 .attrs = tg3_attributes,
10471};
10472
Michael Chanaed93e02012-07-16 16:24:02 +000010473static void tg3_hwmon_close(struct tg3 *tp)
10474{
Michael Chanaed93e02012-07-16 16:24:02 +000010475 if (tp->hwmon_dev) {
10476 hwmon_device_unregister(tp->hwmon_dev);
10477 tp->hwmon_dev = NULL;
10478 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10479 }
Michael Chanaed93e02012-07-16 16:24:02 +000010480}
10481
10482static void tg3_hwmon_open(struct tg3 *tp)
10483{
Michael Chanaed93e02012-07-16 16:24:02 +000010484 int i, err;
10485 u32 size = 0;
10486 struct pci_dev *pdev = tp->pdev;
10487 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10488
10489 tg3_sd_scan_scratchpad(tp, ocirs);
10490
10491 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10492 if (!ocirs[i].src_data_length)
10493 continue;
10494
10495 size += ocirs[i].src_hdr_length;
10496 size += ocirs[i].src_data_length;
10497 }
10498
10499 if (!size)
10500 return;
10501
10502 /* Register hwmon sysfs hooks */
10503 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10504 if (err) {
10505 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10506 return;
10507 }
10508
10509 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10510 if (IS_ERR(tp->hwmon_dev)) {
10511 tp->hwmon_dev = NULL;
10512 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10513 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10514 }
Michael Chanaed93e02012-07-16 16:24:02 +000010515}
10516
10517
Linus Torvalds1da177e2005-04-16 15:20:36 -070010518#define TG3_STAT_ADD32(PSTAT, REG) \
10519do { u32 __val = tr32(REG); \
10520 (PSTAT)->low += __val; \
10521 if ((PSTAT)->low < __val) \
10522 (PSTAT)->high += 1; \
10523} while (0)
10524
10525static void tg3_periodic_fetch_stats(struct tg3 *tp)
10526{
10527 struct tg3_hw_stats *sp = tp->hw_stats;
10528
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010529 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530 return;
10531
10532 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10533 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10534 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10535 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10536 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10537 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10538 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10539 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10540 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10541 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10542 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10543 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10544 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010545 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10546 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10547 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10548 u32 val;
10549
10550 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10551 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10552 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10553 tg3_flag_clear(tp, 5719_RDMA_BUG);
10554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010555
10556 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10557 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10558 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10559 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10560 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10561 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10562 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10563 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10564 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10565 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10566 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10567 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10568 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10569 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010570
10571 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010572 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10573 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10574 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010575 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10576 } else {
10577 u32 val = tr32(HOSTCC_FLOW_ATTN);
10578 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10579 if (val) {
10580 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10581 sp->rx_discards.low += val;
10582 if (sp->rx_discards.low < val)
10583 sp->rx_discards.high += 1;
10584 }
10585 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10586 }
Michael Chan463d3052006-05-22 16:36:27 -070010587 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010588}
10589
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010590static void tg3_chk_missed_msi(struct tg3 *tp)
10591{
10592 u32 i;
10593
10594 for (i = 0; i < tp->irq_cnt; i++) {
10595 struct tg3_napi *tnapi = &tp->napi[i];
10596
10597 if (tg3_has_work(tnapi)) {
10598 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10599 tnapi->last_tx_cons == tnapi->tx_cons) {
10600 if (tnapi->chk_msi_cnt < 1) {
10601 tnapi->chk_msi_cnt++;
10602 return;
10603 }
Matt Carlson7f230732011-08-31 11:44:48 +000010604 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010605 }
10606 }
10607 tnapi->chk_msi_cnt = 0;
10608 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10609 tnapi->last_tx_cons = tnapi->tx_cons;
10610 }
10611}
10612
Linus Torvalds1da177e2005-04-16 15:20:36 -070010613static void tg3_timer(unsigned long __opaque)
10614{
10615 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010616
Matt Carlson5b190622011-11-04 09:15:04 +000010617 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010618 goto restart_timer;
10619
David S. Millerf47c11e2005-06-24 20:18:35 -070010620 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010621
Joe Perches41535772013-02-16 11:20:04 +000010622 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010623 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010624 tg3_chk_missed_msi(tp);
10625
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010626 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10627 /* BCM4785: Flush posted writes from GbE to host memory. */
10628 tr32(HOSTCC_MODE);
10629 }
10630
Joe Perches63c3a662011-04-26 08:12:10 +000010631 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010632 /* All of this garbage is because when using non-tagged
10633 * IRQ status the mailbox/status_block protocol the chip
10634 * uses with the cpu is race prone.
10635 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010636 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010637 tw32(GRC_LOCAL_CTRL,
10638 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10639 } else {
10640 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010641 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010643
David S. Millerfac9b832005-05-18 22:46:34 -070010644 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010645 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010646 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010647 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010649 }
10650
Linus Torvalds1da177e2005-04-16 15:20:36 -070010651 /* This part only runs once per second. */
10652 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010653 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010654 tg3_periodic_fetch_stats(tp);
10655
Matt Carlsonb0c59432011-05-19 12:12:48 +000010656 if (tp->setlpicnt && !--tp->setlpicnt)
10657 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010658
Joe Perches63c3a662011-04-26 08:12:10 +000010659 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010660 u32 mac_stat;
10661 int phy_event;
10662
10663 mac_stat = tr32(MAC_STATUS);
10664
10665 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010666 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010667 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10668 phy_event = 1;
10669 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10670 phy_event = 1;
10671
10672 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010673 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010674 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010675 u32 mac_stat = tr32(MAC_STATUS);
10676 int need_setup = 0;
10677
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010678 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010679 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10680 need_setup = 1;
10681 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010682 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010683 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10684 MAC_STATUS_SIGNAL_DET))) {
10685 need_setup = 1;
10686 }
10687 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010688 if (!tp->serdes_counter) {
10689 tw32_f(MAC_MODE,
10690 (tp->mac_mode &
10691 ~MAC_MODE_PORT_MODE_MASK));
10692 udelay(40);
10693 tw32_f(MAC_MODE, tp->mac_mode);
10694 udelay(40);
10695 }
Joe Perches953c96e2013-04-09 10:18:14 +000010696 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010697 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010698 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010699 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010700 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702
10703 tp->timer_counter = tp->timer_multiplier;
10704 }
10705
Michael Chan130b8e42006-09-27 16:00:40 -070010706 /* Heartbeat is only sent once every 2 seconds.
10707 *
10708 * The heartbeat is to tell the ASF firmware that the host
10709 * driver is still alive. In the event that the OS crashes,
10710 * ASF needs to reset the hardware to free up the FIFO space
10711 * that may be filled with rx packets destined for the host.
10712 * If the FIFO is full, ASF will no longer function properly.
10713 *
10714 * Unintended resets have been reported on real time kernels
10715 * where the timer doesn't run on time. Netpoll will also have
10716 * same problem.
10717 *
10718 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10719 * to check the ring condition when the heartbeat is expiring
10720 * before doing the reset. This will prevent most unintended
10721 * resets.
10722 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010723 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010724 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010725 tg3_wait_for_event_ack(tp);
10726
Michael Chanbbadf502006-04-06 21:46:34 -070010727 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010728 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010729 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010730 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10731 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010732
10733 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010734 }
10735 tp->asf_counter = tp->asf_multiplier;
10736 }
10737
David S. Millerf47c11e2005-06-24 20:18:35 -070010738 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739
Michael Chanf475f162006-03-27 23:20:14 -080010740restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741 tp->timer.expires = jiffies + tp->timer_offset;
10742 add_timer(&tp->timer);
10743}
10744
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010745static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010746{
10747 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010748 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010749 !tg3_flag(tp, 57765_CLASS))
10750 tp->timer_offset = HZ;
10751 else
10752 tp->timer_offset = HZ / 10;
10753
10754 BUG_ON(tp->timer_offset > HZ);
10755
10756 tp->timer_multiplier = (HZ / tp->timer_offset);
10757 tp->asf_multiplier = (HZ / tp->timer_offset) *
10758 TG3_FW_UPDATE_FREQ_SEC;
10759
10760 init_timer(&tp->timer);
10761 tp->timer.data = (unsigned long) tp;
10762 tp->timer.function = tg3_timer;
10763}
10764
10765static void tg3_timer_start(struct tg3 *tp)
10766{
10767 tp->asf_counter = tp->asf_multiplier;
10768 tp->timer_counter = tp->timer_multiplier;
10769
10770 tp->timer.expires = jiffies + tp->timer_offset;
10771 add_timer(&tp->timer);
10772}
10773
10774static void tg3_timer_stop(struct tg3 *tp)
10775{
10776 del_timer_sync(&tp->timer);
10777}
10778
10779/* Restart hardware after configuration changes, self-test, etc.
10780 * Invoked with tp->lock held.
10781 */
Joe Perches953c96e2013-04-09 10:18:14 +000010782static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000010783 __releases(tp->lock)
10784 __acquires(tp->lock)
10785{
10786 int err;
10787
10788 err = tg3_init_hw(tp, reset_phy);
10789 if (err) {
10790 netdev_err(tp->dev,
10791 "Failed to re-initialize device, aborting\n");
10792 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10793 tg3_full_unlock(tp);
10794 tg3_timer_stop(tp);
10795 tp->irq_sync = 0;
10796 tg3_napi_enable(tp);
10797 dev_close(tp->dev);
10798 tg3_full_lock(tp, 0);
10799 }
10800 return err;
10801}
10802
10803static void tg3_reset_task(struct work_struct *work)
10804{
10805 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10806 int err;
10807
10808 tg3_full_lock(tp, 0);
10809
10810 if (!netif_running(tp->dev)) {
10811 tg3_flag_clear(tp, RESET_TASK_PENDING);
10812 tg3_full_unlock(tp);
10813 return;
10814 }
10815
10816 tg3_full_unlock(tp);
10817
10818 tg3_phy_stop(tp);
10819
10820 tg3_netif_stop(tp);
10821
10822 tg3_full_lock(tp, 1);
10823
10824 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10825 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10826 tp->write32_rx_mbox = tg3_write_flush_reg32;
10827 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10828 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10829 }
10830
10831 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000010832 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000010833 if (err)
10834 goto out;
10835
10836 tg3_netif_start(tp);
10837
10838out:
10839 tg3_full_unlock(tp);
10840
10841 if (!err)
10842 tg3_phy_start(tp);
10843
10844 tg3_flag_clear(tp, RESET_TASK_PENDING);
10845}
10846
Matt Carlson4f125f42009-09-01 12:55:02 +000010847static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010848{
David Howells7d12e782006-10-05 14:55:46 +010010849 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010850 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010851 char *name;
10852 struct tg3_napi *tnapi = &tp->napi[irq_num];
10853
10854 if (tp->irq_cnt == 1)
10855 name = tp->dev->name;
10856 else {
10857 name = &tnapi->irq_lbl[0];
10858 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10859 name[IFNAMSIZ-1] = 0;
10860 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010861
Joe Perches63c3a662011-04-26 08:12:10 +000010862 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010863 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010864 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010865 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010866 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010867 } else {
10868 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010869 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010870 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010871 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010872 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010873
10874 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010875}
10876
Michael Chan79381092005-04-21 17:13:59 -070010877static int tg3_test_interrupt(struct tg3 *tp)
10878{
Matt Carlson09943a12009-08-28 14:01:57 +000010879 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010880 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010881 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010882 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010883
Michael Chand4bc3922005-05-29 14:59:20 -070010884 if (!netif_running(dev))
10885 return -ENODEV;
10886
Michael Chan79381092005-04-21 17:13:59 -070010887 tg3_disable_ints(tp);
10888
Matt Carlson4f125f42009-09-01 12:55:02 +000010889 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010890
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010891 /*
10892 * Turn off MSI one shot mode. Otherwise this test has no
10893 * observable way to know whether the interrupt was delivered.
10894 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010895 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010896 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10897 tw32(MSGINT_MODE, val);
10898 }
10899
Matt Carlson4f125f42009-09-01 12:55:02 +000010900 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010901 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010902 if (err)
10903 return err;
10904
Matt Carlson898a56f2009-08-28 14:02:40 +000010905 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010906 tg3_enable_ints(tp);
10907
10908 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010909 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010910
10911 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010912 u32 int_mbox, misc_host_ctrl;
10913
Matt Carlson898a56f2009-08-28 14:02:40 +000010914 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010915 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10916
10917 if ((int_mbox != 0) ||
10918 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10919 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010920 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010921 }
10922
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010923 if (tg3_flag(tp, 57765_PLUS) &&
10924 tnapi->hw_status->status_tag != tnapi->last_tag)
10925 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10926
Michael Chan79381092005-04-21 17:13:59 -070010927 msleep(10);
10928 }
10929
10930 tg3_disable_ints(tp);
10931
Matt Carlson4f125f42009-09-01 12:55:02 +000010932 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010933
Matt Carlson4f125f42009-09-01 12:55:02 +000010934 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010935
10936 if (err)
10937 return err;
10938
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010939 if (intr_ok) {
10940 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010941 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010942 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10943 tw32(MSGINT_MODE, val);
10944 }
Michael Chan79381092005-04-21 17:13:59 -070010945 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010946 }
Michael Chan79381092005-04-21 17:13:59 -070010947
10948 return -EIO;
10949}
10950
10951/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10952 * successfully restored
10953 */
10954static int tg3_test_msi(struct tg3 *tp)
10955{
Michael Chan79381092005-04-21 17:13:59 -070010956 int err;
10957 u16 pci_cmd;
10958
Joe Perches63c3a662011-04-26 08:12:10 +000010959 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010960 return 0;
10961
10962 /* Turn off SERR reporting in case MSI terminates with Master
10963 * Abort.
10964 */
10965 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10966 pci_write_config_word(tp->pdev, PCI_COMMAND,
10967 pci_cmd & ~PCI_COMMAND_SERR);
10968
10969 err = tg3_test_interrupt(tp);
10970
10971 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10972
10973 if (!err)
10974 return 0;
10975
10976 /* other failures */
10977 if (err != -EIO)
10978 return err;
10979
10980 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010981 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10982 "to INTx mode. Please report this failure to the PCI "
10983 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010984
Matt Carlson4f125f42009-09-01 12:55:02 +000010985 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010986
Michael Chan79381092005-04-21 17:13:59 -070010987 pci_disable_msi(tp->pdev);
10988
Joe Perches63c3a662011-04-26 08:12:10 +000010989 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010990 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010991
Matt Carlson4f125f42009-09-01 12:55:02 +000010992 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010993 if (err)
10994 return err;
10995
10996 /* Need to reset the chip because the MSI cycle may have terminated
10997 * with Master Abort.
10998 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010999 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011000
Michael Chan944d9802005-05-29 14:57:48 -070011001 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011002 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011003
David S. Millerf47c11e2005-06-24 20:18:35 -070011004 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011005
11006 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011007 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011008
11009 return err;
11010}
11011
Matt Carlson9e9fd122009-01-19 16:57:45 -080011012static int tg3_request_firmware(struct tg3 *tp)
11013{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011014 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011015
11016 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011017 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11018 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011019 return -ENOENT;
11020 }
11021
Nithin Sujir77997ea2013-03-06 17:02:32 +000011022 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011023
11024 /* Firmware blob starts with version numbers, followed by
11025 * start address and _full_ length including BSS sections
11026 * (which must be longer than the actual data, of course
11027 */
11028
Nithin Sujir77997ea2013-03-06 17:02:32 +000011029 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11030 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011031 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11032 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011033 release_firmware(tp->fw);
11034 tp->fw = NULL;
11035 return -EINVAL;
11036 }
11037
11038 /* We no longer need firmware; we have it. */
11039 tp->fw_needed = NULL;
11040 return 0;
11041}
11042
Michael Chan91024262012-09-28 07:12:38 +000011043static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011044{
Michael Chan91024262012-09-28 07:12:38 +000011045 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011046
Michael Chan91024262012-09-28 07:12:38 +000011047 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011048 /* We want as many rx rings enabled as there are cpus.
11049 * In multiqueue MSI-X mode, the first MSI-X vector
11050 * only deals with link interrupts, etc, so we add
11051 * one to the number of vectors we are requesting.
11052 */
Michael Chan91024262012-09-28 07:12:38 +000011053 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011054 }
Matt Carlson679563f2009-09-01 12:55:46 +000011055
Michael Chan91024262012-09-28 07:12:38 +000011056 return irq_cnt;
11057}
11058
11059static bool tg3_enable_msix(struct tg3 *tp)
11060{
11061 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011062 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011063
Michael Chan09681692012-09-28 07:12:42 +000011064 tp->txq_cnt = tp->txq_req;
11065 tp->rxq_cnt = tp->rxq_req;
11066 if (!tp->rxq_cnt)
11067 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011068 if (tp->rxq_cnt > tp->rxq_max)
11069 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011070
11071 /* Disable multiple TX rings by default. Simple round-robin hardware
11072 * scheduling of the TX rings can cause starvation of rings with
11073 * small packets when other rings have TSO or jumbo packets.
11074 */
11075 if (!tp->txq_req)
11076 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011077
11078 tp->irq_cnt = tg3_irq_count(tp);
11079
Matt Carlson679563f2009-09-01 12:55:46 +000011080 for (i = 0; i < tp->irq_max; i++) {
11081 msix_ent[i].entry = i;
11082 msix_ent[i].vector = 0;
11083 }
11084
11085 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011086 if (rc < 0) {
11087 return false;
11088 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011089 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11090 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011091 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11092 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011093 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011094 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011095 if (tp->txq_cnt)
11096 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011097 }
11098
11099 for (i = 0; i < tp->irq_max; i++)
11100 tp->napi[i].irq_vec = msix_ent[i].vector;
11101
Michael Chan49a359e2012-09-28 07:12:37 +000011102 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011103 pci_disable_msix(tp->pdev);
11104 return false;
11105 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011106
Michael Chan91024262012-09-28 07:12:38 +000011107 if (tp->irq_cnt == 1)
11108 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011109
Michael Chan91024262012-09-28 07:12:38 +000011110 tg3_flag_set(tp, ENABLE_RSS);
11111
11112 if (tp->txq_cnt > 1)
11113 tg3_flag_set(tp, ENABLE_TSS);
11114
11115 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011116
Matt Carlson679563f2009-09-01 12:55:46 +000011117 return true;
11118}
11119
Matt Carlson07b01732009-08-28 14:01:15 +000011120static void tg3_ints_init(struct tg3 *tp)
11121{
Joe Perches63c3a662011-04-26 08:12:10 +000011122 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11123 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011124 /* All MSI supporting chips should support tagged
11125 * status. Assert that this is the case.
11126 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011127 netdev_warn(tp->dev,
11128 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011129 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011130 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011131
Joe Perches63c3a662011-04-26 08:12:10 +000011132 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11133 tg3_flag_set(tp, USING_MSIX);
11134 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11135 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011136
Joe Perches63c3a662011-04-26 08:12:10 +000011137 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011138 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011139 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011140 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011141 if (!tg3_flag(tp, 1SHOT_MSI))
11142 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011143 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11144 }
11145defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011146 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011147 tp->irq_cnt = 1;
11148 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011149 }
11150
11151 if (tp->irq_cnt == 1) {
11152 tp->txq_cnt = 1;
11153 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011154 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011155 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011156 }
Matt Carlson07b01732009-08-28 14:01:15 +000011157}
11158
11159static void tg3_ints_fini(struct tg3 *tp)
11160{
Joe Perches63c3a662011-04-26 08:12:10 +000011161 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011162 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011163 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011164 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011165 tg3_flag_clear(tp, USING_MSI);
11166 tg3_flag_clear(tp, USING_MSIX);
11167 tg3_flag_clear(tp, ENABLE_RSS);
11168 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011169}
11170
Matt Carlsonbe947302012-12-03 19:36:57 +000011171static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11172 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011173{
Michael Chand8f4cd32012-09-28 07:12:40 +000011174 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011175 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011176
Matt Carlson679563f2009-09-01 12:55:46 +000011177 /*
11178 * Setup interrupts first so we know how
11179 * many NAPI resources to allocate
11180 */
11181 tg3_ints_init(tp);
11182
Matt Carlson90415472011-12-16 13:33:23 +000011183 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011184
Linus Torvalds1da177e2005-04-16 15:20:36 -070011185 /* The placement of this call is tied
11186 * to the setup and use of Host TX descriptors.
11187 */
11188 err = tg3_alloc_consistent(tp);
11189 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000011190 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011191
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011192 tg3_napi_init(tp);
11193
Matt Carlsonfed97812009-09-01 13:10:19 +000011194 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011195
Matt Carlson4f125f42009-09-01 12:55:02 +000011196 for (i = 0; i < tp->irq_cnt; i++) {
11197 struct tg3_napi *tnapi = &tp->napi[i];
11198 err = tg3_request_irq(tp, i);
11199 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011200 for (i--; i >= 0; i--) {
11201 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011202 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011203 }
11204 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000011205 }
11206 }
Matt Carlson07b01732009-08-28 14:01:15 +000011207
David S. Millerf47c11e2005-06-24 20:18:35 -070011208 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011209
Michael Chand8f4cd32012-09-28 07:12:40 +000011210 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011211 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011212 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011213 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011214 }
11215
David S. Millerf47c11e2005-06-24 20:18:35 -070011216 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011217
Matt Carlson07b01732009-08-28 14:01:15 +000011218 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000011219 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011220
Michael Chand8f4cd32012-09-28 07:12:40 +000011221 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011222 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011223
Michael Chan79381092005-04-21 17:13:59 -070011224 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011225 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011226 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011227 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011228 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011229
Matt Carlson679563f2009-09-01 12:55:46 +000011230 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070011231 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011232
Joe Perches63c3a662011-04-26 08:12:10 +000011233 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011234 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011235
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011236 tw32(PCIE_TRANSACTION_CFG,
11237 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011238 }
Michael Chan79381092005-04-21 17:13:59 -070011239 }
11240
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011241 tg3_phy_start(tp);
11242
Michael Chanaed93e02012-07-16 16:24:02 +000011243 tg3_hwmon_open(tp);
11244
David S. Millerf47c11e2005-06-24 20:18:35 -070011245 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011246
Matt Carlson21f76382012-02-22 12:35:21 +000011247 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011248 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011249 tg3_enable_ints(tp);
11250
Matt Carlsonbe947302012-12-03 19:36:57 +000011251 if (init)
11252 tg3_ptp_init(tp);
11253 else
11254 tg3_ptp_resume(tp);
11255
11256
David S. Millerf47c11e2005-06-24 20:18:35 -070011257 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011258
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011259 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011260
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011261 /*
11262 * Reset loopback feature if it was turned on while the device was down
11263 * make sure that it's installed properly now.
11264 */
11265 if (dev->features & NETIF_F_LOOPBACK)
11266 tg3_set_loopback(dev, dev->features);
11267
Linus Torvalds1da177e2005-04-16 15:20:36 -070011268 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011269
Matt Carlson679563f2009-09-01 12:55:46 +000011270err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000011271 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11272 struct tg3_napi *tnapi = &tp->napi[i];
11273 free_irq(tnapi->irq_vec, tnapi);
11274 }
Matt Carlson07b01732009-08-28 14:01:15 +000011275
Matt Carlson679563f2009-09-01 12:55:46 +000011276err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000011277 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011278 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011279 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011280
11281err_out1:
11282 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011283
Matt Carlson07b01732009-08-28 14:01:15 +000011284 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011285}
11286
Michael Chan65138592012-09-28 07:12:41 +000011287static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011288{
Matt Carlson4f125f42009-09-01 12:55:02 +000011289 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011290
Matt Carlsondb219972011-11-04 09:15:03 +000011291 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011292 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011293
Matt Carlson21f76382012-02-22 12:35:21 +000011294 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011295
Michael Chanaed93e02012-07-16 16:24:02 +000011296 tg3_hwmon_close(tp);
11297
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011298 tg3_phy_stop(tp);
11299
David S. Millerf47c11e2005-06-24 20:18:35 -070011300 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011301
11302 tg3_disable_ints(tp);
11303
Michael Chan944d9802005-05-29 14:57:48 -070011304 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011305 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011306 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011307
David S. Millerf47c11e2005-06-24 20:18:35 -070011308 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011309
Matt Carlson4f125f42009-09-01 12:55:02 +000011310 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11311 struct tg3_napi *tnapi = &tp->napi[i];
11312 free_irq(tnapi->irq_vec, tnapi);
11313 }
Matt Carlson07b01732009-08-28 14:01:15 +000011314
11315 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011316
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011317 tg3_napi_fini(tp);
11318
Linus Torvalds1da177e2005-04-16 15:20:36 -070011319 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011320}
11321
Michael Chand8f4cd32012-09-28 07:12:40 +000011322static int tg3_open(struct net_device *dev)
11323{
11324 struct tg3 *tp = netdev_priv(dev);
11325 int err;
11326
11327 if (tp->fw_needed) {
11328 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011329 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11330 if (err) {
11331 netdev_warn(tp->dev, "EEE capability disabled\n");
11332 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11333 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11334 netdev_warn(tp->dev, "EEE capability restored\n");
11335 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11336 }
11337 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011338 if (err)
11339 return err;
11340 } else if (err) {
11341 netdev_warn(tp->dev, "TSO capability disabled\n");
11342 tg3_flag_clear(tp, TSO_CAPABLE);
11343 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11344 netdev_notice(tp->dev, "TSO capability restored\n");
11345 tg3_flag_set(tp, TSO_CAPABLE);
11346 }
11347 }
11348
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011349 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011350
11351 err = tg3_power_up(tp);
11352 if (err)
11353 return err;
11354
11355 tg3_full_lock(tp, 0);
11356
11357 tg3_disable_ints(tp);
11358 tg3_flag_clear(tp, INIT_COMPLETE);
11359
11360 tg3_full_unlock(tp);
11361
Nithin Sujir942d1af2013-04-09 08:48:07 +000011362 err = tg3_start(tp,
11363 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11364 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011365 if (err) {
11366 tg3_frob_aux_power(tp, false);
11367 pci_set_power_state(tp->pdev, PCI_D3hot);
11368 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011369
Matt Carlson7d41e492012-12-03 19:36:58 +000011370 if (tg3_flag(tp, PTP_CAPABLE)) {
11371 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11372 &tp->pdev->dev);
11373 if (IS_ERR(tp->ptp_clock))
11374 tp->ptp_clock = NULL;
11375 }
11376
Linus Torvalds1da177e2005-04-16 15:20:36 -070011377 return err;
11378}
11379
11380static int tg3_close(struct net_device *dev)
11381{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011382 struct tg3 *tp = netdev_priv(dev);
11383
Matt Carlsonbe947302012-12-03 19:36:57 +000011384 tg3_ptp_fini(tp);
11385
Michael Chan65138592012-09-28 07:12:41 +000011386 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011387
11388 /* Clear stats across close / open calls */
11389 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11390 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011391
11392 tg3_power_down(tp);
11393
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011394 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011395
11396 return 0;
11397}
11398
11399static inline u64 get_stat64(tg3_stat64_t *val)
11400{
11401 return ((u64)val->high << 32) | ((u64)val->low);
11402}
11403
11404static u64 tg3_calc_crc_errors(struct tg3 *tp)
11405{
11406 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11407
11408 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011409 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11410 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011411 u32 val;
11412
11413 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11414 tg3_writephy(tp, MII_TG3_TEST1,
11415 val | MII_TG3_TEST1_CRC_EN);
11416 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11417 } else
11418 val = 0;
11419
11420 tp->phy_crc_errors += val;
11421
11422 return tp->phy_crc_errors;
11423 }
11424
11425 return get_stat64(&hw_stats->rx_fcs_errors);
11426}
11427
11428#define ESTAT_ADD(member) \
11429 estats->member = old_estats->member + \
11430 get_stat64(&hw_stats->member)
11431
11432static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11433{
11434 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11435 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11436
11437 ESTAT_ADD(rx_octets);
11438 ESTAT_ADD(rx_fragments);
11439 ESTAT_ADD(rx_ucast_packets);
11440 ESTAT_ADD(rx_mcast_packets);
11441 ESTAT_ADD(rx_bcast_packets);
11442 ESTAT_ADD(rx_fcs_errors);
11443 ESTAT_ADD(rx_align_errors);
11444 ESTAT_ADD(rx_xon_pause_rcvd);
11445 ESTAT_ADD(rx_xoff_pause_rcvd);
11446 ESTAT_ADD(rx_mac_ctrl_rcvd);
11447 ESTAT_ADD(rx_xoff_entered);
11448 ESTAT_ADD(rx_frame_too_long_errors);
11449 ESTAT_ADD(rx_jabbers);
11450 ESTAT_ADD(rx_undersize_packets);
11451 ESTAT_ADD(rx_in_length_errors);
11452 ESTAT_ADD(rx_out_length_errors);
11453 ESTAT_ADD(rx_64_or_less_octet_packets);
11454 ESTAT_ADD(rx_65_to_127_octet_packets);
11455 ESTAT_ADD(rx_128_to_255_octet_packets);
11456 ESTAT_ADD(rx_256_to_511_octet_packets);
11457 ESTAT_ADD(rx_512_to_1023_octet_packets);
11458 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11459 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11460 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11461 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11462 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11463
11464 ESTAT_ADD(tx_octets);
11465 ESTAT_ADD(tx_collisions);
11466 ESTAT_ADD(tx_xon_sent);
11467 ESTAT_ADD(tx_xoff_sent);
11468 ESTAT_ADD(tx_flow_control);
11469 ESTAT_ADD(tx_mac_errors);
11470 ESTAT_ADD(tx_single_collisions);
11471 ESTAT_ADD(tx_mult_collisions);
11472 ESTAT_ADD(tx_deferred);
11473 ESTAT_ADD(tx_excessive_collisions);
11474 ESTAT_ADD(tx_late_collisions);
11475 ESTAT_ADD(tx_collide_2times);
11476 ESTAT_ADD(tx_collide_3times);
11477 ESTAT_ADD(tx_collide_4times);
11478 ESTAT_ADD(tx_collide_5times);
11479 ESTAT_ADD(tx_collide_6times);
11480 ESTAT_ADD(tx_collide_7times);
11481 ESTAT_ADD(tx_collide_8times);
11482 ESTAT_ADD(tx_collide_9times);
11483 ESTAT_ADD(tx_collide_10times);
11484 ESTAT_ADD(tx_collide_11times);
11485 ESTAT_ADD(tx_collide_12times);
11486 ESTAT_ADD(tx_collide_13times);
11487 ESTAT_ADD(tx_collide_14times);
11488 ESTAT_ADD(tx_collide_15times);
11489 ESTAT_ADD(tx_ucast_packets);
11490 ESTAT_ADD(tx_mcast_packets);
11491 ESTAT_ADD(tx_bcast_packets);
11492 ESTAT_ADD(tx_carrier_sense_errors);
11493 ESTAT_ADD(tx_discards);
11494 ESTAT_ADD(tx_errors);
11495
11496 ESTAT_ADD(dma_writeq_full);
11497 ESTAT_ADD(dma_write_prioq_full);
11498 ESTAT_ADD(rxbds_empty);
11499 ESTAT_ADD(rx_discards);
11500 ESTAT_ADD(rx_errors);
11501 ESTAT_ADD(rx_threshold_hit);
11502
11503 ESTAT_ADD(dma_readq_full);
11504 ESTAT_ADD(dma_read_prioq_full);
11505 ESTAT_ADD(tx_comp_queue_full);
11506
11507 ESTAT_ADD(ring_set_send_prod_index);
11508 ESTAT_ADD(ring_status_update);
11509 ESTAT_ADD(nic_irqs);
11510 ESTAT_ADD(nic_avoided_irqs);
11511 ESTAT_ADD(nic_tx_threshold_hit);
11512
Matt Carlson4452d092011-05-19 12:12:51 +000011513 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011514}
11515
Matt Carlson65ec6982012-02-28 23:33:37 +000011516static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011517{
Eric Dumazet511d2222010-07-07 20:44:24 +000011518 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011519 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11520
Linus Torvalds1da177e2005-04-16 15:20:36 -070011521 stats->rx_packets = old_stats->rx_packets +
11522 get_stat64(&hw_stats->rx_ucast_packets) +
11523 get_stat64(&hw_stats->rx_mcast_packets) +
11524 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011525
Linus Torvalds1da177e2005-04-16 15:20:36 -070011526 stats->tx_packets = old_stats->tx_packets +
11527 get_stat64(&hw_stats->tx_ucast_packets) +
11528 get_stat64(&hw_stats->tx_mcast_packets) +
11529 get_stat64(&hw_stats->tx_bcast_packets);
11530
11531 stats->rx_bytes = old_stats->rx_bytes +
11532 get_stat64(&hw_stats->rx_octets);
11533 stats->tx_bytes = old_stats->tx_bytes +
11534 get_stat64(&hw_stats->tx_octets);
11535
11536 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011537 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011538 stats->tx_errors = old_stats->tx_errors +
11539 get_stat64(&hw_stats->tx_errors) +
11540 get_stat64(&hw_stats->tx_mac_errors) +
11541 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11542 get_stat64(&hw_stats->tx_discards);
11543
11544 stats->multicast = old_stats->multicast +
11545 get_stat64(&hw_stats->rx_mcast_packets);
11546 stats->collisions = old_stats->collisions +
11547 get_stat64(&hw_stats->tx_collisions);
11548
11549 stats->rx_length_errors = old_stats->rx_length_errors +
11550 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11551 get_stat64(&hw_stats->rx_undersize_packets);
11552
11553 stats->rx_over_errors = old_stats->rx_over_errors +
11554 get_stat64(&hw_stats->rxbds_empty);
11555 stats->rx_frame_errors = old_stats->rx_frame_errors +
11556 get_stat64(&hw_stats->rx_align_errors);
11557 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11558 get_stat64(&hw_stats->tx_discards);
11559 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11560 get_stat64(&hw_stats->tx_carrier_sense_errors);
11561
11562 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011563 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011564
John W. Linville4f63b872005-09-12 14:43:18 -070011565 stats->rx_missed_errors = old_stats->rx_missed_errors +
11566 get_stat64(&hw_stats->rx_discards);
11567
Eric Dumazetb0057c52010-10-10 19:55:52 +000011568 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011569 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011570}
11571
Linus Torvalds1da177e2005-04-16 15:20:36 -070011572static int tg3_get_regs_len(struct net_device *dev)
11573{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011574 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011575}
11576
11577static void tg3_get_regs(struct net_device *dev,
11578 struct ethtool_regs *regs, void *_p)
11579{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011581
11582 regs->version = 0;
11583
Matt Carlson97bd8e42011-04-13 11:05:04 +000011584 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011585
Matt Carlson80096062010-08-02 11:26:06 +000011586 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011587 return;
11588
David S. Millerf47c11e2005-06-24 20:18:35 -070011589 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011590
Matt Carlson97bd8e42011-04-13 11:05:04 +000011591 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011592
David S. Millerf47c11e2005-06-24 20:18:35 -070011593 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011594}
11595
11596static int tg3_get_eeprom_len(struct net_device *dev)
11597{
11598 struct tg3 *tp = netdev_priv(dev);
11599
11600 return tp->nvram_size;
11601}
11602
Linus Torvalds1da177e2005-04-16 15:20:36 -070011603static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11604{
11605 struct tg3 *tp = netdev_priv(dev);
11606 int ret;
11607 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011608 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011609 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011610
Joe Perches63c3a662011-04-26 08:12:10 +000011611 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011612 return -EINVAL;
11613
Matt Carlson80096062010-08-02 11:26:06 +000011614 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011615 return -EAGAIN;
11616
Linus Torvalds1da177e2005-04-16 15:20:36 -070011617 offset = eeprom->offset;
11618 len = eeprom->len;
11619 eeprom->len = 0;
11620
11621 eeprom->magic = TG3_EEPROM_MAGIC;
11622
11623 if (offset & 3) {
11624 /* adjustments to start on required 4 byte boundary */
11625 b_offset = offset & 3;
11626 b_count = 4 - b_offset;
11627 if (b_count > len) {
11628 /* i.e. offset=1 len=2 */
11629 b_count = len;
11630 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011631 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011632 if (ret)
11633 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011634 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011635 len -= b_count;
11636 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011637 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011638 }
11639
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011640 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011641 pd = &data[eeprom->len];
11642 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011643 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011644 if (ret) {
11645 eeprom->len += i;
11646 return ret;
11647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011648 memcpy(pd + i, &val, 4);
11649 }
11650 eeprom->len += i;
11651
11652 if (len & 3) {
11653 /* read last bytes not ending on 4 byte boundary */
11654 pd = &data[eeprom->len];
11655 b_count = len & 3;
11656 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011657 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011658 if (ret)
11659 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011660 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011661 eeprom->len += b_count;
11662 }
11663 return 0;
11664}
11665
Linus Torvalds1da177e2005-04-16 15:20:36 -070011666static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11667{
11668 struct tg3 *tp = netdev_priv(dev);
11669 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011670 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011671 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011672 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011673
Matt Carlson80096062010-08-02 11:26:06 +000011674 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011675 return -EAGAIN;
11676
Joe Perches63c3a662011-04-26 08:12:10 +000011677 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011678 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011679 return -EINVAL;
11680
11681 offset = eeprom->offset;
11682 len = eeprom->len;
11683
11684 if ((b_offset = (offset & 3))) {
11685 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011686 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011687 if (ret)
11688 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011689 len += b_offset;
11690 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070011691 if (len < 4)
11692 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011693 }
11694
11695 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070011696 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011697 /* adjustments to end on required 4 byte boundary */
11698 odd_len = 1;
11699 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011700 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011701 if (ret)
11702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011703 }
11704
11705 buf = data;
11706 if (b_offset || odd_len) {
11707 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011708 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011709 return -ENOMEM;
11710 if (b_offset)
11711 memcpy(buf, &start, 4);
11712 if (odd_len)
11713 memcpy(buf+len-4, &end, 4);
11714 memcpy(buf + b_offset, data, eeprom->len);
11715 }
11716
11717 ret = tg3_nvram_write_block(tp, offset, len, buf);
11718
11719 if (buf != data)
11720 kfree(buf);
11721
11722 return ret;
11723}
11724
11725static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11726{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011727 struct tg3 *tp = netdev_priv(dev);
11728
Joe Perches63c3a662011-04-26 08:12:10 +000011729 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011730 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011731 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011732 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011733 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11734 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011735 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011736
Linus Torvalds1da177e2005-04-16 15:20:36 -070011737 cmd->supported = (SUPPORTED_Autoneg);
11738
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011739 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011740 cmd->supported |= (SUPPORTED_1000baseT_Half |
11741 SUPPORTED_1000baseT_Full);
11742
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011743 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011744 cmd->supported |= (SUPPORTED_100baseT_Half |
11745 SUPPORTED_100baseT_Full |
11746 SUPPORTED_10baseT_Half |
11747 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011748 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011749 cmd->port = PORT_TP;
11750 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011751 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011752 cmd->port = PORT_FIBRE;
11753 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011754
Linus Torvalds1da177e2005-04-16 15:20:36 -070011755 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011756 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11757 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11758 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11759 cmd->advertising |= ADVERTISED_Pause;
11760 } else {
11761 cmd->advertising |= ADVERTISED_Pause |
11762 ADVERTISED_Asym_Pause;
11763 }
11764 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11765 cmd->advertising |= ADVERTISED_Asym_Pause;
11766 }
11767 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011768 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011769 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011770 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011771 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011772 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11773 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11774 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11775 else
11776 cmd->eth_tp_mdix = ETH_TP_MDI;
11777 }
Matt Carlson64c22182010-10-14 10:37:44 +000011778 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011779 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11780 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011781 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011782 }
Matt Carlson882e9792009-09-01 13:21:36 +000011783 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011784 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011785 cmd->autoneg = tp->link_config.autoneg;
11786 cmd->maxtxpkt = 0;
11787 cmd->maxrxpkt = 0;
11788 return 0;
11789}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011790
Linus Torvalds1da177e2005-04-16 15:20:36 -070011791static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11792{
11793 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011794 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011795
Joe Perches63c3a662011-04-26 08:12:10 +000011796 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011797 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011798 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011799 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011800 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11801 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011802 }
11803
Matt Carlson7e5856b2009-02-25 14:23:01 +000011804 if (cmd->autoneg != AUTONEG_ENABLE &&
11805 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011806 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011807
11808 if (cmd->autoneg == AUTONEG_DISABLE &&
11809 cmd->duplex != DUPLEX_FULL &&
11810 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011811 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011812
Matt Carlson7e5856b2009-02-25 14:23:01 +000011813 if (cmd->autoneg == AUTONEG_ENABLE) {
11814 u32 mask = ADVERTISED_Autoneg |
11815 ADVERTISED_Pause |
11816 ADVERTISED_Asym_Pause;
11817
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011818 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011819 mask |= ADVERTISED_1000baseT_Half |
11820 ADVERTISED_1000baseT_Full;
11821
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011822 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011823 mask |= ADVERTISED_100baseT_Half |
11824 ADVERTISED_100baseT_Full |
11825 ADVERTISED_10baseT_Half |
11826 ADVERTISED_10baseT_Full |
11827 ADVERTISED_TP;
11828 else
11829 mask |= ADVERTISED_FIBRE;
11830
11831 if (cmd->advertising & ~mask)
11832 return -EINVAL;
11833
11834 mask &= (ADVERTISED_1000baseT_Half |
11835 ADVERTISED_1000baseT_Full |
11836 ADVERTISED_100baseT_Half |
11837 ADVERTISED_100baseT_Full |
11838 ADVERTISED_10baseT_Half |
11839 ADVERTISED_10baseT_Full);
11840
11841 cmd->advertising &= mask;
11842 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011843 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011844 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011845 return -EINVAL;
11846
11847 if (cmd->duplex != DUPLEX_FULL)
11848 return -EINVAL;
11849 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011850 if (speed != SPEED_100 &&
11851 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011852 return -EINVAL;
11853 }
11854 }
11855
David S. Millerf47c11e2005-06-24 20:18:35 -070011856 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011857
11858 tp->link_config.autoneg = cmd->autoneg;
11859 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011860 tp->link_config.advertising = (cmd->advertising |
11861 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011862 tp->link_config.speed = SPEED_UNKNOWN;
11863 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011864 } else {
11865 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011866 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011867 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011868 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011869
Nithin Sujirfdad8de2013-04-09 08:48:08 +000011870 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
11871
Nithin Sujirce20f162013-04-09 08:48:04 +000011872 tg3_warn_mgmt_link_flap(tp);
11873
Linus Torvalds1da177e2005-04-16 15:20:36 -070011874 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000011875 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011876
David S. Millerf47c11e2005-06-24 20:18:35 -070011877 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011878
Linus Torvalds1da177e2005-04-16 15:20:36 -070011879 return 0;
11880}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011881
Linus Torvalds1da177e2005-04-16 15:20:36 -070011882static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11883{
11884 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011885
Rick Jones68aad782011-11-07 13:29:27 +000011886 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11887 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11888 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11889 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011890}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011891
Linus Torvalds1da177e2005-04-16 15:20:36 -070011892static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11893{
11894 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011895
Joe Perches63c3a662011-04-26 08:12:10 +000011896 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011897 wol->supported = WAKE_MAGIC;
11898 else
11899 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011900 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011901 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011902 wol->wolopts = WAKE_MAGIC;
11903 memset(&wol->sopass, 0, sizeof(wol->sopass));
11904}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011905
Linus Torvalds1da177e2005-04-16 15:20:36 -070011906static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11907{
11908 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011909 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011910
Linus Torvalds1da177e2005-04-16 15:20:36 -070011911 if (wol->wolopts & ~WAKE_MAGIC)
11912 return -EINVAL;
11913 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011914 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011916
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011917 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11918
David S. Millerf47c11e2005-06-24 20:18:35 -070011919 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011920 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011921 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011922 else
Joe Perches63c3a662011-04-26 08:12:10 +000011923 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011924 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011925
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926 return 0;
11927}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011928
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929static u32 tg3_get_msglevel(struct net_device *dev)
11930{
11931 struct tg3 *tp = netdev_priv(dev);
11932 return tp->msg_enable;
11933}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011934
Linus Torvalds1da177e2005-04-16 15:20:36 -070011935static void tg3_set_msglevel(struct net_device *dev, u32 value)
11936{
11937 struct tg3 *tp = netdev_priv(dev);
11938 tp->msg_enable = value;
11939}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011940
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941static int tg3_nway_reset(struct net_device *dev)
11942{
11943 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011944 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011945
Linus Torvalds1da177e2005-04-16 15:20:36 -070011946 if (!netif_running(dev))
11947 return -EAGAIN;
11948
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011949 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011950 return -EINVAL;
11951
Nithin Sujirce20f162013-04-09 08:48:04 +000011952 tg3_warn_mgmt_link_flap(tp);
11953
Joe Perches63c3a662011-04-26 08:12:10 +000011954 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011955 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011956 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011957 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011958 } else {
11959 u32 bmcr;
11960
11961 spin_lock_bh(&tp->lock);
11962 r = -EINVAL;
11963 tg3_readphy(tp, MII_BMCR, &bmcr);
11964 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11965 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011966 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011967 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11968 BMCR_ANENABLE);
11969 r = 0;
11970 }
11971 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011972 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011973
Linus Torvalds1da177e2005-04-16 15:20:36 -070011974 return r;
11975}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011976
Linus Torvalds1da177e2005-04-16 15:20:36 -070011977static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11978{
11979 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011980
Matt Carlson2c49a442010-09-30 10:34:35 +000011981 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011982 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011983 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011984 else
11985 ering->rx_jumbo_max_pending = 0;
11986
11987 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011988
11989 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011990 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011991 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11992 else
11993 ering->rx_jumbo_pending = 0;
11994
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011995 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011996}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011997
Linus Torvalds1da177e2005-04-16 15:20:36 -070011998static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11999{
12000 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012001 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012002
Matt Carlson2c49a442010-09-30 10:34:35 +000012003 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12004 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012005 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12006 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012007 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012008 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012010
Michael Chanbbe832c2005-06-24 20:20:04 -070012011 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012012 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012013 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012014 irq_sync = 1;
12015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012016
Michael Chanbbe832c2005-06-24 20:20:04 -070012017 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012018
Linus Torvalds1da177e2005-04-16 15:20:36 -070012019 tp->rx_pending = ering->rx_pending;
12020
Joe Perches63c3a662011-04-26 08:12:10 +000012021 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022 tp->rx_pending > 63)
12023 tp->rx_pending = 63;
12024 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012025
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012026 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012027 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012028
12029 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012030 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012031 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012032 if (!err)
12033 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012034 }
12035
David S. Millerf47c11e2005-06-24 20:18:35 -070012036 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012037
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012038 if (irq_sync && !err)
12039 tg3_phy_start(tp);
12040
Michael Chanb9ec6c12006-07-25 16:37:27 -070012041 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012042}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012043
Linus Torvalds1da177e2005-04-16 15:20:36 -070012044static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12045{
12046 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012047
Joe Perches63c3a662011-04-26 08:12:10 +000012048 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012049
Matt Carlson4a2db502011-12-08 14:40:17 +000012050 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012051 epause->rx_pause = 1;
12052 else
12053 epause->rx_pause = 0;
12054
Matt Carlson4a2db502011-12-08 14:40:17 +000012055 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012056 epause->tx_pause = 1;
12057 else
12058 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012059}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012060
Linus Torvalds1da177e2005-04-16 15:20:36 -070012061static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12062{
12063 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012064 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012065
Nithin Sujirce20f162013-04-09 08:48:04 +000012066 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12067 tg3_warn_mgmt_link_flap(tp);
12068
Joe Perches63c3a662011-04-26 08:12:10 +000012069 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012070 u32 newadv;
12071 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012072
Matt Carlson27121682010-02-17 15:16:57 +000012073 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012074
Matt Carlson27121682010-02-17 15:16:57 +000012075 if (!(phydev->supported & SUPPORTED_Pause) ||
12076 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012077 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012078 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012079
Matt Carlson27121682010-02-17 15:16:57 +000012080 tp->link_config.flowctrl = 0;
12081 if (epause->rx_pause) {
12082 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012083
Matt Carlson27121682010-02-17 15:16:57 +000012084 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012085 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012086 newadv = ADVERTISED_Pause;
12087 } else
12088 newadv = ADVERTISED_Pause |
12089 ADVERTISED_Asym_Pause;
12090 } else if (epause->tx_pause) {
12091 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12092 newadv = ADVERTISED_Asym_Pause;
12093 } else
12094 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012095
Matt Carlson27121682010-02-17 15:16:57 +000012096 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012097 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012098 else
Joe Perches63c3a662011-04-26 08:12:10 +000012099 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012100
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012101 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012102 u32 oldadv = phydev->advertising &
12103 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12104 if (oldadv != newadv) {
12105 phydev->advertising &=
12106 ~(ADVERTISED_Pause |
12107 ADVERTISED_Asym_Pause);
12108 phydev->advertising |= newadv;
12109 if (phydev->autoneg) {
12110 /*
12111 * Always renegotiate the link to
12112 * inform our link partner of our
12113 * flow control settings, even if the
12114 * flow control is forced. Let
12115 * tg3_adjust_link() do the final
12116 * flow control setup.
12117 */
12118 return phy_start_aneg(phydev);
12119 }
12120 }
12121
12122 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012123 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012124 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012125 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012126 ~(ADVERTISED_Pause |
12127 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012128 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012129 }
12130 } else {
12131 int irq_sync = 0;
12132
12133 if (netif_running(dev)) {
12134 tg3_netif_stop(tp);
12135 irq_sync = 1;
12136 }
12137
12138 tg3_full_lock(tp, irq_sync);
12139
12140 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012141 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012142 else
Joe Perches63c3a662011-04-26 08:12:10 +000012143 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012144 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012145 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012146 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012147 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012148 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012149 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012150 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012151 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012152
12153 if (netif_running(dev)) {
12154 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012155 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012156 if (!err)
12157 tg3_netif_start(tp);
12158 }
12159
12160 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012162
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012163 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12164
Michael Chanb9ec6c12006-07-25 16:37:27 -070012165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012166}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012167
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012168static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012169{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012170 switch (sset) {
12171 case ETH_SS_TEST:
12172 return TG3_NUM_TEST;
12173 case ETH_SS_STATS:
12174 return TG3_NUM_STATS;
12175 default:
12176 return -EOPNOTSUPP;
12177 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012178}
12179
Matt Carlson90415472011-12-16 13:33:23 +000012180static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12181 u32 *rules __always_unused)
12182{
12183 struct tg3 *tp = netdev_priv(dev);
12184
12185 if (!tg3_flag(tp, SUPPORT_MSIX))
12186 return -EOPNOTSUPP;
12187
12188 switch (info->cmd) {
12189 case ETHTOOL_GRXRINGS:
12190 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012191 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012192 else {
12193 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012194 if (info->data > TG3_RSS_MAX_NUM_QS)
12195 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012196 }
12197
12198 /* The first interrupt vector only
12199 * handles link interrupts.
12200 */
12201 info->data -= 1;
12202 return 0;
12203
12204 default:
12205 return -EOPNOTSUPP;
12206 }
12207}
12208
12209static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12210{
12211 u32 size = 0;
12212 struct tg3 *tp = netdev_priv(dev);
12213
12214 if (tg3_flag(tp, SUPPORT_MSIX))
12215 size = TG3_RSS_INDIR_TBL_SIZE;
12216
12217 return size;
12218}
12219
12220static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12221{
12222 struct tg3 *tp = netdev_priv(dev);
12223 int i;
12224
12225 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12226 indir[i] = tp->rss_ind_tbl[i];
12227
12228 return 0;
12229}
12230
12231static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12232{
12233 struct tg3 *tp = netdev_priv(dev);
12234 size_t i;
12235
12236 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12237 tp->rss_ind_tbl[i] = indir[i];
12238
12239 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12240 return 0;
12241
12242 /* It is legal to write the indirection
12243 * table while the device is running.
12244 */
12245 tg3_full_lock(tp, 0);
12246 tg3_rss_write_indir_tbl(tp);
12247 tg3_full_unlock(tp);
12248
12249 return 0;
12250}
12251
Michael Chan09681692012-09-28 07:12:42 +000012252static void tg3_get_channels(struct net_device *dev,
12253 struct ethtool_channels *channel)
12254{
12255 struct tg3 *tp = netdev_priv(dev);
12256 u32 deflt_qs = netif_get_num_default_rss_queues();
12257
12258 channel->max_rx = tp->rxq_max;
12259 channel->max_tx = tp->txq_max;
12260
12261 if (netif_running(dev)) {
12262 channel->rx_count = tp->rxq_cnt;
12263 channel->tx_count = tp->txq_cnt;
12264 } else {
12265 if (tp->rxq_req)
12266 channel->rx_count = tp->rxq_req;
12267 else
12268 channel->rx_count = min(deflt_qs, tp->rxq_max);
12269
12270 if (tp->txq_req)
12271 channel->tx_count = tp->txq_req;
12272 else
12273 channel->tx_count = min(deflt_qs, tp->txq_max);
12274 }
12275}
12276
12277static int tg3_set_channels(struct net_device *dev,
12278 struct ethtool_channels *channel)
12279{
12280 struct tg3 *tp = netdev_priv(dev);
12281
12282 if (!tg3_flag(tp, SUPPORT_MSIX))
12283 return -EOPNOTSUPP;
12284
12285 if (channel->rx_count > tp->rxq_max ||
12286 channel->tx_count > tp->txq_max)
12287 return -EINVAL;
12288
12289 tp->rxq_req = channel->rx_count;
12290 tp->txq_req = channel->tx_count;
12291
12292 if (!netif_running(dev))
12293 return 0;
12294
12295 tg3_stop(tp);
12296
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012297 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012298
Matt Carlsonbe947302012-12-03 19:36:57 +000012299 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012300
12301 return 0;
12302}
12303
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012304static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012305{
12306 switch (stringset) {
12307 case ETH_SS_STATS:
12308 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12309 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012310 case ETH_SS_TEST:
12311 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012313 default:
12314 WARN_ON(1); /* we need a WARN() */
12315 break;
12316 }
12317}
12318
stephen hemminger81b87092011-04-04 08:43:50 +000012319static int tg3_set_phys_id(struct net_device *dev,
12320 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012321{
12322 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012323
12324 if (!netif_running(tp->dev))
12325 return -EAGAIN;
12326
stephen hemminger81b87092011-04-04 08:43:50 +000012327 switch (state) {
12328 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012329 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012330
stephen hemminger81b87092011-04-04 08:43:50 +000012331 case ETHTOOL_ID_ON:
12332 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12333 LED_CTRL_1000MBPS_ON |
12334 LED_CTRL_100MBPS_ON |
12335 LED_CTRL_10MBPS_ON |
12336 LED_CTRL_TRAFFIC_OVERRIDE |
12337 LED_CTRL_TRAFFIC_BLINK |
12338 LED_CTRL_TRAFFIC_LED);
12339 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012340
stephen hemminger81b87092011-04-04 08:43:50 +000012341 case ETHTOOL_ID_OFF:
12342 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12343 LED_CTRL_TRAFFIC_OVERRIDE);
12344 break;
Michael Chan4009a932005-09-05 17:52:54 -070012345
stephen hemminger81b87092011-04-04 08:43:50 +000012346 case ETHTOOL_ID_INACTIVE:
12347 tw32(MAC_LED_CTRL, tp->led_ctrl);
12348 break;
Michael Chan4009a932005-09-05 17:52:54 -070012349 }
stephen hemminger81b87092011-04-04 08:43:50 +000012350
Michael Chan4009a932005-09-05 17:52:54 -070012351 return 0;
12352}
12353
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012354static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012355 struct ethtool_stats *estats, u64 *tmp_stats)
12356{
12357 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012358
Matt Carlsonb546e462012-02-13 15:20:09 +000012359 if (tp->hw_stats)
12360 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12361 else
12362 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012363}
12364
Matt Carlson535a4902011-07-20 10:20:56 +000012365static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012366{
12367 int i;
12368 __be32 *buf;
12369 u32 offset = 0, len = 0;
12370 u32 magic, val;
12371
Joe Perches63c3a662011-04-26 08:12:10 +000012372 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012373 return NULL;
12374
12375 if (magic == TG3_EEPROM_MAGIC) {
12376 for (offset = TG3_NVM_DIR_START;
12377 offset < TG3_NVM_DIR_END;
12378 offset += TG3_NVM_DIRENT_SIZE) {
12379 if (tg3_nvram_read(tp, offset, &val))
12380 return NULL;
12381
12382 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12383 TG3_NVM_DIRTYPE_EXTVPD)
12384 break;
12385 }
12386
12387 if (offset != TG3_NVM_DIR_END) {
12388 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12389 if (tg3_nvram_read(tp, offset + 4, &offset))
12390 return NULL;
12391
12392 offset = tg3_nvram_logical_addr(tp, offset);
12393 }
12394 }
12395
12396 if (!offset || !len) {
12397 offset = TG3_NVM_VPD_OFF;
12398 len = TG3_NVM_VPD_LEN;
12399 }
12400
12401 buf = kmalloc(len, GFP_KERNEL);
12402 if (buf == NULL)
12403 return NULL;
12404
12405 if (magic == TG3_EEPROM_MAGIC) {
12406 for (i = 0; i < len; i += 4) {
12407 /* The data is in little-endian format in NVRAM.
12408 * Use the big-endian read routines to preserve
12409 * the byte order as it exists in NVRAM.
12410 */
12411 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12412 goto error;
12413 }
12414 } else {
12415 u8 *ptr;
12416 ssize_t cnt;
12417 unsigned int pos = 0;
12418
12419 ptr = (u8 *)&buf[0];
12420 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12421 cnt = pci_read_vpd(tp->pdev, pos,
12422 len - pos, ptr);
12423 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12424 cnt = 0;
12425 else if (cnt < 0)
12426 goto error;
12427 }
12428 if (pos != len)
12429 goto error;
12430 }
12431
Matt Carlson535a4902011-07-20 10:20:56 +000012432 *vpdlen = len;
12433
Matt Carlsonc3e94502011-04-13 11:05:08 +000012434 return buf;
12435
12436error:
12437 kfree(buf);
12438 return NULL;
12439}
12440
Michael Chan566f86a2005-05-29 14:56:58 -070012441#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012442#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12443#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12444#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012445#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12446#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012447#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012448#define NVRAM_SELFBOOT_HW_SIZE 0x20
12449#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012450
12451static int tg3_test_nvram(struct tg3 *tp)
12452{
Matt Carlson535a4902011-07-20 10:20:56 +000012453 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012454 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012455 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012456
Joe Perches63c3a662011-04-26 08:12:10 +000012457 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012458 return 0;
12459
Matt Carlsone4f34112009-02-25 14:25:00 +000012460 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012461 return -EIO;
12462
Michael Chan1b277772006-03-20 22:27:48 -080012463 if (magic == TG3_EEPROM_MAGIC)
12464 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012465 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012466 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12467 TG3_EEPROM_SB_FORMAT_1) {
12468 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12469 case TG3_EEPROM_SB_REVISION_0:
12470 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12471 break;
12472 case TG3_EEPROM_SB_REVISION_2:
12473 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12474 break;
12475 case TG3_EEPROM_SB_REVISION_3:
12476 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12477 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012478 case TG3_EEPROM_SB_REVISION_4:
12479 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12480 break;
12481 case TG3_EEPROM_SB_REVISION_5:
12482 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12483 break;
12484 case TG3_EEPROM_SB_REVISION_6:
12485 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12486 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012487 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012488 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012489 }
12490 } else
Michael Chan1b277772006-03-20 22:27:48 -080012491 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012492 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12493 size = NVRAM_SELFBOOT_HW_SIZE;
12494 else
Michael Chan1b277772006-03-20 22:27:48 -080012495 return -EIO;
12496
12497 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012498 if (buf == NULL)
12499 return -ENOMEM;
12500
Michael Chan1b277772006-03-20 22:27:48 -080012501 err = -EIO;
12502 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012503 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12504 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012505 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012506 }
Michael Chan1b277772006-03-20 22:27:48 -080012507 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012508 goto out;
12509
Michael Chan1b277772006-03-20 22:27:48 -080012510 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012511 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012512 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012513 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012514 u8 *buf8 = (u8 *) buf, csum8 = 0;
12515
Al Virob9fc7dc2007-12-17 22:59:57 -080012516 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012517 TG3_EEPROM_SB_REVISION_2) {
12518 /* For rev 2, the csum doesn't include the MBA. */
12519 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12520 csum8 += buf8[i];
12521 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12522 csum8 += buf8[i];
12523 } else {
12524 for (i = 0; i < size; i++)
12525 csum8 += buf8[i];
12526 }
Michael Chan1b277772006-03-20 22:27:48 -080012527
Adrian Bunkad96b482006-04-05 22:21:04 -070012528 if (csum8 == 0) {
12529 err = 0;
12530 goto out;
12531 }
12532
12533 err = -EIO;
12534 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012535 }
Michael Chan566f86a2005-05-29 14:56:58 -070012536
Al Virob9fc7dc2007-12-17 22:59:57 -080012537 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012538 TG3_EEPROM_MAGIC_HW) {
12539 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012540 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012541 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012542
12543 /* Separate the parity bits and the data bytes. */
12544 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12545 if ((i == 0) || (i == 8)) {
12546 int l;
12547 u8 msk;
12548
12549 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12550 parity[k++] = buf8[i] & msk;
12551 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012552 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012553 int l;
12554 u8 msk;
12555
12556 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12557 parity[k++] = buf8[i] & msk;
12558 i++;
12559
12560 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12561 parity[k++] = buf8[i] & msk;
12562 i++;
12563 }
12564 data[j++] = buf8[i];
12565 }
12566
12567 err = -EIO;
12568 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12569 u8 hw8 = hweight8(data[i]);
12570
12571 if ((hw8 & 0x1) && parity[i])
12572 goto out;
12573 else if (!(hw8 & 0x1) && !parity[i])
12574 goto out;
12575 }
12576 err = 0;
12577 goto out;
12578 }
12579
Matt Carlson01c3a392011-03-09 16:58:20 +000012580 err = -EIO;
12581
Michael Chan566f86a2005-05-29 14:56:58 -070012582 /* Bootstrap checksum at offset 0x10 */
12583 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012584 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012585 goto out;
12586
12587 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12588 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012589 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012590 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012591
Matt Carlsonc3e94502011-04-13 11:05:08 +000012592 kfree(buf);
12593
Matt Carlson535a4902011-07-20 10:20:56 +000012594 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012595 if (!buf)
12596 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012597
Matt Carlson535a4902011-07-20 10:20:56 +000012598 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012599 if (i > 0) {
12600 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12601 if (j < 0)
12602 goto out;
12603
Matt Carlson535a4902011-07-20 10:20:56 +000012604 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012605 goto out;
12606
12607 i += PCI_VPD_LRDT_TAG_SIZE;
12608 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12609 PCI_VPD_RO_KEYWORD_CHKSUM);
12610 if (j > 0) {
12611 u8 csum8 = 0;
12612
12613 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12614
12615 for (i = 0; i <= j; i++)
12616 csum8 += ((u8 *)buf)[i];
12617
12618 if (csum8)
12619 goto out;
12620 }
12621 }
12622
Michael Chan566f86a2005-05-29 14:56:58 -070012623 err = 0;
12624
12625out:
12626 kfree(buf);
12627 return err;
12628}
12629
Michael Chanca430072005-05-29 14:57:23 -070012630#define TG3_SERDES_TIMEOUT_SEC 2
12631#define TG3_COPPER_TIMEOUT_SEC 6
12632
12633static int tg3_test_link(struct tg3 *tp)
12634{
12635 int i, max;
12636
12637 if (!netif_running(tp->dev))
12638 return -ENODEV;
12639
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012640 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012641 max = TG3_SERDES_TIMEOUT_SEC;
12642 else
12643 max = TG3_COPPER_TIMEOUT_SEC;
12644
12645 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012646 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012647 return 0;
12648
12649 if (msleep_interruptible(1000))
12650 break;
12651 }
12652
12653 return -EIO;
12654}
12655
Michael Chana71116d2005-05-29 14:58:11 -070012656/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012657static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012658{
Michael Chanb16250e2006-09-27 16:10:14 -070012659 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012660 u32 offset, read_mask, write_mask, val, save_val, read_val;
12661 static struct {
12662 u16 offset;
12663 u16 flags;
12664#define TG3_FL_5705 0x1
12665#define TG3_FL_NOT_5705 0x2
12666#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012667#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012668 u32 read_mask;
12669 u32 write_mask;
12670 } reg_tbl[] = {
12671 /* MAC Control Registers */
12672 { MAC_MODE, TG3_FL_NOT_5705,
12673 0x00000000, 0x00ef6f8c },
12674 { MAC_MODE, TG3_FL_5705,
12675 0x00000000, 0x01ef6b8c },
12676 { MAC_STATUS, TG3_FL_NOT_5705,
12677 0x03800107, 0x00000000 },
12678 { MAC_STATUS, TG3_FL_5705,
12679 0x03800100, 0x00000000 },
12680 { MAC_ADDR_0_HIGH, 0x0000,
12681 0x00000000, 0x0000ffff },
12682 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012683 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012684 { MAC_RX_MTU_SIZE, 0x0000,
12685 0x00000000, 0x0000ffff },
12686 { MAC_TX_MODE, 0x0000,
12687 0x00000000, 0x00000070 },
12688 { MAC_TX_LENGTHS, 0x0000,
12689 0x00000000, 0x00003fff },
12690 { MAC_RX_MODE, TG3_FL_NOT_5705,
12691 0x00000000, 0x000007fc },
12692 { MAC_RX_MODE, TG3_FL_5705,
12693 0x00000000, 0x000007dc },
12694 { MAC_HASH_REG_0, 0x0000,
12695 0x00000000, 0xffffffff },
12696 { MAC_HASH_REG_1, 0x0000,
12697 0x00000000, 0xffffffff },
12698 { MAC_HASH_REG_2, 0x0000,
12699 0x00000000, 0xffffffff },
12700 { MAC_HASH_REG_3, 0x0000,
12701 0x00000000, 0xffffffff },
12702
12703 /* Receive Data and Receive BD Initiator Control Registers. */
12704 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12705 0x00000000, 0xffffffff },
12706 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12707 0x00000000, 0xffffffff },
12708 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12709 0x00000000, 0x00000003 },
12710 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12711 0x00000000, 0xffffffff },
12712 { RCVDBDI_STD_BD+0, 0x0000,
12713 0x00000000, 0xffffffff },
12714 { RCVDBDI_STD_BD+4, 0x0000,
12715 0x00000000, 0xffffffff },
12716 { RCVDBDI_STD_BD+8, 0x0000,
12717 0x00000000, 0xffff0002 },
12718 { RCVDBDI_STD_BD+0xc, 0x0000,
12719 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012720
Michael Chana71116d2005-05-29 14:58:11 -070012721 /* Receive BD Initiator Control Registers. */
12722 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12723 0x00000000, 0xffffffff },
12724 { RCVBDI_STD_THRESH, TG3_FL_5705,
12725 0x00000000, 0x000003ff },
12726 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12727 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012728
Michael Chana71116d2005-05-29 14:58:11 -070012729 /* Host Coalescing Control Registers. */
12730 { HOSTCC_MODE, TG3_FL_NOT_5705,
12731 0x00000000, 0x00000004 },
12732 { HOSTCC_MODE, TG3_FL_5705,
12733 0x00000000, 0x000000f6 },
12734 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12735 0x00000000, 0xffffffff },
12736 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12737 0x00000000, 0x000003ff },
12738 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12739 0x00000000, 0xffffffff },
12740 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12741 0x00000000, 0x000003ff },
12742 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12743 0x00000000, 0xffffffff },
12744 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12745 0x00000000, 0x000000ff },
12746 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12747 0x00000000, 0xffffffff },
12748 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12749 0x00000000, 0x000000ff },
12750 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12751 0x00000000, 0xffffffff },
12752 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12753 0x00000000, 0xffffffff },
12754 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12755 0x00000000, 0xffffffff },
12756 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12757 0x00000000, 0x000000ff },
12758 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12759 0x00000000, 0xffffffff },
12760 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12761 0x00000000, 0x000000ff },
12762 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12763 0x00000000, 0xffffffff },
12764 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12765 0x00000000, 0xffffffff },
12766 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12767 0x00000000, 0xffffffff },
12768 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12769 0x00000000, 0xffffffff },
12770 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12771 0x00000000, 0xffffffff },
12772 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12773 0xffffffff, 0x00000000 },
12774 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12775 0xffffffff, 0x00000000 },
12776
12777 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012778 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012779 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012780 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012781 0x00000000, 0x007fffff },
12782 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12783 0x00000000, 0x0000003f },
12784 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12785 0x00000000, 0x000001ff },
12786 { BUFMGR_MB_HIGH_WATER, 0x0000,
12787 0x00000000, 0x000001ff },
12788 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12789 0xffffffff, 0x00000000 },
12790 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12791 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012792
Michael Chana71116d2005-05-29 14:58:11 -070012793 /* Mailbox Registers */
12794 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12795 0x00000000, 0x000001ff },
12796 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12797 0x00000000, 0x000001ff },
12798 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12799 0x00000000, 0x000007ff },
12800 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12801 0x00000000, 0x000001ff },
12802
12803 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12804 };
12805
Michael Chanb16250e2006-09-27 16:10:14 -070012806 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012807 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012808 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012809 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012810 is_5750 = 1;
12811 }
Michael Chana71116d2005-05-29 14:58:11 -070012812
12813 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12814 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12815 continue;
12816
12817 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12818 continue;
12819
Joe Perches63c3a662011-04-26 08:12:10 +000012820 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012821 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12822 continue;
12823
Michael Chanb16250e2006-09-27 16:10:14 -070012824 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12825 continue;
12826
Michael Chana71116d2005-05-29 14:58:11 -070012827 offset = (u32) reg_tbl[i].offset;
12828 read_mask = reg_tbl[i].read_mask;
12829 write_mask = reg_tbl[i].write_mask;
12830
12831 /* Save the original register content */
12832 save_val = tr32(offset);
12833
12834 /* Determine the read-only value. */
12835 read_val = save_val & read_mask;
12836
12837 /* Write zero to the register, then make sure the read-only bits
12838 * are not changed and the read/write bits are all zeros.
12839 */
12840 tw32(offset, 0);
12841
12842 val = tr32(offset);
12843
12844 /* Test the read-only and read/write bits. */
12845 if (((val & read_mask) != read_val) || (val & write_mask))
12846 goto out;
12847
12848 /* Write ones to all the bits defined by RdMask and WrMask, then
12849 * make sure the read-only bits are not changed and the
12850 * read/write bits are all ones.
12851 */
12852 tw32(offset, read_mask | write_mask);
12853
12854 val = tr32(offset);
12855
12856 /* Test the read-only bits. */
12857 if ((val & read_mask) != read_val)
12858 goto out;
12859
12860 /* Test the read/write bits. */
12861 if ((val & write_mask) != write_mask)
12862 goto out;
12863
12864 tw32(offset, save_val);
12865 }
12866
12867 return 0;
12868
12869out:
Michael Chan9f88f292006-12-07 00:22:54 -080012870 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012871 netdev_err(tp->dev,
12872 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012873 tw32(offset, save_val);
12874 return -EIO;
12875}
12876
Michael Chan7942e1d2005-05-29 14:58:36 -070012877static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12878{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012879 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012880 int i;
12881 u32 j;
12882
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012883 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012884 for (j = 0; j < len; j += 4) {
12885 u32 val;
12886
12887 tg3_write_mem(tp, offset + j, test_pattern[i]);
12888 tg3_read_mem(tp, offset + j, &val);
12889 if (val != test_pattern[i])
12890 return -EIO;
12891 }
12892 }
12893 return 0;
12894}
12895
12896static int tg3_test_memory(struct tg3 *tp)
12897{
12898 static struct mem_entry {
12899 u32 offset;
12900 u32 len;
12901 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012902 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012903 { 0x00002000, 0x1c000},
12904 { 0xffffffff, 0x00000}
12905 }, mem_tbl_5705[] = {
12906 { 0x00000100, 0x0000c},
12907 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012908 { 0x00004000, 0x00800},
12909 { 0x00006000, 0x01000},
12910 { 0x00008000, 0x02000},
12911 { 0x00010000, 0x0e000},
12912 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012913 }, mem_tbl_5755[] = {
12914 { 0x00000200, 0x00008},
12915 { 0x00004000, 0x00800},
12916 { 0x00006000, 0x00800},
12917 { 0x00008000, 0x02000},
12918 { 0x00010000, 0x0c000},
12919 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012920 }, mem_tbl_5906[] = {
12921 { 0x00000200, 0x00008},
12922 { 0x00004000, 0x00400},
12923 { 0x00006000, 0x00400},
12924 { 0x00008000, 0x01000},
12925 { 0x00010000, 0x01000},
12926 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012927 }, mem_tbl_5717[] = {
12928 { 0x00000200, 0x00008},
12929 { 0x00010000, 0x0a000},
12930 { 0x00020000, 0x13c00},
12931 { 0xffffffff, 0x00000}
12932 }, mem_tbl_57765[] = {
12933 { 0x00000200, 0x00008},
12934 { 0x00004000, 0x00800},
12935 { 0x00006000, 0x09800},
12936 { 0x00010000, 0x0a000},
12937 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012938 };
12939 struct mem_entry *mem_tbl;
12940 int err = 0;
12941 int i;
12942
Joe Perches63c3a662011-04-26 08:12:10 +000012943 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012944 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012945 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000012946 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012947 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012948 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012949 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000012950 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080012951 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012952 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012953 mem_tbl = mem_tbl_5705;
12954 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012955 mem_tbl = mem_tbl_570x;
12956
12957 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012958 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12959 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012960 break;
12961 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012962
Michael Chan7942e1d2005-05-29 14:58:36 -070012963 return err;
12964}
12965
Matt Carlsonbb158d62011-04-25 12:42:47 +000012966#define TG3_TSO_MSS 500
12967
12968#define TG3_TSO_IP_HDR_LEN 20
12969#define TG3_TSO_TCP_HDR_LEN 20
12970#define TG3_TSO_TCP_OPT_LEN 12
12971
12972static const u8 tg3_tso_header[] = {
129730x08, 0x00,
129740x45, 0x00, 0x00, 0x00,
129750x00, 0x00, 0x40, 0x00,
129760x40, 0x06, 0x00, 0x00,
129770x0a, 0x00, 0x00, 0x01,
129780x0a, 0x00, 0x00, 0x02,
129790x0d, 0x00, 0xe0, 0x00,
129800x00, 0x00, 0x01, 0x00,
129810x00, 0x00, 0x02, 0x00,
129820x80, 0x10, 0x10, 0x00,
129830x14, 0x09, 0x00, 0x00,
129840x01, 0x01, 0x08, 0x0a,
129850x11, 0x11, 0x11, 0x11,
129860x11, 0x11, 0x11, 0x11,
12987};
Michael Chan9f40dea2005-09-05 17:53:06 -070012988
Matt Carlson28a45952011-08-19 13:58:22 +000012989static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012990{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012991 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012992 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012993 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012994 struct sk_buff *skb;
12995 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012996 dma_addr_t map;
12997 int num_pkts, tx_len, rx_len, i, err;
12998 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012999 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013000 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013001
Matt Carlsonc8873402010-02-12 14:47:11 +000013002 tnapi = &tp->napi[0];
13003 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013004 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013005 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013006 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013007 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013008 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013009 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013010 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013011
Michael Chanc76949a2005-05-29 14:58:59 -070013012 err = -EIO;
13013
Matt Carlson4852a862011-04-13 11:05:07 +000013014 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013015 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013016 if (!skb)
13017 return -ENOMEM;
13018
Michael Chanc76949a2005-05-29 14:58:59 -070013019 tx_data = skb_put(skb, tx_len);
13020 memcpy(tx_data, tp->dev->dev_addr, 6);
13021 memset(tx_data + 6, 0x0, 8);
13022
Matt Carlson4852a862011-04-13 11:05:07 +000013023 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013024
Matt Carlson28a45952011-08-19 13:58:22 +000013025 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013026 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13027
13028 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13029 TG3_TSO_TCP_OPT_LEN;
13030
13031 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13032 sizeof(tg3_tso_header));
13033 mss = TG3_TSO_MSS;
13034
13035 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13036 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13037
13038 /* Set the total length field in the IP header */
13039 iph->tot_len = htons((u16)(mss + hdr_len));
13040
13041 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13042 TXD_FLAG_CPU_POST_DMA);
13043
Joe Perches63c3a662011-04-26 08:12:10 +000013044 if (tg3_flag(tp, HW_TSO_1) ||
13045 tg3_flag(tp, HW_TSO_2) ||
13046 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013047 struct tcphdr *th;
13048 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13049 th = (struct tcphdr *)&tx_data[val];
13050 th->check = 0;
13051 } else
13052 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13053
Joe Perches63c3a662011-04-26 08:12:10 +000013054 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013055 mss |= (hdr_len & 0xc) << 12;
13056 if (hdr_len & 0x10)
13057 base_flags |= 0x00000010;
13058 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013059 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013060 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013061 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013062 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013063 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13064 } else {
13065 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13066 }
13067
13068 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13069 } else {
13070 num_pkts = 1;
13071 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013072
13073 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13074 tx_len > VLAN_ETH_FRAME_LEN)
13075 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013076 }
13077
13078 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013079 tx_data[i] = (u8) (i & 0xff);
13080
Alexander Duyckf4188d82009-12-02 16:48:38 +000013081 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13082 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013083 dev_kfree_skb(skb);
13084 return -EIO;
13085 }
Michael Chanc76949a2005-05-29 14:58:59 -070013086
Matt Carlson0d681b22011-07-27 14:20:49 +000013087 val = tnapi->tx_prod;
13088 tnapi->tx_buffers[val].skb = skb;
13089 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13090
Michael Chanc76949a2005-05-29 14:58:59 -070013091 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013092 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013093
13094 udelay(10);
13095
Matt Carlson898a56f2009-08-28 14:02:40 +000013096 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013097
Matt Carlson84b67b22011-07-27 14:20:52 +000013098 budget = tg3_tx_avail(tnapi);
13099 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013100 base_flags | TXD_FLAG_END, mss, 0)) {
13101 tnapi->tx_buffers[val].skb = NULL;
13102 dev_kfree_skb(skb);
13103 return -EIO;
13104 }
Michael Chanc76949a2005-05-29 14:58:59 -070013105
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013106 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013107
Michael Chan6541b802012-03-04 14:48:14 +000013108 /* Sync BD data before updating mailbox */
13109 wmb();
13110
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013111 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13112 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013113
13114 udelay(10);
13115
Matt Carlson303fc922009-11-02 14:27:34 +000013116 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13117 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013118 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013119 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013120
13121 udelay(10);
13122
Matt Carlson898a56f2009-08-28 14:02:40 +000013123 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13124 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013125 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013126 (rx_idx == (rx_start_idx + num_pkts)))
13127 break;
13128 }
13129
Matt Carlsonba1142e2011-11-04 09:15:00 +000013130 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013131 dev_kfree_skb(skb);
13132
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013133 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013134 goto out;
13135
13136 if (rx_idx != rx_start_idx + num_pkts)
13137 goto out;
13138
Matt Carlsonbb158d62011-04-25 12:42:47 +000013139 val = data_off;
13140 while (rx_idx != rx_start_idx) {
13141 desc = &rnapi->rx_rcb[rx_start_idx++];
13142 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13143 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013144
Matt Carlsonbb158d62011-04-25 12:42:47 +000013145 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13146 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013147 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013148
Matt Carlsonbb158d62011-04-25 12:42:47 +000013149 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13150 - ETH_FCS_LEN;
13151
Matt Carlson28a45952011-08-19 13:58:22 +000013152 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013153 if (rx_len != tx_len)
13154 goto out;
13155
13156 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13157 if (opaque_key != RXD_OPAQUE_RING_STD)
13158 goto out;
13159 } else {
13160 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13161 goto out;
13162 }
13163 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13164 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013165 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013166 goto out;
13167 }
13168
13169 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013170 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013171 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13172 mapping);
13173 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013174 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013175 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13176 mapping);
13177 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013178 goto out;
13179
Matt Carlsonbb158d62011-04-25 12:42:47 +000013180 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13181 PCI_DMA_FROMDEVICE);
13182
Eric Dumazet9205fd92011-11-18 06:47:01 +000013183 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013184 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013185 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013186 goto out;
13187 }
Matt Carlson4852a862011-04-13 11:05:07 +000013188 }
13189
Michael Chanc76949a2005-05-29 14:58:59 -070013190 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013191
Eric Dumazet9205fd92011-11-18 06:47:01 +000013192 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013193out:
13194 return err;
13195}
13196
Matt Carlson00c266b2011-04-25 12:42:46 +000013197#define TG3_STD_LOOPBACK_FAILED 1
13198#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013199#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013200#define TG3_LOOPBACK_FAILED \
13201 (TG3_STD_LOOPBACK_FAILED | \
13202 TG3_JMB_LOOPBACK_FAILED | \
13203 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013204
Matt Carlson941ec902011-08-19 13:58:23 +000013205static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013206{
Matt Carlson28a45952011-08-19 13:58:22 +000013207 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013208 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013209 u32 jmb_pkt_sz = 9000;
13210
13211 if (tp->dma_limit)
13212 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013213
Matt Carlsonab789042011-01-25 15:58:54 +000013214 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13215 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13216
Matt Carlson28a45952011-08-19 13:58:22 +000013217 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013218 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13219 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013220 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013221 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013222 goto done;
13223 }
13224
Joe Perches953c96e2013-04-09 10:18:14 +000013225 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013226 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013227 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13228 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013229 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013230 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013231 goto done;
13232 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013233
Joe Perches63c3a662011-04-26 08:12:10 +000013234 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013235 int i;
13236
13237 /* Reroute all rx packets to the 1st queue */
13238 for (i = MAC_RSS_INDIR_TBL_0;
13239 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13240 tw32(i, 0x0);
13241 }
13242
Matt Carlson6e01b202011-08-19 13:58:20 +000013243 /* HW errata - mac loopback fails in some cases on 5780.
13244 * Normal traffic and PHY loopback are not affected by
13245 * errata. Also, the MAC loopback test is deprecated for
13246 * all newer ASIC revisions.
13247 */
Joe Perches41535772013-02-16 11:20:04 +000013248 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013249 !tg3_flag(tp, CPMU_PRESENT)) {
13250 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013251
Matt Carlson28a45952011-08-19 13:58:22 +000013252 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013253 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013254
13255 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013256 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013257 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013258
13259 tg3_mac_loopback(tp, false);
13260 }
Matt Carlson4852a862011-04-13 11:05:07 +000013261
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013262 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013263 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013264 int i;
13265
Matt Carlson941ec902011-08-19 13:58:23 +000013266 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013267
13268 /* Wait for link */
13269 for (i = 0; i < 100; i++) {
13270 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13271 break;
13272 mdelay(1);
13273 }
13274
Matt Carlson28a45952011-08-19 13:58:22 +000013275 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013276 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013277 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013278 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013279 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013280 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013281 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013282 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013283
Matt Carlson941ec902011-08-19 13:58:23 +000013284 if (do_extlpbk) {
13285 tg3_phy_lpbk_set(tp, 0, true);
13286
13287 /* All link indications report up, but the hardware
13288 * isn't really ready for about 20 msec. Double it
13289 * to be sure.
13290 */
13291 mdelay(40);
13292
13293 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013294 data[TG3_EXT_LOOPB_TEST] |=
13295 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013296 if (tg3_flag(tp, TSO_CAPABLE) &&
13297 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013298 data[TG3_EXT_LOOPB_TEST] |=
13299 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013300 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013301 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013302 data[TG3_EXT_LOOPB_TEST] |=
13303 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013304 }
13305
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013306 /* Re-enable gphy autopowerdown. */
13307 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13308 tg3_phy_toggle_apd(tp, true);
13309 }
Matt Carlson6833c042008-11-21 17:18:59 -080013310
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013311 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13312 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013313
Matt Carlsonab789042011-01-25 15:58:54 +000013314done:
13315 tp->phy_flags |= eee_cap;
13316
Michael Chan9f40dea2005-09-05 17:53:06 -070013317 return err;
13318}
13319
Michael Chan4cafd3f2005-05-29 14:56:34 -070013320static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13321 u64 *data)
13322{
Michael Chan566f86a2005-05-29 14:56:58 -070013323 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013324 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013325
Matt Carlsonbed98292011-07-13 09:27:29 +000013326 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
13327 tg3_power_up(tp)) {
13328 etest->flags |= ETH_TEST_FL_FAILED;
13329 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13330 return;
13331 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013332
Michael Chan566f86a2005-05-29 14:56:58 -070013333 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13334
13335 if (tg3_test_nvram(tp) != 0) {
13336 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013337 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013338 }
Matt Carlson941ec902011-08-19 13:58:23 +000013339 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013340 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013341 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013342 }
Michael Chana71116d2005-05-29 14:58:11 -070013343 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013344 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013345
Michael Chanbbe832c2005-06-24 20:20:04 -070013346 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013347 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013348 tg3_netif_stop(tp);
13349 irq_sync = 1;
13350 }
13351
13352 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013353 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013354 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013355 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013356 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013357 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013358 if (!err)
13359 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013360
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013361 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013362 tg3_phy_reset(tp);
13363
Michael Chana71116d2005-05-29 14:58:11 -070013364 if (tg3_test_registers(tp) != 0) {
13365 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013366 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013367 }
Matt Carlson28a45952011-08-19 13:58:22 +000013368
Michael Chan7942e1d2005-05-29 14:58:36 -070013369 if (tg3_test_memory(tp) != 0) {
13370 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013371 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013372 }
Matt Carlson28a45952011-08-19 13:58:22 +000013373
Matt Carlson941ec902011-08-19 13:58:23 +000013374 if (doextlpbk)
13375 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13376
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013377 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013378 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013379
David S. Millerf47c11e2005-06-24 20:18:35 -070013380 tg3_full_unlock(tp);
13381
Michael Chand4bc3922005-05-29 14:59:20 -070013382 if (tg3_test_interrupt(tp) != 0) {
13383 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013384 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013385 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013386
13387 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013388
Michael Chana71116d2005-05-29 14:58:11 -070013389 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13390 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013391 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013392 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013393 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013394 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013395 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013396
13397 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013398
13399 if (irq_sync && !err2)
13400 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013401 }
Matt Carlson80096062010-08-02 11:26:06 +000013402 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013403 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013404
Michael Chan4cafd3f2005-05-29 14:56:34 -070013405}
13406
Matt Carlson0a633ac2012-12-03 19:36:59 +000013407static int tg3_hwtstamp_ioctl(struct net_device *dev,
13408 struct ifreq *ifr, int cmd)
13409{
13410 struct tg3 *tp = netdev_priv(dev);
13411 struct hwtstamp_config stmpconf;
13412
13413 if (!tg3_flag(tp, PTP_CAPABLE))
13414 return -EINVAL;
13415
13416 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13417 return -EFAULT;
13418
13419 if (stmpconf.flags)
13420 return -EINVAL;
13421
13422 switch (stmpconf.tx_type) {
13423 case HWTSTAMP_TX_ON:
13424 tg3_flag_set(tp, TX_TSTAMP_EN);
13425 break;
13426 case HWTSTAMP_TX_OFF:
13427 tg3_flag_clear(tp, TX_TSTAMP_EN);
13428 break;
13429 default:
13430 return -ERANGE;
13431 }
13432
13433 switch (stmpconf.rx_filter) {
13434 case HWTSTAMP_FILTER_NONE:
13435 tp->rxptpctl = 0;
13436 break;
13437 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13438 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13439 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13440 break;
13441 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13442 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13443 TG3_RX_PTP_CTL_SYNC_EVNT;
13444 break;
13445 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13446 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13447 TG3_RX_PTP_CTL_DELAY_REQ;
13448 break;
13449 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13450 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13451 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13452 break;
13453 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13454 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13455 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13456 break;
13457 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13458 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13459 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13460 break;
13461 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13462 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13463 TG3_RX_PTP_CTL_SYNC_EVNT;
13464 break;
13465 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13466 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13467 TG3_RX_PTP_CTL_SYNC_EVNT;
13468 break;
13469 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13470 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13471 TG3_RX_PTP_CTL_SYNC_EVNT;
13472 break;
13473 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13474 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13475 TG3_RX_PTP_CTL_DELAY_REQ;
13476 break;
13477 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13478 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13479 TG3_RX_PTP_CTL_DELAY_REQ;
13480 break;
13481 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13482 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13483 TG3_RX_PTP_CTL_DELAY_REQ;
13484 break;
13485 default:
13486 return -ERANGE;
13487 }
13488
13489 if (netif_running(dev) && tp->rxptpctl)
13490 tw32(TG3_RX_PTP_CTL,
13491 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13492
13493 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13494 -EFAULT : 0;
13495}
13496
Linus Torvalds1da177e2005-04-16 15:20:36 -070013497static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13498{
13499 struct mii_ioctl_data *data = if_mii(ifr);
13500 struct tg3 *tp = netdev_priv(dev);
13501 int err;
13502
Joe Perches63c3a662011-04-26 08:12:10 +000013503 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013504 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013505 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013506 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013507 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013508 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013509 }
13510
Matt Carlson33f401a2010-04-05 10:19:27 +000013511 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013512 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013513 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013514
13515 /* fallthru */
13516 case SIOCGMIIREG: {
13517 u32 mii_regval;
13518
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013519 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013520 break; /* We have no PHY */
13521
Matt Carlson34eea5a2011-04-20 07:57:38 +000013522 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013523 return -EAGAIN;
13524
David S. Millerf47c11e2005-06-24 20:18:35 -070013525 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013526 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13527 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013528 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013529
13530 data->val_out = mii_regval;
13531
13532 return err;
13533 }
13534
13535 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013536 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013537 break; /* We have no PHY */
13538
Matt Carlson34eea5a2011-04-20 07:57:38 +000013539 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013540 return -EAGAIN;
13541
David S. Millerf47c11e2005-06-24 20:18:35 -070013542 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013543 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13544 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013545 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013546
13547 return err;
13548
Matt Carlson0a633ac2012-12-03 19:36:59 +000013549 case SIOCSHWTSTAMP:
13550 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13551
Linus Torvalds1da177e2005-04-16 15:20:36 -070013552 default:
13553 /* do nothing */
13554 break;
13555 }
13556 return -EOPNOTSUPP;
13557}
13558
David S. Miller15f98502005-05-18 22:49:26 -070013559static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13560{
13561 struct tg3 *tp = netdev_priv(dev);
13562
13563 memcpy(ec, &tp->coal, sizeof(*ec));
13564 return 0;
13565}
13566
Michael Chand244c892005-07-05 14:42:33 -070013567static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13568{
13569 struct tg3 *tp = netdev_priv(dev);
13570 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13571 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13572
Joe Perches63c3a662011-04-26 08:12:10 +000013573 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013574 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13575 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13576 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13577 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13578 }
13579
13580 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13581 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13582 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13583 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13584 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13585 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13586 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13587 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13588 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13589 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13590 return -EINVAL;
13591
13592 /* No rx interrupts will be generated if both are zero */
13593 if ((ec->rx_coalesce_usecs == 0) &&
13594 (ec->rx_max_coalesced_frames == 0))
13595 return -EINVAL;
13596
13597 /* No tx interrupts will be generated if both are zero */
13598 if ((ec->tx_coalesce_usecs == 0) &&
13599 (ec->tx_max_coalesced_frames == 0))
13600 return -EINVAL;
13601
13602 /* Only copy relevant parameters, ignore all others. */
13603 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13604 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13605 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13606 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13607 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13608 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13609 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13610 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13611 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13612
13613 if (netif_running(dev)) {
13614 tg3_full_lock(tp, 0);
13615 __tg3_set_coalesce(tp, &tp->coal);
13616 tg3_full_unlock(tp);
13617 }
13618 return 0;
13619}
13620
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013621static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
13622{
13623 struct tg3 *tp = netdev_priv(dev);
13624
13625 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13626 netdev_warn(tp->dev, "Board does not support EEE!\n");
13627 return -EOPNOTSUPP;
13628 }
13629
13630 if (edata->advertised != tp->eee.advertised) {
13631 netdev_warn(tp->dev,
13632 "Direct manipulation of EEE advertisement is not supported\n");
13633 return -EINVAL;
13634 }
13635
13636 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
13637 netdev_warn(tp->dev,
13638 "Maximal Tx Lpi timer supported is %#x(u)\n",
13639 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
13640 return -EINVAL;
13641 }
13642
13643 tp->eee = *edata;
13644
13645 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
13646 tg3_warn_mgmt_link_flap(tp);
13647
13648 if (netif_running(tp->dev)) {
13649 tg3_full_lock(tp, 0);
13650 tg3_setup_eee(tp);
13651 tg3_phy_reset(tp);
13652 tg3_full_unlock(tp);
13653 }
13654
13655 return 0;
13656}
13657
13658static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
13659{
13660 struct tg3 *tp = netdev_priv(dev);
13661
13662 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13663 netdev_warn(tp->dev,
13664 "Board does not support EEE!\n");
13665 return -EOPNOTSUPP;
13666 }
13667
13668 *edata = tp->eee;
13669 return 0;
13670}
13671
Jeff Garzik7282d492006-09-13 14:30:00 -040013672static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013673 .get_settings = tg3_get_settings,
13674 .set_settings = tg3_set_settings,
13675 .get_drvinfo = tg3_get_drvinfo,
13676 .get_regs_len = tg3_get_regs_len,
13677 .get_regs = tg3_get_regs,
13678 .get_wol = tg3_get_wol,
13679 .set_wol = tg3_set_wol,
13680 .get_msglevel = tg3_get_msglevel,
13681 .set_msglevel = tg3_set_msglevel,
13682 .nway_reset = tg3_nway_reset,
13683 .get_link = ethtool_op_get_link,
13684 .get_eeprom_len = tg3_get_eeprom_len,
13685 .get_eeprom = tg3_get_eeprom,
13686 .set_eeprom = tg3_set_eeprom,
13687 .get_ringparam = tg3_get_ringparam,
13688 .set_ringparam = tg3_set_ringparam,
13689 .get_pauseparam = tg3_get_pauseparam,
13690 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013691 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013692 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013693 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013694 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013695 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013696 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013697 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013698 .get_rxnfc = tg3_get_rxnfc,
13699 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13700 .get_rxfh_indir = tg3_get_rxfh_indir,
13701 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013702 .get_channels = tg3_get_channels,
13703 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013704 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013705 .get_eee = tg3_get_eee,
13706 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013707};
13708
David S. Millerb4017c52012-03-01 17:57:40 -050013709static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13710 struct rtnl_link_stats64 *stats)
13711{
13712 struct tg3 *tp = netdev_priv(dev);
13713
David S. Millerb4017c52012-03-01 17:57:40 -050013714 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013715 if (!tp->hw_stats) {
13716 spin_unlock_bh(&tp->lock);
13717 return &tp->net_stats_prev;
13718 }
13719
David S. Millerb4017c52012-03-01 17:57:40 -050013720 tg3_get_nstats(tp, stats);
13721 spin_unlock_bh(&tp->lock);
13722
13723 return stats;
13724}
13725
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013726static void tg3_set_rx_mode(struct net_device *dev)
13727{
13728 struct tg3 *tp = netdev_priv(dev);
13729
13730 if (!netif_running(dev))
13731 return;
13732
13733 tg3_full_lock(tp, 0);
13734 __tg3_set_rx_mode(dev);
13735 tg3_full_unlock(tp);
13736}
13737
Matt Carlsonfaf16272012-02-13 10:20:07 +000013738static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13739 int new_mtu)
13740{
13741 dev->mtu = new_mtu;
13742
13743 if (new_mtu > ETH_DATA_LEN) {
13744 if (tg3_flag(tp, 5780_CLASS)) {
13745 netdev_update_features(dev);
13746 tg3_flag_clear(tp, TSO_CAPABLE);
13747 } else {
13748 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13749 }
13750 } else {
13751 if (tg3_flag(tp, 5780_CLASS)) {
13752 tg3_flag_set(tp, TSO_CAPABLE);
13753 netdev_update_features(dev);
13754 }
13755 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13756 }
13757}
13758
13759static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13760{
13761 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000013762 int err;
13763 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013764
13765 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13766 return -EINVAL;
13767
13768 if (!netif_running(dev)) {
13769 /* We'll just catch it later when the
13770 * device is up'd.
13771 */
13772 tg3_set_mtu(dev, tp, new_mtu);
13773 return 0;
13774 }
13775
13776 tg3_phy_stop(tp);
13777
13778 tg3_netif_stop(tp);
13779
13780 tg3_full_lock(tp, 1);
13781
13782 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13783
13784 tg3_set_mtu(dev, tp, new_mtu);
13785
Michael Chan2fae5e32012-03-04 14:48:15 +000013786 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13787 * breaks all requests to 256 bytes.
13788 */
Joe Perches41535772013-02-16 11:20:04 +000013789 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000013790 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000013791
13792 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013793
13794 if (!err)
13795 tg3_netif_start(tp);
13796
13797 tg3_full_unlock(tp);
13798
13799 if (!err)
13800 tg3_phy_start(tp);
13801
13802 return err;
13803}
13804
13805static const struct net_device_ops tg3_netdev_ops = {
13806 .ndo_open = tg3_open,
13807 .ndo_stop = tg3_close,
13808 .ndo_start_xmit = tg3_start_xmit,
13809 .ndo_get_stats64 = tg3_get_stats64,
13810 .ndo_validate_addr = eth_validate_addr,
13811 .ndo_set_rx_mode = tg3_set_rx_mode,
13812 .ndo_set_mac_address = tg3_set_mac_addr,
13813 .ndo_do_ioctl = tg3_ioctl,
13814 .ndo_tx_timeout = tg3_tx_timeout,
13815 .ndo_change_mtu = tg3_change_mtu,
13816 .ndo_fix_features = tg3_fix_features,
13817 .ndo_set_features = tg3_set_features,
13818#ifdef CONFIG_NET_POLL_CONTROLLER
13819 .ndo_poll_controller = tg3_poll_controller,
13820#endif
13821};
13822
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013823static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013824{
Michael Chan1b277772006-03-20 22:27:48 -080013825 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013826
13827 tp->nvram_size = EEPROM_CHIP_SIZE;
13828
Matt Carlsone4f34112009-02-25 14:25:00 +000013829 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013830 return;
13831
Michael Chanb16250e2006-09-27 16:10:14 -070013832 if ((magic != TG3_EEPROM_MAGIC) &&
13833 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13834 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013835 return;
13836
13837 /*
13838 * Size the chip by reading offsets at increasing powers of two.
13839 * When we encounter our validation signature, we know the addressing
13840 * has wrapped around, and thus have our chip size.
13841 */
Michael Chan1b277772006-03-20 22:27:48 -080013842 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013843
13844 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013845 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013846 return;
13847
Michael Chan18201802006-03-20 22:29:15 -080013848 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013849 break;
13850
13851 cursize <<= 1;
13852 }
13853
13854 tp->nvram_size = cursize;
13855}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013856
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013857static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013858{
13859 u32 val;
13860
Joe Perches63c3a662011-04-26 08:12:10 +000013861 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013862 return;
13863
13864 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013865 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013866 tg3_get_eeprom_size(tp);
13867 return;
13868 }
13869
Matt Carlson6d348f22009-02-25 14:25:52 +000013870 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013871 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013872 /* This is confusing. We want to operate on the
13873 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13874 * call will read from NVRAM and byteswap the data
13875 * according to the byteswapping settings for all
13876 * other register accesses. This ensures the data we
13877 * want will always reside in the lower 16-bits.
13878 * However, the data in NVRAM is in LE format, which
13879 * means the data from the NVRAM read will always be
13880 * opposite the endianness of the CPU. The 16-bit
13881 * byteswap then brings the data to CPU endianness.
13882 */
13883 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013884 return;
13885 }
13886 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013887 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013888}
13889
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013890static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013891{
13892 u32 nvcfg1;
13893
13894 nvcfg1 = tr32(NVRAM_CFG1);
13895 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013896 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013897 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013898 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13899 tw32(NVRAM_CFG1, nvcfg1);
13900 }
13901
Joe Perches41535772013-02-16 11:20:04 +000013902 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013903 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013904 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013905 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13906 tp->nvram_jedecnum = JEDEC_ATMEL;
13907 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013908 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013909 break;
13910 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13911 tp->nvram_jedecnum = JEDEC_ATMEL;
13912 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13913 break;
13914 case FLASH_VENDOR_ATMEL_EEPROM:
13915 tp->nvram_jedecnum = JEDEC_ATMEL;
13916 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013917 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013918 break;
13919 case FLASH_VENDOR_ST:
13920 tp->nvram_jedecnum = JEDEC_ST;
13921 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013922 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013923 break;
13924 case FLASH_VENDOR_SAIFUN:
13925 tp->nvram_jedecnum = JEDEC_SAIFUN;
13926 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13927 break;
13928 case FLASH_VENDOR_SST_SMALL:
13929 case FLASH_VENDOR_SST_LARGE:
13930 tp->nvram_jedecnum = JEDEC_SST;
13931 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013933 }
Matt Carlson8590a602009-08-28 12:29:16 +000013934 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013935 tp->nvram_jedecnum = JEDEC_ATMEL;
13936 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013937 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013938 }
13939}
13940
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013941static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013942{
13943 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13944 case FLASH_5752PAGE_SIZE_256:
13945 tp->nvram_pagesize = 256;
13946 break;
13947 case FLASH_5752PAGE_SIZE_512:
13948 tp->nvram_pagesize = 512;
13949 break;
13950 case FLASH_5752PAGE_SIZE_1K:
13951 tp->nvram_pagesize = 1024;
13952 break;
13953 case FLASH_5752PAGE_SIZE_2K:
13954 tp->nvram_pagesize = 2048;
13955 break;
13956 case FLASH_5752PAGE_SIZE_4K:
13957 tp->nvram_pagesize = 4096;
13958 break;
13959 case FLASH_5752PAGE_SIZE_264:
13960 tp->nvram_pagesize = 264;
13961 break;
13962 case FLASH_5752PAGE_SIZE_528:
13963 tp->nvram_pagesize = 528;
13964 break;
13965 }
13966}
13967
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013968static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013969{
13970 u32 nvcfg1;
13971
13972 nvcfg1 = tr32(NVRAM_CFG1);
13973
Michael Chane6af3012005-04-21 17:12:05 -070013974 /* NVRAM protection for TPM */
13975 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013976 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013977
Michael Chan361b4ac2005-04-21 17:11:21 -070013978 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013979 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13980 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13981 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013982 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013983 break;
13984 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13985 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013986 tg3_flag_set(tp, NVRAM_BUFFERED);
13987 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013988 break;
13989 case FLASH_5752VENDOR_ST_M45PE10:
13990 case FLASH_5752VENDOR_ST_M45PE20:
13991 case FLASH_5752VENDOR_ST_M45PE40:
13992 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013993 tg3_flag_set(tp, NVRAM_BUFFERED);
13994 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013995 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013996 }
13997
Joe Perches63c3a662011-04-26 08:12:10 +000013998 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013999 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014000 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014001 /* For eeprom, set pagesize to maximum eeprom size */
14002 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14003
14004 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14005 tw32(NVRAM_CFG1, nvcfg1);
14006 }
14007}
14008
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014009static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014010{
Matt Carlson989a9d22007-05-05 11:51:05 -070014011 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014012
14013 nvcfg1 = tr32(NVRAM_CFG1);
14014
14015 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014016 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014017 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014018 protect = 1;
14019 }
Michael Chand3c7b882006-03-23 01:28:25 -080014020
Matt Carlson989a9d22007-05-05 11:51:05 -070014021 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14022 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014023 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14024 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14025 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14026 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14027 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014028 tg3_flag_set(tp, NVRAM_BUFFERED);
14029 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014030 tp->nvram_pagesize = 264;
14031 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14032 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14033 tp->nvram_size = (protect ? 0x3e200 :
14034 TG3_NVRAM_SIZE_512KB);
14035 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14036 tp->nvram_size = (protect ? 0x1f200 :
14037 TG3_NVRAM_SIZE_256KB);
14038 else
14039 tp->nvram_size = (protect ? 0x1f200 :
14040 TG3_NVRAM_SIZE_128KB);
14041 break;
14042 case FLASH_5752VENDOR_ST_M45PE10:
14043 case FLASH_5752VENDOR_ST_M45PE20:
14044 case FLASH_5752VENDOR_ST_M45PE40:
14045 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014046 tg3_flag_set(tp, NVRAM_BUFFERED);
14047 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014048 tp->nvram_pagesize = 256;
14049 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14050 tp->nvram_size = (protect ?
14051 TG3_NVRAM_SIZE_64KB :
14052 TG3_NVRAM_SIZE_128KB);
14053 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14054 tp->nvram_size = (protect ?
14055 TG3_NVRAM_SIZE_64KB :
14056 TG3_NVRAM_SIZE_256KB);
14057 else
14058 tp->nvram_size = (protect ?
14059 TG3_NVRAM_SIZE_128KB :
14060 TG3_NVRAM_SIZE_512KB);
14061 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014062 }
14063}
14064
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014065static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014066{
14067 u32 nvcfg1;
14068
14069 nvcfg1 = tr32(NVRAM_CFG1);
14070
14071 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014072 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14073 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14074 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14075 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14076 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014077 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014078 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014079
Matt Carlson8590a602009-08-28 12:29:16 +000014080 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14081 tw32(NVRAM_CFG1, nvcfg1);
14082 break;
14083 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14084 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14085 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14086 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14087 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014088 tg3_flag_set(tp, NVRAM_BUFFERED);
14089 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014090 tp->nvram_pagesize = 264;
14091 break;
14092 case FLASH_5752VENDOR_ST_M45PE10:
14093 case FLASH_5752VENDOR_ST_M45PE20:
14094 case FLASH_5752VENDOR_ST_M45PE40:
14095 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014096 tg3_flag_set(tp, NVRAM_BUFFERED);
14097 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014098 tp->nvram_pagesize = 256;
14099 break;
Michael Chan1b277772006-03-20 22:27:48 -080014100 }
14101}
14102
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014103static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014104{
14105 u32 nvcfg1, protect = 0;
14106
14107 nvcfg1 = tr32(NVRAM_CFG1);
14108
14109 /* NVRAM protection for TPM */
14110 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014111 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014112 protect = 1;
14113 }
14114
14115 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14116 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014117 case FLASH_5761VENDOR_ATMEL_ADB021D:
14118 case FLASH_5761VENDOR_ATMEL_ADB041D:
14119 case FLASH_5761VENDOR_ATMEL_ADB081D:
14120 case FLASH_5761VENDOR_ATMEL_ADB161D:
14121 case FLASH_5761VENDOR_ATMEL_MDB021D:
14122 case FLASH_5761VENDOR_ATMEL_MDB041D:
14123 case FLASH_5761VENDOR_ATMEL_MDB081D:
14124 case FLASH_5761VENDOR_ATMEL_MDB161D:
14125 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014126 tg3_flag_set(tp, NVRAM_BUFFERED);
14127 tg3_flag_set(tp, FLASH);
14128 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014129 tp->nvram_pagesize = 256;
14130 break;
14131 case FLASH_5761VENDOR_ST_A_M45PE20:
14132 case FLASH_5761VENDOR_ST_A_M45PE40:
14133 case FLASH_5761VENDOR_ST_A_M45PE80:
14134 case FLASH_5761VENDOR_ST_A_M45PE16:
14135 case FLASH_5761VENDOR_ST_M_M45PE20:
14136 case FLASH_5761VENDOR_ST_M_M45PE40:
14137 case FLASH_5761VENDOR_ST_M_M45PE80:
14138 case FLASH_5761VENDOR_ST_M_M45PE16:
14139 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014140 tg3_flag_set(tp, NVRAM_BUFFERED);
14141 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014142 tp->nvram_pagesize = 256;
14143 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014144 }
14145
14146 if (protect) {
14147 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14148 } else {
14149 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014150 case FLASH_5761VENDOR_ATMEL_ADB161D:
14151 case FLASH_5761VENDOR_ATMEL_MDB161D:
14152 case FLASH_5761VENDOR_ST_A_M45PE16:
14153 case FLASH_5761VENDOR_ST_M_M45PE16:
14154 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14155 break;
14156 case FLASH_5761VENDOR_ATMEL_ADB081D:
14157 case FLASH_5761VENDOR_ATMEL_MDB081D:
14158 case FLASH_5761VENDOR_ST_A_M45PE80:
14159 case FLASH_5761VENDOR_ST_M_M45PE80:
14160 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14161 break;
14162 case FLASH_5761VENDOR_ATMEL_ADB041D:
14163 case FLASH_5761VENDOR_ATMEL_MDB041D:
14164 case FLASH_5761VENDOR_ST_A_M45PE40:
14165 case FLASH_5761VENDOR_ST_M_M45PE40:
14166 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14167 break;
14168 case FLASH_5761VENDOR_ATMEL_ADB021D:
14169 case FLASH_5761VENDOR_ATMEL_MDB021D:
14170 case FLASH_5761VENDOR_ST_A_M45PE20:
14171 case FLASH_5761VENDOR_ST_M_M45PE20:
14172 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14173 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014174 }
14175 }
14176}
14177
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014178static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014179{
14180 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014181 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014182 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14183}
14184
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014185static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014186{
14187 u32 nvcfg1;
14188
14189 nvcfg1 = tr32(NVRAM_CFG1);
14190
14191 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14192 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14193 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14194 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014195 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014196 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14197
14198 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14199 tw32(NVRAM_CFG1, nvcfg1);
14200 return;
14201 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14202 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14203 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14204 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14205 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14206 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14207 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14208 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014209 tg3_flag_set(tp, NVRAM_BUFFERED);
14210 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014211
14212 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14213 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14214 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14215 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14216 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14217 break;
14218 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14219 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14220 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14221 break;
14222 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14223 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14224 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14225 break;
14226 }
14227 break;
14228 case FLASH_5752VENDOR_ST_M45PE10:
14229 case FLASH_5752VENDOR_ST_M45PE20:
14230 case FLASH_5752VENDOR_ST_M45PE40:
14231 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014232 tg3_flag_set(tp, NVRAM_BUFFERED);
14233 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014234
14235 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14236 case FLASH_5752VENDOR_ST_M45PE10:
14237 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14238 break;
14239 case FLASH_5752VENDOR_ST_M45PE20:
14240 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14241 break;
14242 case FLASH_5752VENDOR_ST_M45PE40:
14243 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14244 break;
14245 }
14246 break;
14247 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014248 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014249 return;
14250 }
14251
Matt Carlsona1b950d2009-09-01 13:20:17 +000014252 tg3_nvram_get_pagesize(tp, nvcfg1);
14253 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014254 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014255}
14256
14257
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014258static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014259{
14260 u32 nvcfg1;
14261
14262 nvcfg1 = tr32(NVRAM_CFG1);
14263
14264 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14265 case FLASH_5717VENDOR_ATMEL_EEPROM:
14266 case FLASH_5717VENDOR_MICRO_EEPROM:
14267 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014268 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014269 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14270
14271 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14272 tw32(NVRAM_CFG1, nvcfg1);
14273 return;
14274 case FLASH_5717VENDOR_ATMEL_MDB011D:
14275 case FLASH_5717VENDOR_ATMEL_ADB011B:
14276 case FLASH_5717VENDOR_ATMEL_ADB011D:
14277 case FLASH_5717VENDOR_ATMEL_MDB021D:
14278 case FLASH_5717VENDOR_ATMEL_ADB021B:
14279 case FLASH_5717VENDOR_ATMEL_ADB021D:
14280 case FLASH_5717VENDOR_ATMEL_45USPT:
14281 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014282 tg3_flag_set(tp, NVRAM_BUFFERED);
14283 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014284
14285 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14286 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014287 /* Detect size with tg3_nvram_get_size() */
14288 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014289 case FLASH_5717VENDOR_ATMEL_ADB021B:
14290 case FLASH_5717VENDOR_ATMEL_ADB021D:
14291 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14292 break;
14293 default:
14294 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14295 break;
14296 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014297 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014298 case FLASH_5717VENDOR_ST_M_M25PE10:
14299 case FLASH_5717VENDOR_ST_A_M25PE10:
14300 case FLASH_5717VENDOR_ST_M_M45PE10:
14301 case FLASH_5717VENDOR_ST_A_M45PE10:
14302 case FLASH_5717VENDOR_ST_M_M25PE20:
14303 case FLASH_5717VENDOR_ST_A_M25PE20:
14304 case FLASH_5717VENDOR_ST_M_M45PE20:
14305 case FLASH_5717VENDOR_ST_A_M45PE20:
14306 case FLASH_5717VENDOR_ST_25USPT:
14307 case FLASH_5717VENDOR_ST_45USPT:
14308 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014309 tg3_flag_set(tp, NVRAM_BUFFERED);
14310 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014311
14312 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14313 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014314 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014315 /* Detect size with tg3_nvram_get_size() */
14316 break;
14317 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014318 case FLASH_5717VENDOR_ST_A_M45PE20:
14319 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14320 break;
14321 default:
14322 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14323 break;
14324 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014325 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014326 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014327 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014328 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014329 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014330
14331 tg3_nvram_get_pagesize(tp, nvcfg1);
14332 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014333 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014334}
14335
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014336static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014337{
14338 u32 nvcfg1, nvmpinstrp;
14339
14340 nvcfg1 = tr32(NVRAM_CFG1);
14341 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14342
Joe Perches41535772013-02-16 11:20:04 +000014343 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014344 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14345 tg3_flag_set(tp, NO_NVRAM);
14346 return;
14347 }
14348
14349 switch (nvmpinstrp) {
14350 case FLASH_5762_EEPROM_HD:
14351 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014352 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014353 case FLASH_5762_EEPROM_LD:
14354 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014355 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014356 case FLASH_5720VENDOR_M_ST_M45PE20:
14357 /* This pinstrap supports multiple sizes, so force it
14358 * to read the actual size from location 0xf0.
14359 */
14360 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14361 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014362 }
14363 }
14364
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014365 switch (nvmpinstrp) {
14366 case FLASH_5720_EEPROM_HD:
14367 case FLASH_5720_EEPROM_LD:
14368 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014369 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014370
14371 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14372 tw32(NVRAM_CFG1, nvcfg1);
14373 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14374 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14375 else
14376 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14377 return;
14378 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14379 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14380 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14381 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14382 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14383 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14384 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14385 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14386 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14387 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14388 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14389 case FLASH_5720VENDOR_ATMEL_45USPT:
14390 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014391 tg3_flag_set(tp, NVRAM_BUFFERED);
14392 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014393
14394 switch (nvmpinstrp) {
14395 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14396 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14397 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14398 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14399 break;
14400 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14401 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14402 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14403 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14404 break;
14405 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14406 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14407 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14408 break;
14409 default:
Joe Perches41535772013-02-16 11:20:04 +000014410 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014411 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014412 break;
14413 }
14414 break;
14415 case FLASH_5720VENDOR_M_ST_M25PE10:
14416 case FLASH_5720VENDOR_M_ST_M45PE10:
14417 case FLASH_5720VENDOR_A_ST_M25PE10:
14418 case FLASH_5720VENDOR_A_ST_M45PE10:
14419 case FLASH_5720VENDOR_M_ST_M25PE20:
14420 case FLASH_5720VENDOR_M_ST_M45PE20:
14421 case FLASH_5720VENDOR_A_ST_M25PE20:
14422 case FLASH_5720VENDOR_A_ST_M45PE20:
14423 case FLASH_5720VENDOR_M_ST_M25PE40:
14424 case FLASH_5720VENDOR_M_ST_M45PE40:
14425 case FLASH_5720VENDOR_A_ST_M25PE40:
14426 case FLASH_5720VENDOR_A_ST_M45PE40:
14427 case FLASH_5720VENDOR_M_ST_M25PE80:
14428 case FLASH_5720VENDOR_M_ST_M45PE80:
14429 case FLASH_5720VENDOR_A_ST_M25PE80:
14430 case FLASH_5720VENDOR_A_ST_M45PE80:
14431 case FLASH_5720VENDOR_ST_25USPT:
14432 case FLASH_5720VENDOR_ST_45USPT:
14433 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014434 tg3_flag_set(tp, NVRAM_BUFFERED);
14435 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014436
14437 switch (nvmpinstrp) {
14438 case FLASH_5720VENDOR_M_ST_M25PE20:
14439 case FLASH_5720VENDOR_M_ST_M45PE20:
14440 case FLASH_5720VENDOR_A_ST_M25PE20:
14441 case FLASH_5720VENDOR_A_ST_M45PE20:
14442 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14443 break;
14444 case FLASH_5720VENDOR_M_ST_M25PE40:
14445 case FLASH_5720VENDOR_M_ST_M45PE40:
14446 case FLASH_5720VENDOR_A_ST_M25PE40:
14447 case FLASH_5720VENDOR_A_ST_M45PE40:
14448 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14449 break;
14450 case FLASH_5720VENDOR_M_ST_M25PE80:
14451 case FLASH_5720VENDOR_M_ST_M45PE80:
14452 case FLASH_5720VENDOR_A_ST_M25PE80:
14453 case FLASH_5720VENDOR_A_ST_M45PE80:
14454 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14455 break;
14456 default:
Joe Perches41535772013-02-16 11:20:04 +000014457 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014458 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014459 break;
14460 }
14461 break;
14462 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014463 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014464 return;
14465 }
14466
14467 tg3_nvram_get_pagesize(tp, nvcfg1);
14468 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014469 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014470
Joe Perches41535772013-02-16 11:20:04 +000014471 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014472 u32 val;
14473
14474 if (tg3_nvram_read(tp, 0, &val))
14475 return;
14476
14477 if (val != TG3_EEPROM_MAGIC &&
14478 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14479 tg3_flag_set(tp, NO_NVRAM);
14480 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014481}
14482
Linus Torvalds1da177e2005-04-16 15:20:36 -070014483/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014484static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014485{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014486 if (tg3_flag(tp, IS_SSB_CORE)) {
14487 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14488 tg3_flag_clear(tp, NVRAM);
14489 tg3_flag_clear(tp, NVRAM_BUFFERED);
14490 tg3_flag_set(tp, NO_NVRAM);
14491 return;
14492 }
14493
Linus Torvalds1da177e2005-04-16 15:20:36 -070014494 tw32_f(GRC_EEPROM_ADDR,
14495 (EEPROM_ADDR_FSM_RESET |
14496 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14497 EEPROM_ADDR_CLKPERD_SHIFT)));
14498
Michael Chan9d57f012006-12-07 00:23:25 -080014499 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014500
14501 /* Enable seeprom accesses. */
14502 tw32_f(GRC_LOCAL_CTRL,
14503 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14504 udelay(100);
14505
Joe Perches41535772013-02-16 11:20:04 +000014506 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14507 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014508 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014509
Michael Chanec41c7d2006-01-17 02:40:55 -080014510 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014511 netdev_warn(tp->dev,
14512 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014513 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014514 return;
14515 }
Michael Chane6af3012005-04-21 17:12:05 -070014516 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014517
Matt Carlson989a9d22007-05-05 11:51:05 -070014518 tp->nvram_size = 0;
14519
Joe Perches41535772013-02-16 11:20:04 +000014520 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014521 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014522 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014523 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014524 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14525 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14526 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014527 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014528 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014529 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014530 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014531 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014532 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014533 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014534 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014535 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14536 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014537 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014538 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14539 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014540 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014541 else
14542 tg3_get_nvram_info(tp);
14543
Matt Carlson989a9d22007-05-05 11:51:05 -070014544 if (tp->nvram_size == 0)
14545 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014546
Michael Chane6af3012005-04-21 17:12:05 -070014547 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014548 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014549
14550 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014551 tg3_flag_clear(tp, NVRAM);
14552 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553
14554 tg3_get_eeprom_size(tp);
14555 }
14556}
14557
Linus Torvalds1da177e2005-04-16 15:20:36 -070014558struct subsys_tbl_ent {
14559 u16 subsys_vendor, subsys_devid;
14560 u32 phy_id;
14561};
14562
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014563static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014565 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014566 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014567 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014568 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014569 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014570 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014571 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14572 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14573 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014574 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014575 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014576 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014577 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14578 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14579 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014580 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014581 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014582 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014583 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014584 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014585 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014586 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014587
14588 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014589 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014590 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014591 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014592 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014593 { TG3PCI_SUBVENDOR_ID_3COM,
14594 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14595 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014596 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014597 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014598 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014599
14600 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014601 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014602 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014603 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014604 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014605 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014606 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014607 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014608 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014609
14610 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014611 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014612 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014613 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014614 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014615 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14616 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14617 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014618 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014619 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014620 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621
14622 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014623 { TG3PCI_SUBVENDOR_ID_IBM,
14624 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014625};
14626
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014627static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628{
14629 int i;
14630
14631 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14632 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14633 tp->pdev->subsystem_vendor) &&
14634 (subsys_id_to_phy_id[i].subsys_devid ==
14635 tp->pdev->subsystem_device))
14636 return &subsys_id_to_phy_id[i];
14637 }
14638 return NULL;
14639}
14640
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014641static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014642{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014643 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014644
Matt Carlson79eb6902010-02-17 15:17:03 +000014645 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014646 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14647
Gary Zambranoa85feb82007-05-05 11:52:19 -070014648 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014649 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14650 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014651
Joe Perches41535772013-02-16 11:20:04 +000014652 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014653 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014654 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14655 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014656 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014657 val = tr32(VCPU_CFGSHDW);
14658 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014659 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014660 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014661 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014662 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014663 device_set_wakeup_enable(&tp->pdev->dev, true);
14664 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014665 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014666 }
14667
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14669 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14670 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014671 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014672 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014673
14674 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14675 tp->nic_sram_data_cfg = nic_cfg;
14676
14677 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14678 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014679 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14680 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14681 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014682 (ver > 0) && (ver < 0x100))
14683 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14684
Joe Perches41535772013-02-16 11:20:04 +000014685 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014686 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14687
Linus Torvalds1da177e2005-04-16 15:20:36 -070014688 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14689 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14690 eeprom_phy_serdes = 1;
14691
14692 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14693 if (nic_phy_id != 0) {
14694 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14695 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14696
14697 eeprom_phy_id = (id1 >> 16) << 10;
14698 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14699 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14700 } else
14701 eeprom_phy_id = 0;
14702
Michael Chan7d0c41e2005-04-21 17:06:20 -070014703 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014704 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014705 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014706 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014707 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014708 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014709 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014710
Joe Perches63c3a662011-04-26 08:12:10 +000014711 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014712 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14713 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014714 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014715 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14716
14717 switch (led_cfg) {
14718 default:
14719 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14720 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14721 break;
14722
14723 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14724 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14725 break;
14726
14727 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14728 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014729
14730 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14731 * read on some older 5700/5701 bootcode.
14732 */
Joe Perches41535772013-02-16 11:20:04 +000014733 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14734 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014735 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14736
Linus Torvalds1da177e2005-04-16 15:20:36 -070014737 break;
14738
14739 case SHASTA_EXT_LED_SHARED:
14740 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014741 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14742 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014743 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14744 LED_CTRL_MODE_PHY_2);
14745 break;
14746
14747 case SHASTA_EXT_LED_MAC:
14748 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14749 break;
14750
14751 case SHASTA_EXT_LED_COMBO:
14752 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014753 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014754 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14755 LED_CTRL_MODE_PHY_2);
14756 break;
14757
Stephen Hemminger855e1112008-04-16 16:37:28 -070014758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014759
Joe Perches41535772013-02-16 11:20:04 +000014760 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14761 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014762 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14763 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14764
Joe Perches41535772013-02-16 11:20:04 +000014765 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014766 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014767
Michael Chan9d26e212006-12-07 00:21:14 -080014768 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014769 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014770 if ((tp->pdev->subsystem_vendor ==
14771 PCI_VENDOR_ID_ARIMA) &&
14772 (tp->pdev->subsystem_device == 0x205a ||
14773 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014774 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014775 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014776 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14777 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014779
14780 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014781 tg3_flag_set(tp, ENABLE_ASF);
14782 if (tg3_flag(tp, 5750_PLUS))
14783 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014785
14786 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014787 tg3_flag(tp, 5750_PLUS))
14788 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014789
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014790 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014791 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014792 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014793
Joe Perches63c3a662011-04-26 08:12:10 +000014794 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014795 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014796 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014797 device_set_wakeup_enable(&tp->pdev->dev, true);
14798 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014799
Linus Torvalds1da177e2005-04-16 15:20:36 -070014800 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014801 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014802
14803 /* serdes signal pre-emphasis in register 0x590 set by */
14804 /* bootcode if bit 18 is set */
14805 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014806 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014807
Joe Perches63c3a662011-04-26 08:12:10 +000014808 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014809 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14810 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014811 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014812 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014813
Nithin Sujir942d1af2013-04-09 08:48:07 +000014814 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014815 u32 cfg3;
14816
14817 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014818 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
14819 !tg3_flag(tp, 57765_PLUS) &&
14820 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000014821 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014822 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
14823 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
14824 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
14825 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014826 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014827
Matt Carlson14417062010-02-17 15:16:59 +000014828 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014829 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014830 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014831 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014832 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014833 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014834 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014835done:
Joe Perches63c3a662011-04-26 08:12:10 +000014836 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014837 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014838 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014839 else
14840 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014841}
14842
Michael Chanc86a8562013-01-06 12:51:08 +000014843static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14844{
14845 int i, err;
14846 u32 val2, off = offset * 8;
14847
14848 err = tg3_nvram_lock(tp);
14849 if (err)
14850 return err;
14851
14852 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14853 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14854 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14855 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14856 udelay(10);
14857
14858 for (i = 0; i < 100; i++) {
14859 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14860 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14861 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14862 break;
14863 }
14864 udelay(10);
14865 }
14866
14867 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14868
14869 tg3_nvram_unlock(tp);
14870 if (val2 & APE_OTP_STATUS_CMD_DONE)
14871 return 0;
14872
14873 return -EBUSY;
14874}
14875
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014876static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014877{
14878 int i;
14879 u32 val;
14880
14881 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14882 tw32(OTP_CTRL, cmd);
14883
14884 /* Wait for up to 1 ms for command to execute. */
14885 for (i = 0; i < 100; i++) {
14886 val = tr32(OTP_STATUS);
14887 if (val & OTP_STATUS_CMD_DONE)
14888 break;
14889 udelay(10);
14890 }
14891
14892 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14893}
14894
14895/* Read the gphy configuration from the OTP region of the chip. The gphy
14896 * configuration is a 32-bit value that straddles the alignment boundary.
14897 * We do two 32-bit reads and then shift and merge the results.
14898 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014899static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014900{
14901 u32 bhalf_otp, thalf_otp;
14902
14903 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14904
14905 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14906 return 0;
14907
14908 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14909
14910 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14911 return 0;
14912
14913 thalf_otp = tr32(OTP_READ_DATA);
14914
14915 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14916
14917 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14918 return 0;
14919
14920 bhalf_otp = tr32(OTP_READ_DATA);
14921
14922 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14923}
14924
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014925static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014926{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014927 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014928
14929 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14930 adv |= ADVERTISED_1000baseT_Half |
14931 ADVERTISED_1000baseT_Full;
14932
14933 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14934 adv |= ADVERTISED_100baseT_Half |
14935 ADVERTISED_100baseT_Full |
14936 ADVERTISED_10baseT_Half |
14937 ADVERTISED_10baseT_Full |
14938 ADVERTISED_TP;
14939 else
14940 adv |= ADVERTISED_FIBRE;
14941
14942 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014943 tp->link_config.speed = SPEED_UNKNOWN;
14944 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014945 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014946 tp->link_config.active_speed = SPEED_UNKNOWN;
14947 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014948
14949 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014950}
14951
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014952static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014953{
14954 u32 hw_phy_id_1, hw_phy_id_2;
14955 u32 hw_phy_id, hw_phy_id_masked;
14956 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014957
Matt Carlsone256f8a2011-03-09 16:58:24 +000014958 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014959 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014960 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14961
Michael Chan8151ad52012-07-29 19:15:41 +000014962 if (tg3_flag(tp, ENABLE_APE)) {
14963 switch (tp->pci_fn) {
14964 case 0:
14965 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14966 break;
14967 case 1:
14968 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14969 break;
14970 case 2:
14971 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14972 break;
14973 case 3:
14974 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14975 break;
14976 }
14977 }
14978
Nithin Sujir942d1af2013-04-09 08:48:07 +000014979 if (!tg3_flag(tp, ENABLE_ASF) &&
14980 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
14981 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14982 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
14983 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
14984
Joe Perches63c3a662011-04-26 08:12:10 +000014985 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014986 return tg3_phy_init(tp);
14987
Linus Torvalds1da177e2005-04-16 15:20:36 -070014988 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014989 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990 */
14991 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014992 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014993 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014994 } else {
14995 /* Now read the physical PHY_ID from the chip and verify
14996 * that it is sane. If it doesn't look good, we fall back
14997 * to either the hard-coded table based PHY_ID and failing
14998 * that the value found in the eeprom area.
14999 */
15000 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15001 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15002
15003 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15004 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15005 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15006
Matt Carlson79eb6902010-02-17 15:17:03 +000015007 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015008 }
15009
Matt Carlson79eb6902010-02-17 15:17:03 +000015010 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015012 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015013 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015014 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015015 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015016 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015017 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015018 /* Do nothing, phy ID already set up in
15019 * tg3_get_eeprom_hw_cfg().
15020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015021 } else {
15022 struct subsys_tbl_ent *p;
15023
15024 /* No eeprom signature? Try the hardcoded
15025 * subsys device table.
15026 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015027 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015028 if (p) {
15029 tp->phy_id = p->phy_id;
15030 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15031 /* For now we saw the IDs 0xbc050cd0,
15032 * 0xbc050f80 and 0xbc050c30 on devices
15033 * connected to an BCM4785 and there are
15034 * probably more. Just assume that the phy is
15035 * supported when it is connected to a SSB core
15036 * for now.
15037 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015038 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015040
Linus Torvalds1da177e2005-04-16 15:20:36 -070015041 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015042 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015043 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015044 }
15045 }
15046
Matt Carlsona6b68da2010-12-06 08:28:52 +000015047 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015048 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15049 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015050 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015051 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15052 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15053 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15054 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015055 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015056 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15057
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015058 tp->eee.supported = SUPPORTED_100baseT_Full |
15059 SUPPORTED_1000baseT_Full;
15060 tp->eee.advertised = ADVERTISED_100baseT_Full |
15061 ADVERTISED_1000baseT_Full;
15062 tp->eee.eee_enabled = 1;
15063 tp->eee.tx_lpi_enabled = 1;
15064 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15065 }
15066
Matt Carlsone256f8a2011-03-09 16:58:24 +000015067 tg3_phy_init_link_config(tp);
15068
Nithin Sujir942d1af2013-04-09 08:48:07 +000015069 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15070 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015071 !tg3_flag(tp, ENABLE_APE) &&
15072 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015073 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015074
15075 tg3_readphy(tp, MII_BMSR, &bmsr);
15076 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15077 (bmsr & BMSR_LSTATUS))
15078 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015079
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080 err = tg3_phy_reset(tp);
15081 if (err)
15082 return err;
15083
Matt Carlson42b64a42011-05-19 12:12:49 +000015084 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015086 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015087 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15088 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015089
15090 tg3_writephy(tp, MII_BMCR,
15091 BMCR_ANENABLE | BMCR_ANRESTART);
15092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015093 }
15094
15095skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015096 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015097 err = tg3_init_5401phy_dsp(tp);
15098 if (err)
15099 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015100
Linus Torvalds1da177e2005-04-16 15:20:36 -070015101 err = tg3_init_5401phy_dsp(tp);
15102 }
15103
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104 return err;
15105}
15106
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015107static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015108{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015109 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015110 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015111 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015112 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015113
Matt Carlson535a4902011-07-20 10:20:56 +000015114 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015115 if (!vpd_data)
15116 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015117
Matt Carlson535a4902011-07-20 10:20:56 +000015118 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015119 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015120 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015121
15122 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15123 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15124 i += PCI_VPD_LRDT_TAG_SIZE;
15125
Matt Carlson535a4902011-07-20 10:20:56 +000015126 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015127 goto out_not_found;
15128
Matt Carlson184b8902010-04-05 10:19:25 +000015129 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15130 PCI_VPD_RO_KEYWORD_MFR_ID);
15131 if (j > 0) {
15132 len = pci_vpd_info_field_size(&vpd_data[j]);
15133
15134 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15135 if (j + len > block_end || len != 4 ||
15136 memcmp(&vpd_data[j], "1028", 4))
15137 goto partno;
15138
15139 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15140 PCI_VPD_RO_KEYWORD_VENDOR0);
15141 if (j < 0)
15142 goto partno;
15143
15144 len = pci_vpd_info_field_size(&vpd_data[j]);
15145
15146 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15147 if (j + len > block_end)
15148 goto partno;
15149
Kees Cook715230a2013-03-27 06:40:50 +000015150 if (len >= sizeof(tp->fw_ver))
15151 len = sizeof(tp->fw_ver) - 1;
15152 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15153 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15154 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015155 }
15156
15157partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015158 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15159 PCI_VPD_RO_KEYWORD_PARTNO);
15160 if (i < 0)
15161 goto out_not_found;
15162
15163 len = pci_vpd_info_field_size(&vpd_data[i]);
15164
15165 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15166 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015167 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015168 goto out_not_found;
15169
15170 memcpy(tp->board_part_number, &vpd_data[i], len);
15171
Linus Torvalds1da177e2005-04-16 15:20:36 -070015172out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015173 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015174 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015175 return;
15176
15177out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015178 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015179 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15180 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015181 strcpy(tp->board_part_number, "BCM5717");
15182 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15183 strcpy(tp->board_part_number, "BCM5718");
15184 else
15185 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015186 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015187 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15188 strcpy(tp->board_part_number, "BCM57780");
15189 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15190 strcpy(tp->board_part_number, "BCM57760");
15191 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15192 strcpy(tp->board_part_number, "BCM57790");
15193 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15194 strcpy(tp->board_part_number, "BCM57788");
15195 else
15196 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015197 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015198 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15199 strcpy(tp->board_part_number, "BCM57761");
15200 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15201 strcpy(tp->board_part_number, "BCM57765");
15202 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15203 strcpy(tp->board_part_number, "BCM57781");
15204 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15205 strcpy(tp->board_part_number, "BCM57785");
15206 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15207 strcpy(tp->board_part_number, "BCM57791");
15208 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15209 strcpy(tp->board_part_number, "BCM57795");
15210 else
15211 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015212 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015213 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15214 strcpy(tp->board_part_number, "BCM57762");
15215 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15216 strcpy(tp->board_part_number, "BCM57766");
15217 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15218 strcpy(tp->board_part_number, "BCM57782");
15219 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15220 strcpy(tp->board_part_number, "BCM57786");
15221 else
15222 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015223 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015224 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015225 } else {
15226nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015227 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015229}
15230
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015231static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015232{
15233 u32 val;
15234
Matt Carlsone4f34112009-02-25 14:25:00 +000015235 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015236 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015237 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015238 val != 0)
15239 return 0;
15240
15241 return 1;
15242}
15243
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015244static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015245{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015246 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015247 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015248 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015249
15250 if (tg3_nvram_read(tp, 0xc, &offset) ||
15251 tg3_nvram_read(tp, 0x4, &start))
15252 return;
15253
15254 offset = tg3_nvram_logical_addr(tp, offset);
15255
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015256 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015257 return;
15258
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015259 if ((val & 0xfc000000) == 0x0c000000) {
15260 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015261 return;
15262
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015263 if (val == 0)
15264 newver = true;
15265 }
15266
Matt Carlson75f99362010-04-05 10:19:24 +000015267 dst_off = strlen(tp->fw_ver);
15268
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015269 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015270 if (TG3_VER_SIZE - dst_off < 16 ||
15271 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015272 return;
15273
15274 offset = offset + ver_offset - start;
15275 for (i = 0; i < 16; i += 4) {
15276 __be32 v;
15277 if (tg3_nvram_read_be32(tp, offset + i, &v))
15278 return;
15279
Matt Carlson75f99362010-04-05 10:19:24 +000015280 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015281 }
15282 } else {
15283 u32 major, minor;
15284
15285 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15286 return;
15287
15288 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15289 TG3_NVM_BCVER_MAJSFT;
15290 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015291 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15292 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015293 }
15294}
15295
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015296static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015297{
15298 u32 val, major, minor;
15299
15300 /* Use native endian representation */
15301 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15302 return;
15303
15304 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15305 TG3_NVM_HWSB_CFG1_MAJSFT;
15306 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15307 TG3_NVM_HWSB_CFG1_MINSFT;
15308
15309 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15310}
15311
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015312static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015313{
15314 u32 offset, major, minor, build;
15315
Matt Carlson75f99362010-04-05 10:19:24 +000015316 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015317
15318 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15319 return;
15320
15321 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15322 case TG3_EEPROM_SB_REVISION_0:
15323 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15324 break;
15325 case TG3_EEPROM_SB_REVISION_2:
15326 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15327 break;
15328 case TG3_EEPROM_SB_REVISION_3:
15329 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15330 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015331 case TG3_EEPROM_SB_REVISION_4:
15332 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15333 break;
15334 case TG3_EEPROM_SB_REVISION_5:
15335 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15336 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015337 case TG3_EEPROM_SB_REVISION_6:
15338 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15339 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015340 default:
15341 return;
15342 }
15343
Matt Carlsone4f34112009-02-25 14:25:00 +000015344 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015345 return;
15346
15347 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15348 TG3_EEPROM_SB_EDH_BLD_SHFT;
15349 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15350 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15351 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15352
15353 if (minor > 99 || build > 26)
15354 return;
15355
Matt Carlson75f99362010-04-05 10:19:24 +000015356 offset = strlen(tp->fw_ver);
15357 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15358 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015359
15360 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015361 offset = strlen(tp->fw_ver);
15362 if (offset < TG3_VER_SIZE - 1)
15363 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015364 }
15365}
15366
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015367static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015368{
15369 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015370 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015371
15372 for (offset = TG3_NVM_DIR_START;
15373 offset < TG3_NVM_DIR_END;
15374 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015375 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015376 return;
15377
15378 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15379 break;
15380 }
15381
15382 if (offset == TG3_NVM_DIR_END)
15383 return;
15384
Joe Perches63c3a662011-04-26 08:12:10 +000015385 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015386 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015387 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015388 return;
15389
Matt Carlsone4f34112009-02-25 14:25:00 +000015390 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015391 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015392 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015393 return;
15394
15395 offset += val - start;
15396
Matt Carlsonacd9c112009-02-25 14:26:33 +000015397 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015398
Matt Carlsonacd9c112009-02-25 14:26:33 +000015399 tp->fw_ver[vlen++] = ',';
15400 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015401
15402 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015403 __be32 v;
15404 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015405 return;
15406
Al Virob9fc7dc2007-12-17 22:59:57 -080015407 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015408
Matt Carlsonacd9c112009-02-25 14:26:33 +000015409 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15410 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015411 break;
15412 }
15413
Matt Carlsonacd9c112009-02-25 14:26:33 +000015414 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15415 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015416 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015417}
15418
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015419static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015420{
Matt Carlson7fd76442009-02-25 14:27:20 +000015421 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015422
15423 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15424 if (apedata != APE_SEG_SIG_MAGIC)
15425 return;
15426
15427 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15428 if (!(apedata & APE_FW_STATUS_READY))
15429 return;
15430
Michael Chan165f4d12012-07-16 16:23:59 +000015431 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15432 tg3_flag_set(tp, APE_HAS_NCSI);
15433}
15434
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015435static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015436{
15437 int vlen;
15438 u32 apedata;
15439 char *fwtype;
15440
Matt Carlson7fd76442009-02-25 14:27:20 +000015441 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15442
Michael Chan165f4d12012-07-16 16:23:59 +000015443 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015444 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015445 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15446 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015447 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015448 fwtype = "DASH";
15449
Matt Carlson7fd76442009-02-25 14:27:20 +000015450 vlen = strlen(tp->fw_ver);
15451
Matt Carlsonecc79642010-08-02 11:26:01 +000015452 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15453 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015454 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15455 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15456 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15457 (apedata & APE_FW_VERSION_BLDMSK));
15458}
15459
Michael Chanc86a8562013-01-06 12:51:08 +000015460static void tg3_read_otp_ver(struct tg3 *tp)
15461{
15462 u32 val, val2;
15463
Joe Perches41535772013-02-16 11:20:04 +000015464 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015465 return;
15466
15467 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15468 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15469 TG3_OTP_MAGIC0_VALID(val)) {
15470 u64 val64 = (u64) val << 32 | val2;
15471 u32 ver = 0;
15472 int i, vlen;
15473
15474 for (i = 0; i < 7; i++) {
15475 if ((val64 & 0xff) == 0)
15476 break;
15477 ver = val64 & 0xff;
15478 val64 >>= 8;
15479 }
15480 vlen = strlen(tp->fw_ver);
15481 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15482 }
15483}
15484
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015485static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015486{
15487 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015488 bool vpd_vers = false;
15489
15490 if (tp->fw_ver[0] != 0)
15491 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015492
Joe Perches63c3a662011-04-26 08:12:10 +000015493 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015494 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015495 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015496 return;
15497 }
15498
Matt Carlsonacd9c112009-02-25 14:26:33 +000015499 if (tg3_nvram_read(tp, 0, &val))
15500 return;
15501
15502 if (val == TG3_EEPROM_MAGIC)
15503 tg3_read_bc_ver(tp);
15504 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15505 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015506 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15507 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015508
Michael Chan165f4d12012-07-16 16:23:59 +000015509 if (tg3_flag(tp, ENABLE_ASF)) {
15510 if (tg3_flag(tp, ENABLE_APE)) {
15511 tg3_probe_ncsi(tp);
15512 if (!vpd_vers)
15513 tg3_read_dash_ver(tp);
15514 } else if (!vpd_vers) {
15515 tg3_read_mgmtfw_ver(tp);
15516 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015517 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015518
15519 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015520}
15521
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015522static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15523{
Joe Perches63c3a662011-04-26 08:12:10 +000015524 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015525 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015526 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015527 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015528 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015529 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015530}
15531
Matt Carlson41434702011-03-09 16:58:22 +000015532static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015533 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15534 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15535 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15536 { },
15537};
15538
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015539static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015540{
15541 struct pci_dev *peer;
15542 unsigned int func, devnr = tp->pdev->devfn & ~7;
15543
15544 for (func = 0; func < 8; func++) {
15545 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15546 if (peer && peer != tp->pdev)
15547 break;
15548 pci_dev_put(peer);
15549 }
15550 /* 5704 can be configured in single-port mode, set peer to
15551 * tp->pdev in that case.
15552 */
15553 if (!peer) {
15554 peer = tp->pdev;
15555 return peer;
15556 }
15557
15558 /*
15559 * We don't need to keep the refcount elevated; there's no way
15560 * to remove one half of this device without removing the other
15561 */
15562 pci_dev_put(peer);
15563
15564 return peer;
15565}
15566
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015567static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015568{
15569 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015570 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015571 u32 reg;
15572
15573 /* All devices that use the alternate
15574 * ASIC REV location have a CPMU.
15575 */
15576 tg3_flag_set(tp, CPMU_PRESENT);
15577
15578 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015579 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015580 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15581 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015582 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15583 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15584 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15585 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015586 reg = TG3PCI_GEN2_PRODID_ASICREV;
15587 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15588 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15589 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15590 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15591 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15592 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15593 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15594 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15595 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15596 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15597 reg = TG3PCI_GEN15_PRODID_ASICREV;
15598 else
15599 reg = TG3PCI_PRODID_ASICREV;
15600
15601 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15602 }
15603
15604 /* Wrong chip ID in 5752 A0. This code can be removed later
15605 * as A0 is not in production.
15606 */
Joe Perches41535772013-02-16 11:20:04 +000015607 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015608 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15609
Joe Perches41535772013-02-16 11:20:04 +000015610 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015611 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15612
Joe Perches41535772013-02-16 11:20:04 +000015613 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15614 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15615 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015616 tg3_flag_set(tp, 5717_PLUS);
15617
Joe Perches41535772013-02-16 11:20:04 +000015618 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15619 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015620 tg3_flag_set(tp, 57765_CLASS);
15621
Michael Chanc65a17f2013-01-06 12:51:07 +000015622 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015623 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015624 tg3_flag_set(tp, 57765_PLUS);
15625
15626 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015627 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15628 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15629 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15630 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15631 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15632 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015633 tg3_flag(tp, 57765_PLUS))
15634 tg3_flag_set(tp, 5755_PLUS);
15635
Joe Perches41535772013-02-16 11:20:04 +000015636 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15637 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015638 tg3_flag_set(tp, 5780_CLASS);
15639
Joe Perches41535772013-02-16 11:20:04 +000015640 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15641 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15642 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015643 tg3_flag(tp, 5755_PLUS) ||
15644 tg3_flag(tp, 5780_CLASS))
15645 tg3_flag_set(tp, 5750_PLUS);
15646
Joe Perches41535772013-02-16 11:20:04 +000015647 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015648 tg3_flag(tp, 5750_PLUS))
15649 tg3_flag_set(tp, 5705_PLUS);
15650}
15651
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015652static bool tg3_10_100_only_device(struct tg3 *tp,
15653 const struct pci_device_id *ent)
15654{
15655 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15656
Joe Perches41535772013-02-16 11:20:04 +000015657 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15658 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015659 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15660 return true;
15661
15662 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015663 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015664 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15665 return true;
15666 } else {
15667 return true;
15668 }
15669 }
15670
15671 return false;
15672}
15673
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015674static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015675{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015676 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015677 u32 pci_state_reg, grc_misc_cfg;
15678 u32 val;
15679 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015680 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015681
Linus Torvalds1da177e2005-04-16 15:20:36 -070015682 /* Force memory write invalidate off. If we leave it on,
15683 * then on 5700_BX chips we have to enable a workaround.
15684 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15685 * to match the cacheline size. The Broadcom driver have this
15686 * workaround but turns MWI off all the times so never uses
15687 * it. This seems to suggest that the workaround is insufficient.
15688 */
15689 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15690 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15691 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15692
Matt Carlson16821282011-07-13 09:27:28 +000015693 /* Important! -- Make sure register accesses are byteswapped
15694 * correctly. Also, for those chips that require it, make
15695 * sure that indirect register accesses are enabled before
15696 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015697 */
15698 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15699 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015700 tp->misc_host_ctrl |= (misc_ctrl_reg &
15701 MISC_HOST_CTRL_CHIPREV);
15702 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15703 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015704
Matt Carlson42b123b2012-02-13 15:20:13 +000015705 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015706
Michael Chan68929142005-08-09 20:17:14 -070015707 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15708 * we need to disable memory and use config. cycles
15709 * only to access all registers. The 5702/03 chips
15710 * can mistakenly decode the special cycles from the
15711 * ICH chipsets as memory write cycles, causing corruption
15712 * of register and memory space. Only certain ICH bridges
15713 * will drive special cycles with non-zero data during the
15714 * address phase which can fall within the 5703's address
15715 * range. This is not an ICH bug as the PCI spec allows
15716 * non-zero address during special cycles. However, only
15717 * these ICH bridges are known to drive non-zero addresses
15718 * during special cycles.
15719 *
15720 * Since special cycles do not cross PCI bridges, we only
15721 * enable this workaround if the 5703 is on the secondary
15722 * bus of these ICH bridges.
15723 */
Joe Perches41535772013-02-16 11:20:04 +000015724 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15725 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015726 static struct tg3_dev_id {
15727 u32 vendor;
15728 u32 device;
15729 u32 rev;
15730 } ich_chipsets[] = {
15731 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15732 PCI_ANY_ID },
15733 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15734 PCI_ANY_ID },
15735 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15736 0xa },
15737 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15738 PCI_ANY_ID },
15739 { },
15740 };
15741 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15742 struct pci_dev *bridge = NULL;
15743
15744 while (pci_id->vendor != 0) {
15745 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15746 bridge);
15747 if (!bridge) {
15748 pci_id++;
15749 continue;
15750 }
15751 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015752 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015753 continue;
15754 }
15755 if (bridge->subordinate &&
15756 (bridge->subordinate->number ==
15757 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015758 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015759 pci_dev_put(bridge);
15760 break;
15761 }
15762 }
15763 }
15764
Joe Perches41535772013-02-16 11:20:04 +000015765 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015766 static struct tg3_dev_id {
15767 u32 vendor;
15768 u32 device;
15769 } bridge_chipsets[] = {
15770 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15771 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15772 { },
15773 };
15774 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15775 struct pci_dev *bridge = NULL;
15776
15777 while (pci_id->vendor != 0) {
15778 bridge = pci_get_device(pci_id->vendor,
15779 pci_id->device,
15780 bridge);
15781 if (!bridge) {
15782 pci_id++;
15783 continue;
15784 }
15785 if (bridge->subordinate &&
15786 (bridge->subordinate->number <=
15787 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015788 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015789 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015790 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015791 pci_dev_put(bridge);
15792 break;
15793 }
15794 }
15795 }
15796
Michael Chan4a29cc22006-03-19 13:21:12 -080015797 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15798 * DMA addresses > 40-bit. This bridge may have other additional
15799 * 57xx devices behind it in some 4-port NIC designs for example.
15800 * Any tg3 device found behind the bridge will also need the 40-bit
15801 * DMA workaround.
15802 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015803 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015804 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015805 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015806 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015807 struct pci_dev *bridge = NULL;
15808
15809 do {
15810 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15811 PCI_DEVICE_ID_SERVERWORKS_EPB,
15812 bridge);
15813 if (bridge && bridge->subordinate &&
15814 (bridge->subordinate->number <=
15815 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015816 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015817 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015818 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015819 pci_dev_put(bridge);
15820 break;
15821 }
15822 } while (bridge);
15823 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015824
Joe Perches41535772013-02-16 11:20:04 +000015825 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15826 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015827 tp->pdev_peer = tg3_find_peer(tp);
15828
Matt Carlson507399f2009-11-13 13:03:37 +000015829 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015830 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015831 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015832 else if (tg3_flag(tp, 57765_PLUS))
15833 tg3_flag_set(tp, HW_TSO_3);
15834 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015835 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015836 tg3_flag_set(tp, HW_TSO_2);
15837 else if (tg3_flag(tp, 5750_PLUS)) {
15838 tg3_flag_set(tp, HW_TSO_1);
15839 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015840 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15841 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015842 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015843 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15844 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15845 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000015846 tg3_flag_set(tp, FW_TSO);
15847 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015848 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015849 tp->fw_needed = FIRMWARE_TG3TSO5;
15850 else
15851 tp->fw_needed = FIRMWARE_TG3TSO;
15852 }
15853
Matt Carlsondabc5c62011-05-19 12:12:52 +000015854 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015855 if (tg3_flag(tp, HW_TSO_1) ||
15856 tg3_flag(tp, HW_TSO_2) ||
15857 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000015858 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015859 /* For firmware TSO, assume ASF is disabled.
15860 * We'll disable TSO later if we discover ASF
15861 * is enabled in tg3_get_eeprom_hw_cfg().
15862 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015863 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015864 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015865 tg3_flag_clear(tp, TSO_CAPABLE);
15866 tg3_flag_clear(tp, TSO_BUG);
15867 tp->fw_needed = NULL;
15868 }
15869
Joe Perches41535772013-02-16 11:20:04 +000015870 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015871 tp->fw_needed = FIRMWARE_TG3;
15872
Nithin Sujirc4dab502013-03-06 17:02:34 +000015873 if (tg3_asic_rev(tp) == ASIC_REV_57766)
15874 tp->fw_needed = FIRMWARE_TG357766;
15875
Matt Carlson507399f2009-11-13 13:03:37 +000015876 tp->irq_max = 1;
15877
Joe Perches63c3a662011-04-26 08:12:10 +000015878 if (tg3_flag(tp, 5750_PLUS)) {
15879 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015880 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15881 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15882 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15883 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015884 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015885 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015886
Joe Perches63c3a662011-04-26 08:12:10 +000015887 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015888 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015889 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015891
Joe Perches63c3a662011-04-26 08:12:10 +000015892 if (tg3_flag(tp, 57765_PLUS)) {
15893 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015894 tp->irq_max = TG3_IRQ_MAX_VECS;
15895 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015896 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015897
Michael Chan91024262012-09-28 07:12:38 +000015898 tp->txq_max = 1;
15899 tp->rxq_max = 1;
15900 if (tp->irq_max > 1) {
15901 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15902 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15903
Joe Perches41535772013-02-16 11:20:04 +000015904 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15905 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000015906 tp->txq_max = tp->irq_max - 1;
15907 }
15908
Matt Carlsonb7abee62012-06-07 12:56:54 +000015909 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015910 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015911 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015912
Joe Perches41535772013-02-16 11:20:04 +000015913 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015914 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015915
Joe Perches41535772013-02-16 11:20:04 +000015916 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15917 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15918 tg3_asic_rev(tp) == ASIC_REV_5720 ||
15919 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015920 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015921
Joe Perches63c3a662011-04-26 08:12:10 +000015922 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000015923 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015924 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015925
Joe Perches63c3a662011-04-26 08:12:10 +000015926 if (!tg3_flag(tp, 5705_PLUS) ||
15927 tg3_flag(tp, 5780_CLASS) ||
15928 tg3_flag(tp, USE_JUMBO_BDFLAG))
15929 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015930
Matt Carlson52f44902008-11-21 17:17:04 -080015931 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15932 &pci_state_reg);
15933
Jon Mason708ebb3a2011-06-27 12:56:50 +000015934 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015935 u16 lnkctl;
15936
Joe Perches63c3a662011-04-26 08:12:10 +000015937 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015938
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015939 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015940 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000015941 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015942 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015943 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015944 }
Joe Perches41535772013-02-16 11:20:04 +000015945 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
15946 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15947 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
15948 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015949 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015950 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015951 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015952 }
Joe Perches41535772013-02-16 11:20:04 +000015953 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015954 /* BCM5785 devices are effectively PCIe devices, and should
15955 * follow PCIe codepaths, but do not have a PCIe capabilities
15956 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015957 */
Joe Perches63c3a662011-04-26 08:12:10 +000015958 tg3_flag_set(tp, PCI_EXPRESS);
15959 } else if (!tg3_flag(tp, 5705_PLUS) ||
15960 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015961 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15962 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015963 dev_err(&tp->pdev->dev,
15964 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015965 return -EIO;
15966 }
15967
15968 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015969 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015971
Michael Chan399de502005-10-03 14:02:39 -070015972 /* If we have an AMD 762 or VIA K8T800 chipset, write
15973 * reordering to the mailbox registers done by the host
15974 * controller can cause major troubles. We read back from
15975 * every mailbox register write to force the writes to be
15976 * posted to the chip in order.
15977 */
Matt Carlson41434702011-03-09 16:58:22 +000015978 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015979 !tg3_flag(tp, PCI_EXPRESS))
15980 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015981
Matt Carlson69fc4052008-12-21 20:19:57 -080015982 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15983 &tp->pci_cacheline_sz);
15984 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15985 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000015986 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015987 tp->pci_lat_timer < 64) {
15988 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015989 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15990 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015991 }
15992
Matt Carlson16821282011-07-13 09:27:28 +000015993 /* Important! -- It is critical that the PCI-X hw workaround
15994 * situation is decided before the first MMIO register access.
15995 */
Joe Perches41535772013-02-16 11:20:04 +000015996 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080015997 /* 5700 BX chips need to have their TX producer index
15998 * mailboxes written twice to workaround a bug.
15999 */
Joe Perches63c3a662011-04-26 08:12:10 +000016000 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016001
Matt Carlson52f44902008-11-21 17:17:04 -080016002 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016003 *
16004 * The workaround is to use indirect register accesses
16005 * for all chip writes not to mailbox registers.
16006 */
Joe Perches63c3a662011-04-26 08:12:10 +000016007 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016008 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016009
Joe Perches63c3a662011-04-26 08:12:10 +000016010 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016011
16012 /* The chip can have it's power management PCI config
16013 * space registers clobbered due to this bug.
16014 * So explicitly force the chip into D0 here.
16015 */
Matt Carlson9974a352007-10-07 23:27:28 -070016016 pci_read_config_dword(tp->pdev,
16017 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016018 &pm_reg);
16019 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16020 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016021 pci_write_config_dword(tp->pdev,
16022 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016023 pm_reg);
16024
16025 /* Also, force SERR#/PERR# in PCI command. */
16026 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16027 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16028 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16029 }
16030 }
16031
Linus Torvalds1da177e2005-04-16 15:20:36 -070016032 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016033 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016034 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016035 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016036
16037 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016038 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016039 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16040 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16041 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16042 }
16043
Michael Chan1ee582d2005-08-09 20:16:46 -070016044 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016045 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016046 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016047 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016048 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016049 tp->write32_tx_mbox = tg3_write32;
16050 tp->write32_rx_mbox = tg3_write32;
16051
16052 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016053 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016054 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016055 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016056 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016057 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016058 /*
16059 * Back to back register writes can cause problems on these
16060 * chips, the workaround is to read back all reg writes
16061 * except those to mailbox regs.
16062 *
16063 * See tg3_write_indirect_reg32().
16064 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016065 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016066 }
16067
Joe Perches63c3a662011-04-26 08:12:10 +000016068 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016069 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016070 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016071 tp->write32_rx_mbox = tg3_write_flush_reg32;
16072 }
Michael Chan20094932005-08-09 20:16:32 -070016073
Joe Perches63c3a662011-04-26 08:12:10 +000016074 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016075 tp->read32 = tg3_read_indirect_reg32;
16076 tp->write32 = tg3_write_indirect_reg32;
16077 tp->read32_mbox = tg3_read_indirect_mbox;
16078 tp->write32_mbox = tg3_write_indirect_mbox;
16079 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16080 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16081
16082 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016083 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016084
16085 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16086 pci_cmd &= ~PCI_COMMAND_MEMORY;
16087 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16088 }
Joe Perches41535772013-02-16 11:20:04 +000016089 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016090 tp->read32_mbox = tg3_read32_mbox_5906;
16091 tp->write32_mbox = tg3_write32_mbox_5906;
16092 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16093 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16094 }
Michael Chan68929142005-08-09 20:17:14 -070016095
Michael Chanbbadf502006-04-06 21:46:34 -070016096 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016097 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016098 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16099 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016100 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016101
Matt Carlson16821282011-07-13 09:27:28 +000016102 /* The memory arbiter has to be enabled in order for SRAM accesses
16103 * to succeed. Normally on powerup the tg3 chip firmware will make
16104 * sure it is enabled, but other entities such as system netboot
16105 * code might disable it.
16106 */
16107 val = tr32(MEMARB_MODE);
16108 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16109
Matt Carlson9dc5e342011-11-04 09:15:02 +000016110 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016111 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016112 tg3_flag(tp, 5780_CLASS)) {
16113 if (tg3_flag(tp, PCIX_MODE)) {
16114 pci_read_config_dword(tp->pdev,
16115 tp->pcix_cap + PCI_X_STATUS,
16116 &val);
16117 tp->pci_fn = val & 0x7;
16118 }
Joe Perches41535772013-02-16 11:20:04 +000016119 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16120 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16121 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016122 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016123 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16124 val = tr32(TG3_CPMU_STATUS);
16125
Joe Perches41535772013-02-16 11:20:04 +000016126 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016127 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16128 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016129 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16130 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016131 }
16132
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016133 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16134 tp->write32_tx_mbox = tg3_write_flush_reg32;
16135 tp->write32_rx_mbox = tg3_write_flush_reg32;
16136 }
16137
Michael Chan7d0c41e2005-04-21 17:06:20 -070016138 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016139 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016140 * determined before calling tg3_set_power_state() so that
16141 * we know whether or not to switch out of Vaux power.
16142 * When the flag is set, it means that GPIO1 is used for eeprom
16143 * write protect and also implies that it is a LOM where GPIOs
16144 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016145 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016146 tg3_get_eeprom_hw_cfg(tp);
16147
Matt Carlson1caf13e2013-03-06 17:02:29 +000016148 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016149 tg3_flag_clear(tp, TSO_CAPABLE);
16150 tg3_flag_clear(tp, TSO_BUG);
16151 tp->fw_needed = NULL;
16152 }
16153
Joe Perches63c3a662011-04-26 08:12:10 +000016154 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016155 /* Allow reads and writes to the
16156 * APE register and memory space.
16157 */
16158 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016159 PCISTATE_ALLOW_APE_SHMEM_WR |
16160 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016161 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16162 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016163
16164 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016165 }
16166
Matt Carlson16821282011-07-13 09:27:28 +000016167 /* Set up tp->grc_local_ctrl before calling
16168 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16169 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016170 * It is also used as eeprom write protect on LOMs.
16171 */
16172 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016173 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016174 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016175 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16176 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016177 /* Unused GPIO3 must be driven as output on 5752 because there
16178 * are no pull-up resistors on unused GPIO pins.
16179 */
Joe Perches41535772013-02-16 11:20:04 +000016180 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016181 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016182
Joe Perches41535772013-02-16 11:20:04 +000016183 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16184 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016185 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016186 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16187
Matt Carlson8d519ab2009-04-20 06:58:01 +000016188 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16189 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016190 /* Turn off the debug UART. */
16191 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016192 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016193 /* Keep VMain power. */
16194 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16195 GRC_LCLCTRL_GPIO_OUTPUT0;
16196 }
16197
Joe Perches41535772013-02-16 11:20:04 +000016198 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016199 tp->grc_local_ctrl |=
16200 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16201
Matt Carlson16821282011-07-13 09:27:28 +000016202 /* Switch out of Vaux if it is a NIC */
16203 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016204
Linus Torvalds1da177e2005-04-16 15:20:36 -070016205 /* Derive initial jumbo mode from MTU assigned in
16206 * ether_setup() via the alloc_etherdev() call
16207 */
Joe Perches63c3a662011-04-26 08:12:10 +000016208 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16209 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016210
16211 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016212 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16213 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16214 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16215 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016216 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016217 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016218 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016219 }
16220
Joe Perches41535772013-02-16 11:20:04 +000016221 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016222 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016223
Linus Torvalds1da177e2005-04-16 15:20:36 -070016224 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016225 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16226 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16227 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16228 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016229 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16230 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16231 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016232
Joe Perches41535772013-02-16 11:20:04 +000016233 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16234 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016235 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016236 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016237 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016238
Joe Perches63c3a662011-04-26 08:12:10 +000016239 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016240 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016241 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16242 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016243 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016244 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16245 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16246 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16247 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016248 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16249 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016250 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016251 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016252 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016253 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016254 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016256
Joe Perches41535772013-02-16 11:20:04 +000016257 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16258 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016259 tp->phy_otp = tg3_read_otp_phycfg(tp);
16260 if (tp->phy_otp == 0)
16261 tp->phy_otp = TG3_OTP_DEFAULT;
16262 }
16263
Joe Perches63c3a662011-04-26 08:12:10 +000016264 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016265 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16266 else
16267 tp->mi_mode = MAC_MI_MODE_BASE;
16268
Linus Torvalds1da177e2005-04-16 15:20:36 -070016269 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016270 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16271 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016272 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16273
Matt Carlson4d958472011-04-20 07:57:35 +000016274 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016275 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16276 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16277 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016278 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16279 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16280 }
16281
Joe Perches41535772013-02-16 11:20:04 +000016282 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16283 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016284 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016285
Matt Carlson158d7ab2008-05-29 01:37:54 -070016286 err = tg3_mdio_init(tp);
16287 if (err)
16288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016289
16290 /* Initialize data/descriptor byte/word swapping. */
16291 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016292 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16293 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016294 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16295 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16296 GRC_MODE_B2HRX_ENABLE |
16297 GRC_MODE_HTX2B_ENABLE |
16298 GRC_MODE_HOST_STACKUP);
16299 else
16300 val &= GRC_MODE_HOST_STACKUP;
16301
Linus Torvalds1da177e2005-04-16 15:20:36 -070016302 tw32(GRC_MODE, val | tp->grc_mode);
16303
16304 tg3_switch_clocks(tp);
16305
16306 /* Clear this out for sanity. */
16307 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16308
16309 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16310 &pci_state_reg);
16311 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016312 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016313 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16314 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16315 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16316 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016317 void __iomem *sram_base;
16318
16319 /* Write some dummy words into the SRAM status block
16320 * area, see if it reads back correctly. If the return
16321 * value is bad, force enable the PCIX workaround.
16322 */
16323 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16324
16325 writel(0x00000000, sram_base);
16326 writel(0x00000000, sram_base + 4);
16327 writel(0xffffffff, sram_base + 4);
16328 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016329 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016330 }
16331 }
16332
16333 udelay(50);
16334 tg3_nvram_init(tp);
16335
Nithin Sujirc4dab502013-03-06 17:02:34 +000016336 /* If the device has an NVRAM, no need to load patch firmware */
16337 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16338 !tg3_flag(tp, NO_NVRAM))
16339 tp->fw_needed = NULL;
16340
Linus Torvalds1da177e2005-04-16 15:20:36 -070016341 grc_misc_cfg = tr32(GRC_MISC_CFG);
16342 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16343
Joe Perches41535772013-02-16 11:20:04 +000016344 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016345 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16346 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016347 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016348
Joe Perches63c3a662011-04-26 08:12:10 +000016349 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016350 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016351 tg3_flag_set(tp, TAGGED_STATUS);
16352 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016353 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16354 HOSTCC_MODE_CLRTICK_TXBD);
16355
16356 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16357 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16358 tp->misc_host_ctrl);
16359 }
16360
Matt Carlson3bda1252008-08-15 14:08:22 -070016361 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016362 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016363 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016364 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016365 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016366
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016367 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016368 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016369
16370 err = tg3_phy_probe(tp);
16371 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016372 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016373 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016374 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016375 }
16376
Matt Carlson184b8902010-04-05 10:19:25 +000016377 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016378 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016379
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016380 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16381 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016382 } else {
Joe Perches41535772013-02-16 11:20:04 +000016383 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016384 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016385 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016386 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016387 }
16388
16389 /* 5700 {AX,BX} chips have a broken status block link
16390 * change bit implementation, so we must use the
16391 * status register in those cases.
16392 */
Joe Perches41535772013-02-16 11:20:04 +000016393 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016394 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016395 else
Joe Perches63c3a662011-04-26 08:12:10 +000016396 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016397
16398 /* The led_ctrl is set during tg3_phy_probe, here we might
16399 * have to force the link status polling mechanism based
16400 * upon subsystem IDs.
16401 */
16402 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016403 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016404 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16405 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016406 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016407 }
16408
16409 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016410 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016411 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016412 else
Joe Perches63c3a662011-04-26 08:12:10 +000016413 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016414
Eric Dumazet9205fd92011-11-18 06:47:01 +000016415 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016416 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016417 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016418 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016419 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016420#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016421 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016422#endif
16423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016424
Matt Carlson2c49a442010-09-30 10:34:35 +000016425 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16426 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016427 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16428
Matt Carlson2c49a442010-09-30 10:34:35 +000016429 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016430
16431 /* Increment the rx prod index on the rx std ring by at most
16432 * 8 for these chips to workaround hw errata.
16433 */
Joe Perches41535772013-02-16 11:20:04 +000016434 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16435 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16436 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016437 tp->rx_std_max_post = 8;
16438
Joe Perches63c3a662011-04-26 08:12:10 +000016439 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016440 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16441 PCIE_PWR_MGMT_L1_THRESH_MSK;
16442
Linus Torvalds1da177e2005-04-16 15:20:36 -070016443 return err;
16444}
16445
David S. Miller49b6e95f2007-03-29 01:38:42 -070016446#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016447static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016448{
16449 struct net_device *dev = tp->dev;
16450 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016451 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016452 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016453 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016454
David S. Miller49b6e95f2007-03-29 01:38:42 -070016455 addr = of_get_property(dp, "local-mac-address", &len);
16456 if (addr && len == 6) {
16457 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016459 }
16460 return -ENODEV;
16461}
16462
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016463static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016464{
16465 struct net_device *dev = tp->dev;
16466
16467 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
16468 return 0;
16469}
16470#endif
16471
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016472static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016473{
16474 struct net_device *dev = tp->dev;
16475 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016476 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016477 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016478
David S. Miller49b6e95f2007-03-29 01:38:42 -070016479#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016480 if (!tg3_get_macaddr_sparc(tp))
16481 return 0;
16482#endif
16483
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016484 if (tg3_flag(tp, IS_SSB_CORE)) {
16485 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16486 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16487 return 0;
16488 }
16489
Linus Torvalds1da177e2005-04-16 15:20:36 -070016490 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016491 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016492 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016493 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16494 mac_offset = 0xcc;
16495 if (tg3_nvram_lock(tp))
16496 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16497 else
16498 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016499 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016500 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016501 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016502 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016503 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016504 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016505 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016506
16507 /* First try to get it from MAC address mailbox. */
16508 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16509 if ((hi >> 16) == 0x484b) {
16510 dev->dev_addr[0] = (hi >> 8) & 0xff;
16511 dev->dev_addr[1] = (hi >> 0) & 0xff;
16512
16513 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16514 dev->dev_addr[2] = (lo >> 24) & 0xff;
16515 dev->dev_addr[3] = (lo >> 16) & 0xff;
16516 dev->dev_addr[4] = (lo >> 8) & 0xff;
16517 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016518
Michael Chan008652b2006-03-27 23:14:53 -080016519 /* Some old bootcode may report a 0 MAC address in SRAM */
16520 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16521 }
16522 if (!addr_ok) {
16523 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016524 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016525 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016526 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016527 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16528 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016529 }
16530 /* Finally just fetch it out of the MAC control regs. */
16531 else {
16532 hi = tr32(MAC_ADDR_0_HIGH);
16533 lo = tr32(MAC_ADDR_0_LOW);
16534
16535 dev->dev_addr[5] = lo & 0xff;
16536 dev->dev_addr[4] = (lo >> 8) & 0xff;
16537 dev->dev_addr[3] = (lo >> 16) & 0xff;
16538 dev->dev_addr[2] = (lo >> 24) & 0xff;
16539 dev->dev_addr[1] = hi & 0xff;
16540 dev->dev_addr[0] = (hi >> 8) & 0xff;
16541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016542 }
16543
16544 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016545#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016546 if (!tg3_get_default_macaddr_sparc(tp))
16547 return 0;
16548#endif
16549 return -EINVAL;
16550 }
16551 return 0;
16552}
16553
David S. Miller59e6b432005-05-18 22:50:10 -070016554#define BOUNDARY_SINGLE_CACHELINE 1
16555#define BOUNDARY_MULTI_CACHELINE 2
16556
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016557static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016558{
16559 int cacheline_size;
16560 u8 byte;
16561 int goal;
16562
16563 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16564 if (byte == 0)
16565 cacheline_size = 1024;
16566 else
16567 cacheline_size = (int) byte * 4;
16568
16569 /* On 5703 and later chips, the boundary bits have no
16570 * effect.
16571 */
Joe Perches41535772013-02-16 11:20:04 +000016572 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16573 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016574 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016575 goto out;
16576
16577#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16578 goal = BOUNDARY_MULTI_CACHELINE;
16579#else
16580#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16581 goal = BOUNDARY_SINGLE_CACHELINE;
16582#else
16583 goal = 0;
16584#endif
16585#endif
16586
Joe Perches63c3a662011-04-26 08:12:10 +000016587 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016588 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16589 goto out;
16590 }
16591
David S. Miller59e6b432005-05-18 22:50:10 -070016592 if (!goal)
16593 goto out;
16594
16595 /* PCI controllers on most RISC systems tend to disconnect
16596 * when a device tries to burst across a cache-line boundary.
16597 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16598 *
16599 * Unfortunately, for PCI-E there are only limited
16600 * write-side controls for this, and thus for reads
16601 * we will still get the disconnects. We'll also waste
16602 * these PCI cycles for both read and write for chips
16603 * other than 5700 and 5701 which do not implement the
16604 * boundary bits.
16605 */
Joe Perches63c3a662011-04-26 08:12:10 +000016606 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016607 switch (cacheline_size) {
16608 case 16:
16609 case 32:
16610 case 64:
16611 case 128:
16612 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16613 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16614 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16615 } else {
16616 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16617 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16618 }
16619 break;
16620
16621 case 256:
16622 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16623 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16624 break;
16625
16626 default:
16627 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16628 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16629 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016630 }
Joe Perches63c3a662011-04-26 08:12:10 +000016631 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016632 switch (cacheline_size) {
16633 case 16:
16634 case 32:
16635 case 64:
16636 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16637 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16638 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16639 break;
16640 }
16641 /* fallthrough */
16642 case 128:
16643 default:
16644 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16645 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16646 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016647 }
David S. Miller59e6b432005-05-18 22:50:10 -070016648 } else {
16649 switch (cacheline_size) {
16650 case 16:
16651 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16652 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16653 DMA_RWCTRL_WRITE_BNDRY_16);
16654 break;
16655 }
16656 /* fallthrough */
16657 case 32:
16658 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16659 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16660 DMA_RWCTRL_WRITE_BNDRY_32);
16661 break;
16662 }
16663 /* fallthrough */
16664 case 64:
16665 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16666 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16667 DMA_RWCTRL_WRITE_BNDRY_64);
16668 break;
16669 }
16670 /* fallthrough */
16671 case 128:
16672 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16673 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16674 DMA_RWCTRL_WRITE_BNDRY_128);
16675 break;
16676 }
16677 /* fallthrough */
16678 case 256:
16679 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16680 DMA_RWCTRL_WRITE_BNDRY_256);
16681 break;
16682 case 512:
16683 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16684 DMA_RWCTRL_WRITE_BNDRY_512);
16685 break;
16686 case 1024:
16687 default:
16688 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16689 DMA_RWCTRL_WRITE_BNDRY_1024);
16690 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016691 }
David S. Miller59e6b432005-05-18 22:50:10 -070016692 }
16693
16694out:
16695 return val;
16696}
16697
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016698static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000016699 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016700{
16701 struct tg3_internal_buffer_desc test_desc;
16702 u32 sram_dma_descs;
16703 int i, ret;
16704
16705 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16706
16707 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16708 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16709 tw32(RDMAC_STATUS, 0);
16710 tw32(WDMAC_STATUS, 0);
16711
16712 tw32(BUFMGR_MODE, 0);
16713 tw32(FTQ_RESET, 0);
16714
16715 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16716 test_desc.addr_lo = buf_dma & 0xffffffff;
16717 test_desc.nic_mbuf = 0x00002100;
16718 test_desc.len = size;
16719
16720 /*
16721 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16722 * the *second* time the tg3 driver was getting loaded after an
16723 * initial scan.
16724 *
16725 * Broadcom tells me:
16726 * ...the DMA engine is connected to the GRC block and a DMA
16727 * reset may affect the GRC block in some unpredictable way...
16728 * The behavior of resets to individual blocks has not been tested.
16729 *
16730 * Broadcom noted the GRC reset will also reset all sub-components.
16731 */
16732 if (to_device) {
16733 test_desc.cqid_sqid = (13 << 8) | 2;
16734
16735 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16736 udelay(40);
16737 } else {
16738 test_desc.cqid_sqid = (16 << 8) | 7;
16739
16740 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16741 udelay(40);
16742 }
16743 test_desc.flags = 0x00000005;
16744
16745 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16746 u32 val;
16747
16748 val = *(((u32 *)&test_desc) + i);
16749 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16750 sram_dma_descs + (i * sizeof(u32)));
16751 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16752 }
16753 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16754
Matt Carlson859a588792010-04-05 10:19:28 +000016755 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016756 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016757 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016758 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016759
16760 ret = -ENODEV;
16761 for (i = 0; i < 40; i++) {
16762 u32 val;
16763
16764 if (to_device)
16765 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16766 else
16767 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16768 if ((val & 0xffff) == sram_dma_descs) {
16769 ret = 0;
16770 break;
16771 }
16772
16773 udelay(100);
16774 }
16775
16776 return ret;
16777}
16778
David S. Millerded73402005-05-23 13:59:47 -070016779#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016780
Matt Carlson41434702011-03-09 16:58:22 +000016781static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016782 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16783 { },
16784};
16785
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016786static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016787{
16788 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016789 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016790 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016791
Matt Carlson4bae65c2010-11-24 08:31:52 +000016792 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16793 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016794 if (!buf) {
16795 ret = -ENOMEM;
16796 goto out_nofree;
16797 }
16798
16799 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16800 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16801
David S. Miller59e6b432005-05-18 22:50:10 -070016802 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016803
Joe Perches63c3a662011-04-26 08:12:10 +000016804 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016805 goto out;
16806
Joe Perches63c3a662011-04-26 08:12:10 +000016807 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016808 /* DMA read watermark not used on PCIE */
16809 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016810 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016811 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16812 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016813 tp->dma_rwctrl |= 0x003f0000;
16814 else
16815 tp->dma_rwctrl |= 0x003f000f;
16816 } else {
Joe Perches41535772013-02-16 11:20:04 +000016817 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16818 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016819 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016820 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016821
Michael Chan4a29cc22006-03-19 13:21:12 -080016822 /* If the 5704 is behind the EPB bridge, we can
16823 * do the less restrictive ONE_DMA workaround for
16824 * better performance.
16825 */
Joe Perches63c3a662011-04-26 08:12:10 +000016826 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016827 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016828 tp->dma_rwctrl |= 0x8000;
16829 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016830 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16831
Joe Perches41535772013-02-16 11:20:04 +000016832 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016833 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016834 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016835 tp->dma_rwctrl |=
16836 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16837 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16838 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016839 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016840 /* 5780 always in PCIX mode */
16841 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016842 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016843 /* 5714 always in PCIX mode */
16844 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016845 } else {
16846 tp->dma_rwctrl |= 0x001b000f;
16847 }
16848 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016849 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16850 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016851
Joe Perches41535772013-02-16 11:20:04 +000016852 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16853 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016854 tp->dma_rwctrl &= 0xfffffff0;
16855
Joe Perches41535772013-02-16 11:20:04 +000016856 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16857 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016858 /* Remove this if it causes problems for some boards. */
16859 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16860
16861 /* On 5700/5701 chips, we need to set this bit.
16862 * Otherwise the chip will issue cacheline transactions
16863 * to streamable DMA memory with not all the byte
16864 * enables turned on. This is an error on several
16865 * RISC PCI controllers, in particular sparc64.
16866 *
16867 * On 5703/5704 chips, this bit has been reassigned
16868 * a different meaning. In particular, it is used
16869 * on those chips to enable a PCI-X workaround.
16870 */
16871 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16872 }
16873
16874 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16875
16876#if 0
16877 /* Unneeded, already done by tg3_get_invariants. */
16878 tg3_switch_clocks(tp);
16879#endif
16880
Joe Perches41535772013-02-16 11:20:04 +000016881 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16882 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016883 goto out;
16884
David S. Miller59e6b432005-05-18 22:50:10 -070016885 /* It is best to perform DMA test with maximum write burst size
16886 * to expose the 5700/5701 write DMA bug.
16887 */
16888 saved_dma_rwctrl = tp->dma_rwctrl;
16889 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16890 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16891
Linus Torvalds1da177e2005-04-16 15:20:36 -070016892 while (1) {
16893 u32 *p = buf, i;
16894
16895 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16896 p[i] = i;
16897
16898 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000016899 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016900 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016901 dev_err(&tp->pdev->dev,
16902 "%s: Buffer write failed. err = %d\n",
16903 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016904 break;
16905 }
16906
16907#if 0
16908 /* validate data reached card RAM correctly. */
16909 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16910 u32 val;
16911 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16912 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016913 dev_err(&tp->pdev->dev,
16914 "%s: Buffer corrupted on device! "
16915 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016916 /* ret = -ENODEV here? */
16917 }
16918 p[i] = 0;
16919 }
16920#endif
16921 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000016922 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016923 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016924 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16925 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016926 break;
16927 }
16928
16929 /* Verify it. */
16930 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16931 if (p[i] == i)
16932 continue;
16933
David S. Miller59e6b432005-05-18 22:50:10 -070016934 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16935 DMA_RWCTRL_WRITE_BNDRY_16) {
16936 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016937 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16938 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16939 break;
16940 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016941 dev_err(&tp->pdev->dev,
16942 "%s: Buffer corrupted on read back! "
16943 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016944 ret = -ENODEV;
16945 goto out;
16946 }
16947 }
16948
16949 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16950 /* Success. */
16951 ret = 0;
16952 break;
16953 }
16954 }
David S. Miller59e6b432005-05-18 22:50:10 -070016955 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16956 DMA_RWCTRL_WRITE_BNDRY_16) {
16957 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016958 * now look for chipsets that are known to expose the
16959 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016960 */
Matt Carlson41434702011-03-09 16:58:22 +000016961 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016962 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16963 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016964 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016965 /* Safe to use the calculated DMA boundary. */
16966 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016967 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016968
David S. Miller59e6b432005-05-18 22:50:10 -070016969 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016971
16972out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016973 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016974out_nofree:
16975 return ret;
16976}
16977
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016978static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016979{
Joe Perches63c3a662011-04-26 08:12:10 +000016980 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016981 tp->bufmgr_config.mbuf_read_dma_low_water =
16982 DEFAULT_MB_RDMA_LOW_WATER_5705;
16983 tp->bufmgr_config.mbuf_mac_rx_low_water =
16984 DEFAULT_MB_MACRX_LOW_WATER_57765;
16985 tp->bufmgr_config.mbuf_high_water =
16986 DEFAULT_MB_HIGH_WATER_57765;
16987
16988 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16989 DEFAULT_MB_RDMA_LOW_WATER_5705;
16990 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16991 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16992 tp->bufmgr_config.mbuf_high_water_jumbo =
16993 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016994 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016995 tp->bufmgr_config.mbuf_read_dma_low_water =
16996 DEFAULT_MB_RDMA_LOW_WATER_5705;
16997 tp->bufmgr_config.mbuf_mac_rx_low_water =
16998 DEFAULT_MB_MACRX_LOW_WATER_5705;
16999 tp->bufmgr_config.mbuf_high_water =
17000 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017001 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017002 tp->bufmgr_config.mbuf_mac_rx_low_water =
17003 DEFAULT_MB_MACRX_LOW_WATER_5906;
17004 tp->bufmgr_config.mbuf_high_water =
17005 DEFAULT_MB_HIGH_WATER_5906;
17006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017007
Michael Chanfdfec1722005-07-25 12:31:48 -070017008 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17009 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17010 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17011 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17012 tp->bufmgr_config.mbuf_high_water_jumbo =
17013 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17014 } else {
17015 tp->bufmgr_config.mbuf_read_dma_low_water =
17016 DEFAULT_MB_RDMA_LOW_WATER;
17017 tp->bufmgr_config.mbuf_mac_rx_low_water =
17018 DEFAULT_MB_MACRX_LOW_WATER;
17019 tp->bufmgr_config.mbuf_high_water =
17020 DEFAULT_MB_HIGH_WATER;
17021
17022 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17023 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17024 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17025 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17026 tp->bufmgr_config.mbuf_high_water_jumbo =
17027 DEFAULT_MB_HIGH_WATER_JUMBO;
17028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017029
17030 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17031 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17032}
17033
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017034static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017035{
Matt Carlson79eb6902010-02-17 15:17:03 +000017036 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17037 case TG3_PHY_ID_BCM5400: return "5400";
17038 case TG3_PHY_ID_BCM5401: return "5401";
17039 case TG3_PHY_ID_BCM5411: return "5411";
17040 case TG3_PHY_ID_BCM5701: return "5701";
17041 case TG3_PHY_ID_BCM5703: return "5703";
17042 case TG3_PHY_ID_BCM5704: return "5704";
17043 case TG3_PHY_ID_BCM5705: return "5705";
17044 case TG3_PHY_ID_BCM5750: return "5750";
17045 case TG3_PHY_ID_BCM5752: return "5752";
17046 case TG3_PHY_ID_BCM5714: return "5714";
17047 case TG3_PHY_ID_BCM5780: return "5780";
17048 case TG3_PHY_ID_BCM5755: return "5755";
17049 case TG3_PHY_ID_BCM5787: return "5787";
17050 case TG3_PHY_ID_BCM5784: return "5784";
17051 case TG3_PHY_ID_BCM5756: return "5722/5756";
17052 case TG3_PHY_ID_BCM5906: return "5906";
17053 case TG3_PHY_ID_BCM5761: return "5761";
17054 case TG3_PHY_ID_BCM5718C: return "5718C";
17055 case TG3_PHY_ID_BCM5718S: return "5718S";
17056 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017057 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017058 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017059 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017060 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017061 case 0: return "serdes";
17062 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017064}
17065
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017066static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017067{
Joe Perches63c3a662011-04-26 08:12:10 +000017068 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017069 strcpy(str, "PCI Express");
17070 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017071 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017072 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17073
17074 strcpy(str, "PCIX:");
17075
17076 if ((clock_ctrl == 7) ||
17077 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17078 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17079 strcat(str, "133MHz");
17080 else if (clock_ctrl == 0)
17081 strcat(str, "33MHz");
17082 else if (clock_ctrl == 2)
17083 strcat(str, "50MHz");
17084 else if (clock_ctrl == 4)
17085 strcat(str, "66MHz");
17086 else if (clock_ctrl == 6)
17087 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017088 } else {
17089 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017090 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017091 strcat(str, "66MHz");
17092 else
17093 strcat(str, "33MHz");
17094 }
Joe Perches63c3a662011-04-26 08:12:10 +000017095 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017096 strcat(str, ":32-bit");
17097 else
17098 strcat(str, ":64-bit");
17099 return str;
17100}
17101
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017102static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017103{
17104 struct ethtool_coalesce *ec = &tp->coal;
17105
17106 memset(ec, 0, sizeof(*ec));
17107 ec->cmd = ETHTOOL_GCOALESCE;
17108 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17109 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17110 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17111 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17112 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17113 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17114 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17115 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17116 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17117
17118 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17119 HOSTCC_MODE_CLRTICK_TXBD)) {
17120 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17121 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17122 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17123 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17124 }
Michael Chand244c892005-07-05 14:42:33 -070017125
Joe Perches63c3a662011-04-26 08:12:10 +000017126 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017127 ec->rx_coalesce_usecs_irq = 0;
17128 ec->tx_coalesce_usecs_irq = 0;
17129 ec->stats_block_coalesce_usecs = 0;
17130 }
David S. Miller15f98502005-05-18 22:49:26 -070017131}
17132
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017133static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017134 const struct pci_device_id *ent)
17135{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017136 struct net_device *dev;
17137 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017138 int i, err, pm_cap;
17139 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017140 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017141 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017142 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017143
Joe Perches05dbe002010-02-17 19:44:19 +000017144 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017145
17146 err = pci_enable_device(pdev);
17147 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017148 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017149 return err;
17150 }
17151
Linus Torvalds1da177e2005-04-16 15:20:36 -070017152 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17153 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017154 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017155 goto err_out_disable_pdev;
17156 }
17157
17158 pci_set_master(pdev);
17159
17160 /* Find power-management capability. */
17161 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
17162 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000017163 dev_err(&pdev->dev,
17164 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017165 err = -EIO;
17166 goto err_out_free_res;
17167 }
17168
Matt Carlson16821282011-07-13 09:27:28 +000017169 err = pci_set_power_state(pdev, PCI_D0);
17170 if (err) {
17171 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
17172 goto err_out_free_res;
17173 }
17174
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017175 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017176 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017177 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000017178 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017179 }
17180
Linus Torvalds1da177e2005-04-16 15:20:36 -070017181 SET_NETDEV_DEV(dev, &pdev->dev);
17182
Linus Torvalds1da177e2005-04-16 15:20:36 -070017183 tp = netdev_priv(dev);
17184 tp->pdev = pdev;
17185 tp->dev = dev;
17186 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017187 tp->rx_mode = TG3_DEF_RX_MODE;
17188 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017189 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017190
Linus Torvalds1da177e2005-04-16 15:20:36 -070017191 if (tg3_debug > 0)
17192 tp->msg_enable = tg3_debug;
17193 else
17194 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17195
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017196 if (pdev_is_ssb_gige_core(pdev)) {
17197 tg3_flag_set(tp, IS_SSB_CORE);
17198 if (ssb_gige_must_flush_posted_writes(pdev))
17199 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17200 if (ssb_gige_one_dma_at_once(pdev))
17201 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
17202 if (ssb_gige_have_roboswitch(pdev))
17203 tg3_flag_set(tp, ROBOSWITCH);
17204 if (ssb_gige_is_rgmii(pdev))
17205 tg3_flag_set(tp, RGMII_MODE);
17206 }
17207
Linus Torvalds1da177e2005-04-16 15:20:36 -070017208 /* The word/byte swap controls here control register access byte
17209 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17210 * setting below.
17211 */
17212 tp->misc_host_ctrl =
17213 MISC_HOST_CTRL_MASK_PCI_INT |
17214 MISC_HOST_CTRL_WORD_SWAP |
17215 MISC_HOST_CTRL_INDIR_ACCESS |
17216 MISC_HOST_CTRL_PCISTATE_RW;
17217
17218 /* The NONFRM (non-frame) byte/word swap controls take effect
17219 * on descriptor entries, anything which isn't packet data.
17220 *
17221 * The StrongARM chips on the board (one for tx, one for rx)
17222 * are running in big-endian mode.
17223 */
17224 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17225 GRC_MODE_WSWAP_NONFRM_DATA);
17226#ifdef __BIG_ENDIAN
17227 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17228#endif
17229 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017230 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017231 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017232
Matt Carlsond5fe4882008-11-21 17:20:32 -080017233 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017234 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017235 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017236 err = -ENOMEM;
17237 goto err_out_free_dev;
17238 }
17239
Matt Carlsonc9cab242011-07-13 09:27:27 +000017240 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17241 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17242 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17243 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17244 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017245 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017246 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17247 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017248 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
17249 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17250 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
17251 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017252 tg3_flag_set(tp, ENABLE_APE);
17253 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17254 if (!tp->aperegs) {
17255 dev_err(&pdev->dev,
17256 "Cannot map APE registers, aborting\n");
17257 err = -ENOMEM;
17258 goto err_out_iounmap;
17259 }
17260 }
17261
Linus Torvalds1da177e2005-04-16 15:20:36 -070017262 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17263 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017264
Linus Torvalds1da177e2005-04-16 15:20:36 -070017265 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017266 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017267 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017268 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017269
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017270 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017271 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017272 dev_err(&pdev->dev,
17273 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017274 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017275 }
17276
Michael Chan4a29cc22006-03-19 13:21:12 -080017277 /* The EPB bridge inside 5714, 5715, and 5780 and any
17278 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017279 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17280 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17281 * do DMA address check in tg3_start_xmit().
17282 */
Joe Perches63c3a662011-04-26 08:12:10 +000017283 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017284 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017285 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017286 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017287#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017288 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017289#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017290 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017291 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017292
17293 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017294 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017295 err = pci_set_dma_mask(pdev, dma_mask);
17296 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017297 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017298 err = pci_set_consistent_dma_mask(pdev,
17299 persist_dma_mask);
17300 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017301 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17302 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017303 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017304 }
17305 }
17306 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017307 if (err || dma_mask == DMA_BIT_MASK(32)) {
17308 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017309 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017310 dev_err(&pdev->dev,
17311 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017312 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017313 }
17314 }
17315
Michael Chanfdfec1722005-07-25 12:31:48 -070017316 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017317
Patrick McHardyf6469682013-04-19 02:04:27 +000017318 features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlson0da06062011-05-19 12:12:53 +000017319
17320 /* 5700 B0 chips do not support checksumming correctly due
17321 * to hardware bugs.
17322 */
Joe Perches41535772013-02-16 11:20:04 +000017323 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017324 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17325
17326 if (tg3_flag(tp, 5755_PLUS))
17327 features |= NETIF_F_IPV6_CSUM;
17328 }
17329
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017330 /* TSO is on by default on chips that support hardware TSO.
17331 * Firmware TSO on older chips gives lower performance, so it
17332 * is off by default, but can be enabled using ethtool.
17333 */
Joe Perches63c3a662011-04-26 08:12:10 +000017334 if ((tg3_flag(tp, HW_TSO_1) ||
17335 tg3_flag(tp, HW_TSO_2) ||
17336 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017337 (features & NETIF_F_IP_CSUM))
17338 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017339 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017340 if (features & NETIF_F_IPV6_CSUM)
17341 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017342 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017343 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17344 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17345 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17346 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17347 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017348 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017350
Matt Carlsond542fe22011-05-19 16:02:43 +000017351 dev->features |= features;
17352 dev->vlan_features |= features;
17353
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017354 /*
17355 * Add loopback capability only for a subset of devices that support
17356 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17357 * loopback for the remaining devices.
17358 */
Joe Perches41535772013-02-16 11:20:04 +000017359 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017360 !tg3_flag(tp, CPMU_PRESENT))
17361 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017362 features |= NETIF_F_LOOPBACK;
17363
Matt Carlson0da06062011-05-19 12:12:53 +000017364 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017365
Joe Perches41535772013-02-16 11:20:04 +000017366 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017367 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017368 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017369 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017370 tp->rx_pending = 63;
17371 }
17372
Linus Torvalds1da177e2005-04-16 15:20:36 -070017373 err = tg3_get_device_address(tp);
17374 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017375 dev_err(&pdev->dev,
17376 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017377 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017378 }
17379
Matt Carlsonc88864d2007-11-12 21:07:01 -080017380 /*
17381 * Reset chip in case UNDI or EFI driver did not shutdown
17382 * DMA self test will enable WDMAC and we'll see (spurious)
17383 * pending DMA on the PCI bus at that point.
17384 */
17385 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17386 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17387 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17388 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17389 }
17390
17391 err = tg3_test_dma(tp);
17392 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017393 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017394 goto err_out_apeunmap;
17395 }
17396
Matt Carlson78f90dc2009-11-13 13:03:42 +000017397 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17398 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17399 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017400 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017401 struct tg3_napi *tnapi = &tp->napi[i];
17402
17403 tnapi->tp = tp;
17404 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17405
17406 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017407 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017408 intmbx += 0x8;
17409 else
17410 intmbx += 0x4;
17411
17412 tnapi->consmbox = rcvmbx;
17413 tnapi->prodmbox = sndmbx;
17414
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017415 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017416 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017417 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017418 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017419
Joe Perches63c3a662011-04-26 08:12:10 +000017420 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017421 break;
17422
17423 /*
17424 * If we support MSIX, we'll be using RSS. If we're using
17425 * RSS, the first vector only handles link interrupts and the
17426 * remaining vectors handle rx and tx interrupts. Reuse the
17427 * mailbox values for the next iteration. The values we setup
17428 * above are still useful for the single vectored mode.
17429 */
17430 if (!i)
17431 continue;
17432
17433 rcvmbx += 0x8;
17434
17435 if (sndmbx & 0x4)
17436 sndmbx -= 0x4;
17437 else
17438 sndmbx += 0xc;
17439 }
17440
Matt Carlsonc88864d2007-11-12 21:07:01 -080017441 tg3_init_coal(tp);
17442
Michael Chanc49a1562006-12-17 17:07:29 -080017443 pci_set_drvdata(pdev, dev);
17444
Joe Perches41535772013-02-16 11:20:04 +000017445 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17446 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17447 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017448 tg3_flag_set(tp, PTP_CAPABLE);
17449
Matt Carlsoncd0d7222011-07-13 09:27:33 +000017450 if (tg3_flag(tp, 5717_PLUS)) {
17451 /* Resume a low-power mode */
17452 tg3_frob_aux_power(tp, false);
17453 }
17454
Matt Carlson21f76382012-02-22 12:35:21 +000017455 tg3_timer_init(tp);
17456
Michael Chan402e1392013-02-14 12:13:41 +000017457 tg3_carrier_off(tp);
17458
Linus Torvalds1da177e2005-04-16 15:20:36 -070017459 err = register_netdev(dev);
17460 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017461 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017462 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017463 }
17464
Joe Perches05dbe002010-02-17 19:44:19 +000017465 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17466 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017467 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017468 tg3_bus_string(tp, str),
17469 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017470
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017471 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017472 struct phy_device *phydev;
17473 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017474 netdev_info(dev,
17475 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017476 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017477 } else {
17478 char *ethtype;
17479
17480 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17481 ethtype = "10/100Base-TX";
17482 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17483 ethtype = "1000Base-SX";
17484 else
17485 ethtype = "10/100/1000Base-T";
17486
Matt Carlson5129c3a2010-04-05 10:19:23 +000017487 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017488 "(WireSpeed[%d], EEE[%d])\n",
17489 tg3_phy_string(tp), ethtype,
17490 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17491 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017492 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017493
Joe Perches05dbe002010-02-17 19:44:19 +000017494 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017495 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017496 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017497 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017498 tg3_flag(tp, ENABLE_ASF) != 0,
17499 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017500 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17501 tp->dma_rwctrl,
17502 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17503 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017504
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017505 pci_save_state(pdev);
17506
Linus Torvalds1da177e2005-04-16 15:20:36 -070017507 return 0;
17508
Matt Carlson0d3031d2007-10-10 18:02:43 -070017509err_out_apeunmap:
17510 if (tp->aperegs) {
17511 iounmap(tp->aperegs);
17512 tp->aperegs = NULL;
17513 }
17514
Linus Torvalds1da177e2005-04-16 15:20:36 -070017515err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017516 if (tp->regs) {
17517 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017518 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017520
17521err_out_free_dev:
17522 free_netdev(dev);
17523
Matt Carlson16821282011-07-13 09:27:28 +000017524err_out_power_down:
17525 pci_set_power_state(pdev, PCI_D3hot);
17526
Linus Torvalds1da177e2005-04-16 15:20:36 -070017527err_out_free_res:
17528 pci_release_regions(pdev);
17529
17530err_out_disable_pdev:
17531 pci_disable_device(pdev);
17532 pci_set_drvdata(pdev, NULL);
17533 return err;
17534}
17535
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017536static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017537{
17538 struct net_device *dev = pci_get_drvdata(pdev);
17539
17540 if (dev) {
17541 struct tg3 *tp = netdev_priv(dev);
17542
Jesper Juhle3c55302012-04-09 22:50:15 +020017543 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017544
Matt Carlsondb219972011-11-04 09:15:03 +000017545 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017546
David S. Miller1805b2f2011-10-24 18:18:09 -040017547 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017548 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017549 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017550 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017551
Linus Torvalds1da177e2005-04-16 15:20:36 -070017552 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017553 if (tp->aperegs) {
17554 iounmap(tp->aperegs);
17555 tp->aperegs = NULL;
17556 }
Michael Chan68929142005-08-09 20:17:14 -070017557 if (tp->regs) {
17558 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017559 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017561 free_netdev(dev);
17562 pci_release_regions(pdev);
17563 pci_disable_device(pdev);
17564 pci_set_drvdata(pdev, NULL);
17565 }
17566}
17567
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017568#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017569static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017570{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017571 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017572 struct net_device *dev = pci_get_drvdata(pdev);
17573 struct tg3 *tp = netdev_priv(dev);
17574 int err;
17575
17576 if (!netif_running(dev))
17577 return 0;
17578
Matt Carlsondb219972011-11-04 09:15:03 +000017579 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017580 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017581 tg3_netif_stop(tp);
17582
Matt Carlson21f76382012-02-22 12:35:21 +000017583 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017584
David S. Millerf47c11e2005-06-24 20:18:35 -070017585 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017586 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017587 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017588
17589 netif_device_detach(dev);
17590
David S. Millerf47c11e2005-06-24 20:18:35 -070017591 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017592 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017593 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017594 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017595
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017596 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017597 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017598 int err2;
17599
David S. Millerf47c11e2005-06-24 20:18:35 -070017600 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017601
Joe Perches63c3a662011-04-26 08:12:10 +000017602 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017603 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017604 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017606
Matt Carlson21f76382012-02-22 12:35:21 +000017607 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017608
17609 netif_device_attach(dev);
17610 tg3_netif_start(tp);
17611
Michael Chanb9ec6c12006-07-25 16:37:27 -070017612out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017613 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017614
17615 if (!err2)
17616 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017617 }
17618
17619 return err;
17620}
17621
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017622static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017623{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017624 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017625 struct net_device *dev = pci_get_drvdata(pdev);
17626 struct tg3 *tp = netdev_priv(dev);
17627 int err;
17628
17629 if (!netif_running(dev))
17630 return 0;
17631
Linus Torvalds1da177e2005-04-16 15:20:36 -070017632 netif_device_attach(dev);
17633
David S. Millerf47c11e2005-06-24 20:18:35 -070017634 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017635
Joe Perches63c3a662011-04-26 08:12:10 +000017636 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017637 err = tg3_restart_hw(tp,
17638 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017639 if (err)
17640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017641
Matt Carlson21f76382012-02-22 12:35:21 +000017642 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017643
Linus Torvalds1da177e2005-04-16 15:20:36 -070017644 tg3_netif_start(tp);
17645
Michael Chanb9ec6c12006-07-25 16:37:27 -070017646out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017647 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017648
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017649 if (!err)
17650 tg3_phy_start(tp);
17651
Michael Chanb9ec6c12006-07-25 16:37:27 -070017652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017653}
Fabio Estevam42df36a2013-04-16 09:28:29 +000017654#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017655
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017656static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
17657
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017658/**
17659 * tg3_io_error_detected - called when PCI error is detected
17660 * @pdev: Pointer to PCI device
17661 * @state: The current pci connection state
17662 *
17663 * This function is called after a PCI bus error affecting
17664 * this device has been detected.
17665 */
17666static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17667 pci_channel_state_t state)
17668{
17669 struct net_device *netdev = pci_get_drvdata(pdev);
17670 struct tg3 *tp = netdev_priv(netdev);
17671 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17672
17673 netdev_info(netdev, "PCI I/O error detected\n");
17674
17675 rtnl_lock();
17676
17677 if (!netif_running(netdev))
17678 goto done;
17679
17680 tg3_phy_stop(tp);
17681
17682 tg3_netif_stop(tp);
17683
Matt Carlson21f76382012-02-22 12:35:21 +000017684 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017685
17686 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017687 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017688
17689 netif_device_detach(netdev);
17690
17691 /* Clean up software state, even if MMIO is blocked */
17692 tg3_full_lock(tp, 0);
17693 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17694 tg3_full_unlock(tp);
17695
17696done:
17697 if (state == pci_channel_io_perm_failure)
17698 err = PCI_ERS_RESULT_DISCONNECT;
17699 else
17700 pci_disable_device(pdev);
17701
17702 rtnl_unlock();
17703
17704 return err;
17705}
17706
17707/**
17708 * tg3_io_slot_reset - called after the pci bus has been reset.
17709 * @pdev: Pointer to PCI device
17710 *
17711 * Restart the card from scratch, as if from a cold-boot.
17712 * At this point, the card has exprienced a hard reset,
17713 * followed by fixups by BIOS, and has its config space
17714 * set up identically to what it was at cold boot.
17715 */
17716static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17717{
17718 struct net_device *netdev = pci_get_drvdata(pdev);
17719 struct tg3 *tp = netdev_priv(netdev);
17720 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17721 int err;
17722
17723 rtnl_lock();
17724
17725 if (pci_enable_device(pdev)) {
17726 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17727 goto done;
17728 }
17729
17730 pci_set_master(pdev);
17731 pci_restore_state(pdev);
17732 pci_save_state(pdev);
17733
17734 if (!netif_running(netdev)) {
17735 rc = PCI_ERS_RESULT_RECOVERED;
17736 goto done;
17737 }
17738
17739 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017740 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017741 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017742
17743 rc = PCI_ERS_RESULT_RECOVERED;
17744
17745done:
17746 rtnl_unlock();
17747
17748 return rc;
17749}
17750
17751/**
17752 * tg3_io_resume - called when traffic can start flowing again.
17753 * @pdev: Pointer to PCI device
17754 *
17755 * This callback is called when the error recovery driver tells
17756 * us that its OK to resume normal operation.
17757 */
17758static void tg3_io_resume(struct pci_dev *pdev)
17759{
17760 struct net_device *netdev = pci_get_drvdata(pdev);
17761 struct tg3 *tp = netdev_priv(netdev);
17762 int err;
17763
17764 rtnl_lock();
17765
17766 if (!netif_running(netdev))
17767 goto done;
17768
17769 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000017770 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017771 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017772 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017773 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017774 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17775 goto done;
17776 }
17777
17778 netif_device_attach(netdev);
17779
Matt Carlson21f76382012-02-22 12:35:21 +000017780 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017781
17782 tg3_netif_start(tp);
17783
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017784 tg3_full_unlock(tp);
17785
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017786 tg3_phy_start(tp);
17787
17788done:
17789 rtnl_unlock();
17790}
17791
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017792static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017793 .error_detected = tg3_io_error_detected,
17794 .slot_reset = tg3_io_slot_reset,
17795 .resume = tg3_io_resume
17796};
17797
Linus Torvalds1da177e2005-04-16 15:20:36 -070017798static struct pci_driver tg3_driver = {
17799 .name = DRV_MODULE_NAME,
17800 .id_table = tg3_pci_tbl,
17801 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017802 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017803 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000017804 .driver.pm = &tg3_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017805};
17806
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000017807module_pci_driver(tg3_driver);