blob: 26804db1f884864d07a54fe4417ce15a8a50e1e9 [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 Carlson66711e62009-11-13 13:03:49 +0000520 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700521 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
522 TG3_64BIT_REG_LOW, val);
523 return;
524 }
525
526 spin_lock_irqsave(&tp->indirect_lock, flags);
527 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
528 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
529 spin_unlock_irqrestore(&tp->indirect_lock, flags);
530
531 /* In indirect mode when disabling interrupts, we also need
532 * to clear the interrupt bit in the GRC local ctrl register.
533 */
534 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
535 (val == 0x1)) {
536 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
537 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
538 }
539}
540
541static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
542{
543 unsigned long flags;
544 u32 val;
545
546 spin_lock_irqsave(&tp->indirect_lock, flags);
547 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
548 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
549 spin_unlock_irqrestore(&tp->indirect_lock, flags);
550 return val;
551}
552
Michael Chanb401e9e2005-12-19 16:27:04 -0800553/* usec_wait specifies the wait time in usec when writing to certain registers
554 * where it is unsafe to read back the register without some delay.
555 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
556 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
557 */
558static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Joe Perches63c3a662011-04-26 08:12:10 +0000560 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800561 /* Non-posted methods */
562 tp->write32(tp, off, val);
563 else {
564 /* Posted method */
565 tg3_write32(tp, off, val);
566 if (usec_wait)
567 udelay(usec_wait);
568 tp->read32(tp, off);
569 }
570 /* Wait again after the read for the posted method to guarantee that
571 * the wait time is met.
572 */
573 if (usec_wait)
574 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Michael Chan09ee9292005-08-09 20:17:00 -0700577static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
578{
579 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000580 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
581 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
582 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700583 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700584}
585
Michael Chan20094932005-08-09 20:16:32 -0700586static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 void __iomem *mbox = tp->regs + off;
589 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000590 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000592 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
593 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 readl(mbox);
595}
596
Michael Chanb5d37722006-09-27 16:06:21 -0700597static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
598{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000599 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700600}
601
602static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
603{
604 writel(val, tp->regs + off + GRCMBOX_BASE);
605}
606
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000607#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700608#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000609#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
610#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
611#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700612
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000613#define tw32(reg, val) tp->write32(tp, reg, val)
614#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
615#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
616#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
619{
Michael Chan68929142005-08-09 20:17:14 -0700620 unsigned long flags;
621
Joe Perches41535772013-02-16 11:20:04 +0000622 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700623 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
624 return;
625
Michael Chan68929142005-08-09 20:17:14 -0700626 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000627 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700628 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
629 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Michael Chanbbadf502006-04-06 21:46:34 -0700631 /* Always leave this as zero. */
632 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
633 } else {
634 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
635 tw32_f(TG3PCI_MEM_WIN_DATA, val);
636
637 /* Always leave this as zero. */
638 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
639 }
Michael Chan68929142005-08-09 20:17:14 -0700640 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
644{
Michael Chan68929142005-08-09 20:17:14 -0700645 unsigned long flags;
646
Joe Perches41535772013-02-16 11:20:04 +0000647 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700648 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
649 *val = 0;
650 return;
651 }
652
Michael Chan68929142005-08-09 20:17:14 -0700653 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000654 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700655 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
656 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Michael Chanbbadf502006-04-06 21:46:34 -0700658 /* Always leave this as zero. */
659 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
660 } else {
661 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
662 *val = tr32(TG3PCI_MEM_WIN_DATA);
663
664 /* Always leave this as zero. */
665 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
666 }
Michael Chan68929142005-08-09 20:17:14 -0700667 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Matt Carlson0d3031d2007-10-10 18:02:43 -0700670static void tg3_ape_lock_init(struct tg3 *tp)
671{
672 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000673 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674
Joe Perches41535772013-02-16 11:20:04 +0000675 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000676 regbase = TG3_APE_LOCK_GRANT;
677 else
678 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700679
680 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000681 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
682 switch (i) {
683 case TG3_APE_LOCK_PHY0:
684 case TG3_APE_LOCK_PHY1:
685 case TG3_APE_LOCK_PHY2:
686 case TG3_APE_LOCK_PHY3:
687 bit = APE_LOCK_GRANT_DRIVER;
688 break;
689 default:
690 if (!tp->pci_fn)
691 bit = APE_LOCK_GRANT_DRIVER;
692 else
693 bit = 1 << tp->pci_fn;
694 }
695 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 }
697
Matt Carlson0d3031d2007-10-10 18:02:43 -0700698}
699
700static int tg3_ape_lock(struct tg3 *tp, int locknum)
701{
702 int i, off;
703 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000704 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700705
Joe Perches63c3a662011-04-26 08:12:10 +0000706 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707 return 0;
708
709 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000710 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000711 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000713 case TG3_APE_LOCK_GRC:
714 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000715 if (!tp->pci_fn)
716 bit = APE_LOCK_REQ_DRIVER;
717 else
718 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000719 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000720 case TG3_APE_LOCK_PHY0:
721 case TG3_APE_LOCK_PHY1:
722 case TG3_APE_LOCK_PHY2:
723 case TG3_APE_LOCK_PHY3:
724 bit = APE_LOCK_REQ_DRIVER;
725 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000726 default:
727 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700728 }
729
Joe Perches41535772013-02-16 11:20:04 +0000730 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000731 req = TG3_APE_LOCK_REQ;
732 gnt = TG3_APE_LOCK_GRANT;
733 } else {
734 req = TG3_APE_PER_LOCK_REQ;
735 gnt = TG3_APE_PER_LOCK_GRANT;
736 }
737
Matt Carlson0d3031d2007-10-10 18:02:43 -0700738 off = 4 * locknum;
739
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000740 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700741
742 /* Wait for up to 1 millisecond to acquire lock. */
743 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000744 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000745 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746 break;
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
Joe Perches953c96e2013-04-09 10:18:14 +00002323static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002324{
2325 u32 val;
2326
2327 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2328 return;
2329
2330 tp->setlpicnt = 0;
2331
2332 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002333 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002334 tp->link_config.active_duplex == DUPLEX_FULL &&
2335 (tp->link_config.active_speed == SPEED_100 ||
2336 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002337 u32 eeectl;
2338
2339 if (tp->link_config.active_speed == SPEED_1000)
2340 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2341 else
2342 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2343
2344 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2345
Matt Carlson3110f5f52010-12-06 08:28:50 +00002346 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2347 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002348
Matt Carlsonb0c59432011-05-19 12:12:48 +00002349 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2350 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002351 tp->setlpicnt = 2;
2352 }
2353
2354 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002355 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002356 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002357 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002358 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002359 }
2360
Matt Carlson52b02d02010-10-14 10:37:41 +00002361 val = tr32(TG3_CPMU_EEE_MODE);
2362 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2363 }
2364}
2365
Matt Carlsonb0c59432011-05-19 12:12:48 +00002366static void tg3_phy_eee_enable(struct tg3 *tp)
2367{
2368 u32 val;
2369
2370 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002371 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2372 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002373 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002374 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002375 val = MII_TG3_DSP_TAP26_ALNOKO |
2376 MII_TG3_DSP_TAP26_RMRXSTO;
2377 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002378 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002379 }
2380
2381 val = tr32(TG3_CPMU_EEE_MODE);
2382 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2383}
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385static int tg3_wait_macro_done(struct tg3 *tp)
2386{
2387 int limit = 100;
2388
2389 while (limit--) {
2390 u32 tmp32;
2391
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002392 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if ((tmp32 & 0x1000) == 0)
2394 break;
2395 }
2396 }
Roel Kluind4675b52009-02-12 16:33:27 -08002397 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return -EBUSY;
2399
2400 return 0;
2401}
2402
2403static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2404{
2405 static const u32 test_pat[4][6] = {
2406 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2407 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2408 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2409 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2410 };
2411 int chan;
2412
2413 for (chan = 0; chan < 4; chan++) {
2414 int i;
2415
2416 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2417 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002418 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 for (i = 0; i < 6; i++)
2421 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2422 test_pat[chan][i]);
2423
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002424 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (tg3_wait_macro_done(tp)) {
2426 *resetp = 1;
2427 return -EBUSY;
2428 }
2429
2430 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2431 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002432 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (tg3_wait_macro_done(tp)) {
2434 *resetp = 1;
2435 return -EBUSY;
2436 }
2437
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002438 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (tg3_wait_macro_done(tp)) {
2440 *resetp = 1;
2441 return -EBUSY;
2442 }
2443
2444 for (i = 0; i < 6; i += 2) {
2445 u32 low, high;
2446
2447 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2448 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2449 tg3_wait_macro_done(tp)) {
2450 *resetp = 1;
2451 return -EBUSY;
2452 }
2453 low &= 0x7fff;
2454 high &= 0x000f;
2455 if (low != test_pat[chan][i] ||
2456 high != test_pat[chan][i+1]) {
2457 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2458 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2459 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2460
2461 return -EBUSY;
2462 }
2463 }
2464 }
2465
2466 return 0;
2467}
2468
2469static int tg3_phy_reset_chanpat(struct tg3 *tp)
2470{
2471 int chan;
2472
2473 for (chan = 0; chan < 4; chan++) {
2474 int i;
2475
2476 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2477 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002478 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 for (i = 0; i < 6; i++)
2480 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002481 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (tg3_wait_macro_done(tp))
2483 return -EBUSY;
2484 }
2485
2486 return 0;
2487}
2488
2489static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2490{
2491 u32 reg32, phy9_orig;
2492 int retries, do_phy_reset, err;
2493
2494 retries = 10;
2495 do_phy_reset = 1;
2496 do {
2497 if (do_phy_reset) {
2498 err = tg3_bmcr_reset(tp);
2499 if (err)
2500 return err;
2501 do_phy_reset = 0;
2502 }
2503
2504 /* Disable transmitter and interrupt. */
2505 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2506 continue;
2507
2508 reg32 |= 0x3000;
2509 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2510
2511 /* Set full-duplex, 1000 mbps. */
2512 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002513 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002516 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 continue;
2518
Matt Carlson221c5632011-06-13 13:39:01 +00002519 tg3_writephy(tp, MII_CTRL1000,
2520 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002522 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002523 if (err)
2524 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002527 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2530 if (!err)
2531 break;
2532 } while (--retries);
2533
2534 err = tg3_phy_reset_chanpat(tp);
2535 if (err)
2536 return err;
2537
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002538 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002541 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002543 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Matt Carlson221c5632011-06-13 13:39:01 +00002545 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2548 reg32 &= ~0x3000;
2549 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2550 } else if (!err)
2551 err = -EBUSY;
2552
2553 return err;
2554}
2555
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002556static void tg3_carrier_off(struct tg3 *tp)
2557{
2558 netif_carrier_off(tp->dev);
2559 tp->link_up = false;
2560}
2561
Nithin Sujirce20f162013-04-09 08:48:04 +00002562static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2563{
2564 if (tg3_flag(tp, ENABLE_ASF))
2565 netdev_warn(tp->dev,
2566 "Management side-band traffic will be interrupted during phy settings change\n");
2567}
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569/* This will reset the tigon3 PHY if there is no valid
2570 * link unless the FORCE argument is non-zero.
2571 */
2572static int tg3_phy_reset(struct tg3 *tp)
2573{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002574 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 int err;
2576
Joe Perches41535772013-02-16 11:20:04 +00002577 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002578 val = tr32(GRC_MISC_CFG);
2579 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2580 udelay(40);
2581 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002582 err = tg3_readphy(tp, MII_BMSR, &val);
2583 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (err != 0)
2585 return -EBUSY;
2586
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002587 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002588 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002589 tg3_link_report(tp);
2590 }
2591
Joe Perches41535772013-02-16 11:20:04 +00002592 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2593 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2594 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 err = tg3_phy_reset_5703_4_5(tp);
2596 if (err)
2597 return err;
2598 goto out;
2599 }
2600
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002601 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002602 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2603 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002604 cpmuctrl = tr32(TG3_CPMU_CTRL);
2605 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2606 tw32(TG3_CPMU_CTRL,
2607 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2608 }
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 err = tg3_bmcr_reset(tp);
2611 if (err)
2612 return err;
2613
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002614 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002615 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2616 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002617
2618 tw32(TG3_CPMU_CTRL, cpmuctrl);
2619 }
2620
Joe Perches41535772013-02-16 11:20:04 +00002621 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2622 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002623 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2624 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2625 CPMU_LSPD_1000MB_MACCLK_12_5) {
2626 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2627 udelay(40);
2628 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2629 }
2630 }
2631
Joe Perches63c3a662011-04-26 08:12:10 +00002632 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002633 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002634 return 0;
2635
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002636 tg3_phy_apply_otp(tp);
2637
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002638 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002639 tg3_phy_toggle_apd(tp, true);
2640 else
2641 tg3_phy_toggle_apd(tp, false);
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002644 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002645 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002646 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2647 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002648 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002650
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002651 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002652 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2653 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002655
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002656 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002657 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002658 tg3_phydsp_write(tp, 0x000a, 0x310b);
2659 tg3_phydsp_write(tp, 0x201f, 0x9506);
2660 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002661 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002662 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002663 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002664 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002665 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2666 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2667 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2668 tg3_writephy(tp, MII_TG3_TEST1,
2669 MII_TG3_TEST1_TRIM_EN | 0x4);
2670 } else
2671 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2672
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002673 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002674 }
Michael Chanc424cb22006-04-29 18:56:34 -07002675 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 /* Set Extended packet length bit (bit 14) on all chips that */
2678 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002679 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002681 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002682 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002684 err = tg3_phy_auxctl_read(tp,
2685 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2686 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002687 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2688 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690
2691 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2692 * jumbo frames transmission.
2693 */
Joe Perches63c3a662011-04-26 08:12:10 +00002694 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002695 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002696 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002697 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699
Joe Perches41535772013-02-16 11:20:04 +00002700 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002701 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002702 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002703 }
2704
Joe Perches41535772013-02-16 11:20:04 +00002705 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002706 tg3_phydsp_write(tp, 0xffb, 0x4000);
2707
Joe Perches953c96e2013-04-09 10:18:14 +00002708 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 tg3_phy_set_wirespeed(tp);
2710 return 0;
2711}
2712
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002713#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2714#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2715#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2716 TG3_GPIO_MSG_NEED_VAUX)
2717#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2718 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2719 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2720 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2721 (TG3_GPIO_MSG_DRVR_PRES << 12))
2722
2723#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2724 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2725 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2726 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2727 (TG3_GPIO_MSG_NEED_VAUX << 12))
2728
2729static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2730{
2731 u32 status, shift;
2732
Joe Perches41535772013-02-16 11:20:04 +00002733 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2734 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002735 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2736 else
2737 status = tr32(TG3_CPMU_DRV_STATUS);
2738
2739 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2740 status &= ~(TG3_GPIO_MSG_MASK << shift);
2741 status |= (newstat << shift);
2742
Joe Perches41535772013-02-16 11:20:04 +00002743 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2744 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002745 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2746 else
2747 tw32(TG3_CPMU_DRV_STATUS, status);
2748
2749 return status >> TG3_APE_GPIO_MSG_SHIFT;
2750}
2751
Matt Carlson520b2752011-06-13 13:39:02 +00002752static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2753{
2754 if (!tg3_flag(tp, IS_NIC))
2755 return 0;
2756
Joe Perches41535772013-02-16 11:20:04 +00002757 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2758 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2759 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002760 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2761 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002762
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002763 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2764
2765 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2766 TG3_GRC_LCLCTL_PWRSW_DELAY);
2767
2768 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2769 } else {
2770 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2771 TG3_GRC_LCLCTL_PWRSW_DELAY);
2772 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002773
Matt Carlson520b2752011-06-13 13:39:02 +00002774 return 0;
2775}
2776
2777static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2778{
2779 u32 grc_local_ctrl;
2780
2781 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002782 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2783 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002784 return;
2785
2786 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2787
2788 tw32_wait_f(GRC_LOCAL_CTRL,
2789 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2790 TG3_GRC_LCLCTL_PWRSW_DELAY);
2791
2792 tw32_wait_f(GRC_LOCAL_CTRL,
2793 grc_local_ctrl,
2794 TG3_GRC_LCLCTL_PWRSW_DELAY);
2795
2796 tw32_wait_f(GRC_LOCAL_CTRL,
2797 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2798 TG3_GRC_LCLCTL_PWRSW_DELAY);
2799}
2800
2801static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2802{
2803 if (!tg3_flag(tp, IS_NIC))
2804 return;
2805
Joe Perches41535772013-02-16 11:20:04 +00002806 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2807 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002808 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2809 (GRC_LCLCTRL_GPIO_OE0 |
2810 GRC_LCLCTRL_GPIO_OE1 |
2811 GRC_LCLCTRL_GPIO_OE2 |
2812 GRC_LCLCTRL_GPIO_OUTPUT0 |
2813 GRC_LCLCTRL_GPIO_OUTPUT1),
2814 TG3_GRC_LCLCTL_PWRSW_DELAY);
2815 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2816 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2817 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2818 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2819 GRC_LCLCTRL_GPIO_OE1 |
2820 GRC_LCLCTRL_GPIO_OE2 |
2821 GRC_LCLCTRL_GPIO_OUTPUT0 |
2822 GRC_LCLCTRL_GPIO_OUTPUT1 |
2823 tp->grc_local_ctrl;
2824 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2825 TG3_GRC_LCLCTL_PWRSW_DELAY);
2826
2827 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2828 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2829 TG3_GRC_LCLCTL_PWRSW_DELAY);
2830
2831 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2832 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2833 TG3_GRC_LCLCTL_PWRSW_DELAY);
2834 } else {
2835 u32 no_gpio2;
2836 u32 grc_local_ctrl = 0;
2837
2838 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002839 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002840 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2841 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2842 grc_local_ctrl,
2843 TG3_GRC_LCLCTL_PWRSW_DELAY);
2844 }
2845
2846 /* On 5753 and variants, GPIO2 cannot be used. */
2847 no_gpio2 = tp->nic_sram_data_cfg &
2848 NIC_SRAM_DATA_CFG_NO_GPIO2;
2849
2850 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2851 GRC_LCLCTRL_GPIO_OE1 |
2852 GRC_LCLCTRL_GPIO_OE2 |
2853 GRC_LCLCTRL_GPIO_OUTPUT1 |
2854 GRC_LCLCTRL_GPIO_OUTPUT2;
2855 if (no_gpio2) {
2856 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2857 GRC_LCLCTRL_GPIO_OUTPUT2);
2858 }
2859 tw32_wait_f(GRC_LOCAL_CTRL,
2860 tp->grc_local_ctrl | grc_local_ctrl,
2861 TG3_GRC_LCLCTL_PWRSW_DELAY);
2862
2863 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2864
2865 tw32_wait_f(GRC_LOCAL_CTRL,
2866 tp->grc_local_ctrl | grc_local_ctrl,
2867 TG3_GRC_LCLCTL_PWRSW_DELAY);
2868
2869 if (!no_gpio2) {
2870 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2871 tw32_wait_f(GRC_LOCAL_CTRL,
2872 tp->grc_local_ctrl | grc_local_ctrl,
2873 TG3_GRC_LCLCTL_PWRSW_DELAY);
2874 }
2875 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002876}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002877
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002878static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002879{
2880 u32 msg = 0;
2881
2882 /* Serialize power state transitions */
2883 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2884 return;
2885
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002886 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002887 msg = TG3_GPIO_MSG_NEED_VAUX;
2888
2889 msg = tg3_set_function_status(tp, msg);
2890
2891 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2892 goto done;
2893
2894 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2895 tg3_pwrsrc_switch_to_vaux(tp);
2896 else
2897 tg3_pwrsrc_die_with_vmain(tp);
2898
2899done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002900 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002901}
2902
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002903static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Matt Carlson683644b2011-03-09 16:58:23 +00002905 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Matt Carlson334355a2010-01-20 16:58:10 +00002907 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002908 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return;
2910
Joe Perches41535772013-02-16 11:20:04 +00002911 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2912 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2913 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002914 tg3_frob_aux_power_5717(tp, include_wol ?
2915 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002916 return;
2917 }
2918
2919 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002920 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002922 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002923
Michael Chanbc1c7562006-03-20 17:48:03 -08002924 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002925 if (dev_peer) {
2926 struct tg3 *tp_peer = netdev_priv(dev_peer);
2927
Joe Perches63c3a662011-04-26 08:12:10 +00002928 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002929 return;
2930
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002931 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002932 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002933 need_vaux = true;
2934 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002937 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2938 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002939 need_vaux = true;
2940
Matt Carlson520b2752011-06-13 13:39:02 +00002941 if (need_vaux)
2942 tg3_pwrsrc_switch_to_vaux(tp);
2943 else
2944 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002947static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2948{
2949 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2950 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002951 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002952 if (speed != SPEED_10)
2953 return 1;
2954 } else if (speed == SPEED_10)
2955 return 1;
2956
2957 return 0;
2958}
2959
Matt Carlson0a459aa2008-11-03 16:54:15 -08002960static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002961{
Matt Carlsonce057f02007-11-12 21:08:03 -08002962 u32 val;
2963
Nithin Sujir942d1af2013-04-09 08:48:07 +00002964 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
2965 return;
2966
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002967 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00002968 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08002969 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2970 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2971
2972 sg_dig_ctrl |=
2973 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2974 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2975 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2976 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002977 return;
Michael Chan51297242007-02-13 12:17:57 -08002978 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002979
Joe Perches41535772013-02-16 11:20:04 +00002980 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002981 tg3_bmcr_reset(tp);
2982 val = tr32(GRC_MISC_CFG);
2983 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2984 udelay(40);
2985 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002986 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002987 u32 phytest;
2988 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2989 u32 phy;
2990
2991 tg3_writephy(tp, MII_ADVERTISE, 0);
2992 tg3_writephy(tp, MII_BMCR,
2993 BMCR_ANENABLE | BMCR_ANRESTART);
2994
2995 tg3_writephy(tp, MII_TG3_FET_TEST,
2996 phytest | MII_TG3_FET_SHADOW_EN);
2997 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2998 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2999 tg3_writephy(tp,
3000 MII_TG3_FET_SHDW_AUXMODE4,
3001 phy);
3002 }
3003 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3004 }
3005 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003006 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07003007 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3008 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003009
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003010 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3011 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3012 MII_TG3_AUXCTL_PCTL_VREG_11V;
3013 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003014 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003015
Michael Chan15c3b692006-03-22 01:06:52 -08003016 /* The PHY should not be powered down on some chips because
3017 * of bugs.
3018 */
Joe Perches41535772013-02-16 11:20:04 +00003019 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3020 tg3_asic_rev(tp) == ASIC_REV_5704 ||
3021 (tg3_asic_rev(tp) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00003022 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
Joe Perches41535772013-02-16 11:20:04 +00003023 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00003024 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08003025 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003026
Joe Perches41535772013-02-16 11:20:04 +00003027 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3028 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003029 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3030 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3031 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3032 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3033 }
3034
Michael Chan15c3b692006-03-22 01:06:52 -08003035 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3036}
3037
Matt Carlson3f007892008-11-03 16:51:36 -08003038/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003039static int tg3_nvram_lock(struct tg3 *tp)
3040{
Joe Perches63c3a662011-04-26 08:12:10 +00003041 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003042 int i;
3043
3044 if (tp->nvram_lock_cnt == 0) {
3045 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3046 for (i = 0; i < 8000; i++) {
3047 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3048 break;
3049 udelay(20);
3050 }
3051 if (i == 8000) {
3052 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3053 return -ENODEV;
3054 }
3055 }
3056 tp->nvram_lock_cnt++;
3057 }
3058 return 0;
3059}
3060
3061/* tp->lock is held. */
3062static void tg3_nvram_unlock(struct tg3 *tp)
3063{
Joe Perches63c3a662011-04-26 08:12:10 +00003064 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003065 if (tp->nvram_lock_cnt > 0)
3066 tp->nvram_lock_cnt--;
3067 if (tp->nvram_lock_cnt == 0)
3068 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3069 }
3070}
3071
3072/* tp->lock is held. */
3073static void tg3_enable_nvram_access(struct tg3 *tp)
3074{
Joe Perches63c3a662011-04-26 08:12:10 +00003075 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003076 u32 nvaccess = tr32(NVRAM_ACCESS);
3077
3078 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3079 }
3080}
3081
3082/* tp->lock is held. */
3083static void tg3_disable_nvram_access(struct tg3 *tp)
3084{
Joe Perches63c3a662011-04-26 08:12:10 +00003085 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003086 u32 nvaccess = tr32(NVRAM_ACCESS);
3087
3088 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3089 }
3090}
3091
3092static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3093 u32 offset, u32 *val)
3094{
3095 u32 tmp;
3096 int i;
3097
3098 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3099 return -EINVAL;
3100
3101 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3102 EEPROM_ADDR_DEVID_MASK |
3103 EEPROM_ADDR_READ);
3104 tw32(GRC_EEPROM_ADDR,
3105 tmp |
3106 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3107 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3108 EEPROM_ADDR_ADDR_MASK) |
3109 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3110
3111 for (i = 0; i < 1000; i++) {
3112 tmp = tr32(GRC_EEPROM_ADDR);
3113
3114 if (tmp & EEPROM_ADDR_COMPLETE)
3115 break;
3116 msleep(1);
3117 }
3118 if (!(tmp & EEPROM_ADDR_COMPLETE))
3119 return -EBUSY;
3120
Matt Carlson62cedd12009-04-20 14:52:29 -07003121 tmp = tr32(GRC_EEPROM_DATA);
3122
3123 /*
3124 * The data will always be opposite the native endian
3125 * format. Perform a blind byteswap to compensate.
3126 */
3127 *val = swab32(tmp);
3128
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003129 return 0;
3130}
3131
3132#define NVRAM_CMD_TIMEOUT 10000
3133
3134static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3135{
3136 int i;
3137
3138 tw32(NVRAM_CMD, nvram_cmd);
3139 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3140 udelay(10);
3141 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3142 udelay(10);
3143 break;
3144 }
3145 }
3146
3147 if (i == NVRAM_CMD_TIMEOUT)
3148 return -EBUSY;
3149
3150 return 0;
3151}
3152
3153static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3154{
Joe Perches63c3a662011-04-26 08:12:10 +00003155 if (tg3_flag(tp, NVRAM) &&
3156 tg3_flag(tp, NVRAM_BUFFERED) &&
3157 tg3_flag(tp, FLASH) &&
3158 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003159 (tp->nvram_jedecnum == JEDEC_ATMEL))
3160
3161 addr = ((addr / tp->nvram_pagesize) <<
3162 ATMEL_AT45DB0X1B_PAGE_POS) +
3163 (addr % tp->nvram_pagesize);
3164
3165 return addr;
3166}
3167
3168static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3169{
Joe Perches63c3a662011-04-26 08:12:10 +00003170 if (tg3_flag(tp, NVRAM) &&
3171 tg3_flag(tp, NVRAM_BUFFERED) &&
3172 tg3_flag(tp, FLASH) &&
3173 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003174 (tp->nvram_jedecnum == JEDEC_ATMEL))
3175
3176 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3177 tp->nvram_pagesize) +
3178 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3179
3180 return addr;
3181}
3182
Matt Carlsone4f34112009-02-25 14:25:00 +00003183/* NOTE: Data read in from NVRAM is byteswapped according to
3184 * the byteswapping settings for all other register accesses.
3185 * tg3 devices are BE devices, so on a BE machine, the data
3186 * returned will be exactly as it is seen in NVRAM. On a LE
3187 * machine, the 32-bit value will be byteswapped.
3188 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003189static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3190{
3191 int ret;
3192
Joe Perches63c3a662011-04-26 08:12:10 +00003193 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003194 return tg3_nvram_read_using_eeprom(tp, offset, val);
3195
3196 offset = tg3_nvram_phys_addr(tp, offset);
3197
3198 if (offset > NVRAM_ADDR_MSK)
3199 return -EINVAL;
3200
3201 ret = tg3_nvram_lock(tp);
3202 if (ret)
3203 return ret;
3204
3205 tg3_enable_nvram_access(tp);
3206
3207 tw32(NVRAM_ADDR, offset);
3208 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3209 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3210
3211 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003212 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003213
3214 tg3_disable_nvram_access(tp);
3215
3216 tg3_nvram_unlock(tp);
3217
3218 return ret;
3219}
3220
Matt Carlsona9dc5292009-02-25 14:25:30 +00003221/* Ensures NVRAM data is in bytestream format. */
3222static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003223{
3224 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003225 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003226 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003227 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003228 return res;
3229}
3230
Matt Carlsondbe9b922012-02-13 10:20:09 +00003231static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3232 u32 offset, u32 len, u8 *buf)
3233{
3234 int i, j, rc = 0;
3235 u32 val;
3236
3237 for (i = 0; i < len; i += 4) {
3238 u32 addr;
3239 __be32 data;
3240
3241 addr = offset + i;
3242
3243 memcpy(&data, buf + i, 4);
3244
3245 /*
3246 * The SEEPROM interface expects the data to always be opposite
3247 * the native endian format. We accomplish this by reversing
3248 * all the operations that would have been performed on the
3249 * data from a call to tg3_nvram_read_be32().
3250 */
3251 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3252
3253 val = tr32(GRC_EEPROM_ADDR);
3254 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3255
3256 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3257 EEPROM_ADDR_READ);
3258 tw32(GRC_EEPROM_ADDR, val |
3259 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3260 (addr & EEPROM_ADDR_ADDR_MASK) |
3261 EEPROM_ADDR_START |
3262 EEPROM_ADDR_WRITE);
3263
3264 for (j = 0; j < 1000; j++) {
3265 val = tr32(GRC_EEPROM_ADDR);
3266
3267 if (val & EEPROM_ADDR_COMPLETE)
3268 break;
3269 msleep(1);
3270 }
3271 if (!(val & EEPROM_ADDR_COMPLETE)) {
3272 rc = -EBUSY;
3273 break;
3274 }
3275 }
3276
3277 return rc;
3278}
3279
3280/* offset and length are dword aligned */
3281static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3282 u8 *buf)
3283{
3284 int ret = 0;
3285 u32 pagesize = tp->nvram_pagesize;
3286 u32 pagemask = pagesize - 1;
3287 u32 nvram_cmd;
3288 u8 *tmp;
3289
3290 tmp = kmalloc(pagesize, GFP_KERNEL);
3291 if (tmp == NULL)
3292 return -ENOMEM;
3293
3294 while (len) {
3295 int j;
3296 u32 phy_addr, page_off, size;
3297
3298 phy_addr = offset & ~pagemask;
3299
3300 for (j = 0; j < pagesize; j += 4) {
3301 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3302 (__be32 *) (tmp + j));
3303 if (ret)
3304 break;
3305 }
3306 if (ret)
3307 break;
3308
3309 page_off = offset & pagemask;
3310 size = pagesize;
3311 if (len < size)
3312 size = len;
3313
3314 len -= size;
3315
3316 memcpy(tmp + page_off, buf, size);
3317
3318 offset = offset + (pagesize - page_off);
3319
3320 tg3_enable_nvram_access(tp);
3321
3322 /*
3323 * Before we can erase the flash page, we need
3324 * to issue a special "write enable" command.
3325 */
3326 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3327
3328 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3329 break;
3330
3331 /* Erase the target page */
3332 tw32(NVRAM_ADDR, phy_addr);
3333
3334 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3335 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3336
3337 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3338 break;
3339
3340 /* Issue another write enable to start the write. */
3341 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3342
3343 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3344 break;
3345
3346 for (j = 0; j < pagesize; j += 4) {
3347 __be32 data;
3348
3349 data = *((__be32 *) (tmp + j));
3350
3351 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3352
3353 tw32(NVRAM_ADDR, phy_addr + j);
3354
3355 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3356 NVRAM_CMD_WR;
3357
3358 if (j == 0)
3359 nvram_cmd |= NVRAM_CMD_FIRST;
3360 else if (j == (pagesize - 4))
3361 nvram_cmd |= NVRAM_CMD_LAST;
3362
3363 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3364 if (ret)
3365 break;
3366 }
3367 if (ret)
3368 break;
3369 }
3370
3371 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3372 tg3_nvram_exec_cmd(tp, nvram_cmd);
3373
3374 kfree(tmp);
3375
3376 return ret;
3377}
3378
3379/* offset and length are dword aligned */
3380static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3381 u8 *buf)
3382{
3383 int i, ret = 0;
3384
3385 for (i = 0; i < len; i += 4, offset += 4) {
3386 u32 page_off, phy_addr, nvram_cmd;
3387 __be32 data;
3388
3389 memcpy(&data, buf + i, 4);
3390 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3391
3392 page_off = offset % tp->nvram_pagesize;
3393
3394 phy_addr = tg3_nvram_phys_addr(tp, offset);
3395
Matt Carlsondbe9b922012-02-13 10:20:09 +00003396 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3397
3398 if (page_off == 0 || i == 0)
3399 nvram_cmd |= NVRAM_CMD_FIRST;
3400 if (page_off == (tp->nvram_pagesize - 4))
3401 nvram_cmd |= NVRAM_CMD_LAST;
3402
3403 if (i == (len - 4))
3404 nvram_cmd |= NVRAM_CMD_LAST;
3405
Matt Carlson42278222012-02-13 15:20:11 +00003406 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3407 !tg3_flag(tp, FLASH) ||
3408 !tg3_flag(tp, 57765_PLUS))
3409 tw32(NVRAM_ADDR, phy_addr);
3410
Joe Perches41535772013-02-16 11:20:04 +00003411 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003412 !tg3_flag(tp, 5755_PLUS) &&
3413 (tp->nvram_jedecnum == JEDEC_ST) &&
3414 (nvram_cmd & NVRAM_CMD_FIRST)) {
3415 u32 cmd;
3416
3417 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3418 ret = tg3_nvram_exec_cmd(tp, cmd);
3419 if (ret)
3420 break;
3421 }
3422 if (!tg3_flag(tp, FLASH)) {
3423 /* We always do complete word writes to eeprom. */
3424 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3425 }
3426
3427 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3428 if (ret)
3429 break;
3430 }
3431 return ret;
3432}
3433
3434/* offset and length are dword aligned */
3435static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3436{
3437 int ret;
3438
3439 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3440 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3441 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3442 udelay(40);
3443 }
3444
3445 if (!tg3_flag(tp, NVRAM)) {
3446 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3447 } else {
3448 u32 grc_mode;
3449
3450 ret = tg3_nvram_lock(tp);
3451 if (ret)
3452 return ret;
3453
3454 tg3_enable_nvram_access(tp);
3455 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3456 tw32(NVRAM_WRITE1, 0x406);
3457
3458 grc_mode = tr32(GRC_MODE);
3459 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3460
3461 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3462 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3463 buf);
3464 } else {
3465 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3466 buf);
3467 }
3468
3469 grc_mode = tr32(GRC_MODE);
3470 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3471
3472 tg3_disable_nvram_access(tp);
3473 tg3_nvram_unlock(tp);
3474 }
3475
3476 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3477 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3478 udelay(40);
3479 }
3480
3481 return ret;
3482}
3483
Matt Carlson997b4f12011-08-31 11:44:53 +00003484#define RX_CPU_SCRATCH_BASE 0x30000
3485#define RX_CPU_SCRATCH_SIZE 0x04000
3486#define TX_CPU_SCRATCH_BASE 0x34000
3487#define TX_CPU_SCRATCH_SIZE 0x04000
3488
3489/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003490static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003491{
3492 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003493 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003494
Nithin Sujir837c45b2013-03-06 17:02:30 +00003495 for (i = 0; i < iters; i++) {
3496 tw32(cpu_base + CPU_STATE, 0xffffffff);
3497 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3498 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3499 break;
3500 }
3501
3502 return (i == iters) ? -EBUSY : 0;
3503}
3504
3505/* tp->lock is held. */
3506static int tg3_rxcpu_pause(struct tg3 *tp)
3507{
3508 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3509
3510 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3511 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3512 udelay(10);
3513
3514 return rc;
3515}
3516
3517/* tp->lock is held. */
3518static int tg3_txcpu_pause(struct tg3 *tp)
3519{
3520 return tg3_pause_cpu(tp, TX_CPU_BASE);
3521}
3522
3523/* tp->lock is held. */
3524static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3525{
3526 tw32(cpu_base + CPU_STATE, 0xffffffff);
3527 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3528}
3529
3530/* tp->lock is held. */
3531static void tg3_rxcpu_resume(struct tg3 *tp)
3532{
3533 tg3_resume_cpu(tp, RX_CPU_BASE);
3534}
3535
3536/* tp->lock is held. */
3537static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3538{
3539 int rc;
3540
3541 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003542
Joe Perches41535772013-02-16 11:20:04 +00003543 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003544 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3545
3546 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3547 return 0;
3548 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003549 if (cpu_base == RX_CPU_BASE) {
3550 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003551 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003552 /*
3553 * There is only an Rx CPU for the 5750 derivative in the
3554 * BCM4785.
3555 */
3556 if (tg3_flag(tp, IS_SSB_CORE))
3557 return 0;
3558
Nithin Sujir837c45b2013-03-06 17:02:30 +00003559 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003560 }
3561
Nithin Sujir837c45b2013-03-06 17:02:30 +00003562 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003563 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003564 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003565 return -ENODEV;
3566 }
3567
3568 /* Clear firmware's nvram arbitration. */
3569 if (tg3_flag(tp, NVRAM))
3570 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3571 return 0;
3572}
3573
Nithin Sujir31f11a92013-03-06 17:02:33 +00003574static int tg3_fw_data_len(struct tg3 *tp,
3575 const struct tg3_firmware_hdr *fw_hdr)
3576{
3577 int fw_len;
3578
3579 /* Non fragmented firmware have one firmware header followed by a
3580 * contiguous chunk of data to be written. The length field in that
3581 * header is not the length of data to be written but the complete
3582 * length of the bss. The data length is determined based on
3583 * tp->fw->size minus headers.
3584 *
3585 * Fragmented firmware have a main header followed by multiple
3586 * fragments. Each fragment is identical to non fragmented firmware
3587 * with a firmware header followed by a contiguous chunk of data. In
3588 * the main header, the length field is unused and set to 0xffffffff.
3589 * In each fragment header the length is the entire size of that
3590 * fragment i.e. fragment data + header length. Data length is
3591 * therefore length field in the header minus TG3_FW_HDR_LEN.
3592 */
3593 if (tp->fw_len == 0xffffffff)
3594 fw_len = be32_to_cpu(fw_hdr->len);
3595 else
3596 fw_len = tp->fw->size;
3597
3598 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3599}
3600
Matt Carlson997b4f12011-08-31 11:44:53 +00003601/* tp->lock is held. */
3602static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3603 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003604 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003605{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003606 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003607 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003608 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003609
3610 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3611 netdev_err(tp->dev,
3612 "%s: Trying to load TX cpu firmware which is 5705\n",
3613 __func__);
3614 return -EINVAL;
3615 }
3616
Nithin Sujirc4dab502013-03-06 17:02:34 +00003617 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003618 write_op = tg3_write_mem;
3619 else
3620 write_op = tg3_write_indirect_reg32;
3621
Nithin Sujirc4dab502013-03-06 17:02:34 +00003622 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3623 /* It is possible that bootcode is still loading at this point.
3624 * Get the nvram lock first before halting the cpu.
3625 */
3626 int lock_err = tg3_nvram_lock(tp);
3627 err = tg3_halt_cpu(tp, cpu_base);
3628 if (!lock_err)
3629 tg3_nvram_unlock(tp);
3630 if (err)
3631 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003632
Nithin Sujirc4dab502013-03-06 17:02:34 +00003633 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3634 write_op(tp, cpu_scratch_base + i, 0);
3635 tw32(cpu_base + CPU_STATE, 0xffffffff);
3636 tw32(cpu_base + CPU_MODE,
3637 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3638 } else {
3639 /* Subtract additional main header for fragmented firmware and
3640 * advance to the first fragment
3641 */
3642 total_len -= TG3_FW_HDR_LEN;
3643 fw_hdr++;
3644 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003645
Nithin Sujir31f11a92013-03-06 17:02:33 +00003646 do {
3647 u32 *fw_data = (u32 *)(fw_hdr + 1);
3648 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3649 write_op(tp, cpu_scratch_base +
3650 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3651 (i * sizeof(u32)),
3652 be32_to_cpu(fw_data[i]));
3653
3654 total_len -= be32_to_cpu(fw_hdr->len);
3655
3656 /* Advance to next fragment */
3657 fw_hdr = (struct tg3_firmware_hdr *)
3658 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3659 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003660
3661 err = 0;
3662
3663out:
3664 return err;
3665}
3666
3667/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003668static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3669{
3670 int i;
3671 const int iters = 5;
3672
3673 tw32(cpu_base + CPU_STATE, 0xffffffff);
3674 tw32_f(cpu_base + CPU_PC, pc);
3675
3676 for (i = 0; i < iters; i++) {
3677 if (tr32(cpu_base + CPU_PC) == pc)
3678 break;
3679 tw32(cpu_base + CPU_STATE, 0xffffffff);
3680 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3681 tw32_f(cpu_base + CPU_PC, pc);
3682 udelay(1000);
3683 }
3684
3685 return (i == iters) ? -EBUSY : 0;
3686}
3687
3688/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003689static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3690{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003691 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003692 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003693
Nithin Sujir77997ea2013-03-06 17:02:32 +00003694 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003695
3696 /* Firmware blob starts with version numbers, followed by
3697 start address and length. We are setting complete length.
3698 length = end_address_of_bss - start_address_of_text.
3699 Remainder is the blob to be loaded contiguously
3700 from start address. */
3701
Matt Carlson997b4f12011-08-31 11:44:53 +00003702 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3703 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003704 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003705 if (err)
3706 return err;
3707
3708 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3709 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003710 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003711 if (err)
3712 return err;
3713
3714 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003715 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3716 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003717 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003718 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3719 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003720 tr32(RX_CPU_BASE + CPU_PC),
3721 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003722 return -ENODEV;
3723 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003724
3725 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003726
3727 return 0;
3728}
3729
Nithin Sujirc4dab502013-03-06 17:02:34 +00003730static int tg3_validate_rxcpu_state(struct tg3 *tp)
3731{
3732 const int iters = 1000;
3733 int i;
3734 u32 val;
3735
3736 /* Wait for boot code to complete initialization and enter service
3737 * loop. It is then safe to download service patches
3738 */
3739 for (i = 0; i < iters; i++) {
3740 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3741 break;
3742
3743 udelay(10);
3744 }
3745
3746 if (i == iters) {
3747 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3748 return -EBUSY;
3749 }
3750
3751 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3752 if (val & 0xff) {
3753 netdev_warn(tp->dev,
3754 "Other patches exist. Not downloading EEE patch\n");
3755 return -EEXIST;
3756 }
3757
3758 return 0;
3759}
3760
3761/* tp->lock is held. */
3762static void tg3_load_57766_firmware(struct tg3 *tp)
3763{
3764 struct tg3_firmware_hdr *fw_hdr;
3765
3766 if (!tg3_flag(tp, NO_NVRAM))
3767 return;
3768
3769 if (tg3_validate_rxcpu_state(tp))
3770 return;
3771
3772 if (!tp->fw)
3773 return;
3774
3775 /* This firmware blob has a different format than older firmware
3776 * releases as given below. The main difference is we have fragmented
3777 * data to be written to non-contiguous locations.
3778 *
3779 * In the beginning we have a firmware header identical to other
3780 * firmware which consists of version, base addr and length. The length
3781 * here is unused and set to 0xffffffff.
3782 *
3783 * This is followed by a series of firmware fragments which are
3784 * individually identical to previous firmware. i.e. they have the
3785 * firmware header and followed by data for that fragment. The version
3786 * field of the individual fragment header is unused.
3787 */
3788
3789 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3790 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3791 return;
3792
3793 if (tg3_rxcpu_pause(tp))
3794 return;
3795
3796 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3797 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3798
3799 tg3_rxcpu_resume(tp);
3800}
3801
Matt Carlson997b4f12011-08-31 11:44:53 +00003802/* tp->lock is held. */
3803static int tg3_load_tso_firmware(struct tg3 *tp)
3804{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003805 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003806 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003807 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003808
Matt Carlson1caf13e2013-03-06 17:02:29 +00003809 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003810 return 0;
3811
Nithin Sujir77997ea2013-03-06 17:02:32 +00003812 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003813
3814 /* Firmware blob starts with version numbers, followed by
3815 start address and length. We are setting complete length.
3816 length = end_address_of_bss - start_address_of_text.
3817 Remainder is the blob to be loaded contiguously
3818 from start address. */
3819
Matt Carlson997b4f12011-08-31 11:44:53 +00003820 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003821
Joe Perches41535772013-02-16 11:20:04 +00003822 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003823 cpu_base = RX_CPU_BASE;
3824 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3825 } else {
3826 cpu_base = TX_CPU_BASE;
3827 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3828 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3829 }
3830
3831 err = tg3_load_firmware_cpu(tp, cpu_base,
3832 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003833 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003834 if (err)
3835 return err;
3836
3837 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003838 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3839 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003840 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003841 netdev_err(tp->dev,
3842 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003843 __func__, tr32(cpu_base + CPU_PC),
3844 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003845 return -ENODEV;
3846 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003847
3848 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003849 return 0;
3850}
3851
3852
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003853/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003854static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003855{
3856 u32 addr_high, addr_low;
3857 int i;
3858
3859 addr_high = ((tp->dev->dev_addr[0] << 8) |
3860 tp->dev->dev_addr[1]);
3861 addr_low = ((tp->dev->dev_addr[2] << 24) |
3862 (tp->dev->dev_addr[3] << 16) |
3863 (tp->dev->dev_addr[4] << 8) |
3864 (tp->dev->dev_addr[5] << 0));
3865 for (i = 0; i < 4; i++) {
3866 if (i == 1 && skip_mac_1)
3867 continue;
3868 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3869 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3870 }
3871
Joe Perches41535772013-02-16 11:20:04 +00003872 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3873 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003874 for (i = 0; i < 12; i++) {
3875 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3876 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3877 }
3878 }
3879
3880 addr_high = (tp->dev->dev_addr[0] +
3881 tp->dev->dev_addr[1] +
3882 tp->dev->dev_addr[2] +
3883 tp->dev->dev_addr[3] +
3884 tp->dev->dev_addr[4] +
3885 tp->dev->dev_addr[5]) &
3886 TX_BACKOFF_SEED_MASK;
3887 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3888}
3889
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003890static void tg3_enable_register_access(struct tg3 *tp)
3891{
3892 /*
3893 * Make sure register accesses (indirect or otherwise) will function
3894 * correctly.
3895 */
3896 pci_write_config_dword(tp->pdev,
3897 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3898}
3899
3900static int tg3_power_up(struct tg3 *tp)
3901{
Matt Carlsonbed98292011-07-13 09:27:29 +00003902 int err;
3903
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003904 tg3_enable_register_access(tp);
3905
Matt Carlsonbed98292011-07-13 09:27:29 +00003906 err = pci_set_power_state(tp->pdev, PCI_D0);
3907 if (!err) {
3908 /* Switch out of Vaux if it is a NIC */
3909 tg3_pwrsrc_switch_to_vmain(tp);
3910 } else {
3911 netdev_err(tp->dev, "Transition to D0 failed\n");
3912 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003913
Matt Carlsonbed98292011-07-13 09:27:29 +00003914 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003915}
3916
Joe Perches953c96e2013-04-09 10:18:14 +00003917static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00003918
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003919static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920{
3921 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003922 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003924 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003925
3926 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003927 if (tg3_flag(tp, CLKREQ_BUG))
3928 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3929 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3932 tw32(TG3PCI_MISC_HOST_CTRL,
3933 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3934
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003935 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003936 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003937
Joe Perches63c3a662011-04-26 08:12:10 +00003938 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003939 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003940 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003941 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003942 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003943 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003944
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003945 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003946
Matt Carlson80096062010-08-02 11:26:06 +00003947 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003948
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003949 tp->link_config.speed = phydev->speed;
3950 tp->link_config.duplex = phydev->duplex;
3951 tp->link_config.autoneg = phydev->autoneg;
3952 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003953
3954 advertising = ADVERTISED_TP |
3955 ADVERTISED_Pause |
3956 ADVERTISED_Autoneg |
3957 ADVERTISED_10baseT_Half;
3958
Joe Perches63c3a662011-04-26 08:12:10 +00003959 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3960 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003961 advertising |=
3962 ADVERTISED_100baseT_Half |
3963 ADVERTISED_100baseT_Full |
3964 ADVERTISED_10baseT_Full;
3965 else
3966 advertising |= ADVERTISED_10baseT_Full;
3967 }
3968
3969 phydev->advertising = advertising;
3970
3971 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003972
3973 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003974 if (phyid != PHY_ID_BCMAC131) {
3975 phyid &= PHY_BCM_OUI_MASK;
3976 if (phyid == PHY_BCM_OUI_1 ||
3977 phyid == PHY_BCM_OUI_2 ||
3978 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003979 do_low_power = true;
3980 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003981 }
Matt Carlsondd477002008-05-25 23:45:58 -07003982 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003983 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003984
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003985 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003986 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Matt Carlson2855b9f2012-02-13 15:20:14 +00003988 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00003989 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 }
3991
Joe Perches41535772013-02-16 11:20:04 +00003992 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07003993 u32 val;
3994
3995 val = tr32(GRC_VCPU_EXT_CTRL);
3996 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003997 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003998 int i;
3999 u32 val;
4000
4001 for (i = 0; i < 200; i++) {
4002 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4003 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4004 break;
4005 msleep(1);
4006 }
4007 }
Joe Perches63c3a662011-04-26 08:12:10 +00004008 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004009 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4010 WOL_DRV_STATE_SHUTDOWN |
4011 WOL_DRV_WOL |
4012 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004013
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004014 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 u32 mac_mode;
4016
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004017 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004018 if (do_low_power &&
4019 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4020 tg3_phy_auxctl_write(tp,
4021 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4022 MII_TG3_AUXCTL_PCTL_WOL_EN |
4023 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4024 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004025 udelay(40);
4026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004028 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004029 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004030 else if (tp->phy_flags &
4031 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4032 if (tp->link_config.active_speed == SPEED_1000)
4033 mac_mode = MAC_MODE_PORT_MODE_GMII;
4034 else
4035 mac_mode = MAC_MODE_PORT_MODE_MII;
4036 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004037 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004039 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004040 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004041 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004042 SPEED_100 : SPEED_10;
4043 if (tg3_5700_link_polarity(tp, speed))
4044 mac_mode |= MAC_MODE_LINK_POLARITY;
4045 else
4046 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 } else {
4049 mac_mode = MAC_MODE_PORT_MODE_TBI;
4050 }
4051
Joe Perches63c3a662011-04-26 08:12:10 +00004052 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 tw32(MAC_LED_CTRL, tp->led_ctrl);
4054
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004055 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004056 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4057 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004058 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Joe Perches63c3a662011-04-26 08:12:10 +00004060 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004061 mac_mode |= MAC_MODE_APE_TX_EN |
4062 MAC_MODE_APE_RX_EN |
4063 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 tw32_f(MAC_MODE, mac_mode);
4066 udelay(100);
4067
4068 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4069 udelay(10);
4070 }
4071
Joe Perches63c3a662011-04-26 08:12:10 +00004072 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004073 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4074 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 u32 base_val;
4076
4077 base_val = tp->pci_clock_ctrl;
4078 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4079 CLOCK_CTRL_TXCLK_DISABLE);
4080
Michael Chanb401e9e2005-12-19 16:27:04 -08004081 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4082 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004083 } else if (tg3_flag(tp, 5780_CLASS) ||
4084 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004085 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004086 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004087 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 u32 newbits1, newbits2;
4089
Joe Perches41535772013-02-16 11:20:04 +00004090 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4091 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4093 CLOCK_CTRL_TXCLK_DISABLE |
4094 CLOCK_CTRL_ALTCLK);
4095 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004096 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 newbits1 = CLOCK_CTRL_625_CORE;
4098 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4099 } else {
4100 newbits1 = CLOCK_CTRL_ALTCLK;
4101 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4102 }
4103
Michael Chanb401e9e2005-12-19 16:27:04 -08004104 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4105 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Michael Chanb401e9e2005-12-19 16:27:04 -08004107 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4108 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Joe Perches63c3a662011-04-26 08:12:10 +00004110 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 u32 newbits3;
4112
Joe Perches41535772013-02-16 11:20:04 +00004113 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4114 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4116 CLOCK_CTRL_TXCLK_DISABLE |
4117 CLOCK_CTRL_44MHZ_CORE);
4118 } else {
4119 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4120 }
4121
Michael Chanb401e9e2005-12-19 16:27:04 -08004122 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4123 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
4125 }
4126
Joe Perches63c3a662011-04-26 08:12:10 +00004127 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004128 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004129
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004130 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004133 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004134 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4135 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 u32 val = tr32(0x7d00);
4137
4138 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4139 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004140 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004141 int err;
4142
4143 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004145 if (!err)
4146 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 }
4149
Michael Chanbbadf502006-04-06 21:46:34 -07004150 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return 0;
4153}
4154
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004155static void tg3_power_down(struct tg3 *tp)
4156{
4157 tg3_power_down_prepare(tp);
4158
Joe Perches63c3a662011-04-26 08:12:10 +00004159 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004160 pci_set_power_state(tp->pdev, PCI_D3hot);
4161}
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4164{
4165 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4166 case MII_TG3_AUX_STAT_10HALF:
4167 *speed = SPEED_10;
4168 *duplex = DUPLEX_HALF;
4169 break;
4170
4171 case MII_TG3_AUX_STAT_10FULL:
4172 *speed = SPEED_10;
4173 *duplex = DUPLEX_FULL;
4174 break;
4175
4176 case MII_TG3_AUX_STAT_100HALF:
4177 *speed = SPEED_100;
4178 *duplex = DUPLEX_HALF;
4179 break;
4180
4181 case MII_TG3_AUX_STAT_100FULL:
4182 *speed = SPEED_100;
4183 *duplex = DUPLEX_FULL;
4184 break;
4185
4186 case MII_TG3_AUX_STAT_1000HALF:
4187 *speed = SPEED_1000;
4188 *duplex = DUPLEX_HALF;
4189 break;
4190
4191 case MII_TG3_AUX_STAT_1000FULL:
4192 *speed = SPEED_1000;
4193 *duplex = DUPLEX_FULL;
4194 break;
4195
4196 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004197 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004198 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4199 SPEED_10;
4200 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4201 DUPLEX_HALF;
4202 break;
4203 }
Matt Carlsone7405222012-02-13 15:20:16 +00004204 *speed = SPEED_UNKNOWN;
4205 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
Matt Carlson42b64a42011-05-19 12:12:49 +00004210static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Matt Carlson42b64a42011-05-19 12:12:49 +00004212 int err = 0;
4213 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Matt Carlson42b64a42011-05-19 12:12:49 +00004215 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004216 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004217 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Matt Carlson42b64a42011-05-19 12:12:49 +00004219 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4220 if (err)
4221 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Matt Carlson4f272092011-12-14 11:09:57 +00004223 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4224 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004225
Joe Perches41535772013-02-16 11:20:04 +00004226 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4227 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004228 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004229
Matt Carlson4f272092011-12-14 11:09:57 +00004230 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4231 if (err)
4232 goto done;
4233 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004234
Matt Carlson42b64a42011-05-19 12:12:49 +00004235 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4236 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Matt Carlson42b64a42011-05-19 12:12:49 +00004238 tw32(TG3_CPMU_EEE_MODE,
4239 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004240
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004241 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004242 if (!err) {
4243 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004244
Matt Carlsona6b68da2010-12-06 08:28:52 +00004245 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004246 /* Advertise 100-BaseTX EEE ability */
4247 if (advertise & ADVERTISED_100baseT_Full)
4248 val |= MDIO_AN_EEE_ADV_100TX;
4249 /* Advertise 1000-BaseT EEE ability */
4250 if (advertise & ADVERTISED_1000baseT_Full)
4251 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004252
4253 if (!tp->eee.eee_enabled) {
4254 val = 0;
4255 tp->eee.advertised = 0;
4256 } else {
4257 tp->eee.advertised = advertise &
4258 (ADVERTISED_100baseT_Full |
4259 ADVERTISED_1000baseT_Full);
4260 }
4261
Matt Carlson42b64a42011-05-19 12:12:49 +00004262 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004263 if (err)
4264 val = 0;
4265
Joe Perches41535772013-02-16 11:20:04 +00004266 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004267 case ASIC_REV_5717:
4268 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004269 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004270 case ASIC_REV_5719:
4271 /* If we advertised any eee advertisements above... */
4272 if (val)
4273 val = MII_TG3_DSP_TAP26_ALNOKO |
4274 MII_TG3_DSP_TAP26_RMRXSTO |
4275 MII_TG3_DSP_TAP26_OPCSINPT;
4276 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4277 /* Fall through */
4278 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004279 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004280 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4281 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4282 MII_TG3_DSP_CH34TP2_HIBW01);
4283 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004284
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004285 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004286 if (!err)
4287 err = err2;
4288 }
4289
4290done:
4291 return err;
4292}
4293
4294static void tg3_phy_copper_begin(struct tg3 *tp)
4295{
Matt Carlsond13ba512012-02-22 12:35:19 +00004296 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4297 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4298 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004299
Nithin Sujir942d1af2013-04-09 08:48:07 +00004300 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4301 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004302 adv = ADVERTISED_10baseT_Half |
4303 ADVERTISED_10baseT_Full;
4304 if (tg3_flag(tp, WOL_SPEED_100MB))
4305 adv |= ADVERTISED_100baseT_Half |
4306 ADVERTISED_100baseT_Full;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004307 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK)
4308 adv |= ADVERTISED_1000baseT_Half |
4309 ADVERTISED_1000baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004310
Matt Carlsond13ba512012-02-22 12:35:19 +00004311 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004312 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004313 adv = tp->link_config.advertising;
4314 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4315 adv &= ~(ADVERTISED_1000baseT_Half |
4316 ADVERTISED_1000baseT_Full);
4317
4318 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004319 }
4320
Matt Carlsond13ba512012-02-22 12:35:19 +00004321 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004322
Nithin Sujir942d1af2013-04-09 08:48:07 +00004323 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4324 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4325 /* Normally during power down we want to autonegotiate
4326 * the lowest possible speed for WOL. However, to avoid
4327 * link flap, we leave it untouched.
4328 */
4329 return;
4330 }
4331
Matt Carlsond13ba512012-02-22 12:35:19 +00004332 tg3_writephy(tp, MII_BMCR,
4333 BMCR_ANENABLE | BMCR_ANRESTART);
4334 } else {
4335 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 u32 bmcr, orig_bmcr;
4337
4338 tp->link_config.active_speed = tp->link_config.speed;
4339 tp->link_config.active_duplex = tp->link_config.duplex;
4340
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004341 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4342 /* With autoneg disabled, 5715 only links up when the
4343 * advertisement register has the configured speed
4344 * enabled.
4345 */
4346 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4347 }
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 bmcr = 0;
4350 switch (tp->link_config.speed) {
4351 default:
4352 case SPEED_10:
4353 break;
4354
4355 case SPEED_100:
4356 bmcr |= BMCR_SPEED100;
4357 break;
4358
4359 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004360 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364 if (tp->link_config.duplex == DUPLEX_FULL)
4365 bmcr |= BMCR_FULLDPLX;
4366
4367 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4368 (bmcr != orig_bmcr)) {
4369 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4370 for (i = 0; i < 1500; i++) {
4371 u32 tmp;
4372
4373 udelay(10);
4374 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4375 tg3_readphy(tp, MII_BMSR, &tmp))
4376 continue;
4377 if (!(tmp & BMSR_LSTATUS)) {
4378 udelay(40);
4379 break;
4380 }
4381 }
4382 tg3_writephy(tp, MII_BMCR, bmcr);
4383 udelay(40);
4384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 }
4386}
4387
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004388static int tg3_phy_pull_config(struct tg3 *tp)
4389{
4390 int err;
4391 u32 val;
4392
4393 err = tg3_readphy(tp, MII_BMCR, &val);
4394 if (err)
4395 goto done;
4396
4397 if (!(val & BMCR_ANENABLE)) {
4398 tp->link_config.autoneg = AUTONEG_DISABLE;
4399 tp->link_config.advertising = 0;
4400 tg3_flag_clear(tp, PAUSE_AUTONEG);
4401
4402 err = -EIO;
4403
4404 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4405 case 0:
4406 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4407 goto done;
4408
4409 tp->link_config.speed = SPEED_10;
4410 break;
4411 case BMCR_SPEED100:
4412 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4413 goto done;
4414
4415 tp->link_config.speed = SPEED_100;
4416 break;
4417 case BMCR_SPEED1000:
4418 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4419 tp->link_config.speed = SPEED_1000;
4420 break;
4421 }
4422 /* Fall through */
4423 default:
4424 goto done;
4425 }
4426
4427 if (val & BMCR_FULLDPLX)
4428 tp->link_config.duplex = DUPLEX_FULL;
4429 else
4430 tp->link_config.duplex = DUPLEX_HALF;
4431
4432 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4433
4434 err = 0;
4435 goto done;
4436 }
4437
4438 tp->link_config.autoneg = AUTONEG_ENABLE;
4439 tp->link_config.advertising = ADVERTISED_Autoneg;
4440 tg3_flag_set(tp, PAUSE_AUTONEG);
4441
4442 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4443 u32 adv;
4444
4445 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4446 if (err)
4447 goto done;
4448
4449 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4450 tp->link_config.advertising |= adv | ADVERTISED_TP;
4451
4452 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4453 } else {
4454 tp->link_config.advertising |= ADVERTISED_FIBRE;
4455 }
4456
4457 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4458 u32 adv;
4459
4460 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4461 err = tg3_readphy(tp, MII_CTRL1000, &val);
4462 if (err)
4463 goto done;
4464
4465 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4466 } else {
4467 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4468 if (err)
4469 goto done;
4470
4471 adv = tg3_decode_flowctrl_1000X(val);
4472 tp->link_config.flowctrl = adv;
4473
4474 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4475 adv = mii_adv_to_ethtool_adv_x(val);
4476 }
4477
4478 tp->link_config.advertising |= adv;
4479 }
4480
4481done:
4482 return err;
4483}
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485static int tg3_init_5401phy_dsp(struct tg3 *tp)
4486{
4487 int err;
4488
4489 /* Turn off tap power management. */
4490 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004491 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004493 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4494 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4495 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4496 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4497 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
4499 udelay(40);
4500
4501 return err;
4502}
4503
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004504static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4505{
4506 u32 val;
4507 u32 tgtadv = 0;
4508 u32 advertising = tp->link_config.advertising;
4509
4510 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4511 return true;
4512
4513 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
4514 return false;
4515
4516 val &= (MDIO_AN_EEE_ADV_100TX | MDIO_AN_EEE_ADV_1000T);
4517
4518
4519 if (advertising & ADVERTISED_100baseT_Full)
4520 tgtadv |= MDIO_AN_EEE_ADV_100TX;
4521 if (advertising & ADVERTISED_1000baseT_Full)
4522 tgtadv |= MDIO_AN_EEE_ADV_1000T;
4523
4524 if (val != tgtadv)
4525 return false;
4526
4527 return true;
4528}
4529
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004530static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004532 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004533
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004534 advertising = tp->link_config.advertising;
4535 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004537 advmsk = ADVERTISE_ALL;
4538 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004539 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004540 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004543 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4544 return false;
4545
4546 if ((*lcladv & advmsk) != tgtadv)
4547 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004548
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004549 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 u32 tg3_ctrl;
4551
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004552 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004553
Matt Carlson221c5632011-06-13 13:39:01 +00004554 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004555 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Matt Carlson3198e072012-02-13 15:20:10 +00004557 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004558 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4559 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004560 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4561 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4562 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4563 } else {
4564 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4565 }
4566
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004567 if (tg3_ctrl != tgtadv)
4568 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004570
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004571 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004572}
4573
Matt Carlson859edb22011-12-08 14:40:16 +00004574static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4575{
4576 u32 lpeth = 0;
4577
4578 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4579 u32 val;
4580
4581 if (tg3_readphy(tp, MII_STAT1000, &val))
4582 return false;
4583
4584 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4585 }
4586
4587 if (tg3_readphy(tp, MII_LPA, rmtadv))
4588 return false;
4589
4590 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4591 tp->link_config.rmt_adv = lpeth;
4592
4593 return true;
4594}
4595
Joe Perches953c96e2013-04-09 10:18:14 +00004596static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004597{
4598 if (curr_link_up != tp->link_up) {
4599 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004600 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004601 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004602 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004603 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4604 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4605 }
4606
4607 tg3_link_report(tp);
4608 return true;
4609 }
4610
4611 return false;
4612}
4613
Michael Chan3310e242013-04-09 08:48:05 +00004614static void tg3_clear_mac_status(struct tg3 *tp)
4615{
4616 tw32(MAC_EVENT, 0);
4617
4618 tw32_f(MAC_STATUS,
4619 MAC_STATUS_SYNC_CHANGED |
4620 MAC_STATUS_CFG_CHANGED |
4621 MAC_STATUS_MI_COMPLETION |
4622 MAC_STATUS_LNKSTATE_CHANGED);
4623 udelay(40);
4624}
4625
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004626static void tg3_setup_eee(struct tg3 *tp)
4627{
4628 u32 val;
4629
4630 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4631 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4632 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4633 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4634
4635 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4636
4637 tw32_f(TG3_CPMU_EEE_CTRL,
4638 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4639
4640 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4641 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4642 TG3_CPMU_EEEMD_LPI_IN_RX |
4643 TG3_CPMU_EEEMD_EEE_ENABLE;
4644
4645 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4646 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4647
4648 if (tg3_flag(tp, ENABLE_APE))
4649 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4650
4651 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4652
4653 tw32_f(TG3_CPMU_EEE_DBTMR1,
4654 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4655 (tp->eee.tx_lpi_timer & 0xffff));
4656
4657 tw32_f(TG3_CPMU_EEE_DBTMR2,
4658 TG3_CPMU_DBTMR2_APE_TX_2047US |
4659 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4660}
4661
Joe Perches953c96e2013-04-09 10:18:14 +00004662static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
Joe Perches953c96e2013-04-09 10:18:14 +00004664 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004665 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004666 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 u16 current_speed;
4668 u8 current_duplex;
4669 int i, err;
4670
Michael Chan3310e242013-04-09 08:48:05 +00004671 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Matt Carlson8ef21422008-05-02 16:47:53 -07004673 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4674 tw32_f(MAC_MI_MODE,
4675 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4676 udelay(80);
4677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004679 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 /* Some third-party PHYs need to be reset on link going
4682 * down.
4683 */
Joe Perches41535772013-02-16 11:20:04 +00004684 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4685 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4686 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004687 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 tg3_readphy(tp, MII_BMSR, &bmsr);
4689 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4690 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004691 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 }
4693 if (force_reset)
4694 tg3_phy_reset(tp);
4695
Matt Carlson79eb6902010-02-17 15:17:03 +00004696 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 tg3_readphy(tp, MII_BMSR, &bmsr);
4698 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004699 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 bmsr = 0;
4701
4702 if (!(bmsr & BMSR_LSTATUS)) {
4703 err = tg3_init_5401phy_dsp(tp);
4704 if (err)
4705 return err;
4706
4707 tg3_readphy(tp, MII_BMSR, &bmsr);
4708 for (i = 0; i < 1000; i++) {
4709 udelay(10);
4710 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4711 (bmsr & BMSR_LSTATUS)) {
4712 udelay(40);
4713 break;
4714 }
4715 }
4716
Matt Carlson79eb6902010-02-17 15:17:03 +00004717 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4718 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 !(bmsr & BMSR_LSTATUS) &&
4720 tp->link_config.active_speed == SPEED_1000) {
4721 err = tg3_phy_reset(tp);
4722 if (!err)
4723 err = tg3_init_5401phy_dsp(tp);
4724 if (err)
4725 return err;
4726 }
4727 }
Joe Perches41535772013-02-16 11:20:04 +00004728 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4729 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 /* 5701 {A0,B0} CRC bug workaround */
4731 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004732 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4733 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4734 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
4736
4737 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004738 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4739 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004741 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004743 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4745
Joe Perches41535772013-02-16 11:20:04 +00004746 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4747 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4749 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4750 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4751 else
4752 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4753 }
4754
Joe Perches953c96e2013-04-09 10:18:14 +00004755 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004756 current_speed = SPEED_UNKNOWN;
4757 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004758 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004759 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004761 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004762 err = tg3_phy_auxctl_read(tp,
4763 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4764 &val);
4765 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004766 tg3_phy_auxctl_write(tp,
4767 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4768 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 goto relink;
4770 }
4771 }
4772
4773 bmsr = 0;
4774 for (i = 0; i < 100; i++) {
4775 tg3_readphy(tp, MII_BMSR, &bmsr);
4776 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4777 (bmsr & BMSR_LSTATUS))
4778 break;
4779 udelay(40);
4780 }
4781
4782 if (bmsr & BMSR_LSTATUS) {
4783 u32 aux_stat, bmcr;
4784
4785 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4786 for (i = 0; i < 2000; i++) {
4787 udelay(10);
4788 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4789 aux_stat)
4790 break;
4791 }
4792
4793 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4794 &current_speed,
4795 &current_duplex);
4796
4797 bmcr = 0;
4798 for (i = 0; i < 200; i++) {
4799 tg3_readphy(tp, MII_BMCR, &bmcr);
4800 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4801 continue;
4802 if (bmcr && bmcr != 0x7fff)
4803 break;
4804 udelay(10);
4805 }
4806
Matt Carlsonef167e22007-12-20 20:10:01 -08004807 lcl_adv = 0;
4808 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Matt Carlsonef167e22007-12-20 20:10:01 -08004810 tp->link_config.active_speed = current_speed;
4811 tp->link_config.active_duplex = current_duplex;
4812
4813 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004814 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4815
Matt Carlsonef167e22007-12-20 20:10:01 -08004816 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004817 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004818 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004819 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004820 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004821
4822 /* EEE settings changes take effect only after a phy
4823 * reset. If we have skipped a reset due to Link Flap
4824 * Avoidance being enabled, do it now.
4825 */
4826 if (!eee_config_ok &&
4827 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
4828 !force_reset)
4829 tg3_phy_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 } else {
4831 if (!(bmcr & BMCR_ANENABLE) &&
4832 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004833 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004834 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 }
4836 }
4837
Joe Perches953c96e2013-04-09 10:18:14 +00004838 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004839 tp->link_config.active_duplex == DUPLEX_FULL) {
4840 u32 reg, bit;
4841
4842 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4843 reg = MII_TG3_FET_GEN_STAT;
4844 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4845 } else {
4846 reg = MII_TG3_EXT_STAT;
4847 bit = MII_TG3_EXT_STAT_MDIX;
4848 }
4849
4850 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4851 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4852
Matt Carlsonef167e22007-12-20 20:10:01 -08004853 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004858 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 tg3_phy_copper_begin(tp);
4860
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004861 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004862 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004863 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4864 current_speed = SPEED_1000;
4865 current_duplex = DUPLEX_FULL;
4866 tp->link_config.active_speed = current_speed;
4867 tp->link_config.active_duplex = current_duplex;
4868 }
4869
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004870 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004871 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4872 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004873 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 }
4875
4876 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004877 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 if (tp->link_config.active_speed == SPEED_100 ||
4879 tp->link_config.active_speed == SPEED_10)
4880 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4881 else
4882 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004883 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004884 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4885 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4887
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004888 /* In order for the 5750 core in BCM4785 chip to work properly
4889 * in RGMII mode, the Led Control Register must be set up.
4890 */
4891 if (tg3_flag(tp, RGMII_MODE)) {
4892 u32 led_ctrl = tr32(MAC_LED_CTRL);
4893 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4894
4895 if (tp->link_config.active_speed == SPEED_10)
4896 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4897 else if (tp->link_config.active_speed == SPEED_100)
4898 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4899 LED_CTRL_100MBPS_ON);
4900 else if (tp->link_config.active_speed == SPEED_1000)
4901 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4902 LED_CTRL_1000MBPS_ON);
4903
4904 tw32(MAC_LED_CTRL, led_ctrl);
4905 udelay(40);
4906 }
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4909 if (tp->link_config.active_duplex == DUPLEX_HALF)
4910 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4911
Joe Perches41535772013-02-16 11:20:04 +00004912 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00004913 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004914 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004916 else
4917 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 }
4919
4920 /* ??? Without this setting Netgear GA302T PHY does not
4921 * ??? send/receive packets...
4922 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004923 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004924 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4926 tw32_f(MAC_MI_MODE, tp->mi_mode);
4927 udelay(80);
4928 }
4929
4930 tw32_f(MAC_MODE, tp->mac_mode);
4931 udelay(40);
4932
Matt Carlson52b02d02010-10-14 10:37:41 +00004933 tg3_phy_eee_adjust(tp, current_link_up);
4934
Joe Perches63c3a662011-04-26 08:12:10 +00004935 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 /* Polled via timer. */
4937 tw32_f(MAC_EVENT, 0);
4938 } else {
4939 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4940 }
4941 udelay(40);
4942
Joe Perches41535772013-02-16 11:20:04 +00004943 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00004944 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004946 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 udelay(120);
4948 tw32_f(MAC_STATUS,
4949 (MAC_STATUS_SYNC_CHANGED |
4950 MAC_STATUS_CFG_CHANGED));
4951 udelay(40);
4952 tg3_write_mem(tp,
4953 NIC_SRAM_FIRMWARE_MBOX,
4954 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4955 }
4956
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004957 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004958 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004959 if (tp->link_config.active_speed == SPEED_100 ||
4960 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004961 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4962 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004963 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004964 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4965 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004966 }
4967
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004968 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 return 0;
4971}
4972
4973struct tg3_fiber_aneginfo {
4974 int state;
4975#define ANEG_STATE_UNKNOWN 0
4976#define ANEG_STATE_AN_ENABLE 1
4977#define ANEG_STATE_RESTART_INIT 2
4978#define ANEG_STATE_RESTART 3
4979#define ANEG_STATE_DISABLE_LINK_OK 4
4980#define ANEG_STATE_ABILITY_DETECT_INIT 5
4981#define ANEG_STATE_ABILITY_DETECT 6
4982#define ANEG_STATE_ACK_DETECT_INIT 7
4983#define ANEG_STATE_ACK_DETECT 8
4984#define ANEG_STATE_COMPLETE_ACK_INIT 9
4985#define ANEG_STATE_COMPLETE_ACK 10
4986#define ANEG_STATE_IDLE_DETECT_INIT 11
4987#define ANEG_STATE_IDLE_DETECT 12
4988#define ANEG_STATE_LINK_OK 13
4989#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4990#define ANEG_STATE_NEXT_PAGE_WAIT 15
4991
4992 u32 flags;
4993#define MR_AN_ENABLE 0x00000001
4994#define MR_RESTART_AN 0x00000002
4995#define MR_AN_COMPLETE 0x00000004
4996#define MR_PAGE_RX 0x00000008
4997#define MR_NP_LOADED 0x00000010
4998#define MR_TOGGLE_TX 0x00000020
4999#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5000#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5001#define MR_LP_ADV_SYM_PAUSE 0x00000100
5002#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5003#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5004#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5005#define MR_LP_ADV_NEXT_PAGE 0x00001000
5006#define MR_TOGGLE_RX 0x00002000
5007#define MR_NP_RX 0x00004000
5008
5009#define MR_LINK_OK 0x80000000
5010
5011 unsigned long link_time, cur_time;
5012
5013 u32 ability_match_cfg;
5014 int ability_match_count;
5015
5016 char ability_match, idle_match, ack_match;
5017
5018 u32 txconfig, rxconfig;
5019#define ANEG_CFG_NP 0x00000080
5020#define ANEG_CFG_ACK 0x00000040
5021#define ANEG_CFG_RF2 0x00000020
5022#define ANEG_CFG_RF1 0x00000010
5023#define ANEG_CFG_PS2 0x00000001
5024#define ANEG_CFG_PS1 0x00008000
5025#define ANEG_CFG_HD 0x00004000
5026#define ANEG_CFG_FD 0x00002000
5027#define ANEG_CFG_INVAL 0x00001f06
5028
5029};
5030#define ANEG_OK 0
5031#define ANEG_DONE 1
5032#define ANEG_TIMER_ENAB 2
5033#define ANEG_FAILED -1
5034
5035#define ANEG_STATE_SETTLE_TIME 10000
5036
5037static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5038 struct tg3_fiber_aneginfo *ap)
5039{
Matt Carlson5be73b42007-12-20 20:09:29 -08005040 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 unsigned long delta;
5042 u32 rx_cfg_reg;
5043 int ret;
5044
5045 if (ap->state == ANEG_STATE_UNKNOWN) {
5046 ap->rxconfig = 0;
5047 ap->link_time = 0;
5048 ap->cur_time = 0;
5049 ap->ability_match_cfg = 0;
5050 ap->ability_match_count = 0;
5051 ap->ability_match = 0;
5052 ap->idle_match = 0;
5053 ap->ack_match = 0;
5054 }
5055 ap->cur_time++;
5056
5057 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5058 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5059
5060 if (rx_cfg_reg != ap->ability_match_cfg) {
5061 ap->ability_match_cfg = rx_cfg_reg;
5062 ap->ability_match = 0;
5063 ap->ability_match_count = 0;
5064 } else {
5065 if (++ap->ability_match_count > 1) {
5066 ap->ability_match = 1;
5067 ap->ability_match_cfg = rx_cfg_reg;
5068 }
5069 }
5070 if (rx_cfg_reg & ANEG_CFG_ACK)
5071 ap->ack_match = 1;
5072 else
5073 ap->ack_match = 0;
5074
5075 ap->idle_match = 0;
5076 } else {
5077 ap->idle_match = 1;
5078 ap->ability_match_cfg = 0;
5079 ap->ability_match_count = 0;
5080 ap->ability_match = 0;
5081 ap->ack_match = 0;
5082
5083 rx_cfg_reg = 0;
5084 }
5085
5086 ap->rxconfig = rx_cfg_reg;
5087 ret = ANEG_OK;
5088
Matt Carlson33f401a2010-04-05 10:19:27 +00005089 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 case ANEG_STATE_UNKNOWN:
5091 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5092 ap->state = ANEG_STATE_AN_ENABLE;
5093
5094 /* fallthru */
5095 case ANEG_STATE_AN_ENABLE:
5096 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5097 if (ap->flags & MR_AN_ENABLE) {
5098 ap->link_time = 0;
5099 ap->cur_time = 0;
5100 ap->ability_match_cfg = 0;
5101 ap->ability_match_count = 0;
5102 ap->ability_match = 0;
5103 ap->idle_match = 0;
5104 ap->ack_match = 0;
5105
5106 ap->state = ANEG_STATE_RESTART_INIT;
5107 } else {
5108 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5109 }
5110 break;
5111
5112 case ANEG_STATE_RESTART_INIT:
5113 ap->link_time = ap->cur_time;
5114 ap->flags &= ~(MR_NP_LOADED);
5115 ap->txconfig = 0;
5116 tw32(MAC_TX_AUTO_NEG, 0);
5117 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5118 tw32_f(MAC_MODE, tp->mac_mode);
5119 udelay(40);
5120
5121 ret = ANEG_TIMER_ENAB;
5122 ap->state = ANEG_STATE_RESTART;
5123
5124 /* fallthru */
5125 case ANEG_STATE_RESTART:
5126 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005127 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005129 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 break;
5132
5133 case ANEG_STATE_DISABLE_LINK_OK:
5134 ret = ANEG_DONE;
5135 break;
5136
5137 case ANEG_STATE_ABILITY_DETECT_INIT:
5138 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005139 ap->txconfig = ANEG_CFG_FD;
5140 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5141 if (flowctrl & ADVERTISE_1000XPAUSE)
5142 ap->txconfig |= ANEG_CFG_PS1;
5143 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5144 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5146 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5147 tw32_f(MAC_MODE, tp->mac_mode);
5148 udelay(40);
5149
5150 ap->state = ANEG_STATE_ABILITY_DETECT;
5151 break;
5152
5153 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005154 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 break;
5157
5158 case ANEG_STATE_ACK_DETECT_INIT:
5159 ap->txconfig |= ANEG_CFG_ACK;
5160 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5161 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5162 tw32_f(MAC_MODE, tp->mac_mode);
5163 udelay(40);
5164
5165 ap->state = ANEG_STATE_ACK_DETECT;
5166
5167 /* fallthru */
5168 case ANEG_STATE_ACK_DETECT:
5169 if (ap->ack_match != 0) {
5170 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5171 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5172 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5173 } else {
5174 ap->state = ANEG_STATE_AN_ENABLE;
5175 }
5176 } else if (ap->ability_match != 0 &&
5177 ap->rxconfig == 0) {
5178 ap->state = ANEG_STATE_AN_ENABLE;
5179 }
5180 break;
5181
5182 case ANEG_STATE_COMPLETE_ACK_INIT:
5183 if (ap->rxconfig & ANEG_CFG_INVAL) {
5184 ret = ANEG_FAILED;
5185 break;
5186 }
5187 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5188 MR_LP_ADV_HALF_DUPLEX |
5189 MR_LP_ADV_SYM_PAUSE |
5190 MR_LP_ADV_ASYM_PAUSE |
5191 MR_LP_ADV_REMOTE_FAULT1 |
5192 MR_LP_ADV_REMOTE_FAULT2 |
5193 MR_LP_ADV_NEXT_PAGE |
5194 MR_TOGGLE_RX |
5195 MR_NP_RX);
5196 if (ap->rxconfig & ANEG_CFG_FD)
5197 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5198 if (ap->rxconfig & ANEG_CFG_HD)
5199 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5200 if (ap->rxconfig & ANEG_CFG_PS1)
5201 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5202 if (ap->rxconfig & ANEG_CFG_PS2)
5203 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5204 if (ap->rxconfig & ANEG_CFG_RF1)
5205 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5206 if (ap->rxconfig & ANEG_CFG_RF2)
5207 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5208 if (ap->rxconfig & ANEG_CFG_NP)
5209 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5210
5211 ap->link_time = ap->cur_time;
5212
5213 ap->flags ^= (MR_TOGGLE_TX);
5214 if (ap->rxconfig & 0x0008)
5215 ap->flags |= MR_TOGGLE_RX;
5216 if (ap->rxconfig & ANEG_CFG_NP)
5217 ap->flags |= MR_NP_RX;
5218 ap->flags |= MR_PAGE_RX;
5219
5220 ap->state = ANEG_STATE_COMPLETE_ACK;
5221 ret = ANEG_TIMER_ENAB;
5222 break;
5223
5224 case ANEG_STATE_COMPLETE_ACK:
5225 if (ap->ability_match != 0 &&
5226 ap->rxconfig == 0) {
5227 ap->state = ANEG_STATE_AN_ENABLE;
5228 break;
5229 }
5230 delta = ap->cur_time - ap->link_time;
5231 if (delta > ANEG_STATE_SETTLE_TIME) {
5232 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5233 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5234 } else {
5235 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5236 !(ap->flags & MR_NP_RX)) {
5237 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5238 } else {
5239 ret = ANEG_FAILED;
5240 }
5241 }
5242 }
5243 break;
5244
5245 case ANEG_STATE_IDLE_DETECT_INIT:
5246 ap->link_time = ap->cur_time;
5247 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5248 tw32_f(MAC_MODE, tp->mac_mode);
5249 udelay(40);
5250
5251 ap->state = ANEG_STATE_IDLE_DETECT;
5252 ret = ANEG_TIMER_ENAB;
5253 break;
5254
5255 case ANEG_STATE_IDLE_DETECT:
5256 if (ap->ability_match != 0 &&
5257 ap->rxconfig == 0) {
5258 ap->state = ANEG_STATE_AN_ENABLE;
5259 break;
5260 }
5261 delta = ap->cur_time - ap->link_time;
5262 if (delta > ANEG_STATE_SETTLE_TIME) {
5263 /* XXX another gem from the Broadcom driver :( */
5264 ap->state = ANEG_STATE_LINK_OK;
5265 }
5266 break;
5267
5268 case ANEG_STATE_LINK_OK:
5269 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5270 ret = ANEG_DONE;
5271 break;
5272
5273 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5274 /* ??? unimplemented */
5275 break;
5276
5277 case ANEG_STATE_NEXT_PAGE_WAIT:
5278 /* ??? unimplemented */
5279 break;
5280
5281 default:
5282 ret = ANEG_FAILED;
5283 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285
5286 return ret;
5287}
5288
Matt Carlson5be73b42007-12-20 20:09:29 -08005289static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
5291 int res = 0;
5292 struct tg3_fiber_aneginfo aninfo;
5293 int status = ANEG_FAILED;
5294 unsigned int tick;
5295 u32 tmp;
5296
5297 tw32_f(MAC_TX_AUTO_NEG, 0);
5298
5299 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5300 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5301 udelay(40);
5302
5303 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5304 udelay(40);
5305
5306 memset(&aninfo, 0, sizeof(aninfo));
5307 aninfo.flags |= MR_AN_ENABLE;
5308 aninfo.state = ANEG_STATE_UNKNOWN;
5309 aninfo.cur_time = 0;
5310 tick = 0;
5311 while (++tick < 195000) {
5312 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5313 if (status == ANEG_DONE || status == ANEG_FAILED)
5314 break;
5315
5316 udelay(1);
5317 }
5318
5319 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5320 tw32_f(MAC_MODE, tp->mac_mode);
5321 udelay(40);
5322
Matt Carlson5be73b42007-12-20 20:09:29 -08005323 *txflags = aninfo.txconfig;
5324 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
5326 if (status == ANEG_DONE &&
5327 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5328 MR_LP_ADV_FULL_DUPLEX)))
5329 res = 1;
5330
5331 return res;
5332}
5333
5334static void tg3_init_bcm8002(struct tg3 *tp)
5335{
5336 u32 mac_status = tr32(MAC_STATUS);
5337 int i;
5338
5339 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005340 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 !(mac_status & MAC_STATUS_PCS_SYNCED))
5342 return;
5343
5344 /* Set PLL lock range. */
5345 tg3_writephy(tp, 0x16, 0x8007);
5346
5347 /* SW reset */
5348 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5349
5350 /* Wait for reset to complete. */
5351 /* XXX schedule_timeout() ... */
5352 for (i = 0; i < 500; i++)
5353 udelay(10);
5354
5355 /* Config mode; select PMA/Ch 1 regs. */
5356 tg3_writephy(tp, 0x10, 0x8411);
5357
5358 /* Enable auto-lock and comdet, select txclk for tx. */
5359 tg3_writephy(tp, 0x11, 0x0a10);
5360
5361 tg3_writephy(tp, 0x18, 0x00a0);
5362 tg3_writephy(tp, 0x16, 0x41ff);
5363
5364 /* Assert and deassert POR. */
5365 tg3_writephy(tp, 0x13, 0x0400);
5366 udelay(40);
5367 tg3_writephy(tp, 0x13, 0x0000);
5368
5369 tg3_writephy(tp, 0x11, 0x0a50);
5370 udelay(40);
5371 tg3_writephy(tp, 0x11, 0x0a10);
5372
5373 /* Wait for signal to stabilize */
5374 /* XXX schedule_timeout() ... */
5375 for (i = 0; i < 15000; i++)
5376 udelay(10);
5377
5378 /* Deselect the channel register so we can read the PHYID
5379 * later.
5380 */
5381 tg3_writephy(tp, 0x10, 0x8011);
5382}
5383
Joe Perches953c96e2013-04-09 10:18:14 +00005384static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005386 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005387 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 u32 sg_dig_ctrl, sg_dig_status;
5389 u32 serdes_cfg, expected_sg_dig_ctrl;
5390 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391
5392 serdes_cfg = 0;
5393 expected_sg_dig_ctrl = 0;
5394 workaround = 0;
5395 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005396 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Joe Perches41535772013-02-16 11:20:04 +00005398 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5399 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 workaround = 1;
5401 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5402 port_a = 0;
5403
5404 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5405 /* preserve bits 20-23 for voltage regulator */
5406 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5407 }
5408
5409 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5410
5411 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005412 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 if (workaround) {
5414 u32 val = serdes_cfg;
5415
5416 if (port_a)
5417 val |= 0xc010000;
5418 else
5419 val |= 0x4010000;
5420 tw32_f(MAC_SERDES_CFG, val);
5421 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005422
5423 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 }
5425 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5426 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005427 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 }
5429 goto out;
5430 }
5431
5432 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005433 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
Matt Carlson82cd3d12007-12-20 20:09:00 -08005435 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5436 if (flowctrl & ADVERTISE_1000XPAUSE)
5437 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5438 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5439 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
5441 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005442 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005443 tp->serdes_counter &&
5444 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5445 MAC_STATUS_RCVD_CFG)) ==
5446 MAC_STATUS_PCS_SYNCED)) {
5447 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005448 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005449 goto out;
5450 }
5451restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 if (workaround)
5453 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005454 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 udelay(5);
5456 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5457
Michael Chan3d3ebe72006-09-27 15:59:15 -07005458 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005459 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5461 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005462 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 mac_status = tr32(MAC_STATUS);
5464
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005465 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005467 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Matt Carlson82cd3d12007-12-20 20:09:00 -08005469 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5470 local_adv |= ADVERTISE_1000XPAUSE;
5471 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5472 local_adv |= ADVERTISE_1000XPSE_ASYM;
5473
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005474 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005475 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005476 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005477 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Matt Carlson859edb22011-12-08 14:40:16 +00005479 tp->link_config.rmt_adv =
5480 mii_adv_to_ethtool_adv_x(remote_adv);
5481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005483 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005484 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005485 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005486 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005487 if (tp->serdes_counter)
5488 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 else {
5490 if (workaround) {
5491 u32 val = serdes_cfg;
5492
5493 if (port_a)
5494 val |= 0xc010000;
5495 else
5496 val |= 0x4010000;
5497
5498 tw32_f(MAC_SERDES_CFG, val);
5499 }
5500
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005501 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 udelay(40);
5503
5504 /* Link parallel detection - link is up */
5505 /* only if we have PCS_SYNC and not */
5506 /* receiving config code words */
5507 mac_status = tr32(MAC_STATUS);
5508 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5509 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5510 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005511 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005512 tp->phy_flags |=
5513 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005514 tp->serdes_counter =
5515 SERDES_PARALLEL_DET_TIMEOUT;
5516 } else
5517 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 }
5519 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005520 } else {
5521 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005522 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 }
5524
5525out:
5526 return current_link_up;
5527}
5528
Joe Perches953c96e2013-04-09 10:18:14 +00005529static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
Joe Perches953c96e2013-04-09 10:18:14 +00005531 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005533 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
5536 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005537 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005539
Matt Carlson5be73b42007-12-20 20:09:29 -08005540 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5541 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Matt Carlson5be73b42007-12-20 20:09:29 -08005543 if (txflags & ANEG_CFG_PS1)
5544 local_adv |= ADVERTISE_1000XPAUSE;
5545 if (txflags & ANEG_CFG_PS2)
5546 local_adv |= ADVERTISE_1000XPSE_ASYM;
5547
5548 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5549 remote_adv |= LPA_1000XPAUSE;
5550 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5551 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
Matt Carlson859edb22011-12-08 14:40:16 +00005553 tp->link_config.rmt_adv =
5554 mii_adv_to_ethtool_adv_x(remote_adv);
5555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 tg3_setup_flow_control(tp, local_adv, remote_adv);
5557
Joe Perches953c96e2013-04-09 10:18:14 +00005558 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 }
5560 for (i = 0; i < 30; i++) {
5561 udelay(20);
5562 tw32_f(MAC_STATUS,
5563 (MAC_STATUS_SYNC_CHANGED |
5564 MAC_STATUS_CFG_CHANGED));
5565 udelay(40);
5566 if ((tr32(MAC_STATUS) &
5567 (MAC_STATUS_SYNC_CHANGED |
5568 MAC_STATUS_CFG_CHANGED)) == 0)
5569 break;
5570 }
5571
5572 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005573 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5575 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005576 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005578 tg3_setup_flow_control(tp, 0, 0);
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005581 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5584 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005585
5586 tw32_f(MAC_MODE, tp->mac_mode);
5587 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 }
5589
5590out:
5591 return current_link_up;
5592}
5593
Joe Perches953c96e2013-04-09 10:18:14 +00005594static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595{
5596 u32 orig_pause_cfg;
5597 u16 orig_active_speed;
5598 u8 orig_active_duplex;
5599 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005600 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 int i;
5602
Matt Carlson8d018622007-12-20 20:05:44 -08005603 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 orig_active_speed = tp->link_config.active_speed;
5605 orig_active_duplex = tp->link_config.active_duplex;
5606
Joe Perches63c3a662011-04-26 08:12:10 +00005607 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005608 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005609 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 mac_status = tr32(MAC_STATUS);
5611 mac_status &= (MAC_STATUS_PCS_SYNCED |
5612 MAC_STATUS_SIGNAL_DET |
5613 MAC_STATUS_CFG_CHANGED |
5614 MAC_STATUS_RCVD_CFG);
5615 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5616 MAC_STATUS_SIGNAL_DET)) {
5617 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5618 MAC_STATUS_CFG_CHANGED));
5619 return 0;
5620 }
5621 }
5622
5623 tw32_f(MAC_TX_AUTO_NEG, 0);
5624
5625 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5626 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5627 tw32_f(MAC_MODE, tp->mac_mode);
5628 udelay(40);
5629
Matt Carlson79eb6902010-02-17 15:17:03 +00005630 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 tg3_init_bcm8002(tp);
5632
5633 /* Enable link change event even when serdes polling. */
5634 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5635 udelay(40);
5636
Joe Perches953c96e2013-04-09 10:18:14 +00005637 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005638 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 mac_status = tr32(MAC_STATUS);
5640
Joe Perches63c3a662011-04-26 08:12:10 +00005641 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5643 else
5644 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5645
Matt Carlson898a56f2009-08-28 14:02:40 +00005646 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005648 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650 for (i = 0; i < 100; i++) {
5651 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5652 MAC_STATUS_CFG_CHANGED));
5653 udelay(5);
5654 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005655 MAC_STATUS_CFG_CHANGED |
5656 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 break;
5658 }
5659
5660 mac_status = tr32(MAC_STATUS);
5661 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005662 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005663 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5664 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 tw32_f(MAC_MODE, (tp->mac_mode |
5666 MAC_MODE_SEND_CONFIGS));
5667 udelay(1);
5668 tw32_f(MAC_MODE, tp->mac_mode);
5669 }
5670 }
5671
Joe Perches953c96e2013-04-09 10:18:14 +00005672 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 tp->link_config.active_speed = SPEED_1000;
5674 tp->link_config.active_duplex = DUPLEX_FULL;
5675 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5676 LED_CTRL_LNKLED_OVERRIDE |
5677 LED_CTRL_1000MBPS_ON));
5678 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005679 tp->link_config.active_speed = SPEED_UNKNOWN;
5680 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5682 LED_CTRL_LNKLED_OVERRIDE |
5683 LED_CTRL_TRAFFIC_OVERRIDE));
5684 }
5685
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005686 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005687 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 if (orig_pause_cfg != now_pause_cfg ||
5689 orig_active_speed != tp->link_config.active_speed ||
5690 orig_active_duplex != tp->link_config.active_duplex)
5691 tg3_link_report(tp);
5692 }
5693
5694 return 0;
5695}
5696
Joe Perches953c96e2013-04-09 10:18:14 +00005697static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005698{
Joe Perches953c96e2013-04-09 10:18:14 +00005699 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005700 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005701 u16 current_speed = SPEED_UNKNOWN;
5702 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005703 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005704 u32 local_adv, remote_adv, sgsr;
5705
5706 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5707 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5708 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5709 (sgsr & SERDES_TG3_SGMII_MODE)) {
5710
5711 if (force_reset)
5712 tg3_phy_reset(tp);
5713
5714 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5715
5716 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5717 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5718 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005719 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005720 if (sgsr & SERDES_TG3_SPEED_1000) {
5721 current_speed = SPEED_1000;
5722 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5723 } else if (sgsr & SERDES_TG3_SPEED_100) {
5724 current_speed = SPEED_100;
5725 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5726 } else {
5727 current_speed = SPEED_10;
5728 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5729 }
5730
5731 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5732 current_duplex = DUPLEX_FULL;
5733 else
5734 current_duplex = DUPLEX_HALF;
5735 }
5736
5737 tw32_f(MAC_MODE, tp->mac_mode);
5738 udelay(40);
5739
5740 tg3_clear_mac_status(tp);
5741
5742 goto fiber_setup_done;
5743 }
Michael Chan747e8f82005-07-25 12:33:22 -07005744
5745 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5746 tw32_f(MAC_MODE, tp->mac_mode);
5747 udelay(40);
5748
Michael Chan3310e242013-04-09 08:48:05 +00005749 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005750
5751 if (force_reset)
5752 tg3_phy_reset(tp);
5753
Matt Carlson859edb22011-12-08 14:40:16 +00005754 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005755
5756 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5757 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005758 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005759 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5760 bmsr |= BMSR_LSTATUS;
5761 else
5762 bmsr &= ~BMSR_LSTATUS;
5763 }
Michael Chan747e8f82005-07-25 12:33:22 -07005764
5765 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5766
5767 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005768 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005769 /* do nothing, just check for link up at the end */
5770 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005771 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005772
5773 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005774 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5775 ADVERTISE_1000XPAUSE |
5776 ADVERTISE_1000XPSE_ASYM |
5777 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005778
Matt Carlson28011cf2011-11-16 18:36:59 -05005779 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005780 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005781
Matt Carlson28011cf2011-11-16 18:36:59 -05005782 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5783 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005784 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5785 tg3_writephy(tp, MII_BMCR, bmcr);
5786
5787 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005788 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005789 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005790
5791 return err;
5792 }
5793 } else {
5794 u32 new_bmcr;
5795
5796 bmcr &= ~BMCR_SPEED1000;
5797 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5798
5799 if (tp->link_config.duplex == DUPLEX_FULL)
5800 new_bmcr |= BMCR_FULLDPLX;
5801
5802 if (new_bmcr != bmcr) {
5803 /* BMCR_SPEED1000 is a reserved bit that needs
5804 * to be set on write.
5805 */
5806 new_bmcr |= BMCR_SPEED1000;
5807
5808 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005809 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005810 u32 adv;
5811
5812 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5813 adv &= ~(ADVERTISE_1000XFULL |
5814 ADVERTISE_1000XHALF |
5815 ADVERTISE_SLCT);
5816 tg3_writephy(tp, MII_ADVERTISE, adv);
5817 tg3_writephy(tp, MII_BMCR, bmcr |
5818 BMCR_ANRESTART |
5819 BMCR_ANENABLE);
5820 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005821 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005822 }
5823 tg3_writephy(tp, MII_BMCR, new_bmcr);
5824 bmcr = new_bmcr;
5825 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5826 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005827 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005828 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5829 bmsr |= BMSR_LSTATUS;
5830 else
5831 bmsr &= ~BMSR_LSTATUS;
5832 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005833 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005834 }
5835 }
5836
5837 if (bmsr & BMSR_LSTATUS) {
5838 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005839 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005840 if (bmcr & BMCR_FULLDPLX)
5841 current_duplex = DUPLEX_FULL;
5842 else
5843 current_duplex = DUPLEX_HALF;
5844
Matt Carlsonef167e22007-12-20 20:10:01 -08005845 local_adv = 0;
5846 remote_adv = 0;
5847
Michael Chan747e8f82005-07-25 12:33:22 -07005848 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005849 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005850
5851 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5852 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5853 common = local_adv & remote_adv;
5854 if (common & (ADVERTISE_1000XHALF |
5855 ADVERTISE_1000XFULL)) {
5856 if (common & ADVERTISE_1000XFULL)
5857 current_duplex = DUPLEX_FULL;
5858 else
5859 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005860
5861 tp->link_config.rmt_adv =
5862 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005863 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005864 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005865 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005866 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005867 }
Michael Chan747e8f82005-07-25 12:33:22 -07005868 }
5869 }
5870
Michael Chan85730a62013-04-09 08:48:06 +00005871fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005872 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005873 tg3_setup_flow_control(tp, local_adv, remote_adv);
5874
Michael Chan747e8f82005-07-25 12:33:22 -07005875 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5876 if (tp->link_config.active_duplex == DUPLEX_HALF)
5877 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5878
5879 tw32_f(MAC_MODE, tp->mac_mode);
5880 udelay(40);
5881
5882 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5883
5884 tp->link_config.active_speed = current_speed;
5885 tp->link_config.active_duplex = current_duplex;
5886
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005887 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005888 return err;
5889}
5890
5891static void tg3_serdes_parallel_detect(struct tg3 *tp)
5892{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005893 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005894 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005895 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005896 return;
5897 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005898
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005899 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005900 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5901 u32 bmcr;
5902
5903 tg3_readphy(tp, MII_BMCR, &bmcr);
5904 if (bmcr & BMCR_ANENABLE) {
5905 u32 phy1, phy2;
5906
5907 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005908 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5909 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005910
5911 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005912 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5913 MII_TG3_DSP_EXP1_INT_STAT);
5914 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5915 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005916
5917 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5918 /* We have signal detect and not receiving
5919 * config code words, link is up by parallel
5920 * detection.
5921 */
5922
5923 bmcr &= ~BMCR_ANENABLE;
5924 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5925 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005926 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005927 }
5928 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005929 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005930 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005931 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005932 u32 phy2;
5933
5934 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005935 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5936 MII_TG3_DSP_EXP1_INT_STAT);
5937 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005938 if (phy2 & 0x20) {
5939 u32 bmcr;
5940
5941 /* Config code words received, turn on autoneg. */
5942 tg3_readphy(tp, MII_BMCR, &bmcr);
5943 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5944
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005945 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005946
5947 }
5948 }
5949}
5950
Joe Perches953c96e2013-04-09 10:18:14 +00005951static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005953 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 int err;
5955
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005956 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005958 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005959 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005960 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
Joe Perches41535772013-02-16 11:20:04 +00005963 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005964 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005965
5966 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5967 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5968 scale = 65;
5969 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5970 scale = 6;
5971 else
5972 scale = 12;
5973
5974 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5975 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5976 tw32(GRC_MISC_CFG, val);
5977 }
5978
Matt Carlsonf2096f92011-04-05 14:22:48 +00005979 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5980 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00005981 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
5982 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005983 val |= tr32(MAC_TX_LENGTHS) &
5984 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5985 TX_LENGTHS_CNT_DWN_VAL_MSK);
5986
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 if (tp->link_config.active_speed == SPEED_1000 &&
5988 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005989 tw32(MAC_TX_LENGTHS, val |
5990 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005992 tw32(MAC_TX_LENGTHS, val |
5993 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
Joe Perches63c3a662011-04-26 08:12:10 +00005995 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005996 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005998 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 } else {
6000 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6001 }
6002 }
6003
Joe Perches63c3a662011-04-26 08:12:10 +00006004 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006005 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006006 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006007 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6008 tp->pwrmgmt_thresh;
6009 else
6010 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6011 tw32(PCIE_PWR_MGMT_THRESH, val);
6012 }
6013
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 return err;
6015}
6016
Matt Carlsonbe947302012-12-03 19:36:57 +00006017/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006018static u64 tg3_refclk_read(struct tg3 *tp)
6019{
6020 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6021 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6022}
6023
6024/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006025static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6026{
6027 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
6028 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6029 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
6030 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
6031}
6032
Matt Carlson7d41e492012-12-03 19:36:58 +00006033static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6034static inline void tg3_full_unlock(struct tg3 *tp);
6035static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6036{
6037 struct tg3 *tp = netdev_priv(dev);
6038
6039 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6040 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006041 SOF_TIMESTAMPING_SOFTWARE;
6042
6043 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006044 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006045 SOF_TIMESTAMPING_RX_HARDWARE |
6046 SOF_TIMESTAMPING_RAW_HARDWARE;
6047 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006048
6049 if (tp->ptp_clock)
6050 info->phc_index = ptp_clock_index(tp->ptp_clock);
6051 else
6052 info->phc_index = -1;
6053
6054 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6055
6056 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6057 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6058 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6059 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6060 return 0;
6061}
6062
6063static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6064{
6065 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6066 bool neg_adj = false;
6067 u32 correction = 0;
6068
6069 if (ppb < 0) {
6070 neg_adj = true;
6071 ppb = -ppb;
6072 }
6073
6074 /* Frequency adjustment is performed using hardware with a 24 bit
6075 * accumulator and a programmable correction value. On each clk, the
6076 * correction value gets added to the accumulator and when it
6077 * overflows, the time counter is incremented/decremented.
6078 *
6079 * So conversion from ppb to correction value is
6080 * ppb * (1 << 24) / 1000000000
6081 */
6082 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6083 TG3_EAV_REF_CLK_CORRECT_MASK;
6084
6085 tg3_full_lock(tp, 0);
6086
6087 if (correction)
6088 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6089 TG3_EAV_REF_CLK_CORRECT_EN |
6090 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6091 else
6092 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6093
6094 tg3_full_unlock(tp);
6095
6096 return 0;
6097}
6098
6099static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6100{
6101 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6102
6103 tg3_full_lock(tp, 0);
6104 tp->ptp_adjust += delta;
6105 tg3_full_unlock(tp);
6106
6107 return 0;
6108}
6109
6110static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6111{
6112 u64 ns;
6113 u32 remainder;
6114 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6115
6116 tg3_full_lock(tp, 0);
6117 ns = tg3_refclk_read(tp);
6118 ns += tp->ptp_adjust;
6119 tg3_full_unlock(tp);
6120
6121 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6122 ts->tv_nsec = remainder;
6123
6124 return 0;
6125}
6126
6127static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6128 const struct timespec *ts)
6129{
6130 u64 ns;
6131 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6132
6133 ns = timespec_to_ns(ts);
6134
6135 tg3_full_lock(tp, 0);
6136 tg3_refclk_write(tp, ns);
6137 tp->ptp_adjust = 0;
6138 tg3_full_unlock(tp);
6139
6140 return 0;
6141}
6142
6143static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6144 struct ptp_clock_request *rq, int on)
6145{
6146 return -EOPNOTSUPP;
6147}
6148
6149static const struct ptp_clock_info tg3_ptp_caps = {
6150 .owner = THIS_MODULE,
6151 .name = "tg3 clock",
6152 .max_adj = 250000000,
6153 .n_alarm = 0,
6154 .n_ext_ts = 0,
6155 .n_per_out = 0,
6156 .pps = 0,
6157 .adjfreq = tg3_ptp_adjfreq,
6158 .adjtime = tg3_ptp_adjtime,
6159 .gettime = tg3_ptp_gettime,
6160 .settime = tg3_ptp_settime,
6161 .enable = tg3_ptp_enable,
6162};
6163
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006164static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6165 struct skb_shared_hwtstamps *timestamp)
6166{
6167 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6168 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6169 tp->ptp_adjust);
6170}
6171
Matt Carlsonbe947302012-12-03 19:36:57 +00006172/* tp->lock must be held */
6173static void tg3_ptp_init(struct tg3 *tp)
6174{
6175 if (!tg3_flag(tp, PTP_CAPABLE))
6176 return;
6177
6178 /* Initialize the hardware clock to the system time. */
6179 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6180 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006181 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006182}
6183
6184/* tp->lock must be held */
6185static void tg3_ptp_resume(struct tg3 *tp)
6186{
6187 if (!tg3_flag(tp, PTP_CAPABLE))
6188 return;
6189
6190 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6191 tp->ptp_adjust = 0;
6192}
6193
6194static void tg3_ptp_fini(struct tg3 *tp)
6195{
6196 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6197 return;
6198
Matt Carlson7d41e492012-12-03 19:36:58 +00006199 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006200 tp->ptp_clock = NULL;
6201 tp->ptp_adjust = 0;
6202}
6203
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006204static inline int tg3_irq_sync(struct tg3 *tp)
6205{
6206 return tp->irq_sync;
6207}
6208
Matt Carlson97bd8e42011-04-13 11:05:04 +00006209static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6210{
6211 int i;
6212
6213 dst = (u32 *)((u8 *)dst + off);
6214 for (i = 0; i < len; i += sizeof(u32))
6215 *dst++ = tr32(off + i);
6216}
6217
6218static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6219{
6220 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6221 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6222 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6223 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6224 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6225 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6226 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6227 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6228 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6229 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6230 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6231 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6232 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6233 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6234 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6235 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6236 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6237 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6238 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6239
Joe Perches63c3a662011-04-26 08:12:10 +00006240 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006241 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6242
6243 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6244 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6245 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6246 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6247 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6248 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6249 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6250 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6251
Joe Perches63c3a662011-04-26 08:12:10 +00006252 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006253 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6254 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6255 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6256 }
6257
6258 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6259 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6260 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6261 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6262 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6263
Joe Perches63c3a662011-04-26 08:12:10 +00006264 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006265 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6266}
6267
6268static void tg3_dump_state(struct tg3 *tp)
6269{
6270 int i;
6271 u32 *regs;
6272
6273 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006274 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006275 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006276
Joe Perches63c3a662011-04-26 08:12:10 +00006277 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006278 /* Read up to but not including private PCI registers */
6279 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6280 regs[i / sizeof(u32)] = tr32(i);
6281 } else
6282 tg3_dump_legacy_regs(tp, regs);
6283
6284 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6285 if (!regs[i + 0] && !regs[i + 1] &&
6286 !regs[i + 2] && !regs[i + 3])
6287 continue;
6288
6289 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6290 i * 4,
6291 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6292 }
6293
6294 kfree(regs);
6295
6296 for (i = 0; i < tp->irq_cnt; i++) {
6297 struct tg3_napi *tnapi = &tp->napi[i];
6298
6299 /* SW status block */
6300 netdev_err(tp->dev,
6301 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6302 i,
6303 tnapi->hw_status->status,
6304 tnapi->hw_status->status_tag,
6305 tnapi->hw_status->rx_jumbo_consumer,
6306 tnapi->hw_status->rx_consumer,
6307 tnapi->hw_status->rx_mini_consumer,
6308 tnapi->hw_status->idx[0].rx_producer,
6309 tnapi->hw_status->idx[0].tx_consumer);
6310
6311 netdev_err(tp->dev,
6312 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6313 i,
6314 tnapi->last_tag, tnapi->last_irq_tag,
6315 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6316 tnapi->rx_rcb_ptr,
6317 tnapi->prodring.rx_std_prod_idx,
6318 tnapi->prodring.rx_std_cons_idx,
6319 tnapi->prodring.rx_jmb_prod_idx,
6320 tnapi->prodring.rx_jmb_cons_idx);
6321 }
6322}
6323
Michael Chandf3e6542006-05-26 17:48:07 -07006324/* This is called whenever we suspect that the system chipset is re-
6325 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6326 * is bogus tx completions. We try to recover by setting the
6327 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6328 * in the workqueue.
6329 */
6330static void tg3_tx_recover(struct tg3 *tp)
6331{
Joe Perches63c3a662011-04-26 08:12:10 +00006332 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006333 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6334
Matt Carlson5129c3a2010-04-05 10:19:23 +00006335 netdev_warn(tp->dev,
6336 "The system may be re-ordering memory-mapped I/O "
6337 "cycles to the network device, attempting to recover. "
6338 "Please report the problem to the driver maintainer "
6339 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006340
6341 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006342 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006343 spin_unlock(&tp->lock);
6344}
6345
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006346static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006347{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006348 /* Tell compiler to fetch tx indices from memory. */
6349 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006350 return tnapi->tx_pending -
6351 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006352}
6353
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354/* Tigon3 never reports partial packet sends. So we do not
6355 * need special logic to handle SKBs that have not had all
6356 * of their frags sent yet, like SunGEM does.
6357 */
Matt Carlson17375d22009-08-28 14:02:18 +00006358static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359{
Matt Carlson17375d22009-08-28 14:02:18 +00006360 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006361 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006362 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006363 struct netdev_queue *txq;
6364 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006365 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006366
Joe Perches63c3a662011-04-26 08:12:10 +00006367 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006368 index--;
6369
6370 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006373 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006375 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
Michael Chandf3e6542006-05-26 17:48:07 -07006377 if (unlikely(skb == NULL)) {
6378 tg3_tx_recover(tp);
6379 return;
6380 }
6381
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006382 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6383 struct skb_shared_hwtstamps timestamp;
6384 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6385 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6386
6387 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6388
6389 skb_tstamp_tx(skb, &timestamp);
6390 }
6391
Alexander Duyckf4188d82009-12-02 16:48:38 +00006392 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006393 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006394 skb_headlen(skb),
6395 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
6397 ri->skb = NULL;
6398
Matt Carlsone01ee142011-07-27 14:20:50 +00006399 while (ri->fragmented) {
6400 ri->fragmented = false;
6401 sw_idx = NEXT_TX(sw_idx);
6402 ri = &tnapi->tx_buffers[sw_idx];
6403 }
6404
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 sw_idx = NEXT_TX(sw_idx);
6406
6407 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006408 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006409 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6410 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006411
6412 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006413 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006414 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006415 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006416
6417 while (ri->fragmented) {
6418 ri->fragmented = false;
6419 sw_idx = NEXT_TX(sw_idx);
6420 ri = &tnapi->tx_buffers[sw_idx];
6421 }
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 sw_idx = NEXT_TX(sw_idx);
6424 }
6425
Tom Herbert298376d2011-11-28 16:33:30 +00006426 pkts_compl++;
6427 bytes_compl += skb->len;
6428
David S. Millerf47c11e2005-06-24 20:18:35 -07006429 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006430
6431 if (unlikely(tx_bug)) {
6432 tg3_tx_recover(tp);
6433 return;
6434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 }
6436
Tom Herbert5cb917b2012-03-05 19:53:50 +00006437 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006438
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006439 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
Michael Chan1b2a7202006-08-07 21:46:02 -07006441 /* Need to make the tx_cons update visible to tg3_start_xmit()
6442 * before checking for netif_queue_stopped(). Without the
6443 * memory barrier, there is a small possibility that tg3_start_xmit()
6444 * will miss it and cause the queue to be stopped forever.
6445 */
6446 smp_mb();
6447
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006448 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006449 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006450 __netif_tx_lock(txq, smp_processor_id());
6451 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006452 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006453 netif_tx_wake_queue(txq);
6454 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456}
6457
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006458static void tg3_frag_free(bool is_frag, void *data)
6459{
6460 if (is_frag)
6461 put_page(virt_to_head_page(data));
6462 else
6463 kfree(data);
6464}
6465
Eric Dumazet9205fd92011-11-18 06:47:01 +00006466static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006467{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006468 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6469 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6470
Eric Dumazet9205fd92011-11-18 06:47:01 +00006471 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006472 return;
6473
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006474 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006475 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006476 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006477 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006478}
6479
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006480
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481/* Returns size of skb allocated or < 0 on error.
6482 *
6483 * We only need to fill in the address because the other members
6484 * of the RX descriptor are invariant, see tg3_init_rings.
6485 *
6486 * Note the purposeful assymetry of cpu vs. chip accesses. For
6487 * posting buffers we only dirty the first cache line of the RX
6488 * descriptor (containing the address). Whereas for the RX status
6489 * buffers the cpu only reads the last cacheline of the RX descriptor
6490 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6491 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006492static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006493 u32 opaque_key, u32 dest_idx_unmasked,
6494 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495{
6496 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006497 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006498 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006500 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 switch (opaque_key) {
6503 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006504 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006505 desc = &tpr->rx_std[dest_idx];
6506 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006507 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 break;
6509
6510 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006511 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006512 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006513 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006514 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 break;
6516
6517 default:
6518 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520
6521 /* Do not overwrite any of the map or rp information
6522 * until we are sure we can commit to a new buffer.
6523 *
6524 * Callers depend upon this behavior and assume that
6525 * we leave everything unchanged if we fail.
6526 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006527 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6528 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006529 if (skb_size <= PAGE_SIZE) {
6530 data = netdev_alloc_frag(skb_size);
6531 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006532 } else {
6533 data = kmalloc(skb_size, GFP_ATOMIC);
6534 *frag_size = 0;
6535 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006536 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 return -ENOMEM;
6538
Eric Dumazet9205fd92011-11-18 06:47:01 +00006539 mapping = pci_map_single(tp->pdev,
6540 data + TG3_RX_OFFSET(tp),
6541 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006543 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006544 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006545 return -EIO;
6546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
Eric Dumazet9205fd92011-11-18 06:47:01 +00006548 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006549 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 desc->addr_hi = ((u64)mapping >> 32);
6552 desc->addr_lo = ((u64)mapping & 0xffffffff);
6553
Eric Dumazet9205fd92011-11-18 06:47:01 +00006554 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555}
6556
6557/* We only need to move over in the address because the other
6558 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006559 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 */
Matt Carlsona3896162009-11-13 13:03:44 +00006561static void tg3_recycle_rx(struct tg3_napi *tnapi,
6562 struct tg3_rx_prodring_set *dpr,
6563 u32 opaque_key, int src_idx,
6564 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565{
Matt Carlson17375d22009-08-28 14:02:18 +00006566 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6568 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006569 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006570 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
6572 switch (opaque_key) {
6573 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006574 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006575 dest_desc = &dpr->rx_std[dest_idx];
6576 dest_map = &dpr->rx_std_buffers[dest_idx];
6577 src_desc = &spr->rx_std[src_idx];
6578 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 break;
6580
6581 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006582 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006583 dest_desc = &dpr->rx_jmb[dest_idx].std;
6584 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6585 src_desc = &spr->rx_jmb[src_idx].std;
6586 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 break;
6588
6589 default:
6590 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
Eric Dumazet9205fd92011-11-18 06:47:01 +00006593 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006594 dma_unmap_addr_set(dest_map, mapping,
6595 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 dest_desc->addr_hi = src_desc->addr_hi;
6597 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006598
6599 /* Ensure that the update to the skb happens after the physical
6600 * addresses have been transferred to the new BD location.
6601 */
6602 smp_wmb();
6603
Eric Dumazet9205fd92011-11-18 06:47:01 +00006604 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605}
6606
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607/* The RX ring scheme is composed of multiple rings which post fresh
6608 * buffers to the chip, and one special ring the chip uses to report
6609 * status back to the host.
6610 *
6611 * The special ring reports the status of received packets to the
6612 * host. The chip does not write into the original descriptor the
6613 * RX buffer was obtained from. The chip simply takes the original
6614 * descriptor as provided by the host, updates the status and length
6615 * field, then writes this into the next status ring entry.
6616 *
6617 * Each ring the host uses to post buffers to the chip is described
6618 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6619 * it is first placed into the on-chip ram. When the packet's length
6620 * is known, it walks down the TG3_BDINFO entries to select the ring.
6621 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6622 * which is within the range of the new packet's length is chosen.
6623 *
6624 * The "separate ring for rx status" scheme may sound queer, but it makes
6625 * sense from a cache coherency perspective. If only the host writes
6626 * to the buffer post rings, and only the chip writes to the rx status
6627 * rings, then cache lines never move beyond shared-modified state.
6628 * If both the host and chip were to write into the same ring, cache line
6629 * eviction could occur since both entities want it in an exclusive state.
6630 */
Matt Carlson17375d22009-08-28 14:02:18 +00006631static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632{
Matt Carlson17375d22009-08-28 14:02:18 +00006633 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006634 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006635 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006636 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006637 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006639 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006641 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 /*
6643 * We need to order the read of hw_idx and the read of
6644 * the opaque cookie.
6645 */
6646 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 work_mask = 0;
6648 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006649 std_prod_idx = tpr->rx_std_prod_idx;
6650 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006652 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006653 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 unsigned int len;
6655 struct sk_buff *skb;
6656 dma_addr_t dma_addr;
6657 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006658 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006659 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
6661 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6662 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6663 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006664 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006665 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006666 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006667 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006668 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006670 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006671 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006672 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006673 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006674 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
6677 work_mask |= opaque_key;
6678
6679 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6680 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6681 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006682 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 desc_idx, *post_ptr);
6684 drop_it_no_recycle:
6685 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006686 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 goto next_pkt;
6688 }
6689
Eric Dumazet9205fd92011-11-18 06:47:01 +00006690 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006691 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6692 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006694 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6695 RXD_FLAG_PTPSTAT_PTPV1 ||
6696 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6697 RXD_FLAG_PTPSTAT_PTPV2) {
6698 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6699 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6700 }
6701
Matt Carlsond2757fc2010-04-12 06:58:27 +00006702 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006704 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705
Eric Dumazet9205fd92011-11-18 06:47:01 +00006706 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006707 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 if (skb_size < 0)
6709 goto drop_it;
6710
Matt Carlson287be122009-08-28 13:58:46 +00006711 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 PCI_DMA_FROMDEVICE);
6713
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006714 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006715 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006716 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006717 goto drop_it_no_recycle;
6718 }
6719 skb_reserve(skb, TG3_RX_OFFSET(tp));
6720 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006721 * after the usage of the old DMA mapping.
6722 */
6723 smp_wmb();
6724
Eric Dumazet9205fd92011-11-18 06:47:01 +00006725 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006726
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006728 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 desc_idx, *post_ptr);
6730
Eric Dumazet9205fd92011-11-18 06:47:01 +00006731 skb = netdev_alloc_skb(tp->dev,
6732 len + TG3_RAW_IP_ALIGN);
6733 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 goto drop_it_no_recycle;
6735
Eric Dumazet9205fd92011-11-18 06:47:01 +00006736 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006738 memcpy(skb->data,
6739 data + TG3_RX_OFFSET(tp),
6740 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 }
6743
Eric Dumazet9205fd92011-11-18 06:47:01 +00006744 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006745 if (tstamp)
6746 tg3_hwclock_to_timestamp(tp, tstamp,
6747 skb_hwtstamps(skb));
6748
Michał Mirosławdc668912011-04-07 03:35:07 +00006749 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6751 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6752 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6753 skb->ip_summed = CHECKSUM_UNNECESSARY;
6754 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006755 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
6757 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006758
6759 if (len > (tp->dev->mtu + ETH_HLEN) &&
6760 skb->protocol != htons(ETH_P_8021Q)) {
6761 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006762 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006763 }
6764
Matt Carlson9dc7a112010-04-12 06:58:28 +00006765 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006766 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006767 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006768 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006769
Matt Carlsonbf933c82011-01-25 15:58:49 +00006770 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 received++;
6773 budget--;
6774
6775next_pkt:
6776 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006777
6778 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006779 tpr->rx_std_prod_idx = std_prod_idx &
6780 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006781 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6782 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006783 work_mask &= ~RXD_OPAQUE_RING_STD;
6784 rx_std_posted = 0;
6785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006787 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006788 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006789
6790 /* Refresh hw_idx to see if there is new work */
6791 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006792 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006793 rmb();
6794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 }
6796
6797 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006798 tnapi->rx_rcb_ptr = sw_idx;
6799 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
6801 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006802 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006803 /* Sync BD data before updating mailbox */
6804 wmb();
6805
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006806 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006807 tpr->rx_std_prod_idx = std_prod_idx &
6808 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006809 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6810 tpr->rx_std_prod_idx);
6811 }
6812 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006813 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6814 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006815 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6816 tpr->rx_jmb_prod_idx);
6817 }
6818 mmiowb();
6819 } else if (work_mask) {
6820 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6821 * updated before the producer indices can be updated.
6822 */
6823 smp_wmb();
6824
Matt Carlson2c49a442010-09-30 10:34:35 +00006825 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6826 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006827
Michael Chan7ae52892012-03-21 15:38:33 +00006828 if (tnapi != &tp->napi[1]) {
6829 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006830 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
6834 return received;
6835}
6836
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006837static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006840 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006841 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6842
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 if (sblk->status & SD_STATUS_LINK_CHG) {
6844 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006845 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006846 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006847 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006848 tw32_f(MAC_STATUS,
6849 (MAC_STATUS_SYNC_CHANGED |
6850 MAC_STATUS_CFG_CHANGED |
6851 MAC_STATUS_MI_COMPLETION |
6852 MAC_STATUS_LNKSTATE_CHANGED));
6853 udelay(40);
6854 } else
Joe Perches953c96e2013-04-09 10:18:14 +00006855 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07006856 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 }
6858 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006859}
6860
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006861static int tg3_rx_prodring_xfer(struct tg3 *tp,
6862 struct tg3_rx_prodring_set *dpr,
6863 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006864{
6865 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006866 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006867
6868 while (1) {
6869 src_prod_idx = spr->rx_std_prod_idx;
6870
6871 /* Make sure updates to the rx_std_buffers[] entries and the
6872 * standard producer index are seen in the correct order.
6873 */
6874 smp_rmb();
6875
6876 if (spr->rx_std_cons_idx == src_prod_idx)
6877 break;
6878
6879 if (spr->rx_std_cons_idx < src_prod_idx)
6880 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6881 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006882 cpycnt = tp->rx_std_ring_mask + 1 -
6883 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006884
Matt Carlson2c49a442010-09-30 10:34:35 +00006885 cpycnt = min(cpycnt,
6886 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006887
6888 si = spr->rx_std_cons_idx;
6889 di = dpr->rx_std_prod_idx;
6890
Matt Carlsone92967b2010-02-12 14:47:06 +00006891 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006892 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006893 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006894 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006895 break;
6896 }
6897 }
6898
6899 if (!cpycnt)
6900 break;
6901
6902 /* Ensure that updates to the rx_std_buffers ring and the
6903 * shadowed hardware producer ring from tg3_recycle_skb() are
6904 * ordered correctly WRT the skb check above.
6905 */
6906 smp_rmb();
6907
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006908 memcpy(&dpr->rx_std_buffers[di],
6909 &spr->rx_std_buffers[si],
6910 cpycnt * sizeof(struct ring_info));
6911
6912 for (i = 0; i < cpycnt; i++, di++, si++) {
6913 struct tg3_rx_buffer_desc *sbd, *dbd;
6914 sbd = &spr->rx_std[si];
6915 dbd = &dpr->rx_std[di];
6916 dbd->addr_hi = sbd->addr_hi;
6917 dbd->addr_lo = sbd->addr_lo;
6918 }
6919
Matt Carlson2c49a442010-09-30 10:34:35 +00006920 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6921 tp->rx_std_ring_mask;
6922 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6923 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006924 }
6925
6926 while (1) {
6927 src_prod_idx = spr->rx_jmb_prod_idx;
6928
6929 /* Make sure updates to the rx_jmb_buffers[] entries and
6930 * the jumbo producer index are seen in the correct order.
6931 */
6932 smp_rmb();
6933
6934 if (spr->rx_jmb_cons_idx == src_prod_idx)
6935 break;
6936
6937 if (spr->rx_jmb_cons_idx < src_prod_idx)
6938 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6939 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006940 cpycnt = tp->rx_jmb_ring_mask + 1 -
6941 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006942
6943 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006944 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006945
6946 si = spr->rx_jmb_cons_idx;
6947 di = dpr->rx_jmb_prod_idx;
6948
Matt Carlsone92967b2010-02-12 14:47:06 +00006949 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006950 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006951 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006952 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006953 break;
6954 }
6955 }
6956
6957 if (!cpycnt)
6958 break;
6959
6960 /* Ensure that updates to the rx_jmb_buffers ring and the
6961 * shadowed hardware producer ring from tg3_recycle_skb() are
6962 * ordered correctly WRT the skb check above.
6963 */
6964 smp_rmb();
6965
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006966 memcpy(&dpr->rx_jmb_buffers[di],
6967 &spr->rx_jmb_buffers[si],
6968 cpycnt * sizeof(struct ring_info));
6969
6970 for (i = 0; i < cpycnt; i++, di++, si++) {
6971 struct tg3_rx_buffer_desc *sbd, *dbd;
6972 sbd = &spr->rx_jmb[si].std;
6973 dbd = &dpr->rx_jmb[di].std;
6974 dbd->addr_hi = sbd->addr_hi;
6975 dbd->addr_lo = sbd->addr_lo;
6976 }
6977
Matt Carlson2c49a442010-09-30 10:34:35 +00006978 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6979 tp->rx_jmb_ring_mask;
6980 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6981 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006982 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006983
6984 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006985}
6986
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006987static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6988{
6989 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
6991 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006992 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006993 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006994 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006995 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 }
6997
Matt Carlsonf891ea12012-04-24 13:37:01 +00006998 if (!tnapi->rx_rcb_prod_idx)
6999 return work_done;
7000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 /* run RX thread, within the bounds set by NAPI.
7002 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007003 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007005 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007006 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007
Joe Perches63c3a662011-04-26 08:12:10 +00007008 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007009 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007010 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007011 u32 std_prod_idx = dpr->rx_std_prod_idx;
7012 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007013
Michael Chan7ae52892012-03-21 15:38:33 +00007014 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007015 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007016 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007017 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007018
7019 wmb();
7020
Matt Carlsone4af1af2010-02-12 14:47:05 +00007021 if (std_prod_idx != dpr->rx_std_prod_idx)
7022 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7023 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007024
Matt Carlsone4af1af2010-02-12 14:47:05 +00007025 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7026 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7027 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007028
7029 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007030
7031 if (err)
7032 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007033 }
7034
David S. Miller6f535762007-10-11 18:08:29 -07007035 return work_done;
7036}
David S. Millerf7383c22005-05-18 22:50:53 -07007037
Matt Carlsondb219972011-11-04 09:15:03 +00007038static inline void tg3_reset_task_schedule(struct tg3 *tp)
7039{
7040 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7041 schedule_work(&tp->reset_task);
7042}
7043
7044static inline void tg3_reset_task_cancel(struct tg3 *tp)
7045{
7046 cancel_work_sync(&tp->reset_task);
7047 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007048 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007049}
7050
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007051static int tg3_poll_msix(struct napi_struct *napi, int budget)
7052{
7053 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7054 struct tg3 *tp = tnapi->tp;
7055 int work_done = 0;
7056 struct tg3_hw_status *sblk = tnapi->hw_status;
7057
7058 while (1) {
7059 work_done = tg3_poll_work(tnapi, work_done, budget);
7060
Joe Perches63c3a662011-04-26 08:12:10 +00007061 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007062 goto tx_recovery;
7063
7064 if (unlikely(work_done >= budget))
7065 break;
7066
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007067 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007068 * to tell the hw how much work has been processed,
7069 * so we must read it before checking for more work.
7070 */
7071 tnapi->last_tag = sblk->status_tag;
7072 tnapi->last_irq_tag = tnapi->last_tag;
7073 rmb();
7074
7075 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007076 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7077 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007078
7079 /* This test here is not race free, but will reduce
7080 * the number of interrupts by looping again.
7081 */
7082 if (tnapi == &tp->napi[1] && tp->rx_refill)
7083 continue;
7084
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007085 napi_complete(napi);
7086 /* Reenable interrupts. */
7087 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007088
7089 /* This test here is synchronized by napi_schedule()
7090 * and napi_complete() to close the race condition.
7091 */
7092 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7093 tw32(HOSTCC_MODE, tp->coalesce_mode |
7094 HOSTCC_MODE_ENABLE |
7095 tnapi->coal_now);
7096 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007097 mmiowb();
7098 break;
7099 }
7100 }
7101
7102 return work_done;
7103
7104tx_recovery:
7105 /* work_done is guaranteed to be less than budget. */
7106 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007107 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007108 return work_done;
7109}
7110
Matt Carlsone64de4e2011-04-13 11:05:05 +00007111static void tg3_process_error(struct tg3 *tp)
7112{
7113 u32 val;
7114 bool real_error = false;
7115
Joe Perches63c3a662011-04-26 08:12:10 +00007116 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007117 return;
7118
7119 /* Check Flow Attention register */
7120 val = tr32(HOSTCC_FLOW_ATTN);
7121 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7122 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7123 real_error = true;
7124 }
7125
7126 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7127 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7128 real_error = true;
7129 }
7130
7131 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7132 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7133 real_error = true;
7134 }
7135
7136 if (!real_error)
7137 return;
7138
7139 tg3_dump_state(tp);
7140
Joe Perches63c3a662011-04-26 08:12:10 +00007141 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007142 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007143}
7144
David S. Miller6f535762007-10-11 18:08:29 -07007145static int tg3_poll(struct napi_struct *napi, int budget)
7146{
Matt Carlson8ef04422009-08-28 14:01:37 +00007147 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7148 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007149 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007150 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007151
7152 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007153 if (sblk->status & SD_STATUS_ERROR)
7154 tg3_process_error(tp);
7155
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007156 tg3_poll_link(tp);
7157
Matt Carlson17375d22009-08-28 14:02:18 +00007158 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007159
Joe Perches63c3a662011-04-26 08:12:10 +00007160 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007161 goto tx_recovery;
7162
7163 if (unlikely(work_done >= budget))
7164 break;
7165
Joe Perches63c3a662011-04-26 08:12:10 +00007166 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007167 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007168 * to tell the hw how much work has been processed,
7169 * so we must read it before checking for more work.
7170 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007171 tnapi->last_tag = sblk->status_tag;
7172 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007173 rmb();
7174 } else
7175 sblk->status &= ~SD_STATUS_UPDATED;
7176
Matt Carlson17375d22009-08-28 14:02:18 +00007177 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007178 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007179 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007180 break;
7181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 }
7183
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007184 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007185
7186tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007187 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007188 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007189 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007190 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191}
7192
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007193static void tg3_napi_disable(struct tg3 *tp)
7194{
7195 int i;
7196
7197 for (i = tp->irq_cnt - 1; i >= 0; i--)
7198 napi_disable(&tp->napi[i].napi);
7199}
7200
7201static void tg3_napi_enable(struct tg3 *tp)
7202{
7203 int i;
7204
7205 for (i = 0; i < tp->irq_cnt; i++)
7206 napi_enable(&tp->napi[i].napi);
7207}
7208
7209static void tg3_napi_init(struct tg3 *tp)
7210{
7211 int i;
7212
7213 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7214 for (i = 1; i < tp->irq_cnt; i++)
7215 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7216}
7217
7218static void tg3_napi_fini(struct tg3 *tp)
7219{
7220 int i;
7221
7222 for (i = 0; i < tp->irq_cnt; i++)
7223 netif_napi_del(&tp->napi[i].napi);
7224}
7225
7226static inline void tg3_netif_stop(struct tg3 *tp)
7227{
7228 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7229 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007230 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007231 netif_tx_disable(tp->dev);
7232}
7233
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007234/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007235static inline void tg3_netif_start(struct tg3 *tp)
7236{
Matt Carlsonbe947302012-12-03 19:36:57 +00007237 tg3_ptp_resume(tp);
7238
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007239 /* NOTE: unconditional netif_tx_wake_all_queues is only
7240 * appropriate so long as all callers are assured to
7241 * have free tx slots (such as after tg3_init_hw)
7242 */
7243 netif_tx_wake_all_queues(tp->dev);
7244
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007245 if (tp->link_up)
7246 netif_carrier_on(tp->dev);
7247
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007248 tg3_napi_enable(tp);
7249 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7250 tg3_enable_ints(tp);
7251}
7252
David S. Millerf47c11e2005-06-24 20:18:35 -07007253static void tg3_irq_quiesce(struct tg3 *tp)
7254{
Matt Carlson4f125f42009-09-01 12:55:02 +00007255 int i;
7256
David S. Millerf47c11e2005-06-24 20:18:35 -07007257 BUG_ON(tp->irq_sync);
7258
7259 tp->irq_sync = 1;
7260 smp_mb();
7261
Matt Carlson4f125f42009-09-01 12:55:02 +00007262 for (i = 0; i < tp->irq_cnt; i++)
7263 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007264}
7265
David S. Millerf47c11e2005-06-24 20:18:35 -07007266/* Fully shutdown all tg3 driver activity elsewhere in the system.
7267 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7268 * with as well. Most of the time, this is not necessary except when
7269 * shutting down the device.
7270 */
7271static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7272{
Michael Chan46966542007-07-11 19:47:19 -07007273 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007274 if (irq_sync)
7275 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007276}
7277
7278static inline void tg3_full_unlock(struct tg3 *tp)
7279{
David S. Millerf47c11e2005-06-24 20:18:35 -07007280 spin_unlock_bh(&tp->lock);
7281}
7282
Michael Chanfcfa0a32006-03-20 22:28:41 -08007283/* One-shot MSI handler - Chip automatically disables interrupt
7284 * after sending MSI so driver doesn't have to do it.
7285 */
David Howells7d12e782006-10-05 14:55:46 +01007286static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007287{
Matt Carlson09943a12009-08-28 14:01:57 +00007288 struct tg3_napi *tnapi = dev_id;
7289 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007290
Matt Carlson898a56f2009-08-28 14:02:40 +00007291 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007292 if (tnapi->rx_rcb)
7293 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007294
7295 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007296 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007297
7298 return IRQ_HANDLED;
7299}
7300
Michael Chan88b06bc22005-04-21 17:13:25 -07007301/* MSI ISR - No need to check for interrupt sharing and no need to
7302 * flush status block and interrupt mailbox. PCI ordering rules
7303 * guarantee that MSI will arrive after the status block.
7304 */
David Howells7d12e782006-10-05 14:55:46 +01007305static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007306{
Matt Carlson09943a12009-08-28 14:01:57 +00007307 struct tg3_napi *tnapi = dev_id;
7308 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007309
Matt Carlson898a56f2009-08-28 14:02:40 +00007310 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007311 if (tnapi->rx_rcb)
7312 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007313 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007314 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007315 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007316 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007317 * NIC to stop sending us irqs, engaging "in-intr-handler"
7318 * event coalescing.
7319 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007320 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007321 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007322 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007323
Michael Chan88b06bc22005-04-21 17:13:25 -07007324 return IRQ_RETVAL(1);
7325}
7326
David Howells7d12e782006-10-05 14:55:46 +01007327static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328{
Matt Carlson09943a12009-08-28 14:01:57 +00007329 struct tg3_napi *tnapi = dev_id;
7330 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007331 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 unsigned int handled = 1;
7333
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 /* In INTx mode, it is possible for the interrupt to arrive at
7335 * the CPU before the status block posted prior to the interrupt.
7336 * Reading the PCI State register will confirm whether the
7337 * interrupt is ours and will flush the status block.
7338 */
Michael Chand18edcb2007-03-24 20:57:11 -07007339 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007340 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007341 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7342 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007343 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007344 }
Michael Chand18edcb2007-03-24 20:57:11 -07007345 }
7346
7347 /*
7348 * Writing any value to intr-mbox-0 clears PCI INTA# and
7349 * chip-internal interrupt pending events.
7350 * Writing non-zero to intr-mbox-0 additional tells the
7351 * NIC to stop sending us irqs, engaging "in-intr-handler"
7352 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007353 *
7354 * Flush the mailbox to de-assert the IRQ immediately to prevent
7355 * spurious interrupts. The flush impacts performance but
7356 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007357 */
Michael Chanc04cb342007-05-07 00:26:15 -07007358 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007359 if (tg3_irq_sync(tp))
7360 goto out;
7361 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007362 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007363 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007364 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007365 } else {
7366 /* No work, shared interrupt perhaps? re-enable
7367 * interrupts, and flush that PCI write
7368 */
7369 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7370 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007371 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007372out:
David S. Millerfac9b832005-05-18 22:46:34 -07007373 return IRQ_RETVAL(handled);
7374}
7375
David Howells7d12e782006-10-05 14:55:46 +01007376static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007377{
Matt Carlson09943a12009-08-28 14:01:57 +00007378 struct tg3_napi *tnapi = dev_id;
7379 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007380 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007381 unsigned int handled = 1;
7382
David S. Millerfac9b832005-05-18 22:46:34 -07007383 /* In INTx mode, it is possible for the interrupt to arrive at
7384 * the CPU before the status block posted prior to the interrupt.
7385 * Reading the PCI State register will confirm whether the
7386 * interrupt is ours and will flush the status block.
7387 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007388 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007389 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007390 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7391 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 }
Michael Chand18edcb2007-03-24 20:57:11 -07007394 }
7395
7396 /*
7397 * writing any value to intr-mbox-0 clears PCI INTA# and
7398 * chip-internal interrupt pending events.
7399 * writing non-zero to intr-mbox-0 additional tells the
7400 * NIC to stop sending us irqs, engaging "in-intr-handler"
7401 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007402 *
7403 * Flush the mailbox to de-assert the IRQ immediately to prevent
7404 * spurious interrupts. The flush impacts performance but
7405 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007406 */
Michael Chanc04cb342007-05-07 00:26:15 -07007407 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007408
7409 /*
7410 * In a shared interrupt configuration, sometimes other devices'
7411 * interrupts will scream. We record the current status tag here
7412 * so that the above check can report that the screaming interrupts
7413 * are unhandled. Eventually they will be silenced.
7414 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007415 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007416
Michael Chand18edcb2007-03-24 20:57:11 -07007417 if (tg3_irq_sync(tp))
7418 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007419
Matt Carlson72334482009-08-28 14:03:01 +00007420 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007421
Matt Carlson09943a12009-08-28 14:01:57 +00007422 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007423
David S. Millerf47c11e2005-06-24 20:18:35 -07007424out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 return IRQ_RETVAL(handled);
7426}
7427
Michael Chan79381092005-04-21 17:13:59 -07007428/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007429static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007430{
Matt Carlson09943a12009-08-28 14:01:57 +00007431 struct tg3_napi *tnapi = dev_id;
7432 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007433 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007434
Michael Chanf9804dd2005-09-27 12:13:10 -07007435 if ((sblk->status & SD_STATUS_UPDATED) ||
7436 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007437 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007438 return IRQ_RETVAL(1);
7439 }
7440 return IRQ_RETVAL(0);
7441}
7442
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443#ifdef CONFIG_NET_POLL_CONTROLLER
7444static void tg3_poll_controller(struct net_device *dev)
7445{
Matt Carlson4f125f42009-09-01 12:55:02 +00007446 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007447 struct tg3 *tp = netdev_priv(dev);
7448
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007449 if (tg3_irq_sync(tp))
7450 return;
7451
Matt Carlson4f125f42009-09-01 12:55:02 +00007452 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007453 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454}
7455#endif
7456
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457static void tg3_tx_timeout(struct net_device *dev)
7458{
7459 struct tg3 *tp = netdev_priv(dev);
7460
Michael Chanb0408752007-02-13 12:18:30 -08007461 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007462 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007463 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
Matt Carlsondb219972011-11-04 09:15:03 +00007466 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467}
7468
Michael Chanc58ec932005-09-17 00:46:27 -07007469/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7470static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7471{
7472 u32 base = (u32) mapping & 0xffffffff;
7473
Eric Dumazet807540b2010-09-23 05:40:09 +00007474 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007475}
7476
Michael Chan72f2afb2006-03-06 19:28:35 -08007477/* Test for DMA addresses > 40-bit */
7478static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7479 int len)
7480{
7481#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007482 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007483 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007484 return 0;
7485#else
7486 return 0;
7487#endif
7488}
7489
Matt Carlsond1a3b732011-07-27 14:20:51 +00007490static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007491 dma_addr_t mapping, u32 len, u32 flags,
7492 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007493{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007494 txbd->addr_hi = ((u64) mapping >> 32);
7495 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7496 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7497 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499
Matt Carlson84b67b22011-07-27 14:20:52 +00007500static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007501 dma_addr_t map, u32 len, u32 flags,
7502 u32 mss, u32 vlan)
7503{
7504 struct tg3 *tp = tnapi->tp;
7505 bool hwbug = false;
7506
7507 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007508 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007509
7510 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007511 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007512
7513 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007514 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007515
Matt Carlsona4cb4282011-12-14 11:09:58 +00007516 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007517 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007518 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007519 while (len > tp->dma_limit && *budget) {
7520 u32 frag_len = tp->dma_limit;
7521 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007522
Matt Carlsonb9e45482011-11-04 09:14:59 +00007523 /* Avoid the 8byte DMA problem */
7524 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007525 len += tp->dma_limit / 2;
7526 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007527 }
7528
Matt Carlsonb9e45482011-11-04 09:14:59 +00007529 tnapi->tx_buffers[*entry].fragmented = true;
7530
7531 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7532 frag_len, tmp_flag, mss, vlan);
7533 *budget -= 1;
7534 prvidx = *entry;
7535 *entry = NEXT_TX(*entry);
7536
Matt Carlsone31aa982011-07-27 14:20:53 +00007537 map += frag_len;
7538 }
7539
7540 if (len) {
7541 if (*budget) {
7542 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7543 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007544 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007545 *entry = NEXT_TX(*entry);
7546 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007547 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007548 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007549 }
7550 }
7551 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007552 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7553 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007554 *entry = NEXT_TX(*entry);
7555 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007556
7557 return hwbug;
7558}
7559
Matt Carlson0d681b22011-07-27 14:20:49 +00007560static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007561{
7562 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007563 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007564 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007565
Matt Carlson0d681b22011-07-27 14:20:49 +00007566 skb = txb->skb;
7567 txb->skb = NULL;
7568
Matt Carlson432aa7e2011-05-19 12:12:45 +00007569 pci_unmap_single(tnapi->tp->pdev,
7570 dma_unmap_addr(txb, mapping),
7571 skb_headlen(skb),
7572 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007573
7574 while (txb->fragmented) {
7575 txb->fragmented = false;
7576 entry = NEXT_TX(entry);
7577 txb = &tnapi->tx_buffers[entry];
7578 }
7579
Matt Carlsonba1142e2011-11-04 09:15:00 +00007580 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007581 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007582
7583 entry = NEXT_TX(entry);
7584 txb = &tnapi->tx_buffers[entry];
7585
7586 pci_unmap_page(tnapi->tp->pdev,
7587 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007588 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007589
7590 while (txb->fragmented) {
7591 txb->fragmented = false;
7592 entry = NEXT_TX(entry);
7593 txb = &tnapi->tx_buffers[entry];
7594 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007595 }
7596}
7597
Michael Chan72f2afb2006-03-06 19:28:35 -08007598/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007599static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007600 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007601 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007602 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007604 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007605 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007606 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007607 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608
Joe Perches41535772013-02-16 11:20:04 +00007609 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007610 new_skb = skb_copy(skb, GFP_ATOMIC);
7611 else {
7612 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7613
7614 new_skb = skb_copy_expand(skb,
7615 skb_headroom(skb) + more_headroom,
7616 skb_tailroom(skb), GFP_ATOMIC);
7617 }
7618
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007620 ret = -1;
7621 } else {
7622 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007623 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7624 PCI_DMA_TODEVICE);
7625 /* Make sure the mapping succeeded */
7626 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007627 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007628 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007629 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007630 u32 save_entry = *entry;
7631
Matt Carlson92cd3a12011-07-27 14:20:47 +00007632 base_flags |= TXD_FLAG_END;
7633
Matt Carlson84b67b22011-07-27 14:20:52 +00007634 tnapi->tx_buffers[*entry].skb = new_skb;
7635 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007636 mapping, new_addr);
7637
Matt Carlson84b67b22011-07-27 14:20:52 +00007638 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007639 new_skb->len, base_flags,
7640 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007641 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007642 dev_kfree_skb(new_skb);
7643 ret = -1;
7644 }
Michael Chanc58ec932005-09-17 00:46:27 -07007645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 }
7647
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007649 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651}
7652
Matt Carlson2ffcc982011-05-19 12:12:44 +00007653static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007654
7655/* Use GSO to workaround a rare TSO bug that may be triggered when the
7656 * TSO header is greater than 80 bytes.
7657 */
7658static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7659{
7660 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007661 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007662
7663 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007664 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007665 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007666
7667 /* netif_tx_stop_queue() must be done before checking
7668 * checking tx index in tg3_tx_avail() below, because in
7669 * tg3_tx(), we update tx index before checking for
7670 * netif_tx_queue_stopped().
7671 */
7672 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007673 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007674 return NETDEV_TX_BUSY;
7675
7676 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007677 }
7678
7679 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007680 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007681 goto tg3_tso_bug_end;
7682
7683 do {
7684 nskb = segs;
7685 segs = segs->next;
7686 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007687 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007688 } while (segs);
7689
7690tg3_tso_bug_end:
7691 dev_kfree_skb(skb);
7692
7693 return NETDEV_TX_OK;
7694}
Michael Chan52c0fd82006-06-29 20:15:54 -07007695
Michael Chan5a6f3072006-03-20 22:28:05 -08007696/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007697 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007698 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007699static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007700{
7701 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007702 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007703 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007704 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007705 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007706 struct tg3_napi *tnapi;
7707 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007708 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007709
Matt Carlson24f4efd2009-11-13 13:03:35 +00007710 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7711 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007712 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007713 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714
Matt Carlson84b67b22011-07-27 14:20:52 +00007715 budget = tg3_tx_avail(tnapi);
7716
Michael Chan00b70502006-06-17 21:58:45 -07007717 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007718 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007719 * interrupt. Furthermore, IRQ processing runs lockless so we have
7720 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007722 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007723 if (!netif_tx_queue_stopped(txq)) {
7724 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007725
7726 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007727 netdev_err(dev,
7728 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730 return NETDEV_TX_BUSY;
7731 }
7732
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007733 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007735 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007737
Matt Carlsonbe98da62010-07-11 09:31:46 +00007738 mss = skb_shinfo(skb)->gso_size;
7739 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007740 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007741 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742
7743 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007744 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7745 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746
Matt Carlson34195c32010-07-11 09:31:42 +00007747 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007748 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749
Eric Dumazeta5a11952012-01-23 01:22:09 +00007750 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007751
Eric Dumazeta5a11952012-01-23 01:22:09 +00007752 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007753 iph->check = 0;
7754 iph->tot_len = htons(mss + hdr_len);
7755 }
7756
Michael Chan52c0fd82006-06-29 20:15:54 -07007757 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007758 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007759 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007760
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7762 TXD_FLAG_CPU_POST_DMA);
7763
Joe Perches63c3a662011-04-26 08:12:10 +00007764 if (tg3_flag(tp, HW_TSO_1) ||
7765 tg3_flag(tp, HW_TSO_2) ||
7766 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007767 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007769 } else
7770 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7771 iph->daddr, 0,
7772 IPPROTO_TCP,
7773 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774
Joe Perches63c3a662011-04-26 08:12:10 +00007775 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007776 mss |= (hdr_len & 0xc) << 12;
7777 if (hdr_len & 0x10)
7778 base_flags |= 0x00000010;
7779 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007780 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007781 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007782 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007783 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007784 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 int tsflags;
7786
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007787 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 mss |= (tsflags << 11);
7789 }
7790 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007791 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 int tsflags;
7793
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007794 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 base_flags |= tsflags << 12;
7796 }
7797 }
7798 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007799
Matt Carlson93a700a2011-08-31 11:44:54 +00007800 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7801 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7802 base_flags |= TXD_FLAG_JMB_PKT;
7803
Matt Carlson92cd3a12011-07-27 14:20:47 +00007804 if (vlan_tx_tag_present(skb)) {
7805 base_flags |= TXD_FLAG_VLAN;
7806 vlan = vlan_tx_tag_get(skb);
7807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007809 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7810 tg3_flag(tp, TX_TSTAMP_EN)) {
7811 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7812 base_flags |= TXD_FLAG_HWTSTAMP;
7813 }
7814
Alexander Duyckf4188d82009-12-02 16:48:38 +00007815 len = skb_headlen(skb);
7816
7817 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007818 if (pci_dma_mapping_error(tp->pdev, mapping))
7819 goto drop;
7820
David S. Miller90079ce2008-09-11 04:52:51 -07007821
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007822 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007823 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824
7825 would_hit_hwbug = 0;
7826
Joe Perches63c3a662011-04-26 08:12:10 +00007827 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007828 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829
Matt Carlson84b67b22011-07-27 14:20:52 +00007830 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007831 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007832 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007833 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007834 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007835 u32 tmp_mss = mss;
7836
7837 if (!tg3_flag(tp, HW_TSO_1) &&
7838 !tg3_flag(tp, HW_TSO_2) &&
7839 !tg3_flag(tp, HW_TSO_3))
7840 tmp_mss = 0;
7841
Matt Carlsonc5665a52012-02-13 10:20:12 +00007842 /* Now loop through additional data
7843 * fragments, and queue them.
7844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 last = skb_shinfo(skb)->nr_frags - 1;
7846 for (i = 0; i <= last; i++) {
7847 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7848
Eric Dumazet9e903e02011-10-18 21:00:24 +00007849 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007850 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007851 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007853 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007854 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007855 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007856 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007857 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858
Matt Carlsonb9e45482011-11-04 09:14:59 +00007859 if (!budget ||
7860 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007861 len, base_flags |
7862 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007863 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007864 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007865 break;
7866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 }
7868 }
7869
7870 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007871 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872
7873 /* If the workaround fails due to memory/mapping
7874 * failure, silently drop this packet.
7875 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007876 entry = tnapi->tx_prod;
7877 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007878 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007879 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007880 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 }
7882
Richard Cochrand515b452011-06-19 03:31:41 +00007883 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007884 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007885
Michael Chan6541b802012-03-04 14:48:14 +00007886 /* Sync BD data before updating mailbox */
7887 wmb();
7888
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007890 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007892 tnapi->tx_prod = entry;
7893 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007894 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007895
7896 /* netif_tx_stop_queue() must be done before checking
7897 * checking tx index in tg3_tx_avail() below, because in
7898 * tg3_tx(), we update tx index before checking for
7899 * netif_tx_queue_stopped().
7900 */
7901 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007902 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007903 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007906 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007908
7909dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007910 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007911 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007912drop:
7913 dev_kfree_skb(skb);
7914drop_nofree:
7915 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007916 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917}
7918
Matt Carlson6e01b202011-08-19 13:58:20 +00007919static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7920{
7921 if (enable) {
7922 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7923 MAC_MODE_PORT_MODE_MASK);
7924
7925 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7926
7927 if (!tg3_flag(tp, 5705_PLUS))
7928 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7929
7930 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7931 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7932 else
7933 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7934 } else {
7935 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7936
7937 if (tg3_flag(tp, 5705_PLUS) ||
7938 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00007939 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00007940 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7941 }
7942
7943 tw32(MAC_MODE, tp->mac_mode);
7944 udelay(40);
7945}
7946
Matt Carlson941ec902011-08-19 13:58:23 +00007947static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007948{
Matt Carlson941ec902011-08-19 13:58:23 +00007949 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007950
7951 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00007952 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007953
Matt Carlson941ec902011-08-19 13:58:23 +00007954 if (extlpbk && tg3_phy_set_extloopbk(tp))
7955 return -EIO;
7956
7957 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007958 switch (speed) {
7959 case SPEED_10:
7960 break;
7961 case SPEED_100:
7962 bmcr |= BMCR_SPEED100;
7963 break;
7964 case SPEED_1000:
7965 default:
7966 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7967 speed = SPEED_100;
7968 bmcr |= BMCR_SPEED100;
7969 } else {
7970 speed = SPEED_1000;
7971 bmcr |= BMCR_SPEED1000;
7972 }
7973 }
7974
Matt Carlson941ec902011-08-19 13:58:23 +00007975 if (extlpbk) {
7976 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7977 tg3_readphy(tp, MII_CTRL1000, &val);
7978 val |= CTL1000_AS_MASTER |
7979 CTL1000_ENABLE_MASTER;
7980 tg3_writephy(tp, MII_CTRL1000, val);
7981 } else {
7982 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7983 MII_TG3_FET_PTEST_TRIM_2;
7984 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7985 }
7986 } else
7987 bmcr |= BMCR_LOOPBACK;
7988
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007989 tg3_writephy(tp, MII_BMCR, bmcr);
7990
7991 /* The write needs to be flushed for the FETs */
7992 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7993 tg3_readphy(tp, MII_BMCR, &bmcr);
7994
7995 udelay(40);
7996
7997 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00007998 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007999 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008000 MII_TG3_FET_PTEST_FRC_TX_LINK |
8001 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8002
8003 /* The write needs to be flushed for the AC131 */
8004 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8005 }
8006
8007 /* Reset to prevent losing 1st rx packet intermittently */
8008 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8009 tg3_flag(tp, 5780_CLASS)) {
8010 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8011 udelay(10);
8012 tw32_f(MAC_RX_MODE, tp->rx_mode);
8013 }
8014
8015 mac_mode = tp->mac_mode &
8016 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8017 if (speed == SPEED_1000)
8018 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8019 else
8020 mac_mode |= MAC_MODE_PORT_MODE_MII;
8021
Joe Perches41535772013-02-16 11:20:04 +00008022 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008023 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8024
8025 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8026 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8027 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8028 mac_mode |= MAC_MODE_LINK_POLARITY;
8029
8030 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8031 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8032 }
8033
8034 tw32(MAC_MODE, mac_mode);
8035 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008036
8037 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008038}
8039
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008040static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008041{
8042 struct tg3 *tp = netdev_priv(dev);
8043
8044 if (features & NETIF_F_LOOPBACK) {
8045 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8046 return;
8047
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008048 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008049 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008050 netif_carrier_on(tp->dev);
8051 spin_unlock_bh(&tp->lock);
8052 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8053 } else {
8054 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8055 return;
8056
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008057 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008058 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008059 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008060 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008061 spin_unlock_bh(&tp->lock);
8062 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8063 }
8064}
8065
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008066static netdev_features_t tg3_fix_features(struct net_device *dev,
8067 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008068{
8069 struct tg3 *tp = netdev_priv(dev);
8070
Joe Perches63c3a662011-04-26 08:12:10 +00008071 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008072 features &= ~NETIF_F_ALL_TSO;
8073
8074 return features;
8075}
8076
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008077static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008078{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008079 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008080
8081 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8082 tg3_set_loopback(dev, features);
8083
8084 return 0;
8085}
8086
Matt Carlson21f581a2009-08-28 14:00:25 +00008087static void tg3_rx_prodring_free(struct tg3 *tp,
8088 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 int i;
8091
Matt Carlson8fea32b2010-09-15 08:59:58 +00008092 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008093 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008094 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008095 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008096 tp->rx_pkt_map_sz);
8097
Joe Perches63c3a662011-04-26 08:12:10 +00008098 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008099 for (i = tpr->rx_jmb_cons_idx;
8100 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008101 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008102 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008103 TG3_RX_JMB_MAP_SZ);
8104 }
8105 }
8106
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008107 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109
Matt Carlson2c49a442010-09-30 10:34:35 +00008110 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008111 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008112 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113
Joe Perches63c3a662011-04-26 08:12:10 +00008114 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008115 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008116 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008117 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 }
8119}
8120
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008121/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 *
8123 * The chip has been shut down and the driver detached from
8124 * the networking, so no interrupts or new tx packets will
8125 * end up in the driver. tp->{tx,}lock are held and thus
8126 * we may not sleep.
8127 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008128static int tg3_rx_prodring_alloc(struct tg3 *tp,
8129 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130{
Matt Carlson287be122009-08-28 13:58:46 +00008131 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008133 tpr->rx_std_cons_idx = 0;
8134 tpr->rx_std_prod_idx = 0;
8135 tpr->rx_jmb_cons_idx = 0;
8136 tpr->rx_jmb_prod_idx = 0;
8137
Matt Carlson8fea32b2010-09-15 08:59:58 +00008138 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008139 memset(&tpr->rx_std_buffers[0], 0,
8140 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008141 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008142 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008143 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008144 goto done;
8145 }
8146
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008148 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149
Matt Carlson287be122009-08-28 13:58:46 +00008150 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008151 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008152 tp->dev->mtu > ETH_DATA_LEN)
8153 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8154 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008155
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 /* Initialize invariants of the rings, we only set this
8157 * stuff once. This works because the card does not
8158 * write into the rx buffer posting rings.
8159 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008160 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 struct tg3_rx_buffer_desc *rxd;
8162
Matt Carlson21f581a2009-08-28 14:00:25 +00008163 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008164 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8166 rxd->opaque = (RXD_OPAQUE_RING_STD |
8167 (i << RXD_OPAQUE_INDEX_SHIFT));
8168 }
8169
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008170 /* Now allocate fresh SKBs for each rx ring. */
8171 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008172 unsigned int frag_size;
8173
8174 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8175 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008176 netdev_warn(tp->dev,
8177 "Using a smaller RX standard ring. Only "
8178 "%d out of %d buffers were allocated "
8179 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008180 if (i == 0)
8181 goto initfail;
8182 tp->rx_pending = i;
8183 break;
8184 }
8185 }
8186
Joe Perches63c3a662011-04-26 08:12:10 +00008187 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008188 goto done;
8189
Matt Carlson2c49a442010-09-30 10:34:35 +00008190 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008191
Joe Perches63c3a662011-04-26 08:12:10 +00008192 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008193 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194
Matt Carlson2c49a442010-09-30 10:34:35 +00008195 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008196 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197
Matt Carlson0d86df82010-02-17 15:17:00 +00008198 rxd = &tpr->rx_jmb[i].std;
8199 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8200 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8201 RXD_FLAG_JUMBO;
8202 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8203 (i << RXD_OPAQUE_INDEX_SHIFT));
8204 }
8205
8206 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008207 unsigned int frag_size;
8208
8209 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8210 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008211 netdev_warn(tp->dev,
8212 "Using a smaller RX jumbo ring. Only %d "
8213 "out of %d buffers were allocated "
8214 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008215 if (i == 0)
8216 goto initfail;
8217 tp->rx_jumbo_pending = i;
8218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219 }
8220 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008221
8222done:
Michael Chan32d8c572006-07-25 16:38:29 -07008223 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008224
8225initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008226 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008227 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228}
8229
Matt Carlson21f581a2009-08-28 14:00:25 +00008230static void tg3_rx_prodring_fini(struct tg3 *tp,
8231 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232{
Matt Carlson21f581a2009-08-28 14:00:25 +00008233 kfree(tpr->rx_std_buffers);
8234 tpr->rx_std_buffers = NULL;
8235 kfree(tpr->rx_jmb_buffers);
8236 tpr->rx_jmb_buffers = NULL;
8237 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008238 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8239 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008240 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008242 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008243 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8244 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008245 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008247}
8248
Matt Carlson21f581a2009-08-28 14:00:25 +00008249static int tg3_rx_prodring_init(struct tg3 *tp,
8250 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008251{
Matt Carlson2c49a442010-09-30 10:34:35 +00008252 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8253 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008254 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008255 return -ENOMEM;
8256
Matt Carlson4bae65c2010-11-24 08:31:52 +00008257 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8258 TG3_RX_STD_RING_BYTES(tp),
8259 &tpr->rx_std_mapping,
8260 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008261 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008262 goto err_out;
8263
Joe Perches63c3a662011-04-26 08:12:10 +00008264 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008265 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008266 GFP_KERNEL);
8267 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008268 goto err_out;
8269
Matt Carlson4bae65c2010-11-24 08:31:52 +00008270 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8271 TG3_RX_JMB_RING_BYTES(tp),
8272 &tpr->rx_jmb_mapping,
8273 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008274 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008275 goto err_out;
8276 }
8277
8278 return 0;
8279
8280err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008281 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008282 return -ENOMEM;
8283}
8284
8285/* Free up pending packets in all rx/tx rings.
8286 *
8287 * The chip has been shut down and the driver detached from
8288 * the networking, so no interrupts or new tx packets will
8289 * end up in the driver. tp->{tx,}lock is not held and we are not
8290 * in an interrupt context and thus may sleep.
8291 */
8292static void tg3_free_rings(struct tg3 *tp)
8293{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008294 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008295
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008296 for (j = 0; j < tp->irq_cnt; j++) {
8297 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008298
Matt Carlson8fea32b2010-09-15 08:59:58 +00008299 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008300
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008301 if (!tnapi->tx_buffers)
8302 continue;
8303
Matt Carlson0d681b22011-07-27 14:20:49 +00008304 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8305 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008306
Matt Carlson0d681b22011-07-27 14:20:49 +00008307 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008308 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008309
Matt Carlsonba1142e2011-11-04 09:15:00 +00008310 tg3_tx_skb_unmap(tnapi, i,
8311 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008312
8313 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008314 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008315 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008316 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008317}
8318
8319/* Initialize tx/rx rings for packet processing.
8320 *
8321 * The chip has been shut down and the driver detached from
8322 * the networking, so no interrupts or new tx packets will
8323 * end up in the driver. tp->{tx,}lock are held and thus
8324 * we may not sleep.
8325 */
8326static int tg3_init_rings(struct tg3 *tp)
8327{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008328 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008329
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008330 /* Free up all the SKBs. */
8331 tg3_free_rings(tp);
8332
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008333 for (i = 0; i < tp->irq_cnt; i++) {
8334 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008335
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008336 tnapi->last_tag = 0;
8337 tnapi->last_irq_tag = 0;
8338 tnapi->hw_status->status = 0;
8339 tnapi->hw_status->status_tag = 0;
8340 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8341
8342 tnapi->tx_prod = 0;
8343 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008344 if (tnapi->tx_ring)
8345 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008346
8347 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008348 if (tnapi->rx_rcb)
8349 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008350
Matt Carlson8fea32b2010-09-15 08:59:58 +00008351 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008352 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008353 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008354 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008355 }
Matt Carlson72334482009-08-28 14:03:01 +00008356
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008357 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008358}
8359
Michael Chan49a359e2012-09-28 07:12:37 +00008360static void tg3_mem_tx_release(struct tg3 *tp)
8361{
8362 int i;
8363
8364 for (i = 0; i < tp->irq_max; i++) {
8365 struct tg3_napi *tnapi = &tp->napi[i];
8366
8367 if (tnapi->tx_ring) {
8368 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8369 tnapi->tx_ring, tnapi->tx_desc_mapping);
8370 tnapi->tx_ring = NULL;
8371 }
8372
8373 kfree(tnapi->tx_buffers);
8374 tnapi->tx_buffers = NULL;
8375 }
8376}
8377
8378static int tg3_mem_tx_acquire(struct tg3 *tp)
8379{
8380 int i;
8381 struct tg3_napi *tnapi = &tp->napi[0];
8382
8383 /* If multivector TSS is enabled, vector 0 does not handle
8384 * tx interrupts. Don't allocate any resources for it.
8385 */
8386 if (tg3_flag(tp, ENABLE_TSS))
8387 tnapi++;
8388
8389 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8390 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8391 TG3_TX_RING_SIZE, GFP_KERNEL);
8392 if (!tnapi->tx_buffers)
8393 goto err_out;
8394
8395 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8396 TG3_TX_RING_BYTES,
8397 &tnapi->tx_desc_mapping,
8398 GFP_KERNEL);
8399 if (!tnapi->tx_ring)
8400 goto err_out;
8401 }
8402
8403 return 0;
8404
8405err_out:
8406 tg3_mem_tx_release(tp);
8407 return -ENOMEM;
8408}
8409
8410static void tg3_mem_rx_release(struct tg3 *tp)
8411{
8412 int i;
8413
8414 for (i = 0; i < tp->irq_max; i++) {
8415 struct tg3_napi *tnapi = &tp->napi[i];
8416
8417 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8418
8419 if (!tnapi->rx_rcb)
8420 continue;
8421
8422 dma_free_coherent(&tp->pdev->dev,
8423 TG3_RX_RCB_RING_BYTES(tp),
8424 tnapi->rx_rcb,
8425 tnapi->rx_rcb_mapping);
8426 tnapi->rx_rcb = NULL;
8427 }
8428}
8429
8430static int tg3_mem_rx_acquire(struct tg3 *tp)
8431{
8432 unsigned int i, limit;
8433
8434 limit = tp->rxq_cnt;
8435
8436 /* If RSS is enabled, we need a (dummy) producer ring
8437 * set on vector zero. This is the true hw prodring.
8438 */
8439 if (tg3_flag(tp, ENABLE_RSS))
8440 limit++;
8441
8442 for (i = 0; i < limit; i++) {
8443 struct tg3_napi *tnapi = &tp->napi[i];
8444
8445 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8446 goto err_out;
8447
8448 /* If multivector RSS is enabled, vector 0
8449 * does not handle rx or tx interrupts.
8450 * Don't allocate any resources for it.
8451 */
8452 if (!i && tg3_flag(tp, ENABLE_RSS))
8453 continue;
8454
8455 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8456 TG3_RX_RCB_RING_BYTES(tp),
8457 &tnapi->rx_rcb_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008458 GFP_KERNEL | __GFP_ZERO);
Michael Chan49a359e2012-09-28 07:12:37 +00008459 if (!tnapi->rx_rcb)
8460 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008461 }
8462
8463 return 0;
8464
8465err_out:
8466 tg3_mem_rx_release(tp);
8467 return -ENOMEM;
8468}
8469
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008470/*
8471 * Must not be invoked with interrupt sources disabled and
8472 * the hardware shutdown down.
8473 */
8474static void tg3_free_consistent(struct tg3 *tp)
8475{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008476 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008477
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008478 for (i = 0; i < tp->irq_cnt; i++) {
8479 struct tg3_napi *tnapi = &tp->napi[i];
8480
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008481 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008482 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8483 tnapi->hw_status,
8484 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008485 tnapi->hw_status = NULL;
8486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008488
Michael Chan49a359e2012-09-28 07:12:37 +00008489 tg3_mem_rx_release(tp);
8490 tg3_mem_tx_release(tp);
8491
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008493 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8494 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 tp->hw_stats = NULL;
8496 }
8497}
8498
8499/*
8500 * Must not be invoked with interrupt sources disabled and
8501 * the hardware shutdown down. Can sleep.
8502 */
8503static int tg3_alloc_consistent(struct tg3 *tp)
8504{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008505 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008506
Matt Carlson4bae65c2010-11-24 08:31:52 +00008507 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8508 sizeof(struct tg3_hw_stats),
8509 &tp->stats_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008510 GFP_KERNEL | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511 if (!tp->hw_stats)
8512 goto err_out;
8513
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008514 for (i = 0; i < tp->irq_cnt; i++) {
8515 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008516 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008517
Matt Carlson4bae65c2010-11-24 08:31:52 +00008518 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8519 TG3_HW_STATUS_SIZE,
8520 &tnapi->status_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008521 GFP_KERNEL | __GFP_ZERO);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008522 if (!tnapi->hw_status)
8523 goto err_out;
8524
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008525 sblk = tnapi->hw_status;
8526
Michael Chan49a359e2012-09-28 07:12:37 +00008527 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008528 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008529
Michael Chan49a359e2012-09-28 07:12:37 +00008530 /*
8531 * When RSS is enabled, the status block format changes
8532 * slightly. The "rx_jumbo_consumer", "reserved",
8533 * and "rx_mini_consumer" members get mapped to the
8534 * other three rx return ring producer indexes.
8535 */
8536 switch (i) {
8537 case 1:
8538 prodptr = &sblk->idx[0].rx_producer;
8539 break;
8540 case 2:
8541 prodptr = &sblk->rx_jumbo_consumer;
8542 break;
8543 case 3:
8544 prodptr = &sblk->reserved;
8545 break;
8546 case 4:
8547 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008548 break;
8549 }
Michael Chan49a359e2012-09-28 07:12:37 +00008550 tnapi->rx_rcb_prod_idx = prodptr;
8551 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008552 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008553 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008554 }
8555
Michael Chan49a359e2012-09-28 07:12:37 +00008556 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8557 goto err_out;
8558
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 return 0;
8560
8561err_out:
8562 tg3_free_consistent(tp);
8563 return -ENOMEM;
8564}
8565
8566#define MAX_WAIT_CNT 1000
8567
8568/* To stop a block, clear the enable bit and poll till it
8569 * clears. tp->lock is held.
8570 */
Joe Perches953c96e2013-04-09 10:18:14 +00008571static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572{
8573 unsigned int i;
8574 u32 val;
8575
Joe Perches63c3a662011-04-26 08:12:10 +00008576 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 switch (ofs) {
8578 case RCVLSC_MODE:
8579 case DMAC_MODE:
8580 case MBFREE_MODE:
8581 case BUFMGR_MODE:
8582 case MEMARB_MODE:
8583 /* We can't enable/disable these bits of the
8584 * 5705/5750, just say success.
8585 */
8586 return 0;
8587
8588 default:
8589 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591 }
8592
8593 val = tr32(ofs);
8594 val &= ~enable_bit;
8595 tw32_f(ofs, val);
8596
8597 for (i = 0; i < MAX_WAIT_CNT; i++) {
8598 udelay(100);
8599 val = tr32(ofs);
8600 if ((val & enable_bit) == 0)
8601 break;
8602 }
8603
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008604 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008605 dev_err(&tp->pdev->dev,
8606 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8607 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 return -ENODEV;
8609 }
8610
8611 return 0;
8612}
8613
8614/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008615static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616{
8617 int i, err;
8618
8619 tg3_disable_ints(tp);
8620
8621 tp->rx_mode &= ~RX_MODE_ENABLE;
8622 tw32_f(MAC_RX_MODE, tp->rx_mode);
8623 udelay(10);
8624
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008625 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8626 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8627 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8628 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8629 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8630 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008632 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8633 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8634 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8635 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8636 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8637 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8638 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639
8640 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8641 tw32_f(MAC_MODE, tp->mac_mode);
8642 udelay(40);
8643
8644 tp->tx_mode &= ~TX_MODE_ENABLE;
8645 tw32_f(MAC_TX_MODE, tp->tx_mode);
8646
8647 for (i = 0; i < MAX_WAIT_CNT; i++) {
8648 udelay(100);
8649 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8650 break;
8651 }
8652 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008653 dev_err(&tp->pdev->dev,
8654 "%s timed out, TX_MODE_ENABLE will not clear "
8655 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008656 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 }
8658
Michael Chane6de8ad2005-05-05 14:42:41 -07008659 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008660 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8661 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662
8663 tw32(FTQ_RESET, 0xffffffff);
8664 tw32(FTQ_RESET, 0x00000000);
8665
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008666 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8667 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008669 for (i = 0; i < tp->irq_cnt; i++) {
8670 struct tg3_napi *tnapi = &tp->napi[i];
8671 if (tnapi->hw_status)
8672 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008674
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 return err;
8676}
8677
Michael Chanee6a99b2007-07-18 21:49:10 -07008678/* Save PCI command register before chip reset */
8679static void tg3_save_pci_state(struct tg3 *tp)
8680{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008681 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008682}
8683
8684/* Restore PCI state after chip reset */
8685static void tg3_restore_pci_state(struct tg3 *tp)
8686{
8687 u32 val;
8688
8689 /* Re-enable indirect register accesses. */
8690 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8691 tp->misc_host_ctrl);
8692
8693 /* Set MAX PCI retry to zero. */
8694 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008695 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008696 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008697 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008698 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008699 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008700 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008701 PCISTATE_ALLOW_APE_SHMEM_WR |
8702 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008703 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8704
Matt Carlson8a6eac92007-10-21 16:17:55 -07008705 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008706
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008707 if (!tg3_flag(tp, PCI_EXPRESS)) {
8708 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8709 tp->pci_cacheline_sz);
8710 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8711 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008712 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008713
Michael Chanee6a99b2007-07-18 21:49:10 -07008714 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008715 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008716 u16 pcix_cmd;
8717
8718 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8719 &pcix_cmd);
8720 pcix_cmd &= ~PCI_X_CMD_ERO;
8721 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8722 pcix_cmd);
8723 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008724
Joe Perches63c3a662011-04-26 08:12:10 +00008725 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008726
8727 /* Chip reset on 5780 will reset MSI enable bit,
8728 * so need to restore it.
8729 */
Joe Perches63c3a662011-04-26 08:12:10 +00008730 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008731 u16 ctrl;
8732
8733 pci_read_config_word(tp->pdev,
8734 tp->msi_cap + PCI_MSI_FLAGS,
8735 &ctrl);
8736 pci_write_config_word(tp->pdev,
8737 tp->msi_cap + PCI_MSI_FLAGS,
8738 ctrl | PCI_MSI_FLAGS_ENABLE);
8739 val = tr32(MSGINT_MODE);
8740 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8741 }
8742 }
8743}
8744
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745/* tp->lock is held. */
8746static int tg3_chip_reset(struct tg3 *tp)
8747{
8748 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008749 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008750 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751
David S. Millerf49639e2006-06-09 11:58:36 -07008752 tg3_nvram_lock(tp);
8753
Matt Carlson77b483f2008-08-15 14:07:24 -07008754 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8755
David S. Millerf49639e2006-06-09 11:58:36 -07008756 /* No matching tg3_nvram_unlock() after this because
8757 * chip reset below will undo the nvram lock.
8758 */
8759 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760
Michael Chanee6a99b2007-07-18 21:49:10 -07008761 /* GRC_MISC_CFG core clock reset will clear the memory
8762 * enable bit in PCI register 4 and the MSI enable bit
8763 * on some chips, so we save relevant registers here.
8764 */
8765 tg3_save_pci_state(tp);
8766
Joe Perches41535772013-02-16 11:20:04 +00008767 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008768 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008769 tw32(GRC_FASTBOOT_PC, 0);
8770
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 /*
8772 * We must avoid the readl() that normally takes place.
8773 * It locks machines, causes machine checks, and other
8774 * fun things. So, temporarily disable the 5701
8775 * hardware workaround, while we do the reset.
8776 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008777 write_op = tp->write32;
8778 if (write_op == tg3_write_flush_reg32)
8779 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008780
Michael Chand18edcb2007-03-24 20:57:11 -07008781 /* Prevent the irq handler from reading or writing PCI registers
8782 * during chip reset when the memory enable bit in the PCI command
8783 * register may be cleared. The chip does not generate interrupt
8784 * at this time, but the irq handler may still be called due to irq
8785 * sharing or irqpoll.
8786 */
Joe Perches63c3a662011-04-26 08:12:10 +00008787 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008788 for (i = 0; i < tp->irq_cnt; i++) {
8789 struct tg3_napi *tnapi = &tp->napi[i];
8790 if (tnapi->hw_status) {
8791 tnapi->hw_status->status = 0;
8792 tnapi->hw_status->status_tag = 0;
8793 }
8794 tnapi->last_tag = 0;
8795 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008796 }
Michael Chand18edcb2007-03-24 20:57:11 -07008797 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008798
8799 for (i = 0; i < tp->irq_cnt; i++)
8800 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008801
Joe Perches41535772013-02-16 11:20:04 +00008802 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008803 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8804 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8805 }
8806
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807 /* do the reset */
8808 val = GRC_MISC_CFG_CORECLK_RESET;
8809
Joe Perches63c3a662011-04-26 08:12:10 +00008810 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008811 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008812 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008813 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008814 tr32(TG3_PCIE_PHY_TSTCTL) ==
8815 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8816 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8817
Joe Perches41535772013-02-16 11:20:04 +00008818 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819 tw32(GRC_MISC_CFG, (1 << 29));
8820 val |= (1 << 29);
8821 }
8822 }
8823
Joe Perches41535772013-02-16 11:20:04 +00008824 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008825 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8826 tw32(GRC_VCPU_EXT_CTRL,
8827 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8828 }
8829
Matt Carlsonf37500d2010-08-02 11:25:59 +00008830 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008831 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008833
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834 tw32(GRC_MISC_CFG, val);
8835
Michael Chan1ee582d2005-08-09 20:16:46 -07008836 /* restore 5701 hardware bug workaround write method */
8837 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838
8839 /* Unfortunately, we have to delay before the PCI read back.
8840 * Some 575X chips even will not respond to a PCI cfg access
8841 * when the reset command is given to the chip.
8842 *
8843 * How do these hardware designers expect things to work
8844 * properly if the PCI write is posted for a long period
8845 * of time? It is always necessary to have some method by
8846 * which a register read back can occur to push the write
8847 * out which does the reset.
8848 *
8849 * For most tg3 variants the trick below was working.
8850 * Ho hum...
8851 */
8852 udelay(120);
8853
8854 /* Flush PCI posted writes. The normal MMIO registers
8855 * are inaccessible at this time so this is the only
8856 * way to make this reliably (actually, this is no longer
8857 * the case, see above). I tried to use indirect
8858 * register read/write but this upset some 5701 variants.
8859 */
8860 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8861
8862 udelay(120);
8863
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008864 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008865 u16 val16;
8866
Joe Perches41535772013-02-16 11:20:04 +00008867 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008868 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008869 u32 cfg_val;
8870
8871 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008872 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 udelay(100);
8874
8875 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8876 pci_write_config_dword(tp->pdev, 0xc4,
8877 cfg_val | (1 << 15));
8878 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008879
Matt Carlsone7126992009-08-25 10:08:16 +00008880 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008881 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008882 /*
8883 * Older PCIe devices only support the 128 byte
8884 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008885 */
Joe Perches63c3a662011-04-26 08:12:10 +00008886 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008887 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8888 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008889
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008890 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008891 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008892 PCI_EXP_DEVSTA_CED |
8893 PCI_EXP_DEVSTA_NFED |
8894 PCI_EXP_DEVSTA_FED |
8895 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 }
8897
Michael Chanee6a99b2007-07-18 21:49:10 -07008898 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899
Joe Perches63c3a662011-04-26 08:12:10 +00008900 tg3_flag_clear(tp, CHIP_RESETTING);
8901 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008902
Michael Chanee6a99b2007-07-18 21:49:10 -07008903 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008904 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008905 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008906 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
Joe Perches41535772013-02-16 11:20:04 +00008908 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 tg3_stop_fw(tp);
8910 tw32(0x5000, 0x400);
8911 }
8912
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00008913 if (tg3_flag(tp, IS_SSB_CORE)) {
8914 /*
8915 * BCM4785: In order to avoid repercussions from using
8916 * potentially defective internal ROM, stop the Rx RISC CPU,
8917 * which is not required.
8918 */
8919 tg3_stop_fw(tp);
8920 tg3_halt_cpu(tp, RX_CPU_BASE);
8921 }
8922
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923 tw32(GRC_MODE, tp->grc_mode);
8924
Joe Perches41535772013-02-16 11:20:04 +00008925 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008926 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927
8928 tw32(0xc4, val | (1 << 15));
8929 }
8930
8931 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00008932 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00008934 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8936 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8937 }
8938
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008939 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008940 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008941 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008942 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008943 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008944 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008946 val = 0;
8947
8948 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949 udelay(40);
8950
Matt Carlson77b483f2008-08-15 14:07:24 -07008951 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8952
Michael Chan7a6f4362006-09-27 16:03:31 -07008953 err = tg3_poll_fw(tp);
8954 if (err)
8955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956
Matt Carlson0a9140c2009-08-28 12:27:50 +00008957 tg3_mdio_start(tp);
8958
Joe Perches63c3a662011-04-26 08:12:10 +00008959 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00008960 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
8961 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008962 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008963 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964
8965 tw32(0x7c00, val | (1 << 25));
8966 }
8967
Joe Perches41535772013-02-16 11:20:04 +00008968 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00008969 val = tr32(TG3_CPMU_CLCK_ORIDE);
8970 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8971 }
8972
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008974 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00008975 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
8976 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
8977
Joe Perches63c3a662011-04-26 08:12:10 +00008978 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8980 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8981 u32 nic_cfg;
8982
8983 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8984 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008985 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008986 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008987 if (tg3_flag(tp, 5750_PLUS))
8988 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00008989
8990 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
8991 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
8992 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
8993 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
8994 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995 }
8996 }
8997
8998 return 0;
8999}
9000
Matt Carlson65ec6982012-02-28 23:33:37 +00009001static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9002static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009003
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009005static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006{
9007 int err;
9008
9009 tg3_stop_fw(tp);
9010
Michael Chan944d9802005-05-29 14:57:48 -07009011 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009013 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014 err = tg3_chip_reset(tp);
9015
Joe Perches953c96e2013-04-09 10:18:14 +00009016 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009017
Michael Chan944d9802005-05-29 14:57:48 -07009018 tg3_write_sig_legacy(tp, kind);
9019 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020
Matt Carlson92feeab2011-12-08 14:40:14 +00009021 if (tp->hw_stats) {
9022 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009023 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009024 tg3_get_estats(tp, &tp->estats_prev);
9025
9026 /* And make sure the next sample is new data */
9027 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9028 }
9029
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030 if (err)
9031 return err;
9032
9033 return 0;
9034}
9035
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036static int tg3_set_mac_addr(struct net_device *dev, void *p)
9037{
9038 struct tg3 *tp = netdev_priv(dev);
9039 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009040 int err = 0;
9041 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042
Michael Chanf9804dd2005-09-27 12:13:10 -07009043 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009044 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009045
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9047
Michael Chane75f7c92006-03-20 21:33:26 -08009048 if (!netif_running(dev))
9049 return 0;
9050
Joe Perches63c3a662011-04-26 08:12:10 +00009051 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009052 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009053
Michael Chan986e0ae2007-05-05 12:10:20 -07009054 addr0_high = tr32(MAC_ADDR_0_HIGH);
9055 addr0_low = tr32(MAC_ADDR_0_LOW);
9056 addr1_high = tr32(MAC_ADDR_1_HIGH);
9057 addr1_low = tr32(MAC_ADDR_1_LOW);
9058
9059 /* Skip MAC addr 1 if ASF is using it. */
9060 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9061 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009062 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009063 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009064 spin_lock_bh(&tp->lock);
9065 __tg3_set_mac_addr(tp, skip_mac_1);
9066 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067
Michael Chanb9ec6c12006-07-25 16:37:27 -07009068 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069}
9070
9071/* tp->lock is held. */
9072static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9073 dma_addr_t mapping, u32 maxlen_flags,
9074 u32 nic_addr)
9075{
9076 tg3_write_mem(tp,
9077 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9078 ((u64) mapping >> 32));
9079 tg3_write_mem(tp,
9080 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9081 ((u64) mapping & 0xffffffff));
9082 tg3_write_mem(tp,
9083 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9084 maxlen_flags);
9085
Joe Perches63c3a662011-04-26 08:12:10 +00009086 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087 tg3_write_mem(tp,
9088 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9089 nic_addr);
9090}
9091
Michael Chana489b6d2012-09-28 07:12:39 +00009092
9093static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009094{
Michael Chana489b6d2012-09-28 07:12:39 +00009095 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009096
Joe Perches63c3a662011-04-26 08:12:10 +00009097 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009098 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9099 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9100 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009101 } else {
9102 tw32(HOSTCC_TXCOL_TICKS, 0);
9103 tw32(HOSTCC_TXMAX_FRAMES, 0);
9104 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009105
9106 for (; i < tp->txq_cnt; i++) {
9107 u32 reg;
9108
9109 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9110 tw32(reg, ec->tx_coalesce_usecs);
9111 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9112 tw32(reg, ec->tx_max_coalesced_frames);
9113 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9114 tw32(reg, ec->tx_max_coalesced_frames_irq);
9115 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009116 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009117
Michael Chana489b6d2012-09-28 07:12:39 +00009118 for (; i < tp->irq_max - 1; i++) {
9119 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9120 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9121 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9122 }
9123}
9124
9125static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9126{
9127 int i = 0;
9128 u32 limit = tp->rxq_cnt;
9129
Joe Perches63c3a662011-04-26 08:12:10 +00009130 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009131 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9132 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9133 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009134 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009135 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009136 tw32(HOSTCC_RXCOL_TICKS, 0);
9137 tw32(HOSTCC_RXMAX_FRAMES, 0);
9138 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009139 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009140
Michael Chana489b6d2012-09-28 07:12:39 +00009141 for (; i < limit; i++) {
9142 u32 reg;
9143
9144 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9145 tw32(reg, ec->rx_coalesce_usecs);
9146 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9147 tw32(reg, ec->rx_max_coalesced_frames);
9148 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9149 tw32(reg, ec->rx_max_coalesced_frames_irq);
9150 }
9151
9152 for (; i < tp->irq_max - 1; i++) {
9153 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9154 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9155 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9156 }
9157}
9158
9159static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9160{
9161 tg3_coal_tx_init(tp, ec);
9162 tg3_coal_rx_init(tp, ec);
9163
Joe Perches63c3a662011-04-26 08:12:10 +00009164 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009165 u32 val = ec->stats_block_coalesce_usecs;
9166
Matt Carlsonb6080e12009-09-01 13:12:00 +00009167 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9168 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9169
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009170 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009171 val = 0;
9172
9173 tw32(HOSTCC_STAT_COAL_TICKS, val);
9174 }
9175}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176
9177/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009178static void tg3_rings_reset(struct tg3 *tp)
9179{
9180 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009181 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009182 struct tg3_napi *tnapi = &tp->napi[0];
9183
9184 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00009185 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009186 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009187 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00009188 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00009189 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009190 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00009191 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009192 else
9193 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9194
9195 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9196 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9197 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9198 BDINFO_FLAGS_DISABLED);
9199
9200
9201 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00009202 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009203 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00009204 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009205 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches41535772013-02-16 11:20:04 +00009206 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9207 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009208 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009209 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9210 else
9211 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9212
9213 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9214 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9215 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9216 BDINFO_FLAGS_DISABLED);
9217
9218 /* Disable interrupts */
9219 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009220 tp->napi[0].chk_msi_cnt = 0;
9221 tp->napi[0].last_rx_cons = 0;
9222 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009223
9224 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009225 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009226 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009227 tp->napi[i].tx_prod = 0;
9228 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009229 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009230 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009231 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9232 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009233 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009234 tp->napi[i].last_rx_cons = 0;
9235 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009236 }
Joe Perches63c3a662011-04-26 08:12:10 +00009237 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009238 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009239 } else {
9240 tp->napi[0].tx_prod = 0;
9241 tp->napi[0].tx_cons = 0;
9242 tw32_mailbox(tp->napi[0].prodmbox, 0);
9243 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9244 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009245
9246 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009247 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009248 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9249 for (i = 0; i < 16; i++)
9250 tw32_tx_mbox(mbox + i * 8, 0);
9251 }
9252
9253 txrcb = NIC_SRAM_SEND_RCB;
9254 rxrcb = NIC_SRAM_RCV_RET_RCB;
9255
9256 /* Clear status block in ram. */
9257 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9258
9259 /* Set status block DMA address */
9260 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9261 ((u64) tnapi->status_mapping >> 32));
9262 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9263 ((u64) tnapi->status_mapping & 0xffffffff));
9264
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009265 if (tnapi->tx_ring) {
9266 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9267 (TG3_TX_RING_SIZE <<
9268 BDINFO_FLAGS_MAXLEN_SHIFT),
9269 NIC_SRAM_TX_BUFFER_DESC);
9270 txrcb += TG3_BDINFO_SIZE;
9271 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009272
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009273 if (tnapi->rx_rcb) {
9274 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009275 (tp->rx_ret_ring_mask + 1) <<
9276 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009277 rxrcb += TG3_BDINFO_SIZE;
9278 }
9279
9280 stblk = HOSTCC_STATBLCK_RING1;
9281
9282 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9283 u64 mapping = (u64)tnapi->status_mapping;
9284 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9285 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
9286
9287 /* Clear status block in ram. */
9288 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9289
Matt Carlson19cfaec2009-12-03 08:36:20 +00009290 if (tnapi->tx_ring) {
9291 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9292 (TG3_TX_RING_SIZE <<
9293 BDINFO_FLAGS_MAXLEN_SHIFT),
9294 NIC_SRAM_TX_BUFFER_DESC);
9295 txrcb += TG3_BDINFO_SIZE;
9296 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009297
9298 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009299 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009300 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
9301
9302 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009303 rxrcb += TG3_BDINFO_SIZE;
9304 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009305}
9306
Matt Carlsoneb07a942011-04-20 07:57:36 +00009307static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9308{
9309 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9310
Joe Perches63c3a662011-04-26 08:12:10 +00009311 if (!tg3_flag(tp, 5750_PLUS) ||
9312 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009313 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9314 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009315 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009316 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009317 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9318 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009319 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9320 else
9321 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9322
9323 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9324 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9325
9326 val = min(nic_rep_thresh, host_rep_thresh);
9327 tw32(RCVBDI_STD_THRESH, val);
9328
Joe Perches63c3a662011-04-26 08:12:10 +00009329 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009330 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9331
Joe Perches63c3a662011-04-26 08:12:10 +00009332 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009333 return;
9334
Matt Carlson513aa6e2011-11-21 15:01:18 +00009335 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009336
9337 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9338
9339 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9340 tw32(RCVBDI_JUMBO_THRESH, val);
9341
Joe Perches63c3a662011-04-26 08:12:10 +00009342 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009343 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9344}
9345
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009346static inline u32 calc_crc(unsigned char *buf, int len)
9347{
9348 u32 reg;
9349 u32 tmp;
9350 int j, k;
9351
9352 reg = 0xffffffff;
9353
9354 for (j = 0; j < len; j++) {
9355 reg ^= buf[j];
9356
9357 for (k = 0; k < 8; k++) {
9358 tmp = reg & 0x01;
9359
9360 reg >>= 1;
9361
9362 if (tmp)
9363 reg ^= 0xedb88320;
9364 }
9365 }
9366
9367 return ~reg;
9368}
9369
9370static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9371{
9372 /* accept or reject all multicast frames */
9373 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9374 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9375 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9376 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9377}
9378
9379static void __tg3_set_rx_mode(struct net_device *dev)
9380{
9381 struct tg3 *tp = netdev_priv(dev);
9382 u32 rx_mode;
9383
9384 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9385 RX_MODE_KEEP_VLAN_TAG);
9386
9387#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9388 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9389 * flag clear.
9390 */
9391 if (!tg3_flag(tp, ENABLE_ASF))
9392 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9393#endif
9394
9395 if (dev->flags & IFF_PROMISC) {
9396 /* Promiscuous mode. */
9397 rx_mode |= RX_MODE_PROMISC;
9398 } else if (dev->flags & IFF_ALLMULTI) {
9399 /* Accept all multicast. */
9400 tg3_set_multi(tp, 1);
9401 } else if (netdev_mc_empty(dev)) {
9402 /* Reject all multicast. */
9403 tg3_set_multi(tp, 0);
9404 } else {
9405 /* Accept one or more multicast(s). */
9406 struct netdev_hw_addr *ha;
9407 u32 mc_filter[4] = { 0, };
9408 u32 regidx;
9409 u32 bit;
9410 u32 crc;
9411
9412 netdev_for_each_mc_addr(ha, dev) {
9413 crc = calc_crc(ha->addr, ETH_ALEN);
9414 bit = ~crc & 0x7f;
9415 regidx = (bit & 0x60) >> 5;
9416 bit &= 0x1f;
9417 mc_filter[regidx] |= (1 << bit);
9418 }
9419
9420 tw32(MAC_HASH_REG_0, mc_filter[0]);
9421 tw32(MAC_HASH_REG_1, mc_filter[1]);
9422 tw32(MAC_HASH_REG_2, mc_filter[2]);
9423 tw32(MAC_HASH_REG_3, mc_filter[3]);
9424 }
9425
9426 if (rx_mode != tp->rx_mode) {
9427 tp->rx_mode = rx_mode;
9428 tw32_f(MAC_RX_MODE, rx_mode);
9429 udelay(10);
9430 }
9431}
9432
Michael Chan91024262012-09-28 07:12:38 +00009433static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009434{
9435 int i;
9436
9437 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009438 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009439}
9440
9441static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009442{
9443 int i;
9444
9445 if (!tg3_flag(tp, SUPPORT_MSIX))
9446 return;
9447
Michael Chan0b3ba052012-11-14 14:44:29 +00009448 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009449 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009450 return;
9451 }
9452
9453 /* Validate table against current IRQ count */
9454 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009455 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009456 break;
9457 }
9458
9459 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009460 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009461}
9462
Matt Carlson90415472011-12-16 13:33:23 +00009463static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009464{
9465 int i = 0;
9466 u32 reg = MAC_RSS_INDIR_TBL_0;
9467
9468 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9469 u32 val = tp->rss_ind_tbl[i];
9470 i++;
9471 for (; i % 8; i++) {
9472 val <<= 4;
9473 val |= tp->rss_ind_tbl[i];
9474 }
9475 tw32(reg, val);
9476 reg += 4;
9477 }
9478}
9479
Matt Carlson2d31eca2009-09-01 12:53:31 +00009480/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009481static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482{
9483 u32 val, rdmac_mode;
9484 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009485 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486
9487 tg3_disable_ints(tp);
9488
9489 tg3_stop_fw(tp);
9490
9491 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9492
Joe Perches63c3a662011-04-26 08:12:10 +00009493 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009494 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495
Matt Carlson699c0192010-12-06 08:28:51 +00009496 /* Enable MAC control of LPI */
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00009497 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9498 tg3_setup_eee(tp);
Matt Carlson699c0192010-12-06 08:28:51 +00009499
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009500 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9501 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9502 tg3_phy_pull_config(tp);
9503 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9504 }
9505
Matt Carlson603f1172010-02-12 14:47:10 +00009506 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009507 tg3_phy_reset(tp);
9508
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509 err = tg3_chip_reset(tp);
9510 if (err)
9511 return err;
9512
9513 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9514
Joe Perches41535772013-02-16 11:20:04 +00009515 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009516 val = tr32(TG3_CPMU_CTRL);
9517 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9518 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009519
9520 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9521 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9522 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9523 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9524
9525 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9526 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9527 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9528 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9529
9530 val = tr32(TG3_CPMU_HST_ACC);
9531 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9532 val |= CPMU_HST_ACC_MACCLK_6_25;
9533 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009534 }
9535
Joe Perches41535772013-02-16 11:20:04 +00009536 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009537 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9538 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9539 PCIE_PWR_MGMT_L1_THRESH_4MS;
9540 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009541
9542 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9543 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9544
9545 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009546
Matt Carlsonf40386c2009-11-02 14:24:02 +00009547 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9548 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009549 }
9550
Joe Perches63c3a662011-04-26 08:12:10 +00009551 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009552 u32 grc_mode = tr32(GRC_MODE);
9553
9554 /* Access the lower 1K of PL PCIE block registers. */
9555 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9556 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9557
9558 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9559 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9560 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9561
9562 tw32(GRC_MODE, grc_mode);
9563 }
9564
Matt Carlson55086ad2011-12-14 11:09:59 +00009565 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009566 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009567 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009568
Matt Carlson5093eed2010-11-24 08:31:45 +00009569 /* Access the lower 1K of PL PCIE block registers. */
9570 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9571 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009572
Matt Carlson5093eed2010-11-24 08:31:45 +00009573 val = tr32(TG3_PCIE_TLDLPL_PORT +
9574 TG3_PCIE_PL_LO_PHYCTL5);
9575 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9576 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009577
Matt Carlson5093eed2010-11-24 08:31:45 +00009578 tw32(GRC_MODE, grc_mode);
9579 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009580
Joe Perches41535772013-02-16 11:20:04 +00009581 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009582 u32 grc_mode;
9583
9584 /* Fix transmit hangs */
9585 val = tr32(TG3_CPMU_PADRNG_CTL);
9586 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9587 tw32(TG3_CPMU_PADRNG_CTL, val);
9588
9589 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009590
9591 /* Access the lower 1K of DL PCIE block registers. */
9592 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9593 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9594
9595 val = tr32(TG3_PCIE_TLDLPL_PORT +
9596 TG3_PCIE_DL_LO_FTSMAX);
9597 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9598 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9599 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9600
9601 tw32(GRC_MODE, grc_mode);
9602 }
9603
Matt Carlsona977dbe2010-04-12 06:58:26 +00009604 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9605 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9606 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9607 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009608 }
9609
Linus Torvalds1da177e2005-04-16 15:20:36 -07009610 /* This works around an issue with Athlon chipsets on
9611 * B3 tigon3 silicon. This bit has no effect on any
9612 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009613 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009614 */
Joe Perches63c3a662011-04-26 08:12:10 +00009615 if (!tg3_flag(tp, CPMU_PRESENT)) {
9616 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009617 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9618 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009620
Joe Perches41535772013-02-16 11:20:04 +00009621 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009622 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623 val = tr32(TG3PCI_PCISTATE);
9624 val |= PCISTATE_RETRY_SAME_DMA;
9625 tw32(TG3PCI_PCISTATE, val);
9626 }
9627
Joe Perches63c3a662011-04-26 08:12:10 +00009628 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009629 /* Allow reads and writes to the
9630 * APE register and memory space.
9631 */
9632 val = tr32(TG3PCI_PCISTATE);
9633 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009634 PCISTATE_ALLOW_APE_SHMEM_WR |
9635 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009636 tw32(TG3PCI_PCISTATE, val);
9637 }
9638
Joe Perches41535772013-02-16 11:20:04 +00009639 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640 /* Enable some hw fixes. */
9641 val = tr32(TG3PCI_MSI_DATA);
9642 val |= (1 << 26) | (1 << 28) | (1 << 29);
9643 tw32(TG3PCI_MSI_DATA, val);
9644 }
9645
9646 /* Descriptor ring init may make accesses to the
9647 * NIC SRAM area to setup the TX descriptors, so we
9648 * can only do this after the hardware has been
9649 * successfully reset.
9650 */
Michael Chan32d8c572006-07-25 16:38:29 -07009651 err = tg3_init_rings(tp);
9652 if (err)
9653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654
Joe Perches63c3a662011-04-26 08:12:10 +00009655 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009656 val = tr32(TG3PCI_DMA_RW_CTRL) &
9657 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009658 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009659 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009660 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009661 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9662 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009663 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009664 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009665 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9666 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009667 /* This value is determined during the probe time DMA
9668 * engine test, tg3_test_dma.
9669 */
9670 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672
9673 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9674 GRC_MODE_4X_NIC_SEND_RINGS |
9675 GRC_MODE_NO_TX_PHDR_CSUM |
9676 GRC_MODE_NO_RX_PHDR_CSUM);
9677 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009678
9679 /* Pseudo-header checksum is done by hardware logic and not
9680 * the offload processers, so make the chip do the pseudo-
9681 * header checksums on receive. For transmit it is more
9682 * convenient to do the pseudo-header checksum in software
9683 * as Linux does that on transmit for us in all cases.
9684 */
9685 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009687 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9688 if (tp->rxptpctl)
9689 tw32(TG3_RX_PTP_CTL,
9690 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9691
9692 if (tg3_flag(tp, PTP_CAPABLE))
9693 val |= GRC_MODE_TIME_SYNC_ENABLE;
9694
9695 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009696
9697 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9698 val = tr32(GRC_MISC_CFG);
9699 val &= ~0xff;
9700 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9701 tw32(GRC_MISC_CFG, val);
9702
9703 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009704 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009706 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009708 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9710 else
9711 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9712 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9713 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009714 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715 int fw_len;
9716
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009717 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009718 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9719 tw32(BUFMGR_MB_POOL_ADDR,
9720 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9721 tw32(BUFMGR_MB_POOL_SIZE,
9722 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724
Michael Chan0f893dc2005-07-25 12:30:38 -07009725 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9727 tp->bufmgr_config.mbuf_read_dma_low_water);
9728 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9729 tp->bufmgr_config.mbuf_mac_rx_low_water);
9730 tw32(BUFMGR_MB_HIGH_WATER,
9731 tp->bufmgr_config.mbuf_high_water);
9732 } else {
9733 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9734 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9735 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9736 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9737 tw32(BUFMGR_MB_HIGH_WATER,
9738 tp->bufmgr_config.mbuf_high_water_jumbo);
9739 }
9740 tw32(BUFMGR_DMA_LOW_WATER,
9741 tp->bufmgr_config.dma_low_water);
9742 tw32(BUFMGR_DMA_HIGH_WATER,
9743 tp->bufmgr_config.dma_high_water);
9744
Matt Carlsond309a462010-09-30 10:34:31 +00009745 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009746 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009747 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009748 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9749 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9750 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009751 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009752 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753 for (i = 0; i < 2000; i++) {
9754 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9755 break;
9756 udelay(10);
9757 }
9758 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009759 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760 return -ENODEV;
9761 }
9762
Joe Perches41535772013-02-16 11:20:04 +00009763 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009764 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009765
Matt Carlsoneb07a942011-04-20 07:57:36 +00009766 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767
9768 /* Initialize TG3_BDINFO's at:
9769 * RCVDBDI_STD_BD: standard eth size rx ring
9770 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9771 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9772 *
9773 * like so:
9774 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9775 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9776 * ring attribute flags
9777 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9778 *
9779 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9780 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9781 *
9782 * The size of each ring is fixed in the firmware, but the location is
9783 * configurable.
9784 */
9785 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009786 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009787 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009788 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009789 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009790 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9791 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009793 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009794 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9796 BDINFO_FLAGS_DISABLED);
9797
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009798 /* Program the jumbo buffer descriptor ring control
9799 * blocks on those devices that have them.
9800 */
Joe Perches41535772013-02-16 11:20:04 +00009801 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009802 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803
Joe Perches63c3a662011-04-26 08:12:10 +00009804 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009806 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009808 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009809 val = TG3_RX_JMB_RING_SIZE(tp) <<
9810 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009812 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009813 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009814 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009815 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009816 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9817 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818 } else {
9819 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9820 BDINFO_FLAGS_DISABLED);
9821 }
9822
Joe Perches63c3a662011-04-26 08:12:10 +00009823 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009824 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009825 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9826 val |= (TG3_RX_STD_DMA_SZ << 2);
9827 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009828 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009829 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009830 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009831
9832 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833
Matt Carlson411da642009-11-13 13:03:46 +00009834 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009835 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836
Joe Perches63c3a662011-04-26 08:12:10 +00009837 tpr->rx_jmb_prod_idx =
9838 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009839 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840
Matt Carlson2d31eca2009-09-01 12:53:31 +00009841 tg3_rings_reset(tp);
9842
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +00009844 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845
9846 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009847 tw32(MAC_RX_MTU_SIZE,
9848 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849
9850 /* The slot time is changed by tg3_setup_phy if we
9851 * run at gigabit with half duplex.
9852 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009853 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9854 (6 << TX_LENGTHS_IPG_SHIFT) |
9855 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9856
Joe Perches41535772013-02-16 11:20:04 +00009857 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9858 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009859 val |= tr32(MAC_TX_LENGTHS) &
9860 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9861 TX_LENGTHS_CNT_DWN_VAL_MSK);
9862
9863 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864
9865 /* Receive rules. */
9866 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9867 tw32(RCVLPC_CONFIG, 0x0181);
9868
9869 /* Calculate RDMAC_MODE setting early, we need it to determine
9870 * the RCVLPC_STATE_ENABLE mask.
9871 */
9872 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9873 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9874 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9875 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9876 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009877
Joe Perches41535772013-02-16 11:20:04 +00009878 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009879 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9880
Joe Perches41535772013-02-16 11:20:04 +00009881 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
9882 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9883 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009884 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9885 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9886 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9887
Joe Perches41535772013-02-16 11:20:04 +00009888 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9889 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009890 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009891 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009892 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9893 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009894 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9896 }
9897 }
9898
Joe Perches63c3a662011-04-26 08:12:10 +00009899 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009900 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9901
Joe Perches41535772013-02-16 11:20:04 +00009902 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009903 tp->dma_limit = 0;
9904 if (tp->dev->mtu <= ETH_DATA_LEN) {
9905 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
9906 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
9907 }
9908 }
9909
Joe Perches63c3a662011-04-26 08:12:10 +00009910 if (tg3_flag(tp, HW_TSO_1) ||
9911 tg3_flag(tp, HW_TSO_2) ||
9912 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009913 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9914
Matt Carlson108a6c12011-05-19 12:12:47 +00009915 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009916 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9917 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -08009918 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009919
Joe Perches41535772013-02-16 11:20:04 +00009920 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9921 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009922 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9923
Joe Perches41535772013-02-16 11:20:04 +00009924 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
9925 tg3_asic_rev(tp) == ASIC_REV_5784 ||
9926 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9927 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009928 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009929 u32 tgtreg;
9930
Joe Perches41535772013-02-16 11:20:04 +00009931 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009932 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
9933 else
9934 tgtreg = TG3_RDMA_RSRVCTRL_REG;
9935
9936 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +00009937 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9938 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009939 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9940 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9941 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9942 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9943 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9944 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009945 }
Michael Chanc65a17f2013-01-06 12:51:07 +00009946 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009947 }
9948
Joe Perches41535772013-02-16 11:20:04 +00009949 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
9950 tg3_asic_rev(tp) == ASIC_REV_5720 ||
9951 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009952 u32 tgtreg;
9953
Joe Perches41535772013-02-16 11:20:04 +00009954 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009955 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
9956 else
9957 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
9958
9959 val = tr32(tgtreg);
9960 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +00009961 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9962 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9963 }
9964
Linus Torvalds1da177e2005-04-16 15:20:36 -07009965 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009966 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009967 val = tr32(RCVLPC_STATS_ENABLE);
9968 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9969 tw32(RCVLPC_STATS_ENABLE, val);
9970 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009971 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972 val = tr32(RCVLPC_STATS_ENABLE);
9973 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9974 tw32(RCVLPC_STATS_ENABLE, val);
9975 } else {
9976 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9977 }
9978 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9979 tw32(SNDDATAI_STATSENAB, 0xffffff);
9980 tw32(SNDDATAI_STATSCTRL,
9981 (SNDDATAI_SCTRL_ENABLE |
9982 SNDDATAI_SCTRL_FASTUPD));
9983
9984 /* Setup host coalescing engine. */
9985 tw32(HOSTCC_MODE, 0);
9986 for (i = 0; i < 2000; i++) {
9987 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9988 break;
9989 udelay(10);
9990 }
9991
Michael Chand244c892005-07-05 14:42:33 -07009992 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993
Joe Perches63c3a662011-04-26 08:12:10 +00009994 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995 /* Status/statistics block address. See tg3_timer,
9996 * the tg3_periodic_fetch_stats call there, and
9997 * tg3_get_stats to see how this works for 5705/5750 chips.
9998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009999 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10000 ((u64) tp->stats_mapping >> 32));
10001 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10002 ((u64) tp->stats_mapping & 0xffffffff));
10003 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010004
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010006
10007 /* Clear statistics and status block memory areas */
10008 for (i = NIC_SRAM_STATS_BLK;
10009 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10010 i += sizeof(u32)) {
10011 tg3_write_mem(tp, i, 0);
10012 udelay(40);
10013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014 }
10015
10016 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10017
10018 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10019 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010020 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010021 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10022
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010023 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10024 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010025 /* reset to prevent losing 1st rx packet intermittently */
10026 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10027 udelay(10);
10028 }
10029
Matt Carlson3bda1252008-08-15 14:08:22 -070010030 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010031 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10032 MAC_MODE_FHDE_ENABLE;
10033 if (tg3_flag(tp, ENABLE_APE))
10034 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010035 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010036 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010037 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010038 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010039 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10040 udelay(40);
10041
Michael Chan314fba32005-04-21 17:07:04 -070010042 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010043 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010044 * register to preserve the GPIO settings for LOMs. The GPIOs,
10045 * whether used as inputs or outputs, are set by boot code after
10046 * reset.
10047 */
Joe Perches63c3a662011-04-26 08:12:10 +000010048 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010049 u32 gpio_mask;
10050
Michael Chan9d26e212006-12-07 00:21:14 -080010051 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10052 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10053 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010054
Joe Perches41535772013-02-16 11:20:04 +000010055 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010056 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10057 GRC_LCLCTRL_GPIO_OUTPUT3;
10058
Joe Perches41535772013-02-16 11:20:04 +000010059 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010060 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10061
Gary Zambranoaaf84462007-05-05 11:51:45 -070010062 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010063 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10064
10065 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010066 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010067 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10068 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10071 udelay(100);
10072
Matt Carlsonc3b50032012-01-17 15:27:23 +000010073 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010074 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010075 val |= MSGINT_MODE_ENABLE;
10076 if (tp->irq_cnt > 1)
10077 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010078 if (!tg3_flag(tp, 1SHOT_MSI))
10079 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010080 tw32(MSGINT_MODE, val);
10081 }
10082
Joe Perches63c3a662011-04-26 08:12:10 +000010083 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10085 udelay(40);
10086 }
10087
10088 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10089 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10090 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10091 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10092 WDMAC_MODE_LNGREAD_ENAB);
10093
Joe Perches41535772013-02-16 11:20:04 +000010094 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10095 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010096 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010097 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10098 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 /* nothing */
10100 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010101 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102 val |= WDMAC_MODE_RX_ACCEL;
10103 }
10104 }
10105
Michael Chand9ab5ad12006-03-20 22:27:35 -080010106 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010107 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010108 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010109
Joe Perches41535772013-02-16 11:20:04 +000010110 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010111 val |= WDMAC_MODE_BURST_ALL_DATA;
10112
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113 tw32_f(WDMAC_MODE, val);
10114 udelay(40);
10115
Joe Perches63c3a662011-04-26 08:12:10 +000010116 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010117 u16 pcix_cmd;
10118
10119 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10120 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010121 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010122 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10123 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010124 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010125 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10126 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127 }
Matt Carlson9974a352007-10-07 23:27:28 -070010128 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10129 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130 }
10131
10132 tw32_f(RDMAC_MODE, rdmac_mode);
10133 udelay(40);
10134
Joe Perches41535772013-02-16 11:20:04 +000010135 if (tg3_asic_rev(tp) == ASIC_REV_5719) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010136 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10137 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10138 break;
10139 }
10140 if (i < TG3_NUM_RDMA_CHANNELS) {
10141 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10142 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
10143 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10144 tg3_flag_set(tp, 5719_RDMA_BUG);
10145 }
10146 }
10147
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010149 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010151
Joe Perches41535772013-02-16 11:20:04 +000010152 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010153 tw32(SNDDATAC_MODE,
10154 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10155 else
10156 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10157
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10159 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010160 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010161 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010162 val |= RCVDBDI_MODE_LRG_RING_SZ;
10163 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010165 if (tg3_flag(tp, HW_TSO_1) ||
10166 tg3_flag(tp, HW_TSO_2) ||
10167 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010169 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010170 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010171 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10172 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010173 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10174
Joe Perches41535772013-02-16 11:20:04 +000010175 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176 err = tg3_load_5701_a0_firmware_fix(tp);
10177 if (err)
10178 return err;
10179 }
10180
Nithin Sujirc4dab502013-03-06 17:02:34 +000010181 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10182 /* Ignore any errors for the firmware download. If download
10183 * fails, the device will operate with EEE disabled
10184 */
10185 tg3_load_57766_firmware(tp);
10186 }
10187
Joe Perches63c3a662011-04-26 08:12:10 +000010188 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 err = tg3_load_tso_firmware(tp);
10190 if (err)
10191 return err;
10192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193
10194 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010195
Joe Perches63c3a662011-04-26 08:12:10 +000010196 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010197 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010198 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010199
Joe Perches41535772013-02-16 11:20:04 +000010200 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10201 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010202 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10203 tp->tx_mode &= ~val;
10204 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10205 }
10206
Linus Torvalds1da177e2005-04-16 15:20:36 -070010207 tw32_f(MAC_TX_MODE, tp->tx_mode);
10208 udelay(100);
10209
Joe Perches63c3a662011-04-26 08:12:10 +000010210 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010211 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010212
10213 /* Setup the "secret" hash key. */
10214 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10215 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10216 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10217 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10218 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10219 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10220 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10221 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10222 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10223 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10224 }
10225
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010227 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010228 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10229
Joe Perches63c3a662011-04-26 08:12:10 +000010230 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010231 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10232 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10233 RX_MODE_RSS_IPV6_HASH_EN |
10234 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10235 RX_MODE_RSS_IPV4_HASH_EN |
10236 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10237
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238 tw32_f(MAC_RX_MODE, tp->rx_mode);
10239 udelay(10);
10240
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 tw32(MAC_LED_CTRL, tp->led_ctrl);
10242
10243 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010244 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10246 udelay(10);
10247 }
10248 tw32_f(MAC_RX_MODE, tp->rx_mode);
10249 udelay(10);
10250
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010251 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010252 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10253 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254 /* Set drive transmission level to 1.2V */
10255 /* only if the signal pre-emphasis bit is not set */
10256 val = tr32(MAC_SERDES_CFG);
10257 val &= 0xfffff000;
10258 val |= 0x880;
10259 tw32(MAC_SERDES_CFG, val);
10260 }
Joe Perches41535772013-02-16 11:20:04 +000010261 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010262 tw32(MAC_SERDES_CFG, 0x616000);
10263 }
10264
10265 /* Prevent chip from dropping frames when flow control
10266 * is enabled.
10267 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010268 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010269 val = 1;
10270 else
10271 val = 2;
10272 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010273
Joe Perches41535772013-02-16 11:20:04 +000010274 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010275 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010276 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010277 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 }
10279
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010280 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010281 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010282 u32 tmp;
10283
10284 tmp = tr32(SERDES_RX_CTRL);
10285 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10286 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10287 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10288 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10289 }
10290
Joe Perches63c3a662011-04-26 08:12:10 +000010291 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010292 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010293 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294
Joe Perches953c96e2013-04-09 10:18:14 +000010295 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010296 if (err)
10297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010298
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010299 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10300 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010301 u32 tmp;
10302
10303 /* Clear CRC stats. */
10304 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10305 tg3_writephy(tp, MII_TG3_TEST1,
10306 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010307 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010309 }
10310 }
10311
10312 __tg3_set_rx_mode(tp->dev);
10313
10314 /* Initialize receive rules. */
10315 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10316 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10317 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10318 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10319
Joe Perches63c3a662011-04-26 08:12:10 +000010320 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321 limit = 8;
10322 else
10323 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010324 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010325 limit -= 4;
10326 switch (limit) {
10327 case 16:
10328 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10329 case 15:
10330 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10331 case 14:
10332 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10333 case 13:
10334 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10335 case 12:
10336 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10337 case 11:
10338 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10339 case 10:
10340 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10341 case 9:
10342 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10343 case 8:
10344 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10345 case 7:
10346 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10347 case 6:
10348 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10349 case 5:
10350 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10351 case 4:
10352 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10353 case 3:
10354 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10355 case 2:
10356 case 1:
10357
10358 default:
10359 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010361
Joe Perches63c3a662011-04-26 08:12:10 +000010362 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010363 /* Write our heartbeat update interval to APE. */
10364 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10365 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010366
Linus Torvalds1da177e2005-04-16 15:20:36 -070010367 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10368
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369 return 0;
10370}
10371
10372/* Called at device open time to get the chip ready for
10373 * packet processing. Invoked with tp->lock held.
10374 */
Joe Perches953c96e2013-04-09 10:18:14 +000010375static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010377 tg3_switch_clocks(tp);
10378
10379 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10380
Matt Carlson2f751b62008-08-04 23:17:34 -070010381 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010382}
10383
Michael Chanaed93e02012-07-16 16:24:02 +000010384static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10385{
10386 int i;
10387
10388 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10389 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10390
10391 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10392 off += len;
10393
10394 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10395 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10396 memset(ocir, 0, TG3_OCIR_LEN);
10397 }
10398}
10399
10400/* sysfs attributes for hwmon */
10401static ssize_t tg3_show_temp(struct device *dev,
10402 struct device_attribute *devattr, char *buf)
10403{
10404 struct pci_dev *pdev = to_pci_dev(dev);
10405 struct net_device *netdev = pci_get_drvdata(pdev);
10406 struct tg3 *tp = netdev_priv(netdev);
10407 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10408 u32 temperature;
10409
10410 spin_lock_bh(&tp->lock);
10411 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10412 sizeof(temperature));
10413 spin_unlock_bh(&tp->lock);
10414 return sprintf(buf, "%u\n", temperature);
10415}
10416
10417
10418static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10419 TG3_TEMP_SENSOR_OFFSET);
10420static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10421 TG3_TEMP_CAUTION_OFFSET);
10422static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10423 TG3_TEMP_MAX_OFFSET);
10424
10425static struct attribute *tg3_attributes[] = {
10426 &sensor_dev_attr_temp1_input.dev_attr.attr,
10427 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10428 &sensor_dev_attr_temp1_max.dev_attr.attr,
10429 NULL
10430};
10431
10432static const struct attribute_group tg3_group = {
10433 .attrs = tg3_attributes,
10434};
10435
Michael Chanaed93e02012-07-16 16:24:02 +000010436static void tg3_hwmon_close(struct tg3 *tp)
10437{
Michael Chanaed93e02012-07-16 16:24:02 +000010438 if (tp->hwmon_dev) {
10439 hwmon_device_unregister(tp->hwmon_dev);
10440 tp->hwmon_dev = NULL;
10441 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10442 }
Michael Chanaed93e02012-07-16 16:24:02 +000010443}
10444
10445static void tg3_hwmon_open(struct tg3 *tp)
10446{
Michael Chanaed93e02012-07-16 16:24:02 +000010447 int i, err;
10448 u32 size = 0;
10449 struct pci_dev *pdev = tp->pdev;
10450 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10451
10452 tg3_sd_scan_scratchpad(tp, ocirs);
10453
10454 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10455 if (!ocirs[i].src_data_length)
10456 continue;
10457
10458 size += ocirs[i].src_hdr_length;
10459 size += ocirs[i].src_data_length;
10460 }
10461
10462 if (!size)
10463 return;
10464
10465 /* Register hwmon sysfs hooks */
10466 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10467 if (err) {
10468 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10469 return;
10470 }
10471
10472 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10473 if (IS_ERR(tp->hwmon_dev)) {
10474 tp->hwmon_dev = NULL;
10475 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10476 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10477 }
Michael Chanaed93e02012-07-16 16:24:02 +000010478}
10479
10480
Linus Torvalds1da177e2005-04-16 15:20:36 -070010481#define TG3_STAT_ADD32(PSTAT, REG) \
10482do { u32 __val = tr32(REG); \
10483 (PSTAT)->low += __val; \
10484 if ((PSTAT)->low < __val) \
10485 (PSTAT)->high += 1; \
10486} while (0)
10487
10488static void tg3_periodic_fetch_stats(struct tg3 *tp)
10489{
10490 struct tg3_hw_stats *sp = tp->hw_stats;
10491
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010492 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493 return;
10494
10495 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10496 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10497 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10498 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10499 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10500 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10501 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10502 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10503 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10504 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10505 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10506 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10507 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010508 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10509 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10510 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10511 u32 val;
10512
10513 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10514 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10515 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10516 tg3_flag_clear(tp, 5719_RDMA_BUG);
10517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010518
10519 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10520 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10521 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10522 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10523 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10524 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10525 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10526 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10527 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10528 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10529 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10530 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10531 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10532 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010533
10534 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010535 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10536 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10537 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010538 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10539 } else {
10540 u32 val = tr32(HOSTCC_FLOW_ATTN);
10541 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10542 if (val) {
10543 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10544 sp->rx_discards.low += val;
10545 if (sp->rx_discards.low < val)
10546 sp->rx_discards.high += 1;
10547 }
10548 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10549 }
Michael Chan463d3052006-05-22 16:36:27 -070010550 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010551}
10552
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010553static void tg3_chk_missed_msi(struct tg3 *tp)
10554{
10555 u32 i;
10556
10557 for (i = 0; i < tp->irq_cnt; i++) {
10558 struct tg3_napi *tnapi = &tp->napi[i];
10559
10560 if (tg3_has_work(tnapi)) {
10561 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10562 tnapi->last_tx_cons == tnapi->tx_cons) {
10563 if (tnapi->chk_msi_cnt < 1) {
10564 tnapi->chk_msi_cnt++;
10565 return;
10566 }
Matt Carlson7f230732011-08-31 11:44:48 +000010567 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010568 }
10569 }
10570 tnapi->chk_msi_cnt = 0;
10571 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10572 tnapi->last_tx_cons = tnapi->tx_cons;
10573 }
10574}
10575
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576static void tg3_timer(unsigned long __opaque)
10577{
10578 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010579
Matt Carlson5b190622011-11-04 09:15:04 +000010580 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010581 goto restart_timer;
10582
David S. Millerf47c11e2005-06-24 20:18:35 -070010583 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010584
Joe Perches41535772013-02-16 11:20:04 +000010585 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010586 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010587 tg3_chk_missed_msi(tp);
10588
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010589 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10590 /* BCM4785: Flush posted writes from GbE to host memory. */
10591 tr32(HOSTCC_MODE);
10592 }
10593
Joe Perches63c3a662011-04-26 08:12:10 +000010594 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010595 /* All of this garbage is because when using non-tagged
10596 * IRQ status the mailbox/status_block protocol the chip
10597 * uses with the cpu is race prone.
10598 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010599 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010600 tw32(GRC_LOCAL_CTRL,
10601 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10602 } else {
10603 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010604 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606
David S. Millerfac9b832005-05-18 22:46:34 -070010607 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010608 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010609 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010610 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010612 }
10613
Linus Torvalds1da177e2005-04-16 15:20:36 -070010614 /* This part only runs once per second. */
10615 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010616 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010617 tg3_periodic_fetch_stats(tp);
10618
Matt Carlsonb0c59432011-05-19 12:12:48 +000010619 if (tp->setlpicnt && !--tp->setlpicnt)
10620 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010621
Joe Perches63c3a662011-04-26 08:12:10 +000010622 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010623 u32 mac_stat;
10624 int phy_event;
10625
10626 mac_stat = tr32(MAC_STATUS);
10627
10628 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010629 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010630 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10631 phy_event = 1;
10632 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10633 phy_event = 1;
10634
10635 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010636 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010637 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010638 u32 mac_stat = tr32(MAC_STATUS);
10639 int need_setup = 0;
10640
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010641 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010642 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10643 need_setup = 1;
10644 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010645 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010646 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10647 MAC_STATUS_SIGNAL_DET))) {
10648 need_setup = 1;
10649 }
10650 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010651 if (!tp->serdes_counter) {
10652 tw32_f(MAC_MODE,
10653 (tp->mac_mode &
10654 ~MAC_MODE_PORT_MODE_MASK));
10655 udelay(40);
10656 tw32_f(MAC_MODE, tp->mac_mode);
10657 udelay(40);
10658 }
Joe Perches953c96e2013-04-09 10:18:14 +000010659 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010660 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010661 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010662 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010663 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010665
10666 tp->timer_counter = tp->timer_multiplier;
10667 }
10668
Michael Chan130b8e42006-09-27 16:00:40 -070010669 /* Heartbeat is only sent once every 2 seconds.
10670 *
10671 * The heartbeat is to tell the ASF firmware that the host
10672 * driver is still alive. In the event that the OS crashes,
10673 * ASF needs to reset the hardware to free up the FIFO space
10674 * that may be filled with rx packets destined for the host.
10675 * If the FIFO is full, ASF will no longer function properly.
10676 *
10677 * Unintended resets have been reported on real time kernels
10678 * where the timer doesn't run on time. Netpoll will also have
10679 * same problem.
10680 *
10681 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10682 * to check the ring condition when the heartbeat is expiring
10683 * before doing the reset. This will prevent most unintended
10684 * resets.
10685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010686 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010687 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010688 tg3_wait_for_event_ack(tp);
10689
Michael Chanbbadf502006-04-06 21:46:34 -070010690 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010691 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010692 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010693 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10694 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010695
10696 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010697 }
10698 tp->asf_counter = tp->asf_multiplier;
10699 }
10700
David S. Millerf47c11e2005-06-24 20:18:35 -070010701 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702
Michael Chanf475f162006-03-27 23:20:14 -080010703restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704 tp->timer.expires = jiffies + tp->timer_offset;
10705 add_timer(&tp->timer);
10706}
10707
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010708static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010709{
10710 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010711 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010712 !tg3_flag(tp, 57765_CLASS))
10713 tp->timer_offset = HZ;
10714 else
10715 tp->timer_offset = HZ / 10;
10716
10717 BUG_ON(tp->timer_offset > HZ);
10718
10719 tp->timer_multiplier = (HZ / tp->timer_offset);
10720 tp->asf_multiplier = (HZ / tp->timer_offset) *
10721 TG3_FW_UPDATE_FREQ_SEC;
10722
10723 init_timer(&tp->timer);
10724 tp->timer.data = (unsigned long) tp;
10725 tp->timer.function = tg3_timer;
10726}
10727
10728static void tg3_timer_start(struct tg3 *tp)
10729{
10730 tp->asf_counter = tp->asf_multiplier;
10731 tp->timer_counter = tp->timer_multiplier;
10732
10733 tp->timer.expires = jiffies + tp->timer_offset;
10734 add_timer(&tp->timer);
10735}
10736
10737static void tg3_timer_stop(struct tg3 *tp)
10738{
10739 del_timer_sync(&tp->timer);
10740}
10741
10742/* Restart hardware after configuration changes, self-test, etc.
10743 * Invoked with tp->lock held.
10744 */
Joe Perches953c96e2013-04-09 10:18:14 +000010745static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000010746 __releases(tp->lock)
10747 __acquires(tp->lock)
10748{
10749 int err;
10750
10751 err = tg3_init_hw(tp, reset_phy);
10752 if (err) {
10753 netdev_err(tp->dev,
10754 "Failed to re-initialize device, aborting\n");
10755 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10756 tg3_full_unlock(tp);
10757 tg3_timer_stop(tp);
10758 tp->irq_sync = 0;
10759 tg3_napi_enable(tp);
10760 dev_close(tp->dev);
10761 tg3_full_lock(tp, 0);
10762 }
10763 return err;
10764}
10765
10766static void tg3_reset_task(struct work_struct *work)
10767{
10768 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10769 int err;
10770
10771 tg3_full_lock(tp, 0);
10772
10773 if (!netif_running(tp->dev)) {
10774 tg3_flag_clear(tp, RESET_TASK_PENDING);
10775 tg3_full_unlock(tp);
10776 return;
10777 }
10778
10779 tg3_full_unlock(tp);
10780
10781 tg3_phy_stop(tp);
10782
10783 tg3_netif_stop(tp);
10784
10785 tg3_full_lock(tp, 1);
10786
10787 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10788 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10789 tp->write32_rx_mbox = tg3_write_flush_reg32;
10790 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10791 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10792 }
10793
10794 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000010795 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000010796 if (err)
10797 goto out;
10798
10799 tg3_netif_start(tp);
10800
10801out:
10802 tg3_full_unlock(tp);
10803
10804 if (!err)
10805 tg3_phy_start(tp);
10806
10807 tg3_flag_clear(tp, RESET_TASK_PENDING);
10808}
10809
Matt Carlson4f125f42009-09-01 12:55:02 +000010810static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010811{
David Howells7d12e782006-10-05 14:55:46 +010010812 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010813 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010814 char *name;
10815 struct tg3_napi *tnapi = &tp->napi[irq_num];
10816
10817 if (tp->irq_cnt == 1)
10818 name = tp->dev->name;
10819 else {
10820 name = &tnapi->irq_lbl[0];
10821 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10822 name[IFNAMSIZ-1] = 0;
10823 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010824
Joe Perches63c3a662011-04-26 08:12:10 +000010825 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010826 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010827 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010828 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010829 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010830 } else {
10831 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010832 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010833 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010834 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010835 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010836
10837 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010838}
10839
Michael Chan79381092005-04-21 17:13:59 -070010840static int tg3_test_interrupt(struct tg3 *tp)
10841{
Matt Carlson09943a12009-08-28 14:01:57 +000010842 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010843 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010844 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010845 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010846
Michael Chand4bc3922005-05-29 14:59:20 -070010847 if (!netif_running(dev))
10848 return -ENODEV;
10849
Michael Chan79381092005-04-21 17:13:59 -070010850 tg3_disable_ints(tp);
10851
Matt Carlson4f125f42009-09-01 12:55:02 +000010852 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010853
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010854 /*
10855 * Turn off MSI one shot mode. Otherwise this test has no
10856 * observable way to know whether the interrupt was delivered.
10857 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010858 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010859 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10860 tw32(MSGINT_MODE, val);
10861 }
10862
Matt Carlson4f125f42009-09-01 12:55:02 +000010863 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010864 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010865 if (err)
10866 return err;
10867
Matt Carlson898a56f2009-08-28 14:02:40 +000010868 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010869 tg3_enable_ints(tp);
10870
10871 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010872 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010873
10874 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010875 u32 int_mbox, misc_host_ctrl;
10876
Matt Carlson898a56f2009-08-28 14:02:40 +000010877 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010878 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10879
10880 if ((int_mbox != 0) ||
10881 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10882 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010883 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010884 }
10885
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010886 if (tg3_flag(tp, 57765_PLUS) &&
10887 tnapi->hw_status->status_tag != tnapi->last_tag)
10888 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10889
Michael Chan79381092005-04-21 17:13:59 -070010890 msleep(10);
10891 }
10892
10893 tg3_disable_ints(tp);
10894
Matt Carlson4f125f42009-09-01 12:55:02 +000010895 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010896
Matt Carlson4f125f42009-09-01 12:55:02 +000010897 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010898
10899 if (err)
10900 return err;
10901
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010902 if (intr_ok) {
10903 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010904 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010905 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10906 tw32(MSGINT_MODE, val);
10907 }
Michael Chan79381092005-04-21 17:13:59 -070010908 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010909 }
Michael Chan79381092005-04-21 17:13:59 -070010910
10911 return -EIO;
10912}
10913
10914/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10915 * successfully restored
10916 */
10917static int tg3_test_msi(struct tg3 *tp)
10918{
Michael Chan79381092005-04-21 17:13:59 -070010919 int err;
10920 u16 pci_cmd;
10921
Joe Perches63c3a662011-04-26 08:12:10 +000010922 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010923 return 0;
10924
10925 /* Turn off SERR reporting in case MSI terminates with Master
10926 * Abort.
10927 */
10928 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10929 pci_write_config_word(tp->pdev, PCI_COMMAND,
10930 pci_cmd & ~PCI_COMMAND_SERR);
10931
10932 err = tg3_test_interrupt(tp);
10933
10934 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10935
10936 if (!err)
10937 return 0;
10938
10939 /* other failures */
10940 if (err != -EIO)
10941 return err;
10942
10943 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010944 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10945 "to INTx mode. Please report this failure to the PCI "
10946 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010947
Matt Carlson4f125f42009-09-01 12:55:02 +000010948 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010949
Michael Chan79381092005-04-21 17:13:59 -070010950 pci_disable_msi(tp->pdev);
10951
Joe Perches63c3a662011-04-26 08:12:10 +000010952 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010953 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010954
Matt Carlson4f125f42009-09-01 12:55:02 +000010955 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010956 if (err)
10957 return err;
10958
10959 /* Need to reset the chip because the MSI cycle may have terminated
10960 * with Master Abort.
10961 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010962 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010963
Michael Chan944d9802005-05-29 14:57:48 -070010964 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000010965 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070010966
David S. Millerf47c11e2005-06-24 20:18:35 -070010967 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010968
10969 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010970 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010971
10972 return err;
10973}
10974
Matt Carlson9e9fd122009-01-19 16:57:45 -080010975static int tg3_request_firmware(struct tg3 *tp)
10976{
Nithin Sujir77997ea2013-03-06 17:02:32 +000010977 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080010978
10979 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010980 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10981 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010982 return -ENOENT;
10983 }
10984
Nithin Sujir77997ea2013-03-06 17:02:32 +000010985 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080010986
10987 /* Firmware blob starts with version numbers, followed by
10988 * start address and _full_ length including BSS sections
10989 * (which must be longer than the actual data, of course
10990 */
10991
Nithin Sujir77997ea2013-03-06 17:02:32 +000010992 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
10993 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010994 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10995 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010996 release_firmware(tp->fw);
10997 tp->fw = NULL;
10998 return -EINVAL;
10999 }
11000
11001 /* We no longer need firmware; we have it. */
11002 tp->fw_needed = NULL;
11003 return 0;
11004}
11005
Michael Chan91024262012-09-28 07:12:38 +000011006static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011007{
Michael Chan91024262012-09-28 07:12:38 +000011008 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011009
Michael Chan91024262012-09-28 07:12:38 +000011010 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011011 /* We want as many rx rings enabled as there are cpus.
11012 * In multiqueue MSI-X mode, the first MSI-X vector
11013 * only deals with link interrupts, etc, so we add
11014 * one to the number of vectors we are requesting.
11015 */
Michael Chan91024262012-09-28 07:12:38 +000011016 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011017 }
Matt Carlson679563f2009-09-01 12:55:46 +000011018
Michael Chan91024262012-09-28 07:12:38 +000011019 return irq_cnt;
11020}
11021
11022static bool tg3_enable_msix(struct tg3 *tp)
11023{
11024 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011025 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011026
Michael Chan09681692012-09-28 07:12:42 +000011027 tp->txq_cnt = tp->txq_req;
11028 tp->rxq_cnt = tp->rxq_req;
11029 if (!tp->rxq_cnt)
11030 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011031 if (tp->rxq_cnt > tp->rxq_max)
11032 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011033
11034 /* Disable multiple TX rings by default. Simple round-robin hardware
11035 * scheduling of the TX rings can cause starvation of rings with
11036 * small packets when other rings have TSO or jumbo packets.
11037 */
11038 if (!tp->txq_req)
11039 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011040
11041 tp->irq_cnt = tg3_irq_count(tp);
11042
Matt Carlson679563f2009-09-01 12:55:46 +000011043 for (i = 0; i < tp->irq_max; i++) {
11044 msix_ent[i].entry = i;
11045 msix_ent[i].vector = 0;
11046 }
11047
11048 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011049 if (rc < 0) {
11050 return false;
11051 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011052 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11053 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011054 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11055 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011056 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011057 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011058 if (tp->txq_cnt)
11059 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011060 }
11061
11062 for (i = 0; i < tp->irq_max; i++)
11063 tp->napi[i].irq_vec = msix_ent[i].vector;
11064
Michael Chan49a359e2012-09-28 07:12:37 +000011065 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011066 pci_disable_msix(tp->pdev);
11067 return false;
11068 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011069
Michael Chan91024262012-09-28 07:12:38 +000011070 if (tp->irq_cnt == 1)
11071 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011072
Michael Chan91024262012-09-28 07:12:38 +000011073 tg3_flag_set(tp, ENABLE_RSS);
11074
11075 if (tp->txq_cnt > 1)
11076 tg3_flag_set(tp, ENABLE_TSS);
11077
11078 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011079
Matt Carlson679563f2009-09-01 12:55:46 +000011080 return true;
11081}
11082
Matt Carlson07b01732009-08-28 14:01:15 +000011083static void tg3_ints_init(struct tg3 *tp)
11084{
Joe Perches63c3a662011-04-26 08:12:10 +000011085 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11086 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011087 /* All MSI supporting chips should support tagged
11088 * status. Assert that this is the case.
11089 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011090 netdev_warn(tp->dev,
11091 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011092 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011093 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011094
Joe Perches63c3a662011-04-26 08:12:10 +000011095 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11096 tg3_flag_set(tp, USING_MSIX);
11097 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11098 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011099
Joe Perches63c3a662011-04-26 08:12:10 +000011100 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011101 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011102 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011103 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011104 if (!tg3_flag(tp, 1SHOT_MSI))
11105 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011106 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11107 }
11108defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011109 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011110 tp->irq_cnt = 1;
11111 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011112 }
11113
11114 if (tp->irq_cnt == 1) {
11115 tp->txq_cnt = 1;
11116 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011117 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011118 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011119 }
Matt Carlson07b01732009-08-28 14:01:15 +000011120}
11121
11122static void tg3_ints_fini(struct tg3 *tp)
11123{
Joe Perches63c3a662011-04-26 08:12:10 +000011124 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011125 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011126 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011127 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011128 tg3_flag_clear(tp, USING_MSI);
11129 tg3_flag_clear(tp, USING_MSIX);
11130 tg3_flag_clear(tp, ENABLE_RSS);
11131 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011132}
11133
Matt Carlsonbe947302012-12-03 19:36:57 +000011134static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11135 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011136{
Michael Chand8f4cd32012-09-28 07:12:40 +000011137 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011138 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011139
Matt Carlson679563f2009-09-01 12:55:46 +000011140 /*
11141 * Setup interrupts first so we know how
11142 * many NAPI resources to allocate
11143 */
11144 tg3_ints_init(tp);
11145
Matt Carlson90415472011-12-16 13:33:23 +000011146 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011147
Linus Torvalds1da177e2005-04-16 15:20:36 -070011148 /* The placement of this call is tied
11149 * to the setup and use of Host TX descriptors.
11150 */
11151 err = tg3_alloc_consistent(tp);
11152 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000011153 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011154
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011155 tg3_napi_init(tp);
11156
Matt Carlsonfed97812009-09-01 13:10:19 +000011157 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011158
Matt Carlson4f125f42009-09-01 12:55:02 +000011159 for (i = 0; i < tp->irq_cnt; i++) {
11160 struct tg3_napi *tnapi = &tp->napi[i];
11161 err = tg3_request_irq(tp, i);
11162 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011163 for (i--; i >= 0; i--) {
11164 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011165 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011166 }
11167 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000011168 }
11169 }
Matt Carlson07b01732009-08-28 14:01:15 +000011170
David S. Millerf47c11e2005-06-24 20:18:35 -070011171 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011172
Michael Chand8f4cd32012-09-28 07:12:40 +000011173 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011174 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011175 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011176 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011177 }
11178
David S. Millerf47c11e2005-06-24 20:18:35 -070011179 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011180
Matt Carlson07b01732009-08-28 14:01:15 +000011181 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000011182 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011183
Michael Chand8f4cd32012-09-28 07:12:40 +000011184 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011185 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011186
Michael Chan79381092005-04-21 17:13:59 -070011187 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011188 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011189 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011190 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011191 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011192
Matt Carlson679563f2009-09-01 12:55:46 +000011193 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070011194 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011195
Joe Perches63c3a662011-04-26 08:12:10 +000011196 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011197 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011198
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011199 tw32(PCIE_TRANSACTION_CFG,
11200 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011201 }
Michael Chan79381092005-04-21 17:13:59 -070011202 }
11203
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011204 tg3_phy_start(tp);
11205
Michael Chanaed93e02012-07-16 16:24:02 +000011206 tg3_hwmon_open(tp);
11207
David S. Millerf47c11e2005-06-24 20:18:35 -070011208 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011209
Matt Carlson21f76382012-02-22 12:35:21 +000011210 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011211 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011212 tg3_enable_ints(tp);
11213
Matt Carlsonbe947302012-12-03 19:36:57 +000011214 if (init)
11215 tg3_ptp_init(tp);
11216 else
11217 tg3_ptp_resume(tp);
11218
11219
David S. Millerf47c11e2005-06-24 20:18:35 -070011220 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011221
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011222 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011223
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011224 /*
11225 * Reset loopback feature if it was turned on while the device was down
11226 * make sure that it's installed properly now.
11227 */
11228 if (dev->features & NETIF_F_LOOPBACK)
11229 tg3_set_loopback(dev, dev->features);
11230
Linus Torvalds1da177e2005-04-16 15:20:36 -070011231 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011232
Matt Carlson679563f2009-09-01 12:55:46 +000011233err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000011234 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11235 struct tg3_napi *tnapi = &tp->napi[i];
11236 free_irq(tnapi->irq_vec, tnapi);
11237 }
Matt Carlson07b01732009-08-28 14:01:15 +000011238
Matt Carlson679563f2009-09-01 12:55:46 +000011239err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000011240 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011241 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011242 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011243
11244err_out1:
11245 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011246
Matt Carlson07b01732009-08-28 14:01:15 +000011247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011248}
11249
Michael Chan65138592012-09-28 07:12:41 +000011250static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011251{
Matt Carlson4f125f42009-09-01 12:55:02 +000011252 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011253
Matt Carlsondb219972011-11-04 09:15:03 +000011254 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011255 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011256
Matt Carlson21f76382012-02-22 12:35:21 +000011257 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011258
Michael Chanaed93e02012-07-16 16:24:02 +000011259 tg3_hwmon_close(tp);
11260
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011261 tg3_phy_stop(tp);
11262
David S. Millerf47c11e2005-06-24 20:18:35 -070011263 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011264
11265 tg3_disable_ints(tp);
11266
Michael Chan944d9802005-05-29 14:57:48 -070011267 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011268 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011269 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011270
David S. Millerf47c11e2005-06-24 20:18:35 -070011271 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011272
Matt Carlson4f125f42009-09-01 12:55:02 +000011273 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11274 struct tg3_napi *tnapi = &tp->napi[i];
11275 free_irq(tnapi->irq_vec, tnapi);
11276 }
Matt Carlson07b01732009-08-28 14:01:15 +000011277
11278 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011279
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011280 tg3_napi_fini(tp);
11281
Linus Torvalds1da177e2005-04-16 15:20:36 -070011282 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011283}
11284
Michael Chand8f4cd32012-09-28 07:12:40 +000011285static int tg3_open(struct net_device *dev)
11286{
11287 struct tg3 *tp = netdev_priv(dev);
11288 int err;
11289
11290 if (tp->fw_needed) {
11291 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011292 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11293 if (err) {
11294 netdev_warn(tp->dev, "EEE capability disabled\n");
11295 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11296 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11297 netdev_warn(tp->dev, "EEE capability restored\n");
11298 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11299 }
11300 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011301 if (err)
11302 return err;
11303 } else if (err) {
11304 netdev_warn(tp->dev, "TSO capability disabled\n");
11305 tg3_flag_clear(tp, TSO_CAPABLE);
11306 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11307 netdev_notice(tp->dev, "TSO capability restored\n");
11308 tg3_flag_set(tp, TSO_CAPABLE);
11309 }
11310 }
11311
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011312 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011313
11314 err = tg3_power_up(tp);
11315 if (err)
11316 return err;
11317
11318 tg3_full_lock(tp, 0);
11319
11320 tg3_disable_ints(tp);
11321 tg3_flag_clear(tp, INIT_COMPLETE);
11322
11323 tg3_full_unlock(tp);
11324
Nithin Sujir942d1af2013-04-09 08:48:07 +000011325 err = tg3_start(tp,
11326 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11327 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011328 if (err) {
11329 tg3_frob_aux_power(tp, false);
11330 pci_set_power_state(tp->pdev, PCI_D3hot);
11331 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011332
Matt Carlson7d41e492012-12-03 19:36:58 +000011333 if (tg3_flag(tp, PTP_CAPABLE)) {
11334 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11335 &tp->pdev->dev);
11336 if (IS_ERR(tp->ptp_clock))
11337 tp->ptp_clock = NULL;
11338 }
11339
Linus Torvalds1da177e2005-04-16 15:20:36 -070011340 return err;
11341}
11342
11343static int tg3_close(struct net_device *dev)
11344{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011345 struct tg3 *tp = netdev_priv(dev);
11346
Matt Carlsonbe947302012-12-03 19:36:57 +000011347 tg3_ptp_fini(tp);
11348
Michael Chan65138592012-09-28 07:12:41 +000011349 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011350
11351 /* Clear stats across close / open calls */
11352 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11353 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011354
11355 tg3_power_down(tp);
11356
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011357 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011358
11359 return 0;
11360}
11361
11362static inline u64 get_stat64(tg3_stat64_t *val)
11363{
11364 return ((u64)val->high << 32) | ((u64)val->low);
11365}
11366
11367static u64 tg3_calc_crc_errors(struct tg3 *tp)
11368{
11369 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11370
11371 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011372 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11373 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011374 u32 val;
11375
11376 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11377 tg3_writephy(tp, MII_TG3_TEST1,
11378 val | MII_TG3_TEST1_CRC_EN);
11379 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11380 } else
11381 val = 0;
11382
11383 tp->phy_crc_errors += val;
11384
11385 return tp->phy_crc_errors;
11386 }
11387
11388 return get_stat64(&hw_stats->rx_fcs_errors);
11389}
11390
11391#define ESTAT_ADD(member) \
11392 estats->member = old_estats->member + \
11393 get_stat64(&hw_stats->member)
11394
11395static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11396{
11397 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11398 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11399
11400 ESTAT_ADD(rx_octets);
11401 ESTAT_ADD(rx_fragments);
11402 ESTAT_ADD(rx_ucast_packets);
11403 ESTAT_ADD(rx_mcast_packets);
11404 ESTAT_ADD(rx_bcast_packets);
11405 ESTAT_ADD(rx_fcs_errors);
11406 ESTAT_ADD(rx_align_errors);
11407 ESTAT_ADD(rx_xon_pause_rcvd);
11408 ESTAT_ADD(rx_xoff_pause_rcvd);
11409 ESTAT_ADD(rx_mac_ctrl_rcvd);
11410 ESTAT_ADD(rx_xoff_entered);
11411 ESTAT_ADD(rx_frame_too_long_errors);
11412 ESTAT_ADD(rx_jabbers);
11413 ESTAT_ADD(rx_undersize_packets);
11414 ESTAT_ADD(rx_in_length_errors);
11415 ESTAT_ADD(rx_out_length_errors);
11416 ESTAT_ADD(rx_64_or_less_octet_packets);
11417 ESTAT_ADD(rx_65_to_127_octet_packets);
11418 ESTAT_ADD(rx_128_to_255_octet_packets);
11419 ESTAT_ADD(rx_256_to_511_octet_packets);
11420 ESTAT_ADD(rx_512_to_1023_octet_packets);
11421 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11422 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11423 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11424 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11425 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11426
11427 ESTAT_ADD(tx_octets);
11428 ESTAT_ADD(tx_collisions);
11429 ESTAT_ADD(tx_xon_sent);
11430 ESTAT_ADD(tx_xoff_sent);
11431 ESTAT_ADD(tx_flow_control);
11432 ESTAT_ADD(tx_mac_errors);
11433 ESTAT_ADD(tx_single_collisions);
11434 ESTAT_ADD(tx_mult_collisions);
11435 ESTAT_ADD(tx_deferred);
11436 ESTAT_ADD(tx_excessive_collisions);
11437 ESTAT_ADD(tx_late_collisions);
11438 ESTAT_ADD(tx_collide_2times);
11439 ESTAT_ADD(tx_collide_3times);
11440 ESTAT_ADD(tx_collide_4times);
11441 ESTAT_ADD(tx_collide_5times);
11442 ESTAT_ADD(tx_collide_6times);
11443 ESTAT_ADD(tx_collide_7times);
11444 ESTAT_ADD(tx_collide_8times);
11445 ESTAT_ADD(tx_collide_9times);
11446 ESTAT_ADD(tx_collide_10times);
11447 ESTAT_ADD(tx_collide_11times);
11448 ESTAT_ADD(tx_collide_12times);
11449 ESTAT_ADD(tx_collide_13times);
11450 ESTAT_ADD(tx_collide_14times);
11451 ESTAT_ADD(tx_collide_15times);
11452 ESTAT_ADD(tx_ucast_packets);
11453 ESTAT_ADD(tx_mcast_packets);
11454 ESTAT_ADD(tx_bcast_packets);
11455 ESTAT_ADD(tx_carrier_sense_errors);
11456 ESTAT_ADD(tx_discards);
11457 ESTAT_ADD(tx_errors);
11458
11459 ESTAT_ADD(dma_writeq_full);
11460 ESTAT_ADD(dma_write_prioq_full);
11461 ESTAT_ADD(rxbds_empty);
11462 ESTAT_ADD(rx_discards);
11463 ESTAT_ADD(rx_errors);
11464 ESTAT_ADD(rx_threshold_hit);
11465
11466 ESTAT_ADD(dma_readq_full);
11467 ESTAT_ADD(dma_read_prioq_full);
11468 ESTAT_ADD(tx_comp_queue_full);
11469
11470 ESTAT_ADD(ring_set_send_prod_index);
11471 ESTAT_ADD(ring_status_update);
11472 ESTAT_ADD(nic_irqs);
11473 ESTAT_ADD(nic_avoided_irqs);
11474 ESTAT_ADD(nic_tx_threshold_hit);
11475
Matt Carlson4452d092011-05-19 12:12:51 +000011476 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011477}
11478
Matt Carlson65ec6982012-02-28 23:33:37 +000011479static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011480{
Eric Dumazet511d2222010-07-07 20:44:24 +000011481 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11483
Linus Torvalds1da177e2005-04-16 15:20:36 -070011484 stats->rx_packets = old_stats->rx_packets +
11485 get_stat64(&hw_stats->rx_ucast_packets) +
11486 get_stat64(&hw_stats->rx_mcast_packets) +
11487 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011488
Linus Torvalds1da177e2005-04-16 15:20:36 -070011489 stats->tx_packets = old_stats->tx_packets +
11490 get_stat64(&hw_stats->tx_ucast_packets) +
11491 get_stat64(&hw_stats->tx_mcast_packets) +
11492 get_stat64(&hw_stats->tx_bcast_packets);
11493
11494 stats->rx_bytes = old_stats->rx_bytes +
11495 get_stat64(&hw_stats->rx_octets);
11496 stats->tx_bytes = old_stats->tx_bytes +
11497 get_stat64(&hw_stats->tx_octets);
11498
11499 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011500 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011501 stats->tx_errors = old_stats->tx_errors +
11502 get_stat64(&hw_stats->tx_errors) +
11503 get_stat64(&hw_stats->tx_mac_errors) +
11504 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11505 get_stat64(&hw_stats->tx_discards);
11506
11507 stats->multicast = old_stats->multicast +
11508 get_stat64(&hw_stats->rx_mcast_packets);
11509 stats->collisions = old_stats->collisions +
11510 get_stat64(&hw_stats->tx_collisions);
11511
11512 stats->rx_length_errors = old_stats->rx_length_errors +
11513 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11514 get_stat64(&hw_stats->rx_undersize_packets);
11515
11516 stats->rx_over_errors = old_stats->rx_over_errors +
11517 get_stat64(&hw_stats->rxbds_empty);
11518 stats->rx_frame_errors = old_stats->rx_frame_errors +
11519 get_stat64(&hw_stats->rx_align_errors);
11520 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11521 get_stat64(&hw_stats->tx_discards);
11522 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11523 get_stat64(&hw_stats->tx_carrier_sense_errors);
11524
11525 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011526 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011527
John W. Linville4f63b872005-09-12 14:43:18 -070011528 stats->rx_missed_errors = old_stats->rx_missed_errors +
11529 get_stat64(&hw_stats->rx_discards);
11530
Eric Dumazetb0057c52010-10-10 19:55:52 +000011531 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011532 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011533}
11534
Linus Torvalds1da177e2005-04-16 15:20:36 -070011535static int tg3_get_regs_len(struct net_device *dev)
11536{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011537 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011538}
11539
11540static void tg3_get_regs(struct net_device *dev,
11541 struct ethtool_regs *regs, void *_p)
11542{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011543 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011544
11545 regs->version = 0;
11546
Matt Carlson97bd8e42011-04-13 11:05:04 +000011547 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011548
Matt Carlson80096062010-08-02 11:26:06 +000011549 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011550 return;
11551
David S. Millerf47c11e2005-06-24 20:18:35 -070011552 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011553
Matt Carlson97bd8e42011-04-13 11:05:04 +000011554 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011555
David S. Millerf47c11e2005-06-24 20:18:35 -070011556 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011557}
11558
11559static int tg3_get_eeprom_len(struct net_device *dev)
11560{
11561 struct tg3 *tp = netdev_priv(dev);
11562
11563 return tp->nvram_size;
11564}
11565
Linus Torvalds1da177e2005-04-16 15:20:36 -070011566static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11567{
11568 struct tg3 *tp = netdev_priv(dev);
11569 int ret;
11570 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011571 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011572 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011573
Joe Perches63c3a662011-04-26 08:12:10 +000011574 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011575 return -EINVAL;
11576
Matt Carlson80096062010-08-02 11:26:06 +000011577 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011578 return -EAGAIN;
11579
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580 offset = eeprom->offset;
11581 len = eeprom->len;
11582 eeprom->len = 0;
11583
11584 eeprom->magic = TG3_EEPROM_MAGIC;
11585
11586 if (offset & 3) {
11587 /* adjustments to start on required 4 byte boundary */
11588 b_offset = offset & 3;
11589 b_count = 4 - b_offset;
11590 if (b_count > len) {
11591 /* i.e. offset=1 len=2 */
11592 b_count = len;
11593 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011594 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011595 if (ret)
11596 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011597 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598 len -= b_count;
11599 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011600 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011601 }
11602
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011603 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011604 pd = &data[eeprom->len];
11605 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011606 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011607 if (ret) {
11608 eeprom->len += i;
11609 return ret;
11610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011611 memcpy(pd + i, &val, 4);
11612 }
11613 eeprom->len += i;
11614
11615 if (len & 3) {
11616 /* read last bytes not ending on 4 byte boundary */
11617 pd = &data[eeprom->len];
11618 b_count = len & 3;
11619 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011620 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011621 if (ret)
11622 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011623 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011624 eeprom->len += b_count;
11625 }
11626 return 0;
11627}
11628
Linus Torvalds1da177e2005-04-16 15:20:36 -070011629static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11630{
11631 struct tg3 *tp = netdev_priv(dev);
11632 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011633 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011634 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011635 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011636
Matt Carlson80096062010-08-02 11:26:06 +000011637 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011638 return -EAGAIN;
11639
Joe Perches63c3a662011-04-26 08:12:10 +000011640 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011641 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011642 return -EINVAL;
11643
11644 offset = eeprom->offset;
11645 len = eeprom->len;
11646
11647 if ((b_offset = (offset & 3))) {
11648 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011649 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011650 if (ret)
11651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011652 len += b_offset;
11653 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011654 if (len < 4)
11655 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011656 }
11657
11658 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011659 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011660 /* adjustments to end on required 4 byte boundary */
11661 odd_len = 1;
11662 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011663 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011664 if (ret)
11665 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011666 }
11667
11668 buf = data;
11669 if (b_offset || odd_len) {
11670 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011671 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011672 return -ENOMEM;
11673 if (b_offset)
11674 memcpy(buf, &start, 4);
11675 if (odd_len)
11676 memcpy(buf+len-4, &end, 4);
11677 memcpy(buf + b_offset, data, eeprom->len);
11678 }
11679
11680 ret = tg3_nvram_write_block(tp, offset, len, buf);
11681
11682 if (buf != data)
11683 kfree(buf);
11684
11685 return ret;
11686}
11687
11688static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11689{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011690 struct tg3 *tp = netdev_priv(dev);
11691
Joe Perches63c3a662011-04-26 08:12:10 +000011692 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011693 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011694 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011695 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011696 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11697 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011698 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011699
Linus Torvalds1da177e2005-04-16 15:20:36 -070011700 cmd->supported = (SUPPORTED_Autoneg);
11701
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011702 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011703 cmd->supported |= (SUPPORTED_1000baseT_Half |
11704 SUPPORTED_1000baseT_Full);
11705
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011706 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011707 cmd->supported |= (SUPPORTED_100baseT_Half |
11708 SUPPORTED_100baseT_Full |
11709 SUPPORTED_10baseT_Half |
11710 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011711 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011712 cmd->port = PORT_TP;
11713 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011714 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011715 cmd->port = PORT_FIBRE;
11716 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011717
Linus Torvalds1da177e2005-04-16 15:20:36 -070011718 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011719 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11720 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11721 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11722 cmd->advertising |= ADVERTISED_Pause;
11723 } else {
11724 cmd->advertising |= ADVERTISED_Pause |
11725 ADVERTISED_Asym_Pause;
11726 }
11727 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11728 cmd->advertising |= ADVERTISED_Asym_Pause;
11729 }
11730 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011731 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011732 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011733 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011734 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011735 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11736 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11737 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11738 else
11739 cmd->eth_tp_mdix = ETH_TP_MDI;
11740 }
Matt Carlson64c22182010-10-14 10:37:44 +000011741 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011742 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11743 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011744 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011745 }
Matt Carlson882e9792009-09-01 13:21:36 +000011746 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011747 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011748 cmd->autoneg = tp->link_config.autoneg;
11749 cmd->maxtxpkt = 0;
11750 cmd->maxrxpkt = 0;
11751 return 0;
11752}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011753
Linus Torvalds1da177e2005-04-16 15:20:36 -070011754static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11755{
11756 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011757 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011758
Joe Perches63c3a662011-04-26 08:12:10 +000011759 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011760 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011761 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011762 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011763 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11764 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011765 }
11766
Matt Carlson7e5856b2009-02-25 14:23:01 +000011767 if (cmd->autoneg != AUTONEG_ENABLE &&
11768 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011769 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011770
11771 if (cmd->autoneg == AUTONEG_DISABLE &&
11772 cmd->duplex != DUPLEX_FULL &&
11773 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011774 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775
Matt Carlson7e5856b2009-02-25 14:23:01 +000011776 if (cmd->autoneg == AUTONEG_ENABLE) {
11777 u32 mask = ADVERTISED_Autoneg |
11778 ADVERTISED_Pause |
11779 ADVERTISED_Asym_Pause;
11780
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011781 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011782 mask |= ADVERTISED_1000baseT_Half |
11783 ADVERTISED_1000baseT_Full;
11784
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011785 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011786 mask |= ADVERTISED_100baseT_Half |
11787 ADVERTISED_100baseT_Full |
11788 ADVERTISED_10baseT_Half |
11789 ADVERTISED_10baseT_Full |
11790 ADVERTISED_TP;
11791 else
11792 mask |= ADVERTISED_FIBRE;
11793
11794 if (cmd->advertising & ~mask)
11795 return -EINVAL;
11796
11797 mask &= (ADVERTISED_1000baseT_Half |
11798 ADVERTISED_1000baseT_Full |
11799 ADVERTISED_100baseT_Half |
11800 ADVERTISED_100baseT_Full |
11801 ADVERTISED_10baseT_Half |
11802 ADVERTISED_10baseT_Full);
11803
11804 cmd->advertising &= mask;
11805 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011806 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011807 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011808 return -EINVAL;
11809
11810 if (cmd->duplex != DUPLEX_FULL)
11811 return -EINVAL;
11812 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011813 if (speed != SPEED_100 &&
11814 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011815 return -EINVAL;
11816 }
11817 }
11818
David S. Millerf47c11e2005-06-24 20:18:35 -070011819 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011820
11821 tp->link_config.autoneg = cmd->autoneg;
11822 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011823 tp->link_config.advertising = (cmd->advertising |
11824 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011825 tp->link_config.speed = SPEED_UNKNOWN;
11826 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011827 } else {
11828 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011829 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011830 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011831 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011832
Nithin Sujirfdad8de2013-04-09 08:48:08 +000011833 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
11834
Nithin Sujirce20f162013-04-09 08:48:04 +000011835 tg3_warn_mgmt_link_flap(tp);
11836
Linus Torvalds1da177e2005-04-16 15:20:36 -070011837 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000011838 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011839
David S. Millerf47c11e2005-06-24 20:18:35 -070011840 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011841
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842 return 0;
11843}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011844
Linus Torvalds1da177e2005-04-16 15:20:36 -070011845static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11846{
11847 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011848
Rick Jones68aad782011-11-07 13:29:27 +000011849 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11850 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11851 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11852 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011853}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011854
Linus Torvalds1da177e2005-04-16 15:20:36 -070011855static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11856{
11857 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011858
Joe Perches63c3a662011-04-26 08:12:10 +000011859 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011860 wol->supported = WAKE_MAGIC;
11861 else
11862 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011863 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011864 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011865 wol->wolopts = WAKE_MAGIC;
11866 memset(&wol->sopass, 0, sizeof(wol->sopass));
11867}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011868
Linus Torvalds1da177e2005-04-16 15:20:36 -070011869static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11870{
11871 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011872 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011873
Linus Torvalds1da177e2005-04-16 15:20:36 -070011874 if (wol->wolopts & ~WAKE_MAGIC)
11875 return -EINVAL;
11876 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011877 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011878 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011879
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011880 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11881
David S. Millerf47c11e2005-06-24 20:18:35 -070011882 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011883 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011884 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011885 else
Joe Perches63c3a662011-04-26 08:12:10 +000011886 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011887 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011888
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889 return 0;
11890}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011891
Linus Torvalds1da177e2005-04-16 15:20:36 -070011892static u32 tg3_get_msglevel(struct net_device *dev)
11893{
11894 struct tg3 *tp = netdev_priv(dev);
11895 return tp->msg_enable;
11896}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011897
Linus Torvalds1da177e2005-04-16 15:20:36 -070011898static void tg3_set_msglevel(struct net_device *dev, u32 value)
11899{
11900 struct tg3 *tp = netdev_priv(dev);
11901 tp->msg_enable = value;
11902}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011903
Linus Torvalds1da177e2005-04-16 15:20:36 -070011904static int tg3_nway_reset(struct net_device *dev)
11905{
11906 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011907 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011908
Linus Torvalds1da177e2005-04-16 15:20:36 -070011909 if (!netif_running(dev))
11910 return -EAGAIN;
11911
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011912 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011913 return -EINVAL;
11914
Nithin Sujirce20f162013-04-09 08:48:04 +000011915 tg3_warn_mgmt_link_flap(tp);
11916
Joe Perches63c3a662011-04-26 08:12:10 +000011917 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011918 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011919 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011920 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011921 } else {
11922 u32 bmcr;
11923
11924 spin_lock_bh(&tp->lock);
11925 r = -EINVAL;
11926 tg3_readphy(tp, MII_BMCR, &bmcr);
11927 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11928 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011929 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011930 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11931 BMCR_ANENABLE);
11932 r = 0;
11933 }
11934 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011935 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011936
Linus Torvalds1da177e2005-04-16 15:20:36 -070011937 return r;
11938}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011939
Linus Torvalds1da177e2005-04-16 15:20:36 -070011940static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11941{
11942 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011943
Matt Carlson2c49a442010-09-30 10:34:35 +000011944 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011945 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011946 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011947 else
11948 ering->rx_jumbo_max_pending = 0;
11949
11950 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951
11952 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011953 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011954 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11955 else
11956 ering->rx_jumbo_pending = 0;
11957
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011958 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011959}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011960
Linus Torvalds1da177e2005-04-16 15:20:36 -070011961static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11962{
11963 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011964 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011965
Matt Carlson2c49a442010-09-30 10:34:35 +000011966 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11967 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011968 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11969 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011970 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011971 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011972 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011973
Michael Chanbbe832c2005-06-24 20:20:04 -070011974 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011975 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011977 irq_sync = 1;
11978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011979
Michael Chanbbe832c2005-06-24 20:20:04 -070011980 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011981
Linus Torvalds1da177e2005-04-16 15:20:36 -070011982 tp->rx_pending = ering->rx_pending;
11983
Joe Perches63c3a662011-04-26 08:12:10 +000011984 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011985 tp->rx_pending > 63)
11986 tp->rx_pending = 63;
11987 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011988
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011989 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011990 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991
11992 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011993 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011994 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011995 if (!err)
11996 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011997 }
11998
David S. Millerf47c11e2005-06-24 20:18:35 -070011999 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012000
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012001 if (irq_sync && !err)
12002 tg3_phy_start(tp);
12003
Michael Chanb9ec6c12006-07-25 16:37:27 -070012004 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012005}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012006
Linus Torvalds1da177e2005-04-16 15:20:36 -070012007static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12008{
12009 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012010
Joe Perches63c3a662011-04-26 08:12:10 +000012011 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012012
Matt Carlson4a2db502011-12-08 14:40:17 +000012013 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012014 epause->rx_pause = 1;
12015 else
12016 epause->rx_pause = 0;
12017
Matt Carlson4a2db502011-12-08 14:40:17 +000012018 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012019 epause->tx_pause = 1;
12020 else
12021 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012023
Linus Torvalds1da177e2005-04-16 15:20:36 -070012024static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12025{
12026 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012027 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012028
Nithin Sujirce20f162013-04-09 08:48:04 +000012029 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12030 tg3_warn_mgmt_link_flap(tp);
12031
Joe Perches63c3a662011-04-26 08:12:10 +000012032 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012033 u32 newadv;
12034 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012035
Matt Carlson27121682010-02-17 15:16:57 +000012036 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012037
Matt Carlson27121682010-02-17 15:16:57 +000012038 if (!(phydev->supported & SUPPORTED_Pause) ||
12039 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012040 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012041 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012042
Matt Carlson27121682010-02-17 15:16:57 +000012043 tp->link_config.flowctrl = 0;
12044 if (epause->rx_pause) {
12045 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012046
Matt Carlson27121682010-02-17 15:16:57 +000012047 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012048 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012049 newadv = ADVERTISED_Pause;
12050 } else
12051 newadv = ADVERTISED_Pause |
12052 ADVERTISED_Asym_Pause;
12053 } else if (epause->tx_pause) {
12054 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12055 newadv = ADVERTISED_Asym_Pause;
12056 } else
12057 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012058
Matt Carlson27121682010-02-17 15:16:57 +000012059 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012060 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012061 else
Joe Perches63c3a662011-04-26 08:12:10 +000012062 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012063
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012064 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012065 u32 oldadv = phydev->advertising &
12066 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12067 if (oldadv != newadv) {
12068 phydev->advertising &=
12069 ~(ADVERTISED_Pause |
12070 ADVERTISED_Asym_Pause);
12071 phydev->advertising |= newadv;
12072 if (phydev->autoneg) {
12073 /*
12074 * Always renegotiate the link to
12075 * inform our link partner of our
12076 * flow control settings, even if the
12077 * flow control is forced. Let
12078 * tg3_adjust_link() do the final
12079 * flow control setup.
12080 */
12081 return phy_start_aneg(phydev);
12082 }
12083 }
12084
12085 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012086 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012087 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012088 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012089 ~(ADVERTISED_Pause |
12090 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012091 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012092 }
12093 } else {
12094 int irq_sync = 0;
12095
12096 if (netif_running(dev)) {
12097 tg3_netif_stop(tp);
12098 irq_sync = 1;
12099 }
12100
12101 tg3_full_lock(tp, irq_sync);
12102
12103 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012104 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012105 else
Joe Perches63c3a662011-04-26 08:12:10 +000012106 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012107 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012108 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012109 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012110 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012111 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012112 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012113 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012114 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012115
12116 if (netif_running(dev)) {
12117 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012118 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012119 if (!err)
12120 tg3_netif_start(tp);
12121 }
12122
12123 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012125
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012126 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12127
Michael Chanb9ec6c12006-07-25 16:37:27 -070012128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012129}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012130
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012131static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012132{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012133 switch (sset) {
12134 case ETH_SS_TEST:
12135 return TG3_NUM_TEST;
12136 case ETH_SS_STATS:
12137 return TG3_NUM_STATS;
12138 default:
12139 return -EOPNOTSUPP;
12140 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012141}
12142
Matt Carlson90415472011-12-16 13:33:23 +000012143static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12144 u32 *rules __always_unused)
12145{
12146 struct tg3 *tp = netdev_priv(dev);
12147
12148 if (!tg3_flag(tp, SUPPORT_MSIX))
12149 return -EOPNOTSUPP;
12150
12151 switch (info->cmd) {
12152 case ETHTOOL_GRXRINGS:
12153 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012154 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012155 else {
12156 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012157 if (info->data > TG3_RSS_MAX_NUM_QS)
12158 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012159 }
12160
12161 /* The first interrupt vector only
12162 * handles link interrupts.
12163 */
12164 info->data -= 1;
12165 return 0;
12166
12167 default:
12168 return -EOPNOTSUPP;
12169 }
12170}
12171
12172static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12173{
12174 u32 size = 0;
12175 struct tg3 *tp = netdev_priv(dev);
12176
12177 if (tg3_flag(tp, SUPPORT_MSIX))
12178 size = TG3_RSS_INDIR_TBL_SIZE;
12179
12180 return size;
12181}
12182
12183static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12184{
12185 struct tg3 *tp = netdev_priv(dev);
12186 int i;
12187
12188 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12189 indir[i] = tp->rss_ind_tbl[i];
12190
12191 return 0;
12192}
12193
12194static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12195{
12196 struct tg3 *tp = netdev_priv(dev);
12197 size_t i;
12198
12199 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12200 tp->rss_ind_tbl[i] = indir[i];
12201
12202 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12203 return 0;
12204
12205 /* It is legal to write the indirection
12206 * table while the device is running.
12207 */
12208 tg3_full_lock(tp, 0);
12209 tg3_rss_write_indir_tbl(tp);
12210 tg3_full_unlock(tp);
12211
12212 return 0;
12213}
12214
Michael Chan09681692012-09-28 07:12:42 +000012215static void tg3_get_channels(struct net_device *dev,
12216 struct ethtool_channels *channel)
12217{
12218 struct tg3 *tp = netdev_priv(dev);
12219 u32 deflt_qs = netif_get_num_default_rss_queues();
12220
12221 channel->max_rx = tp->rxq_max;
12222 channel->max_tx = tp->txq_max;
12223
12224 if (netif_running(dev)) {
12225 channel->rx_count = tp->rxq_cnt;
12226 channel->tx_count = tp->txq_cnt;
12227 } else {
12228 if (tp->rxq_req)
12229 channel->rx_count = tp->rxq_req;
12230 else
12231 channel->rx_count = min(deflt_qs, tp->rxq_max);
12232
12233 if (tp->txq_req)
12234 channel->tx_count = tp->txq_req;
12235 else
12236 channel->tx_count = min(deflt_qs, tp->txq_max);
12237 }
12238}
12239
12240static int tg3_set_channels(struct net_device *dev,
12241 struct ethtool_channels *channel)
12242{
12243 struct tg3 *tp = netdev_priv(dev);
12244
12245 if (!tg3_flag(tp, SUPPORT_MSIX))
12246 return -EOPNOTSUPP;
12247
12248 if (channel->rx_count > tp->rxq_max ||
12249 channel->tx_count > tp->txq_max)
12250 return -EINVAL;
12251
12252 tp->rxq_req = channel->rx_count;
12253 tp->txq_req = channel->tx_count;
12254
12255 if (!netif_running(dev))
12256 return 0;
12257
12258 tg3_stop(tp);
12259
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012260 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012261
Matt Carlsonbe947302012-12-03 19:36:57 +000012262 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012263
12264 return 0;
12265}
12266
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012267static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012268{
12269 switch (stringset) {
12270 case ETH_SS_STATS:
12271 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12272 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012273 case ETH_SS_TEST:
12274 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012276 default:
12277 WARN_ON(1); /* we need a WARN() */
12278 break;
12279 }
12280}
12281
stephen hemminger81b87092011-04-04 08:43:50 +000012282static int tg3_set_phys_id(struct net_device *dev,
12283 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012284{
12285 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012286
12287 if (!netif_running(tp->dev))
12288 return -EAGAIN;
12289
stephen hemminger81b87092011-04-04 08:43:50 +000012290 switch (state) {
12291 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012292 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012293
stephen hemminger81b87092011-04-04 08:43:50 +000012294 case ETHTOOL_ID_ON:
12295 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12296 LED_CTRL_1000MBPS_ON |
12297 LED_CTRL_100MBPS_ON |
12298 LED_CTRL_10MBPS_ON |
12299 LED_CTRL_TRAFFIC_OVERRIDE |
12300 LED_CTRL_TRAFFIC_BLINK |
12301 LED_CTRL_TRAFFIC_LED);
12302 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012303
stephen hemminger81b87092011-04-04 08:43:50 +000012304 case ETHTOOL_ID_OFF:
12305 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12306 LED_CTRL_TRAFFIC_OVERRIDE);
12307 break;
Michael Chan4009a932005-09-05 17:52:54 -070012308
stephen hemminger81b87092011-04-04 08:43:50 +000012309 case ETHTOOL_ID_INACTIVE:
12310 tw32(MAC_LED_CTRL, tp->led_ctrl);
12311 break;
Michael Chan4009a932005-09-05 17:52:54 -070012312 }
stephen hemminger81b87092011-04-04 08:43:50 +000012313
Michael Chan4009a932005-09-05 17:52:54 -070012314 return 0;
12315}
12316
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012317static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012318 struct ethtool_stats *estats, u64 *tmp_stats)
12319{
12320 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012321
Matt Carlsonb546e462012-02-13 15:20:09 +000012322 if (tp->hw_stats)
12323 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12324 else
12325 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012326}
12327
Matt Carlson535a4902011-07-20 10:20:56 +000012328static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012329{
12330 int i;
12331 __be32 *buf;
12332 u32 offset = 0, len = 0;
12333 u32 magic, val;
12334
Joe Perches63c3a662011-04-26 08:12:10 +000012335 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012336 return NULL;
12337
12338 if (magic == TG3_EEPROM_MAGIC) {
12339 for (offset = TG3_NVM_DIR_START;
12340 offset < TG3_NVM_DIR_END;
12341 offset += TG3_NVM_DIRENT_SIZE) {
12342 if (tg3_nvram_read(tp, offset, &val))
12343 return NULL;
12344
12345 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12346 TG3_NVM_DIRTYPE_EXTVPD)
12347 break;
12348 }
12349
12350 if (offset != TG3_NVM_DIR_END) {
12351 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12352 if (tg3_nvram_read(tp, offset + 4, &offset))
12353 return NULL;
12354
12355 offset = tg3_nvram_logical_addr(tp, offset);
12356 }
12357 }
12358
12359 if (!offset || !len) {
12360 offset = TG3_NVM_VPD_OFF;
12361 len = TG3_NVM_VPD_LEN;
12362 }
12363
12364 buf = kmalloc(len, GFP_KERNEL);
12365 if (buf == NULL)
12366 return NULL;
12367
12368 if (magic == TG3_EEPROM_MAGIC) {
12369 for (i = 0; i < len; i += 4) {
12370 /* The data is in little-endian format in NVRAM.
12371 * Use the big-endian read routines to preserve
12372 * the byte order as it exists in NVRAM.
12373 */
12374 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12375 goto error;
12376 }
12377 } else {
12378 u8 *ptr;
12379 ssize_t cnt;
12380 unsigned int pos = 0;
12381
12382 ptr = (u8 *)&buf[0];
12383 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12384 cnt = pci_read_vpd(tp->pdev, pos,
12385 len - pos, ptr);
12386 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12387 cnt = 0;
12388 else if (cnt < 0)
12389 goto error;
12390 }
12391 if (pos != len)
12392 goto error;
12393 }
12394
Matt Carlson535a4902011-07-20 10:20:56 +000012395 *vpdlen = len;
12396
Matt Carlsonc3e94502011-04-13 11:05:08 +000012397 return buf;
12398
12399error:
12400 kfree(buf);
12401 return NULL;
12402}
12403
Michael Chan566f86a2005-05-29 14:56:58 -070012404#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012405#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12406#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12407#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012408#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12409#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012410#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012411#define NVRAM_SELFBOOT_HW_SIZE 0x20
12412#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012413
12414static int tg3_test_nvram(struct tg3 *tp)
12415{
Matt Carlson535a4902011-07-20 10:20:56 +000012416 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012417 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012418 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012419
Joe Perches63c3a662011-04-26 08:12:10 +000012420 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012421 return 0;
12422
Matt Carlsone4f34112009-02-25 14:25:00 +000012423 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012424 return -EIO;
12425
Michael Chan1b277772006-03-20 22:27:48 -080012426 if (magic == TG3_EEPROM_MAGIC)
12427 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012428 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012429 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12430 TG3_EEPROM_SB_FORMAT_1) {
12431 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12432 case TG3_EEPROM_SB_REVISION_0:
12433 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12434 break;
12435 case TG3_EEPROM_SB_REVISION_2:
12436 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12437 break;
12438 case TG3_EEPROM_SB_REVISION_3:
12439 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12440 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012441 case TG3_EEPROM_SB_REVISION_4:
12442 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12443 break;
12444 case TG3_EEPROM_SB_REVISION_5:
12445 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12446 break;
12447 case TG3_EEPROM_SB_REVISION_6:
12448 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12449 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012450 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012451 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012452 }
12453 } else
Michael Chan1b277772006-03-20 22:27:48 -080012454 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012455 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12456 size = NVRAM_SELFBOOT_HW_SIZE;
12457 else
Michael Chan1b277772006-03-20 22:27:48 -080012458 return -EIO;
12459
12460 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012461 if (buf == NULL)
12462 return -ENOMEM;
12463
Michael Chan1b277772006-03-20 22:27:48 -080012464 err = -EIO;
12465 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012466 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12467 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012468 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012469 }
Michael Chan1b277772006-03-20 22:27:48 -080012470 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012471 goto out;
12472
Michael Chan1b277772006-03-20 22:27:48 -080012473 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012474 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012475 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012476 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012477 u8 *buf8 = (u8 *) buf, csum8 = 0;
12478
Al Virob9fc7dc2007-12-17 22:59:57 -080012479 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012480 TG3_EEPROM_SB_REVISION_2) {
12481 /* For rev 2, the csum doesn't include the MBA. */
12482 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12483 csum8 += buf8[i];
12484 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12485 csum8 += buf8[i];
12486 } else {
12487 for (i = 0; i < size; i++)
12488 csum8 += buf8[i];
12489 }
Michael Chan1b277772006-03-20 22:27:48 -080012490
Adrian Bunkad96b482006-04-05 22:21:04 -070012491 if (csum8 == 0) {
12492 err = 0;
12493 goto out;
12494 }
12495
12496 err = -EIO;
12497 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012498 }
Michael Chan566f86a2005-05-29 14:56:58 -070012499
Al Virob9fc7dc2007-12-17 22:59:57 -080012500 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012501 TG3_EEPROM_MAGIC_HW) {
12502 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012503 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012504 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012505
12506 /* Separate the parity bits and the data bytes. */
12507 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12508 if ((i == 0) || (i == 8)) {
12509 int l;
12510 u8 msk;
12511
12512 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12513 parity[k++] = buf8[i] & msk;
12514 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012515 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012516 int l;
12517 u8 msk;
12518
12519 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12520 parity[k++] = buf8[i] & msk;
12521 i++;
12522
12523 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12524 parity[k++] = buf8[i] & msk;
12525 i++;
12526 }
12527 data[j++] = buf8[i];
12528 }
12529
12530 err = -EIO;
12531 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12532 u8 hw8 = hweight8(data[i]);
12533
12534 if ((hw8 & 0x1) && parity[i])
12535 goto out;
12536 else if (!(hw8 & 0x1) && !parity[i])
12537 goto out;
12538 }
12539 err = 0;
12540 goto out;
12541 }
12542
Matt Carlson01c3a392011-03-09 16:58:20 +000012543 err = -EIO;
12544
Michael Chan566f86a2005-05-29 14:56:58 -070012545 /* Bootstrap checksum at offset 0x10 */
12546 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012547 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012548 goto out;
12549
12550 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12551 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012552 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012553 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012554
Matt Carlsonc3e94502011-04-13 11:05:08 +000012555 kfree(buf);
12556
Matt Carlson535a4902011-07-20 10:20:56 +000012557 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012558 if (!buf)
12559 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012560
Matt Carlson535a4902011-07-20 10:20:56 +000012561 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012562 if (i > 0) {
12563 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12564 if (j < 0)
12565 goto out;
12566
Matt Carlson535a4902011-07-20 10:20:56 +000012567 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012568 goto out;
12569
12570 i += PCI_VPD_LRDT_TAG_SIZE;
12571 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12572 PCI_VPD_RO_KEYWORD_CHKSUM);
12573 if (j > 0) {
12574 u8 csum8 = 0;
12575
12576 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12577
12578 for (i = 0; i <= j; i++)
12579 csum8 += ((u8 *)buf)[i];
12580
12581 if (csum8)
12582 goto out;
12583 }
12584 }
12585
Michael Chan566f86a2005-05-29 14:56:58 -070012586 err = 0;
12587
12588out:
12589 kfree(buf);
12590 return err;
12591}
12592
Michael Chanca430072005-05-29 14:57:23 -070012593#define TG3_SERDES_TIMEOUT_SEC 2
12594#define TG3_COPPER_TIMEOUT_SEC 6
12595
12596static int tg3_test_link(struct tg3 *tp)
12597{
12598 int i, max;
12599
12600 if (!netif_running(tp->dev))
12601 return -ENODEV;
12602
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012603 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012604 max = TG3_SERDES_TIMEOUT_SEC;
12605 else
12606 max = TG3_COPPER_TIMEOUT_SEC;
12607
12608 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012609 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012610 return 0;
12611
12612 if (msleep_interruptible(1000))
12613 break;
12614 }
12615
12616 return -EIO;
12617}
12618
Michael Chana71116d2005-05-29 14:58:11 -070012619/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012620static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012621{
Michael Chanb16250e2006-09-27 16:10:14 -070012622 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012623 u32 offset, read_mask, write_mask, val, save_val, read_val;
12624 static struct {
12625 u16 offset;
12626 u16 flags;
12627#define TG3_FL_5705 0x1
12628#define TG3_FL_NOT_5705 0x2
12629#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012630#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012631 u32 read_mask;
12632 u32 write_mask;
12633 } reg_tbl[] = {
12634 /* MAC Control Registers */
12635 { MAC_MODE, TG3_FL_NOT_5705,
12636 0x00000000, 0x00ef6f8c },
12637 { MAC_MODE, TG3_FL_5705,
12638 0x00000000, 0x01ef6b8c },
12639 { MAC_STATUS, TG3_FL_NOT_5705,
12640 0x03800107, 0x00000000 },
12641 { MAC_STATUS, TG3_FL_5705,
12642 0x03800100, 0x00000000 },
12643 { MAC_ADDR_0_HIGH, 0x0000,
12644 0x00000000, 0x0000ffff },
12645 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012646 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012647 { MAC_RX_MTU_SIZE, 0x0000,
12648 0x00000000, 0x0000ffff },
12649 { MAC_TX_MODE, 0x0000,
12650 0x00000000, 0x00000070 },
12651 { MAC_TX_LENGTHS, 0x0000,
12652 0x00000000, 0x00003fff },
12653 { MAC_RX_MODE, TG3_FL_NOT_5705,
12654 0x00000000, 0x000007fc },
12655 { MAC_RX_MODE, TG3_FL_5705,
12656 0x00000000, 0x000007dc },
12657 { MAC_HASH_REG_0, 0x0000,
12658 0x00000000, 0xffffffff },
12659 { MAC_HASH_REG_1, 0x0000,
12660 0x00000000, 0xffffffff },
12661 { MAC_HASH_REG_2, 0x0000,
12662 0x00000000, 0xffffffff },
12663 { MAC_HASH_REG_3, 0x0000,
12664 0x00000000, 0xffffffff },
12665
12666 /* Receive Data and Receive BD Initiator Control Registers. */
12667 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12668 0x00000000, 0xffffffff },
12669 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12670 0x00000000, 0xffffffff },
12671 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12672 0x00000000, 0x00000003 },
12673 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12674 0x00000000, 0xffffffff },
12675 { RCVDBDI_STD_BD+0, 0x0000,
12676 0x00000000, 0xffffffff },
12677 { RCVDBDI_STD_BD+4, 0x0000,
12678 0x00000000, 0xffffffff },
12679 { RCVDBDI_STD_BD+8, 0x0000,
12680 0x00000000, 0xffff0002 },
12681 { RCVDBDI_STD_BD+0xc, 0x0000,
12682 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012683
Michael Chana71116d2005-05-29 14:58:11 -070012684 /* Receive BD Initiator Control Registers. */
12685 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12686 0x00000000, 0xffffffff },
12687 { RCVBDI_STD_THRESH, TG3_FL_5705,
12688 0x00000000, 0x000003ff },
12689 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12690 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012691
Michael Chana71116d2005-05-29 14:58:11 -070012692 /* Host Coalescing Control Registers. */
12693 { HOSTCC_MODE, TG3_FL_NOT_5705,
12694 0x00000000, 0x00000004 },
12695 { HOSTCC_MODE, TG3_FL_5705,
12696 0x00000000, 0x000000f6 },
12697 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12698 0x00000000, 0xffffffff },
12699 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12700 0x00000000, 0x000003ff },
12701 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12702 0x00000000, 0xffffffff },
12703 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12704 0x00000000, 0x000003ff },
12705 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12706 0x00000000, 0xffffffff },
12707 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12708 0x00000000, 0x000000ff },
12709 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12710 0x00000000, 0xffffffff },
12711 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12712 0x00000000, 0x000000ff },
12713 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12714 0x00000000, 0xffffffff },
12715 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12716 0x00000000, 0xffffffff },
12717 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12718 0x00000000, 0xffffffff },
12719 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12720 0x00000000, 0x000000ff },
12721 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12722 0x00000000, 0xffffffff },
12723 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12724 0x00000000, 0x000000ff },
12725 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12726 0x00000000, 0xffffffff },
12727 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12728 0x00000000, 0xffffffff },
12729 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12730 0x00000000, 0xffffffff },
12731 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12732 0x00000000, 0xffffffff },
12733 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12734 0x00000000, 0xffffffff },
12735 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12736 0xffffffff, 0x00000000 },
12737 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12738 0xffffffff, 0x00000000 },
12739
12740 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012741 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012742 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012743 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012744 0x00000000, 0x007fffff },
12745 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12746 0x00000000, 0x0000003f },
12747 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12748 0x00000000, 0x000001ff },
12749 { BUFMGR_MB_HIGH_WATER, 0x0000,
12750 0x00000000, 0x000001ff },
12751 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12752 0xffffffff, 0x00000000 },
12753 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12754 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012755
Michael Chana71116d2005-05-29 14:58:11 -070012756 /* Mailbox Registers */
12757 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12758 0x00000000, 0x000001ff },
12759 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12760 0x00000000, 0x000001ff },
12761 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12762 0x00000000, 0x000007ff },
12763 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12764 0x00000000, 0x000001ff },
12765
12766 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12767 };
12768
Michael Chanb16250e2006-09-27 16:10:14 -070012769 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012770 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012771 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012772 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012773 is_5750 = 1;
12774 }
Michael Chana71116d2005-05-29 14:58:11 -070012775
12776 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12777 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12778 continue;
12779
12780 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12781 continue;
12782
Joe Perches63c3a662011-04-26 08:12:10 +000012783 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012784 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12785 continue;
12786
Michael Chanb16250e2006-09-27 16:10:14 -070012787 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12788 continue;
12789
Michael Chana71116d2005-05-29 14:58:11 -070012790 offset = (u32) reg_tbl[i].offset;
12791 read_mask = reg_tbl[i].read_mask;
12792 write_mask = reg_tbl[i].write_mask;
12793
12794 /* Save the original register content */
12795 save_val = tr32(offset);
12796
12797 /* Determine the read-only value. */
12798 read_val = save_val & read_mask;
12799
12800 /* Write zero to the register, then make sure the read-only bits
12801 * are not changed and the read/write bits are all zeros.
12802 */
12803 tw32(offset, 0);
12804
12805 val = tr32(offset);
12806
12807 /* Test the read-only and read/write bits. */
12808 if (((val & read_mask) != read_val) || (val & write_mask))
12809 goto out;
12810
12811 /* Write ones to all the bits defined by RdMask and WrMask, then
12812 * make sure the read-only bits are not changed and the
12813 * read/write bits are all ones.
12814 */
12815 tw32(offset, read_mask | write_mask);
12816
12817 val = tr32(offset);
12818
12819 /* Test the read-only bits. */
12820 if ((val & read_mask) != read_val)
12821 goto out;
12822
12823 /* Test the read/write bits. */
12824 if ((val & write_mask) != write_mask)
12825 goto out;
12826
12827 tw32(offset, save_val);
12828 }
12829
12830 return 0;
12831
12832out:
Michael Chan9f88f292006-12-07 00:22:54 -080012833 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012834 netdev_err(tp->dev,
12835 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012836 tw32(offset, save_val);
12837 return -EIO;
12838}
12839
Michael Chan7942e1d2005-05-29 14:58:36 -070012840static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12841{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012842 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012843 int i;
12844 u32 j;
12845
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012846 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012847 for (j = 0; j < len; j += 4) {
12848 u32 val;
12849
12850 tg3_write_mem(tp, offset + j, test_pattern[i]);
12851 tg3_read_mem(tp, offset + j, &val);
12852 if (val != test_pattern[i])
12853 return -EIO;
12854 }
12855 }
12856 return 0;
12857}
12858
12859static int tg3_test_memory(struct tg3 *tp)
12860{
12861 static struct mem_entry {
12862 u32 offset;
12863 u32 len;
12864 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012865 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012866 { 0x00002000, 0x1c000},
12867 { 0xffffffff, 0x00000}
12868 }, mem_tbl_5705[] = {
12869 { 0x00000100, 0x0000c},
12870 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012871 { 0x00004000, 0x00800},
12872 { 0x00006000, 0x01000},
12873 { 0x00008000, 0x02000},
12874 { 0x00010000, 0x0e000},
12875 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012876 }, mem_tbl_5755[] = {
12877 { 0x00000200, 0x00008},
12878 { 0x00004000, 0x00800},
12879 { 0x00006000, 0x00800},
12880 { 0x00008000, 0x02000},
12881 { 0x00010000, 0x0c000},
12882 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012883 }, mem_tbl_5906[] = {
12884 { 0x00000200, 0x00008},
12885 { 0x00004000, 0x00400},
12886 { 0x00006000, 0x00400},
12887 { 0x00008000, 0x01000},
12888 { 0x00010000, 0x01000},
12889 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012890 }, mem_tbl_5717[] = {
12891 { 0x00000200, 0x00008},
12892 { 0x00010000, 0x0a000},
12893 { 0x00020000, 0x13c00},
12894 { 0xffffffff, 0x00000}
12895 }, mem_tbl_57765[] = {
12896 { 0x00000200, 0x00008},
12897 { 0x00004000, 0x00800},
12898 { 0x00006000, 0x09800},
12899 { 0x00010000, 0x0a000},
12900 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012901 };
12902 struct mem_entry *mem_tbl;
12903 int err = 0;
12904 int i;
12905
Joe Perches63c3a662011-04-26 08:12:10 +000012906 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012907 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012908 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000012909 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012910 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012911 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012912 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000012913 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080012914 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012915 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012916 mem_tbl = mem_tbl_5705;
12917 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012918 mem_tbl = mem_tbl_570x;
12919
12920 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012921 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12922 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012923 break;
12924 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012925
Michael Chan7942e1d2005-05-29 14:58:36 -070012926 return err;
12927}
12928
Matt Carlsonbb158d62011-04-25 12:42:47 +000012929#define TG3_TSO_MSS 500
12930
12931#define TG3_TSO_IP_HDR_LEN 20
12932#define TG3_TSO_TCP_HDR_LEN 20
12933#define TG3_TSO_TCP_OPT_LEN 12
12934
12935static const u8 tg3_tso_header[] = {
129360x08, 0x00,
129370x45, 0x00, 0x00, 0x00,
129380x00, 0x00, 0x40, 0x00,
129390x40, 0x06, 0x00, 0x00,
129400x0a, 0x00, 0x00, 0x01,
129410x0a, 0x00, 0x00, 0x02,
129420x0d, 0x00, 0xe0, 0x00,
129430x00, 0x00, 0x01, 0x00,
129440x00, 0x00, 0x02, 0x00,
129450x80, 0x10, 0x10, 0x00,
129460x14, 0x09, 0x00, 0x00,
129470x01, 0x01, 0x08, 0x0a,
129480x11, 0x11, 0x11, 0x11,
129490x11, 0x11, 0x11, 0x11,
12950};
Michael Chan9f40dea2005-09-05 17:53:06 -070012951
Matt Carlson28a45952011-08-19 13:58:22 +000012952static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012953{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012954 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012955 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012956 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012957 struct sk_buff *skb;
12958 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012959 dma_addr_t map;
12960 int num_pkts, tx_len, rx_len, i, err;
12961 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012962 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012963 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012964
Matt Carlsonc8873402010-02-12 14:47:11 +000012965 tnapi = &tp->napi[0];
12966 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012967 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012968 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012969 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012970 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012971 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012972 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012973 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012974
Michael Chanc76949a2005-05-29 14:58:59 -070012975 err = -EIO;
12976
Matt Carlson4852a862011-04-13 11:05:07 +000012977 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012978 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012979 if (!skb)
12980 return -ENOMEM;
12981
Michael Chanc76949a2005-05-29 14:58:59 -070012982 tx_data = skb_put(skb, tx_len);
12983 memcpy(tx_data, tp->dev->dev_addr, 6);
12984 memset(tx_data + 6, 0x0, 8);
12985
Matt Carlson4852a862011-04-13 11:05:07 +000012986 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012987
Matt Carlson28a45952011-08-19 13:58:22 +000012988 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012989 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12990
12991 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12992 TG3_TSO_TCP_OPT_LEN;
12993
12994 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12995 sizeof(tg3_tso_header));
12996 mss = TG3_TSO_MSS;
12997
12998 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12999 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13000
13001 /* Set the total length field in the IP header */
13002 iph->tot_len = htons((u16)(mss + hdr_len));
13003
13004 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13005 TXD_FLAG_CPU_POST_DMA);
13006
Joe Perches63c3a662011-04-26 08:12:10 +000013007 if (tg3_flag(tp, HW_TSO_1) ||
13008 tg3_flag(tp, HW_TSO_2) ||
13009 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013010 struct tcphdr *th;
13011 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13012 th = (struct tcphdr *)&tx_data[val];
13013 th->check = 0;
13014 } else
13015 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13016
Joe Perches63c3a662011-04-26 08:12:10 +000013017 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013018 mss |= (hdr_len & 0xc) << 12;
13019 if (hdr_len & 0x10)
13020 base_flags |= 0x00000010;
13021 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013022 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013023 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013024 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013025 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013026 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13027 } else {
13028 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13029 }
13030
13031 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13032 } else {
13033 num_pkts = 1;
13034 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013035
13036 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13037 tx_len > VLAN_ETH_FRAME_LEN)
13038 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013039 }
13040
13041 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013042 tx_data[i] = (u8) (i & 0xff);
13043
Alexander Duyckf4188d82009-12-02 16:48:38 +000013044 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13045 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013046 dev_kfree_skb(skb);
13047 return -EIO;
13048 }
Michael Chanc76949a2005-05-29 14:58:59 -070013049
Matt Carlson0d681b22011-07-27 14:20:49 +000013050 val = tnapi->tx_prod;
13051 tnapi->tx_buffers[val].skb = skb;
13052 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13053
Michael Chanc76949a2005-05-29 14:58:59 -070013054 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013055 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013056
13057 udelay(10);
13058
Matt Carlson898a56f2009-08-28 14:02:40 +000013059 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013060
Matt Carlson84b67b22011-07-27 14:20:52 +000013061 budget = tg3_tx_avail(tnapi);
13062 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013063 base_flags | TXD_FLAG_END, mss, 0)) {
13064 tnapi->tx_buffers[val].skb = NULL;
13065 dev_kfree_skb(skb);
13066 return -EIO;
13067 }
Michael Chanc76949a2005-05-29 14:58:59 -070013068
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013069 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013070
Michael Chan6541b802012-03-04 14:48:14 +000013071 /* Sync BD data before updating mailbox */
13072 wmb();
13073
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013074 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13075 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013076
13077 udelay(10);
13078
Matt Carlson303fc922009-11-02 14:27:34 +000013079 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13080 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013081 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013082 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013083
13084 udelay(10);
13085
Matt Carlson898a56f2009-08-28 14:02:40 +000013086 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13087 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013088 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013089 (rx_idx == (rx_start_idx + num_pkts)))
13090 break;
13091 }
13092
Matt Carlsonba1142e2011-11-04 09:15:00 +000013093 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013094 dev_kfree_skb(skb);
13095
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013096 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013097 goto out;
13098
13099 if (rx_idx != rx_start_idx + num_pkts)
13100 goto out;
13101
Matt Carlsonbb158d62011-04-25 12:42:47 +000013102 val = data_off;
13103 while (rx_idx != rx_start_idx) {
13104 desc = &rnapi->rx_rcb[rx_start_idx++];
13105 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13106 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013107
Matt Carlsonbb158d62011-04-25 12:42:47 +000013108 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13109 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013110 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013111
Matt Carlsonbb158d62011-04-25 12:42:47 +000013112 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13113 - ETH_FCS_LEN;
13114
Matt Carlson28a45952011-08-19 13:58:22 +000013115 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013116 if (rx_len != tx_len)
13117 goto out;
13118
13119 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13120 if (opaque_key != RXD_OPAQUE_RING_STD)
13121 goto out;
13122 } else {
13123 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13124 goto out;
13125 }
13126 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13127 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013128 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013129 goto out;
13130 }
13131
13132 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013133 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013134 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13135 mapping);
13136 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013137 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013138 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13139 mapping);
13140 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013141 goto out;
13142
Matt Carlsonbb158d62011-04-25 12:42:47 +000013143 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13144 PCI_DMA_FROMDEVICE);
13145
Eric Dumazet9205fd92011-11-18 06:47:01 +000013146 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013147 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013148 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013149 goto out;
13150 }
Matt Carlson4852a862011-04-13 11:05:07 +000013151 }
13152
Michael Chanc76949a2005-05-29 14:58:59 -070013153 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013154
Eric Dumazet9205fd92011-11-18 06:47:01 +000013155 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013156out:
13157 return err;
13158}
13159
Matt Carlson00c266b2011-04-25 12:42:46 +000013160#define TG3_STD_LOOPBACK_FAILED 1
13161#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013162#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013163#define TG3_LOOPBACK_FAILED \
13164 (TG3_STD_LOOPBACK_FAILED | \
13165 TG3_JMB_LOOPBACK_FAILED | \
13166 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013167
Matt Carlson941ec902011-08-19 13:58:23 +000013168static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013169{
Matt Carlson28a45952011-08-19 13:58:22 +000013170 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013171 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013172 u32 jmb_pkt_sz = 9000;
13173
13174 if (tp->dma_limit)
13175 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013176
Matt Carlsonab789042011-01-25 15:58:54 +000013177 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13178 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13179
Matt Carlson28a45952011-08-19 13:58:22 +000013180 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013181 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13182 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013183 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013184 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013185 goto done;
13186 }
13187
Joe Perches953c96e2013-04-09 10:18:14 +000013188 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013189 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013190 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13191 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013192 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013193 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013194 goto done;
13195 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013196
Joe Perches63c3a662011-04-26 08:12:10 +000013197 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013198 int i;
13199
13200 /* Reroute all rx packets to the 1st queue */
13201 for (i = MAC_RSS_INDIR_TBL_0;
13202 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13203 tw32(i, 0x0);
13204 }
13205
Matt Carlson6e01b202011-08-19 13:58:20 +000013206 /* HW errata - mac loopback fails in some cases on 5780.
13207 * Normal traffic and PHY loopback are not affected by
13208 * errata. Also, the MAC loopback test is deprecated for
13209 * all newer ASIC revisions.
13210 */
Joe Perches41535772013-02-16 11:20:04 +000013211 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013212 !tg3_flag(tp, CPMU_PRESENT)) {
13213 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013214
Matt Carlson28a45952011-08-19 13:58:22 +000013215 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013216 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013217
13218 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013219 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013220 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013221
13222 tg3_mac_loopback(tp, false);
13223 }
Matt Carlson4852a862011-04-13 11:05:07 +000013224
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013225 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013226 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013227 int i;
13228
Matt Carlson941ec902011-08-19 13:58:23 +000013229 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013230
13231 /* Wait for link */
13232 for (i = 0; i < 100; i++) {
13233 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13234 break;
13235 mdelay(1);
13236 }
13237
Matt Carlson28a45952011-08-19 13:58:22 +000013238 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013239 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013240 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013241 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013242 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013243 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013244 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013245 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013246
Matt Carlson941ec902011-08-19 13:58:23 +000013247 if (do_extlpbk) {
13248 tg3_phy_lpbk_set(tp, 0, true);
13249
13250 /* All link indications report up, but the hardware
13251 * isn't really ready for about 20 msec. Double it
13252 * to be sure.
13253 */
13254 mdelay(40);
13255
13256 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013257 data[TG3_EXT_LOOPB_TEST] |=
13258 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013259 if (tg3_flag(tp, TSO_CAPABLE) &&
13260 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013261 data[TG3_EXT_LOOPB_TEST] |=
13262 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013263 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013264 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013265 data[TG3_EXT_LOOPB_TEST] |=
13266 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013267 }
13268
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013269 /* Re-enable gphy autopowerdown. */
13270 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13271 tg3_phy_toggle_apd(tp, true);
13272 }
Matt Carlson6833c042008-11-21 17:18:59 -080013273
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013274 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13275 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013276
Matt Carlsonab789042011-01-25 15:58:54 +000013277done:
13278 tp->phy_flags |= eee_cap;
13279
Michael Chan9f40dea2005-09-05 17:53:06 -070013280 return err;
13281}
13282
Michael Chan4cafd3f2005-05-29 14:56:34 -070013283static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13284 u64 *data)
13285{
Michael Chan566f86a2005-05-29 14:56:58 -070013286 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013287 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013288
Matt Carlsonbed98292011-07-13 09:27:29 +000013289 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
13290 tg3_power_up(tp)) {
13291 etest->flags |= ETH_TEST_FL_FAILED;
13292 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13293 return;
13294 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013295
Michael Chan566f86a2005-05-29 14:56:58 -070013296 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13297
13298 if (tg3_test_nvram(tp) != 0) {
13299 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013300 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013301 }
Matt Carlson941ec902011-08-19 13:58:23 +000013302 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013303 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013304 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013305 }
Michael Chana71116d2005-05-29 14:58:11 -070013306 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013307 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013308
Michael Chanbbe832c2005-06-24 20:20:04 -070013309 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013310 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013311 tg3_netif_stop(tp);
13312 irq_sync = 1;
13313 }
13314
13315 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013316 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013317 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013318 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013319 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013320 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013321 if (!err)
13322 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013323
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013324 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013325 tg3_phy_reset(tp);
13326
Michael Chana71116d2005-05-29 14:58:11 -070013327 if (tg3_test_registers(tp) != 0) {
13328 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013329 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013330 }
Matt Carlson28a45952011-08-19 13:58:22 +000013331
Michael Chan7942e1d2005-05-29 14:58:36 -070013332 if (tg3_test_memory(tp) != 0) {
13333 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013334 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013335 }
Matt Carlson28a45952011-08-19 13:58:22 +000013336
Matt Carlson941ec902011-08-19 13:58:23 +000013337 if (doextlpbk)
13338 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13339
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013340 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013341 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013342
David S. Millerf47c11e2005-06-24 20:18:35 -070013343 tg3_full_unlock(tp);
13344
Michael Chand4bc3922005-05-29 14:59:20 -070013345 if (tg3_test_interrupt(tp) != 0) {
13346 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013347 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013348 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013349
13350 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013351
Michael Chana71116d2005-05-29 14:58:11 -070013352 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13353 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013354 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013355 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013356 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013357 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013358 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013359
13360 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013361
13362 if (irq_sync && !err2)
13363 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013364 }
Matt Carlson80096062010-08-02 11:26:06 +000013365 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013366 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013367
Michael Chan4cafd3f2005-05-29 14:56:34 -070013368}
13369
Matt Carlson0a633ac2012-12-03 19:36:59 +000013370static int tg3_hwtstamp_ioctl(struct net_device *dev,
13371 struct ifreq *ifr, int cmd)
13372{
13373 struct tg3 *tp = netdev_priv(dev);
13374 struct hwtstamp_config stmpconf;
13375
13376 if (!tg3_flag(tp, PTP_CAPABLE))
13377 return -EINVAL;
13378
13379 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13380 return -EFAULT;
13381
13382 if (stmpconf.flags)
13383 return -EINVAL;
13384
13385 switch (stmpconf.tx_type) {
13386 case HWTSTAMP_TX_ON:
13387 tg3_flag_set(tp, TX_TSTAMP_EN);
13388 break;
13389 case HWTSTAMP_TX_OFF:
13390 tg3_flag_clear(tp, TX_TSTAMP_EN);
13391 break;
13392 default:
13393 return -ERANGE;
13394 }
13395
13396 switch (stmpconf.rx_filter) {
13397 case HWTSTAMP_FILTER_NONE:
13398 tp->rxptpctl = 0;
13399 break;
13400 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13401 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13402 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13403 break;
13404 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13405 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13406 TG3_RX_PTP_CTL_SYNC_EVNT;
13407 break;
13408 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13409 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13410 TG3_RX_PTP_CTL_DELAY_REQ;
13411 break;
13412 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13413 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13414 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13415 break;
13416 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13417 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13418 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13419 break;
13420 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13421 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13422 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13423 break;
13424 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13425 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13426 TG3_RX_PTP_CTL_SYNC_EVNT;
13427 break;
13428 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13429 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13430 TG3_RX_PTP_CTL_SYNC_EVNT;
13431 break;
13432 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13433 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13434 TG3_RX_PTP_CTL_SYNC_EVNT;
13435 break;
13436 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13437 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13438 TG3_RX_PTP_CTL_DELAY_REQ;
13439 break;
13440 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13441 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13442 TG3_RX_PTP_CTL_DELAY_REQ;
13443 break;
13444 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13445 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13446 TG3_RX_PTP_CTL_DELAY_REQ;
13447 break;
13448 default:
13449 return -ERANGE;
13450 }
13451
13452 if (netif_running(dev) && tp->rxptpctl)
13453 tw32(TG3_RX_PTP_CTL,
13454 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13455
13456 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13457 -EFAULT : 0;
13458}
13459
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13461{
13462 struct mii_ioctl_data *data = if_mii(ifr);
13463 struct tg3 *tp = netdev_priv(dev);
13464 int err;
13465
Joe Perches63c3a662011-04-26 08:12:10 +000013466 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013467 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013468 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013469 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013470 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013471 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013472 }
13473
Matt Carlson33f401a2010-04-05 10:19:27 +000013474 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013475 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013476 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013477
13478 /* fallthru */
13479 case SIOCGMIIREG: {
13480 u32 mii_regval;
13481
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013482 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013483 break; /* We have no PHY */
13484
Matt Carlson34eea5a2011-04-20 07:57:38 +000013485 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013486 return -EAGAIN;
13487
David S. Millerf47c11e2005-06-24 20:18:35 -070013488 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013489 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13490 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013491 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013492
13493 data->val_out = mii_regval;
13494
13495 return err;
13496 }
13497
13498 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013499 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013500 break; /* We have no PHY */
13501
Matt Carlson34eea5a2011-04-20 07:57:38 +000013502 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013503 return -EAGAIN;
13504
David S. Millerf47c11e2005-06-24 20:18:35 -070013505 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013506 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13507 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013508 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013509
13510 return err;
13511
Matt Carlson0a633ac2012-12-03 19:36:59 +000013512 case SIOCSHWTSTAMP:
13513 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13514
Linus Torvalds1da177e2005-04-16 15:20:36 -070013515 default:
13516 /* do nothing */
13517 break;
13518 }
13519 return -EOPNOTSUPP;
13520}
13521
David S. Miller15f98502005-05-18 22:49:26 -070013522static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13523{
13524 struct tg3 *tp = netdev_priv(dev);
13525
13526 memcpy(ec, &tp->coal, sizeof(*ec));
13527 return 0;
13528}
13529
Michael Chand244c892005-07-05 14:42:33 -070013530static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13531{
13532 struct tg3 *tp = netdev_priv(dev);
13533 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13534 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13535
Joe Perches63c3a662011-04-26 08:12:10 +000013536 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013537 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13538 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13539 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13540 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13541 }
13542
13543 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13544 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13545 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13546 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13547 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13548 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13549 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13550 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13551 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13552 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13553 return -EINVAL;
13554
13555 /* No rx interrupts will be generated if both are zero */
13556 if ((ec->rx_coalesce_usecs == 0) &&
13557 (ec->rx_max_coalesced_frames == 0))
13558 return -EINVAL;
13559
13560 /* No tx interrupts will be generated if both are zero */
13561 if ((ec->tx_coalesce_usecs == 0) &&
13562 (ec->tx_max_coalesced_frames == 0))
13563 return -EINVAL;
13564
13565 /* Only copy relevant parameters, ignore all others. */
13566 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13567 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13568 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13569 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13570 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13571 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13572 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13573 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13574 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13575
13576 if (netif_running(dev)) {
13577 tg3_full_lock(tp, 0);
13578 __tg3_set_coalesce(tp, &tp->coal);
13579 tg3_full_unlock(tp);
13580 }
13581 return 0;
13582}
13583
Jeff Garzik7282d492006-09-13 14:30:00 -040013584static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013585 .get_settings = tg3_get_settings,
13586 .set_settings = tg3_set_settings,
13587 .get_drvinfo = tg3_get_drvinfo,
13588 .get_regs_len = tg3_get_regs_len,
13589 .get_regs = tg3_get_regs,
13590 .get_wol = tg3_get_wol,
13591 .set_wol = tg3_set_wol,
13592 .get_msglevel = tg3_get_msglevel,
13593 .set_msglevel = tg3_set_msglevel,
13594 .nway_reset = tg3_nway_reset,
13595 .get_link = ethtool_op_get_link,
13596 .get_eeprom_len = tg3_get_eeprom_len,
13597 .get_eeprom = tg3_get_eeprom,
13598 .set_eeprom = tg3_set_eeprom,
13599 .get_ringparam = tg3_get_ringparam,
13600 .set_ringparam = tg3_set_ringparam,
13601 .get_pauseparam = tg3_get_pauseparam,
13602 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013603 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013604 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013605 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013606 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013607 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013608 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013609 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013610 .get_rxnfc = tg3_get_rxnfc,
13611 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13612 .get_rxfh_indir = tg3_get_rxfh_indir,
13613 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013614 .get_channels = tg3_get_channels,
13615 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013616 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013617};
13618
David S. Millerb4017c52012-03-01 17:57:40 -050013619static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13620 struct rtnl_link_stats64 *stats)
13621{
13622 struct tg3 *tp = netdev_priv(dev);
13623
David S. Millerb4017c52012-03-01 17:57:40 -050013624 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013625 if (!tp->hw_stats) {
13626 spin_unlock_bh(&tp->lock);
13627 return &tp->net_stats_prev;
13628 }
13629
David S. Millerb4017c52012-03-01 17:57:40 -050013630 tg3_get_nstats(tp, stats);
13631 spin_unlock_bh(&tp->lock);
13632
13633 return stats;
13634}
13635
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013636static void tg3_set_rx_mode(struct net_device *dev)
13637{
13638 struct tg3 *tp = netdev_priv(dev);
13639
13640 if (!netif_running(dev))
13641 return;
13642
13643 tg3_full_lock(tp, 0);
13644 __tg3_set_rx_mode(dev);
13645 tg3_full_unlock(tp);
13646}
13647
Matt Carlsonfaf16272012-02-13 10:20:07 +000013648static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13649 int new_mtu)
13650{
13651 dev->mtu = new_mtu;
13652
13653 if (new_mtu > ETH_DATA_LEN) {
13654 if (tg3_flag(tp, 5780_CLASS)) {
13655 netdev_update_features(dev);
13656 tg3_flag_clear(tp, TSO_CAPABLE);
13657 } else {
13658 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13659 }
13660 } else {
13661 if (tg3_flag(tp, 5780_CLASS)) {
13662 tg3_flag_set(tp, TSO_CAPABLE);
13663 netdev_update_features(dev);
13664 }
13665 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13666 }
13667}
13668
13669static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13670{
13671 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000013672 int err;
13673 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013674
13675 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13676 return -EINVAL;
13677
13678 if (!netif_running(dev)) {
13679 /* We'll just catch it later when the
13680 * device is up'd.
13681 */
13682 tg3_set_mtu(dev, tp, new_mtu);
13683 return 0;
13684 }
13685
13686 tg3_phy_stop(tp);
13687
13688 tg3_netif_stop(tp);
13689
13690 tg3_full_lock(tp, 1);
13691
13692 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13693
13694 tg3_set_mtu(dev, tp, new_mtu);
13695
Michael Chan2fae5e32012-03-04 14:48:15 +000013696 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13697 * breaks all requests to 256 bytes.
13698 */
Joe Perches41535772013-02-16 11:20:04 +000013699 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000013700 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000013701
13702 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013703
13704 if (!err)
13705 tg3_netif_start(tp);
13706
13707 tg3_full_unlock(tp);
13708
13709 if (!err)
13710 tg3_phy_start(tp);
13711
13712 return err;
13713}
13714
13715static const struct net_device_ops tg3_netdev_ops = {
13716 .ndo_open = tg3_open,
13717 .ndo_stop = tg3_close,
13718 .ndo_start_xmit = tg3_start_xmit,
13719 .ndo_get_stats64 = tg3_get_stats64,
13720 .ndo_validate_addr = eth_validate_addr,
13721 .ndo_set_rx_mode = tg3_set_rx_mode,
13722 .ndo_set_mac_address = tg3_set_mac_addr,
13723 .ndo_do_ioctl = tg3_ioctl,
13724 .ndo_tx_timeout = tg3_tx_timeout,
13725 .ndo_change_mtu = tg3_change_mtu,
13726 .ndo_fix_features = tg3_fix_features,
13727 .ndo_set_features = tg3_set_features,
13728#ifdef CONFIG_NET_POLL_CONTROLLER
13729 .ndo_poll_controller = tg3_poll_controller,
13730#endif
13731};
13732
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013733static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013734{
Michael Chan1b277772006-03-20 22:27:48 -080013735 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013736
13737 tp->nvram_size = EEPROM_CHIP_SIZE;
13738
Matt Carlsone4f34112009-02-25 14:25:00 +000013739 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013740 return;
13741
Michael Chanb16250e2006-09-27 16:10:14 -070013742 if ((magic != TG3_EEPROM_MAGIC) &&
13743 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13744 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013745 return;
13746
13747 /*
13748 * Size the chip by reading offsets at increasing powers of two.
13749 * When we encounter our validation signature, we know the addressing
13750 * has wrapped around, and thus have our chip size.
13751 */
Michael Chan1b277772006-03-20 22:27:48 -080013752 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013753
13754 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013755 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013756 return;
13757
Michael Chan18201802006-03-20 22:29:15 -080013758 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013759 break;
13760
13761 cursize <<= 1;
13762 }
13763
13764 tp->nvram_size = cursize;
13765}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013766
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013767static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013768{
13769 u32 val;
13770
Joe Perches63c3a662011-04-26 08:12:10 +000013771 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013772 return;
13773
13774 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013775 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013776 tg3_get_eeprom_size(tp);
13777 return;
13778 }
13779
Matt Carlson6d348f22009-02-25 14:25:52 +000013780 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013781 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013782 /* This is confusing. We want to operate on the
13783 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13784 * call will read from NVRAM and byteswap the data
13785 * according to the byteswapping settings for all
13786 * other register accesses. This ensures the data we
13787 * want will always reside in the lower 16-bits.
13788 * However, the data in NVRAM is in LE format, which
13789 * means the data from the NVRAM read will always be
13790 * opposite the endianness of the CPU. The 16-bit
13791 * byteswap then brings the data to CPU endianness.
13792 */
13793 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013794 return;
13795 }
13796 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013797 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013798}
13799
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013800static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013801{
13802 u32 nvcfg1;
13803
13804 nvcfg1 = tr32(NVRAM_CFG1);
13805 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013806 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013807 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013808 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13809 tw32(NVRAM_CFG1, nvcfg1);
13810 }
13811
Joe Perches41535772013-02-16 11:20:04 +000013812 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013813 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013814 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013815 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13816 tp->nvram_jedecnum = JEDEC_ATMEL;
13817 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013818 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013819 break;
13820 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13821 tp->nvram_jedecnum = JEDEC_ATMEL;
13822 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13823 break;
13824 case FLASH_VENDOR_ATMEL_EEPROM:
13825 tp->nvram_jedecnum = JEDEC_ATMEL;
13826 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013827 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013828 break;
13829 case FLASH_VENDOR_ST:
13830 tp->nvram_jedecnum = JEDEC_ST;
13831 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013832 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013833 break;
13834 case FLASH_VENDOR_SAIFUN:
13835 tp->nvram_jedecnum = JEDEC_SAIFUN;
13836 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13837 break;
13838 case FLASH_VENDOR_SST_SMALL:
13839 case FLASH_VENDOR_SST_LARGE:
13840 tp->nvram_jedecnum = JEDEC_SST;
13841 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13842 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013843 }
Matt Carlson8590a602009-08-28 12:29:16 +000013844 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013845 tp->nvram_jedecnum = JEDEC_ATMEL;
13846 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013847 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013848 }
13849}
13850
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013851static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013852{
13853 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13854 case FLASH_5752PAGE_SIZE_256:
13855 tp->nvram_pagesize = 256;
13856 break;
13857 case FLASH_5752PAGE_SIZE_512:
13858 tp->nvram_pagesize = 512;
13859 break;
13860 case FLASH_5752PAGE_SIZE_1K:
13861 tp->nvram_pagesize = 1024;
13862 break;
13863 case FLASH_5752PAGE_SIZE_2K:
13864 tp->nvram_pagesize = 2048;
13865 break;
13866 case FLASH_5752PAGE_SIZE_4K:
13867 tp->nvram_pagesize = 4096;
13868 break;
13869 case FLASH_5752PAGE_SIZE_264:
13870 tp->nvram_pagesize = 264;
13871 break;
13872 case FLASH_5752PAGE_SIZE_528:
13873 tp->nvram_pagesize = 528;
13874 break;
13875 }
13876}
13877
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013878static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013879{
13880 u32 nvcfg1;
13881
13882 nvcfg1 = tr32(NVRAM_CFG1);
13883
Michael Chane6af3012005-04-21 17:12:05 -070013884 /* NVRAM protection for TPM */
13885 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013886 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013887
Michael Chan361b4ac2005-04-21 17:11:21 -070013888 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013889 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13890 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13891 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013892 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013893 break;
13894 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13895 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013896 tg3_flag_set(tp, NVRAM_BUFFERED);
13897 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013898 break;
13899 case FLASH_5752VENDOR_ST_M45PE10:
13900 case FLASH_5752VENDOR_ST_M45PE20:
13901 case FLASH_5752VENDOR_ST_M45PE40:
13902 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013903 tg3_flag_set(tp, NVRAM_BUFFERED);
13904 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013905 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013906 }
13907
Joe Perches63c3a662011-04-26 08:12:10 +000013908 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013909 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013910 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013911 /* For eeprom, set pagesize to maximum eeprom size */
13912 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13913
13914 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13915 tw32(NVRAM_CFG1, nvcfg1);
13916 }
13917}
13918
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013919static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013920{
Matt Carlson989a9d22007-05-05 11:51:05 -070013921 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013922
13923 nvcfg1 = tr32(NVRAM_CFG1);
13924
13925 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013926 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013927 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013928 protect = 1;
13929 }
Michael Chand3c7b882006-03-23 01:28:25 -080013930
Matt Carlson989a9d22007-05-05 11:51:05 -070013931 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13932 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013933 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13934 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13935 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13936 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13937 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013938 tg3_flag_set(tp, NVRAM_BUFFERED);
13939 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013940 tp->nvram_pagesize = 264;
13941 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13942 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13943 tp->nvram_size = (protect ? 0x3e200 :
13944 TG3_NVRAM_SIZE_512KB);
13945 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13946 tp->nvram_size = (protect ? 0x1f200 :
13947 TG3_NVRAM_SIZE_256KB);
13948 else
13949 tp->nvram_size = (protect ? 0x1f200 :
13950 TG3_NVRAM_SIZE_128KB);
13951 break;
13952 case FLASH_5752VENDOR_ST_M45PE10:
13953 case FLASH_5752VENDOR_ST_M45PE20:
13954 case FLASH_5752VENDOR_ST_M45PE40:
13955 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013956 tg3_flag_set(tp, NVRAM_BUFFERED);
13957 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013958 tp->nvram_pagesize = 256;
13959 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13960 tp->nvram_size = (protect ?
13961 TG3_NVRAM_SIZE_64KB :
13962 TG3_NVRAM_SIZE_128KB);
13963 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13964 tp->nvram_size = (protect ?
13965 TG3_NVRAM_SIZE_64KB :
13966 TG3_NVRAM_SIZE_256KB);
13967 else
13968 tp->nvram_size = (protect ?
13969 TG3_NVRAM_SIZE_128KB :
13970 TG3_NVRAM_SIZE_512KB);
13971 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013972 }
13973}
13974
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013975static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013976{
13977 u32 nvcfg1;
13978
13979 nvcfg1 = tr32(NVRAM_CFG1);
13980
13981 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013982 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13983 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13984 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13985 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13986 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013987 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013988 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013989
Matt Carlson8590a602009-08-28 12:29:16 +000013990 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13991 tw32(NVRAM_CFG1, nvcfg1);
13992 break;
13993 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13994 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13995 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13996 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13997 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013998 tg3_flag_set(tp, NVRAM_BUFFERED);
13999 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014000 tp->nvram_pagesize = 264;
14001 break;
14002 case FLASH_5752VENDOR_ST_M45PE10:
14003 case FLASH_5752VENDOR_ST_M45PE20:
14004 case FLASH_5752VENDOR_ST_M45PE40:
14005 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014006 tg3_flag_set(tp, NVRAM_BUFFERED);
14007 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014008 tp->nvram_pagesize = 256;
14009 break;
Michael Chan1b277772006-03-20 22:27:48 -080014010 }
14011}
14012
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014013static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014014{
14015 u32 nvcfg1, protect = 0;
14016
14017 nvcfg1 = tr32(NVRAM_CFG1);
14018
14019 /* NVRAM protection for TPM */
14020 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014021 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014022 protect = 1;
14023 }
14024
14025 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14026 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014027 case FLASH_5761VENDOR_ATMEL_ADB021D:
14028 case FLASH_5761VENDOR_ATMEL_ADB041D:
14029 case FLASH_5761VENDOR_ATMEL_ADB081D:
14030 case FLASH_5761VENDOR_ATMEL_ADB161D:
14031 case FLASH_5761VENDOR_ATMEL_MDB021D:
14032 case FLASH_5761VENDOR_ATMEL_MDB041D:
14033 case FLASH_5761VENDOR_ATMEL_MDB081D:
14034 case FLASH_5761VENDOR_ATMEL_MDB161D:
14035 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014036 tg3_flag_set(tp, NVRAM_BUFFERED);
14037 tg3_flag_set(tp, FLASH);
14038 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014039 tp->nvram_pagesize = 256;
14040 break;
14041 case FLASH_5761VENDOR_ST_A_M45PE20:
14042 case FLASH_5761VENDOR_ST_A_M45PE40:
14043 case FLASH_5761VENDOR_ST_A_M45PE80:
14044 case FLASH_5761VENDOR_ST_A_M45PE16:
14045 case FLASH_5761VENDOR_ST_M_M45PE20:
14046 case FLASH_5761VENDOR_ST_M_M45PE40:
14047 case FLASH_5761VENDOR_ST_M_M45PE80:
14048 case FLASH_5761VENDOR_ST_M_M45PE16:
14049 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014050 tg3_flag_set(tp, NVRAM_BUFFERED);
14051 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014052 tp->nvram_pagesize = 256;
14053 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014054 }
14055
14056 if (protect) {
14057 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14058 } else {
14059 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014060 case FLASH_5761VENDOR_ATMEL_ADB161D:
14061 case FLASH_5761VENDOR_ATMEL_MDB161D:
14062 case FLASH_5761VENDOR_ST_A_M45PE16:
14063 case FLASH_5761VENDOR_ST_M_M45PE16:
14064 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14065 break;
14066 case FLASH_5761VENDOR_ATMEL_ADB081D:
14067 case FLASH_5761VENDOR_ATMEL_MDB081D:
14068 case FLASH_5761VENDOR_ST_A_M45PE80:
14069 case FLASH_5761VENDOR_ST_M_M45PE80:
14070 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14071 break;
14072 case FLASH_5761VENDOR_ATMEL_ADB041D:
14073 case FLASH_5761VENDOR_ATMEL_MDB041D:
14074 case FLASH_5761VENDOR_ST_A_M45PE40:
14075 case FLASH_5761VENDOR_ST_M_M45PE40:
14076 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14077 break;
14078 case FLASH_5761VENDOR_ATMEL_ADB021D:
14079 case FLASH_5761VENDOR_ATMEL_MDB021D:
14080 case FLASH_5761VENDOR_ST_A_M45PE20:
14081 case FLASH_5761VENDOR_ST_M_M45PE20:
14082 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14083 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014084 }
14085 }
14086}
14087
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014088static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014089{
14090 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014091 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014092 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14093}
14094
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014095static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014096{
14097 u32 nvcfg1;
14098
14099 nvcfg1 = tr32(NVRAM_CFG1);
14100
14101 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14102 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14103 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14104 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014105 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014106 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14107
14108 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14109 tw32(NVRAM_CFG1, nvcfg1);
14110 return;
14111 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14112 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14113 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14114 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14115 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14116 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14117 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14118 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014119 tg3_flag_set(tp, NVRAM_BUFFERED);
14120 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014121
14122 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14123 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14124 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14125 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14126 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14127 break;
14128 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14129 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14130 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14131 break;
14132 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14133 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14134 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14135 break;
14136 }
14137 break;
14138 case FLASH_5752VENDOR_ST_M45PE10:
14139 case FLASH_5752VENDOR_ST_M45PE20:
14140 case FLASH_5752VENDOR_ST_M45PE40:
14141 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014142 tg3_flag_set(tp, NVRAM_BUFFERED);
14143 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014144
14145 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14146 case FLASH_5752VENDOR_ST_M45PE10:
14147 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14148 break;
14149 case FLASH_5752VENDOR_ST_M45PE20:
14150 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14151 break;
14152 case FLASH_5752VENDOR_ST_M45PE40:
14153 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14154 break;
14155 }
14156 break;
14157 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014158 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014159 return;
14160 }
14161
Matt Carlsona1b950d2009-09-01 13:20:17 +000014162 tg3_nvram_get_pagesize(tp, nvcfg1);
14163 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014164 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014165}
14166
14167
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014168static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014169{
14170 u32 nvcfg1;
14171
14172 nvcfg1 = tr32(NVRAM_CFG1);
14173
14174 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14175 case FLASH_5717VENDOR_ATMEL_EEPROM:
14176 case FLASH_5717VENDOR_MICRO_EEPROM:
14177 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014178 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014179 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14180
14181 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14182 tw32(NVRAM_CFG1, nvcfg1);
14183 return;
14184 case FLASH_5717VENDOR_ATMEL_MDB011D:
14185 case FLASH_5717VENDOR_ATMEL_ADB011B:
14186 case FLASH_5717VENDOR_ATMEL_ADB011D:
14187 case FLASH_5717VENDOR_ATMEL_MDB021D:
14188 case FLASH_5717VENDOR_ATMEL_ADB021B:
14189 case FLASH_5717VENDOR_ATMEL_ADB021D:
14190 case FLASH_5717VENDOR_ATMEL_45USPT:
14191 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014192 tg3_flag_set(tp, NVRAM_BUFFERED);
14193 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014194
14195 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14196 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014197 /* Detect size with tg3_nvram_get_size() */
14198 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014199 case FLASH_5717VENDOR_ATMEL_ADB021B:
14200 case FLASH_5717VENDOR_ATMEL_ADB021D:
14201 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14202 break;
14203 default:
14204 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14205 break;
14206 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014207 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014208 case FLASH_5717VENDOR_ST_M_M25PE10:
14209 case FLASH_5717VENDOR_ST_A_M25PE10:
14210 case FLASH_5717VENDOR_ST_M_M45PE10:
14211 case FLASH_5717VENDOR_ST_A_M45PE10:
14212 case FLASH_5717VENDOR_ST_M_M25PE20:
14213 case FLASH_5717VENDOR_ST_A_M25PE20:
14214 case FLASH_5717VENDOR_ST_M_M45PE20:
14215 case FLASH_5717VENDOR_ST_A_M45PE20:
14216 case FLASH_5717VENDOR_ST_25USPT:
14217 case FLASH_5717VENDOR_ST_45USPT:
14218 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014219 tg3_flag_set(tp, NVRAM_BUFFERED);
14220 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014221
14222 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14223 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014224 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014225 /* Detect size with tg3_nvram_get_size() */
14226 break;
14227 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014228 case FLASH_5717VENDOR_ST_A_M45PE20:
14229 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14230 break;
14231 default:
14232 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14233 break;
14234 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014235 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014236 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014237 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014238 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014239 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014240
14241 tg3_nvram_get_pagesize(tp, nvcfg1);
14242 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014243 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014244}
14245
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014246static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014247{
14248 u32 nvcfg1, nvmpinstrp;
14249
14250 nvcfg1 = tr32(NVRAM_CFG1);
14251 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14252
Joe Perches41535772013-02-16 11:20:04 +000014253 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014254 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14255 tg3_flag_set(tp, NO_NVRAM);
14256 return;
14257 }
14258
14259 switch (nvmpinstrp) {
14260 case FLASH_5762_EEPROM_HD:
14261 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014262 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014263 case FLASH_5762_EEPROM_LD:
14264 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014265 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014266 case FLASH_5720VENDOR_M_ST_M45PE20:
14267 /* This pinstrap supports multiple sizes, so force it
14268 * to read the actual size from location 0xf0.
14269 */
14270 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14271 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014272 }
14273 }
14274
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014275 switch (nvmpinstrp) {
14276 case FLASH_5720_EEPROM_HD:
14277 case FLASH_5720_EEPROM_LD:
14278 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014279 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014280
14281 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14282 tw32(NVRAM_CFG1, nvcfg1);
14283 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14284 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14285 else
14286 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14287 return;
14288 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14289 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14290 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14291 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14292 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14293 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14294 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14295 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14296 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14297 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14298 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14299 case FLASH_5720VENDOR_ATMEL_45USPT:
14300 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014301 tg3_flag_set(tp, NVRAM_BUFFERED);
14302 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014303
14304 switch (nvmpinstrp) {
14305 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14306 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14307 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14308 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14309 break;
14310 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14311 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14312 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14313 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14314 break;
14315 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14316 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14317 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14318 break;
14319 default:
Joe Perches41535772013-02-16 11:20:04 +000014320 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014321 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014322 break;
14323 }
14324 break;
14325 case FLASH_5720VENDOR_M_ST_M25PE10:
14326 case FLASH_5720VENDOR_M_ST_M45PE10:
14327 case FLASH_5720VENDOR_A_ST_M25PE10:
14328 case FLASH_5720VENDOR_A_ST_M45PE10:
14329 case FLASH_5720VENDOR_M_ST_M25PE20:
14330 case FLASH_5720VENDOR_M_ST_M45PE20:
14331 case FLASH_5720VENDOR_A_ST_M25PE20:
14332 case FLASH_5720VENDOR_A_ST_M45PE20:
14333 case FLASH_5720VENDOR_M_ST_M25PE40:
14334 case FLASH_5720VENDOR_M_ST_M45PE40:
14335 case FLASH_5720VENDOR_A_ST_M25PE40:
14336 case FLASH_5720VENDOR_A_ST_M45PE40:
14337 case FLASH_5720VENDOR_M_ST_M25PE80:
14338 case FLASH_5720VENDOR_M_ST_M45PE80:
14339 case FLASH_5720VENDOR_A_ST_M25PE80:
14340 case FLASH_5720VENDOR_A_ST_M45PE80:
14341 case FLASH_5720VENDOR_ST_25USPT:
14342 case FLASH_5720VENDOR_ST_45USPT:
14343 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014344 tg3_flag_set(tp, NVRAM_BUFFERED);
14345 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014346
14347 switch (nvmpinstrp) {
14348 case FLASH_5720VENDOR_M_ST_M25PE20:
14349 case FLASH_5720VENDOR_M_ST_M45PE20:
14350 case FLASH_5720VENDOR_A_ST_M25PE20:
14351 case FLASH_5720VENDOR_A_ST_M45PE20:
14352 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14353 break;
14354 case FLASH_5720VENDOR_M_ST_M25PE40:
14355 case FLASH_5720VENDOR_M_ST_M45PE40:
14356 case FLASH_5720VENDOR_A_ST_M25PE40:
14357 case FLASH_5720VENDOR_A_ST_M45PE40:
14358 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14359 break;
14360 case FLASH_5720VENDOR_M_ST_M25PE80:
14361 case FLASH_5720VENDOR_M_ST_M45PE80:
14362 case FLASH_5720VENDOR_A_ST_M25PE80:
14363 case FLASH_5720VENDOR_A_ST_M45PE80:
14364 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14365 break;
14366 default:
Joe Perches41535772013-02-16 11:20:04 +000014367 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014368 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014369 break;
14370 }
14371 break;
14372 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014373 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014374 return;
14375 }
14376
14377 tg3_nvram_get_pagesize(tp, nvcfg1);
14378 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014379 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014380
Joe Perches41535772013-02-16 11:20:04 +000014381 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014382 u32 val;
14383
14384 if (tg3_nvram_read(tp, 0, &val))
14385 return;
14386
14387 if (val != TG3_EEPROM_MAGIC &&
14388 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14389 tg3_flag_set(tp, NO_NVRAM);
14390 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014391}
14392
Linus Torvalds1da177e2005-04-16 15:20:36 -070014393/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014394static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014396 if (tg3_flag(tp, IS_SSB_CORE)) {
14397 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14398 tg3_flag_clear(tp, NVRAM);
14399 tg3_flag_clear(tp, NVRAM_BUFFERED);
14400 tg3_flag_set(tp, NO_NVRAM);
14401 return;
14402 }
14403
Linus Torvalds1da177e2005-04-16 15:20:36 -070014404 tw32_f(GRC_EEPROM_ADDR,
14405 (EEPROM_ADDR_FSM_RESET |
14406 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14407 EEPROM_ADDR_CLKPERD_SHIFT)));
14408
Michael Chan9d57f012006-12-07 00:23:25 -080014409 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014410
14411 /* Enable seeprom accesses. */
14412 tw32_f(GRC_LOCAL_CTRL,
14413 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14414 udelay(100);
14415
Joe Perches41535772013-02-16 11:20:04 +000014416 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14417 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014418 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014419
Michael Chanec41c7d2006-01-17 02:40:55 -080014420 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014421 netdev_warn(tp->dev,
14422 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014423 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014424 return;
14425 }
Michael Chane6af3012005-04-21 17:12:05 -070014426 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014427
Matt Carlson989a9d22007-05-05 11:51:05 -070014428 tp->nvram_size = 0;
14429
Joe Perches41535772013-02-16 11:20:04 +000014430 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014431 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014432 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014433 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014434 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14435 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14436 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014437 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014438 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014439 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014440 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014441 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014442 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014443 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014444 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014445 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14446 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014447 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014448 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14449 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014450 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014451 else
14452 tg3_get_nvram_info(tp);
14453
Matt Carlson989a9d22007-05-05 11:51:05 -070014454 if (tp->nvram_size == 0)
14455 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014456
Michael Chane6af3012005-04-21 17:12:05 -070014457 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014458 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014459
14460 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014461 tg3_flag_clear(tp, NVRAM);
14462 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014463
14464 tg3_get_eeprom_size(tp);
14465 }
14466}
14467
Linus Torvalds1da177e2005-04-16 15:20:36 -070014468struct subsys_tbl_ent {
14469 u16 subsys_vendor, subsys_devid;
14470 u32 phy_id;
14471};
14472
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014473static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014474 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014475 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014476 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014477 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014478 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014479 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014480 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014481 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14482 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14483 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014484 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014485 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014486 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014487 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14488 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14489 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014490 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014491 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014492 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014493 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014494 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014495 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014496 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014497
14498 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014499 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014500 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014501 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014502 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014503 { TG3PCI_SUBVENDOR_ID_3COM,
14504 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14505 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014506 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014507 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014508 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014509
14510 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014511 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014512 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014513 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014514 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014515 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014516 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014517 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014518 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014519
14520 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014521 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014522 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014523 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014524 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014525 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14526 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14527 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014528 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014529 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014530 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531
14532 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014533 { TG3PCI_SUBVENDOR_ID_IBM,
14534 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014535};
14536
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014537static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014538{
14539 int i;
14540
14541 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14542 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14543 tp->pdev->subsystem_vendor) &&
14544 (subsys_id_to_phy_id[i].subsys_devid ==
14545 tp->pdev->subsystem_device))
14546 return &subsys_id_to_phy_id[i];
14547 }
14548 return NULL;
14549}
14550
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014551static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014552{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014554
Matt Carlson79eb6902010-02-17 15:17:03 +000014555 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014556 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14557
Gary Zambranoa85feb82007-05-05 11:52:19 -070014558 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014559 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14560 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014561
Joe Perches41535772013-02-16 11:20:04 +000014562 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014563 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014564 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14565 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014566 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014567 val = tr32(VCPU_CFGSHDW);
14568 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014569 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014570 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014571 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014572 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014573 device_set_wakeup_enable(&tp->pdev->dev, true);
14574 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014575 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014576 }
14577
Linus Torvalds1da177e2005-04-16 15:20:36 -070014578 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14579 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14580 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014581 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014582 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014583
14584 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14585 tp->nic_sram_data_cfg = nic_cfg;
14586
14587 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14588 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014589 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14590 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14591 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592 (ver > 0) && (ver < 0x100))
14593 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14594
Joe Perches41535772013-02-16 11:20:04 +000014595 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014596 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14597
Linus Torvalds1da177e2005-04-16 15:20:36 -070014598 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14599 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14600 eeprom_phy_serdes = 1;
14601
14602 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14603 if (nic_phy_id != 0) {
14604 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14605 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14606
14607 eeprom_phy_id = (id1 >> 16) << 10;
14608 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14609 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14610 } else
14611 eeprom_phy_id = 0;
14612
Michael Chan7d0c41e2005-04-21 17:06:20 -070014613 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014614 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014615 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014616 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014617 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014618 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014619 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014620
Joe Perches63c3a662011-04-26 08:12:10 +000014621 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014622 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14623 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014624 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014625 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14626
14627 switch (led_cfg) {
14628 default:
14629 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14630 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14631 break;
14632
14633 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14634 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14635 break;
14636
14637 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14638 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014639
14640 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14641 * read on some older 5700/5701 bootcode.
14642 */
Joe Perches41535772013-02-16 11:20:04 +000014643 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14644 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014645 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14646
Linus Torvalds1da177e2005-04-16 15:20:36 -070014647 break;
14648
14649 case SHASTA_EXT_LED_SHARED:
14650 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014651 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14652 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14654 LED_CTRL_MODE_PHY_2);
14655 break;
14656
14657 case SHASTA_EXT_LED_MAC:
14658 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14659 break;
14660
14661 case SHASTA_EXT_LED_COMBO:
14662 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014663 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014664 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14665 LED_CTRL_MODE_PHY_2);
14666 break;
14667
Stephen Hemminger855e1112008-04-16 16:37:28 -070014668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014669
Joe Perches41535772013-02-16 11:20:04 +000014670 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14671 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014672 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14673 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14674
Joe Perches41535772013-02-16 11:20:04 +000014675 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014676 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014677
Michael Chan9d26e212006-12-07 00:21:14 -080014678 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014679 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014680 if ((tp->pdev->subsystem_vendor ==
14681 PCI_VENDOR_ID_ARIMA) &&
14682 (tp->pdev->subsystem_device == 0x205a ||
14683 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014684 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014685 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014686 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14687 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014689
14690 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014691 tg3_flag_set(tp, ENABLE_ASF);
14692 if (tg3_flag(tp, 5750_PLUS))
14693 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014694 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014695
14696 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014697 tg3_flag(tp, 5750_PLUS))
14698 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014699
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014700 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014701 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014702 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014703
Joe Perches63c3a662011-04-26 08:12:10 +000014704 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014705 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014706 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014707 device_set_wakeup_enable(&tp->pdev->dev, true);
14708 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014709
Linus Torvalds1da177e2005-04-16 15:20:36 -070014710 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014711 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014712
14713 /* serdes signal pre-emphasis in register 0x590 set by */
14714 /* bootcode if bit 18 is set */
14715 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014716 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014717
Joe Perches63c3a662011-04-26 08:12:10 +000014718 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014719 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14720 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014721 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014722 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014723
Nithin Sujir942d1af2013-04-09 08:48:07 +000014724 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014725 u32 cfg3;
14726
14727 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014728 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
14729 !tg3_flag(tp, 57765_PLUS) &&
14730 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000014731 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014732 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
14733 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
14734 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
14735 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014736 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014737
Matt Carlson14417062010-02-17 15:16:59 +000014738 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014739 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014740 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014741 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014742 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014743 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014744 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014745done:
Joe Perches63c3a662011-04-26 08:12:10 +000014746 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014747 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014748 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014749 else
14750 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014751}
14752
Michael Chanc86a8562013-01-06 12:51:08 +000014753static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14754{
14755 int i, err;
14756 u32 val2, off = offset * 8;
14757
14758 err = tg3_nvram_lock(tp);
14759 if (err)
14760 return err;
14761
14762 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14763 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14764 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14765 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14766 udelay(10);
14767
14768 for (i = 0; i < 100; i++) {
14769 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14770 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14771 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14772 break;
14773 }
14774 udelay(10);
14775 }
14776
14777 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14778
14779 tg3_nvram_unlock(tp);
14780 if (val2 & APE_OTP_STATUS_CMD_DONE)
14781 return 0;
14782
14783 return -EBUSY;
14784}
14785
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014786static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014787{
14788 int i;
14789 u32 val;
14790
14791 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14792 tw32(OTP_CTRL, cmd);
14793
14794 /* Wait for up to 1 ms for command to execute. */
14795 for (i = 0; i < 100; i++) {
14796 val = tr32(OTP_STATUS);
14797 if (val & OTP_STATUS_CMD_DONE)
14798 break;
14799 udelay(10);
14800 }
14801
14802 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14803}
14804
14805/* Read the gphy configuration from the OTP region of the chip. The gphy
14806 * configuration is a 32-bit value that straddles the alignment boundary.
14807 * We do two 32-bit reads and then shift and merge the results.
14808 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014809static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014810{
14811 u32 bhalf_otp, thalf_otp;
14812
14813 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14814
14815 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14816 return 0;
14817
14818 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14819
14820 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14821 return 0;
14822
14823 thalf_otp = tr32(OTP_READ_DATA);
14824
14825 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14826
14827 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14828 return 0;
14829
14830 bhalf_otp = tr32(OTP_READ_DATA);
14831
14832 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14833}
14834
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014835static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014836{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014837 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014838
14839 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14840 adv |= ADVERTISED_1000baseT_Half |
14841 ADVERTISED_1000baseT_Full;
14842
14843 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14844 adv |= ADVERTISED_100baseT_Half |
14845 ADVERTISED_100baseT_Full |
14846 ADVERTISED_10baseT_Half |
14847 ADVERTISED_10baseT_Full |
14848 ADVERTISED_TP;
14849 else
14850 adv |= ADVERTISED_FIBRE;
14851
14852 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014853 tp->link_config.speed = SPEED_UNKNOWN;
14854 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014855 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014856 tp->link_config.active_speed = SPEED_UNKNOWN;
14857 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014858
14859 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014860}
14861
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014862static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014863{
14864 u32 hw_phy_id_1, hw_phy_id_2;
14865 u32 hw_phy_id, hw_phy_id_masked;
14866 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014867
Matt Carlsone256f8a2011-03-09 16:58:24 +000014868 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014869 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014870 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14871
Michael Chan8151ad52012-07-29 19:15:41 +000014872 if (tg3_flag(tp, ENABLE_APE)) {
14873 switch (tp->pci_fn) {
14874 case 0:
14875 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14876 break;
14877 case 1:
14878 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14879 break;
14880 case 2:
14881 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14882 break;
14883 case 3:
14884 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14885 break;
14886 }
14887 }
14888
Nithin Sujir942d1af2013-04-09 08:48:07 +000014889 if (!tg3_flag(tp, ENABLE_ASF) &&
14890 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
14891 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14892 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
14893 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
14894
Joe Perches63c3a662011-04-26 08:12:10 +000014895 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014896 return tg3_phy_init(tp);
14897
Linus Torvalds1da177e2005-04-16 15:20:36 -070014898 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014899 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014900 */
14901 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014902 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014903 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014904 } else {
14905 /* Now read the physical PHY_ID from the chip and verify
14906 * that it is sane. If it doesn't look good, we fall back
14907 * to either the hard-coded table based PHY_ID and failing
14908 * that the value found in the eeprom area.
14909 */
14910 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14911 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14912
14913 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14914 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14915 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14916
Matt Carlson79eb6902010-02-17 15:17:03 +000014917 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014918 }
14919
Matt Carlson79eb6902010-02-17 15:17:03 +000014920 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014921 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014922 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014923 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014924 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014925 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014927 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014928 /* Do nothing, phy ID already set up in
14929 * tg3_get_eeprom_hw_cfg().
14930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014931 } else {
14932 struct subsys_tbl_ent *p;
14933
14934 /* No eeprom signature? Try the hardcoded
14935 * subsys device table.
14936 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014937 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014938 if (p) {
14939 tp->phy_id = p->phy_id;
14940 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
14941 /* For now we saw the IDs 0xbc050cd0,
14942 * 0xbc050f80 and 0xbc050c30 on devices
14943 * connected to an BCM4785 and there are
14944 * probably more. Just assume that the phy is
14945 * supported when it is connected to a SSB core
14946 * for now.
14947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014948 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014950
Linus Torvalds1da177e2005-04-16 15:20:36 -070014951 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014952 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014953 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014954 }
14955 }
14956
Matt Carlsona6b68da2010-12-06 08:28:52 +000014957 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000014958 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
14959 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000014960 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000014961 tg3_asic_rev(tp) == ASIC_REV_5762 ||
14962 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
14963 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
14964 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000014965 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000014966 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14967
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000014968 tp->eee.supported = SUPPORTED_100baseT_Full |
14969 SUPPORTED_1000baseT_Full;
14970 tp->eee.advertised = ADVERTISED_100baseT_Full |
14971 ADVERTISED_1000baseT_Full;
14972 tp->eee.eee_enabled = 1;
14973 tp->eee.tx_lpi_enabled = 1;
14974 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
14975 }
14976
Matt Carlsone256f8a2011-03-09 16:58:24 +000014977 tg3_phy_init_link_config(tp);
14978
Nithin Sujir942d1af2013-04-09 08:48:07 +000014979 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
14980 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014981 !tg3_flag(tp, ENABLE_APE) &&
14982 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014983 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014984
14985 tg3_readphy(tp, MII_BMSR, &bmsr);
14986 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14987 (bmsr & BMSR_LSTATUS))
14988 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014989
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990 err = tg3_phy_reset(tp);
14991 if (err)
14992 return err;
14993
Matt Carlson42b64a42011-05-19 12:12:49 +000014994 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014995
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014996 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014997 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14998 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999
15000 tg3_writephy(tp, MII_BMCR,
15001 BMCR_ANENABLE | BMCR_ANRESTART);
15002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015003 }
15004
15005skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015006 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015007 err = tg3_init_5401phy_dsp(tp);
15008 if (err)
15009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015010
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011 err = tg3_init_5401phy_dsp(tp);
15012 }
15013
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014 return err;
15015}
15016
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015017static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015018{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015019 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015020 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015021 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015022 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015023
Matt Carlson535a4902011-07-20 10:20:56 +000015024 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015025 if (!vpd_data)
15026 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015027
Matt Carlson535a4902011-07-20 10:20:56 +000015028 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015029 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015030 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015031
15032 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15033 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15034 i += PCI_VPD_LRDT_TAG_SIZE;
15035
Matt Carlson535a4902011-07-20 10:20:56 +000015036 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015037 goto out_not_found;
15038
Matt Carlson184b8902010-04-05 10:19:25 +000015039 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15040 PCI_VPD_RO_KEYWORD_MFR_ID);
15041 if (j > 0) {
15042 len = pci_vpd_info_field_size(&vpd_data[j]);
15043
15044 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15045 if (j + len > block_end || len != 4 ||
15046 memcmp(&vpd_data[j], "1028", 4))
15047 goto partno;
15048
15049 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15050 PCI_VPD_RO_KEYWORD_VENDOR0);
15051 if (j < 0)
15052 goto partno;
15053
15054 len = pci_vpd_info_field_size(&vpd_data[j]);
15055
15056 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15057 if (j + len > block_end)
15058 goto partno;
15059
Kees Cook715230a2013-03-27 06:40:50 +000015060 if (len >= sizeof(tp->fw_ver))
15061 len = sizeof(tp->fw_ver) - 1;
15062 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15063 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15064 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015065 }
15066
15067partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015068 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15069 PCI_VPD_RO_KEYWORD_PARTNO);
15070 if (i < 0)
15071 goto out_not_found;
15072
15073 len = pci_vpd_info_field_size(&vpd_data[i]);
15074
15075 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15076 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015077 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015078 goto out_not_found;
15079
15080 memcpy(tp->board_part_number, &vpd_data[i], len);
15081
Linus Torvalds1da177e2005-04-16 15:20:36 -070015082out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015083 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015084 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015085 return;
15086
15087out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015088 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015089 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15090 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015091 strcpy(tp->board_part_number, "BCM5717");
15092 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15093 strcpy(tp->board_part_number, "BCM5718");
15094 else
15095 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015096 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015097 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15098 strcpy(tp->board_part_number, "BCM57780");
15099 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15100 strcpy(tp->board_part_number, "BCM57760");
15101 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15102 strcpy(tp->board_part_number, "BCM57790");
15103 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15104 strcpy(tp->board_part_number, "BCM57788");
15105 else
15106 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015107 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015108 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15109 strcpy(tp->board_part_number, "BCM57761");
15110 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15111 strcpy(tp->board_part_number, "BCM57765");
15112 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15113 strcpy(tp->board_part_number, "BCM57781");
15114 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15115 strcpy(tp->board_part_number, "BCM57785");
15116 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15117 strcpy(tp->board_part_number, "BCM57791");
15118 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15119 strcpy(tp->board_part_number, "BCM57795");
15120 else
15121 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015122 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015123 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15124 strcpy(tp->board_part_number, "BCM57762");
15125 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15126 strcpy(tp->board_part_number, "BCM57766");
15127 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15128 strcpy(tp->board_part_number, "BCM57782");
15129 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15130 strcpy(tp->board_part_number, "BCM57786");
15131 else
15132 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015133 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015134 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015135 } else {
15136nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015137 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015139}
15140
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015141static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015142{
15143 u32 val;
15144
Matt Carlsone4f34112009-02-25 14:25:00 +000015145 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015146 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015147 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015148 val != 0)
15149 return 0;
15150
15151 return 1;
15152}
15153
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015154static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015155{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015156 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015157 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015158 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015159
15160 if (tg3_nvram_read(tp, 0xc, &offset) ||
15161 tg3_nvram_read(tp, 0x4, &start))
15162 return;
15163
15164 offset = tg3_nvram_logical_addr(tp, offset);
15165
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015166 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015167 return;
15168
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015169 if ((val & 0xfc000000) == 0x0c000000) {
15170 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015171 return;
15172
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015173 if (val == 0)
15174 newver = true;
15175 }
15176
Matt Carlson75f99362010-04-05 10:19:24 +000015177 dst_off = strlen(tp->fw_ver);
15178
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015179 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015180 if (TG3_VER_SIZE - dst_off < 16 ||
15181 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015182 return;
15183
15184 offset = offset + ver_offset - start;
15185 for (i = 0; i < 16; i += 4) {
15186 __be32 v;
15187 if (tg3_nvram_read_be32(tp, offset + i, &v))
15188 return;
15189
Matt Carlson75f99362010-04-05 10:19:24 +000015190 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015191 }
15192 } else {
15193 u32 major, minor;
15194
15195 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15196 return;
15197
15198 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15199 TG3_NVM_BCVER_MAJSFT;
15200 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015201 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15202 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015203 }
15204}
15205
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015206static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015207{
15208 u32 val, major, minor;
15209
15210 /* Use native endian representation */
15211 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15212 return;
15213
15214 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15215 TG3_NVM_HWSB_CFG1_MAJSFT;
15216 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15217 TG3_NVM_HWSB_CFG1_MINSFT;
15218
15219 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15220}
15221
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015222static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015223{
15224 u32 offset, major, minor, build;
15225
Matt Carlson75f99362010-04-05 10:19:24 +000015226 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015227
15228 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15229 return;
15230
15231 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15232 case TG3_EEPROM_SB_REVISION_0:
15233 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15234 break;
15235 case TG3_EEPROM_SB_REVISION_2:
15236 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15237 break;
15238 case TG3_EEPROM_SB_REVISION_3:
15239 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15240 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015241 case TG3_EEPROM_SB_REVISION_4:
15242 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15243 break;
15244 case TG3_EEPROM_SB_REVISION_5:
15245 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15246 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015247 case TG3_EEPROM_SB_REVISION_6:
15248 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15249 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015250 default:
15251 return;
15252 }
15253
Matt Carlsone4f34112009-02-25 14:25:00 +000015254 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015255 return;
15256
15257 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15258 TG3_EEPROM_SB_EDH_BLD_SHFT;
15259 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15260 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15261 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15262
15263 if (minor > 99 || build > 26)
15264 return;
15265
Matt Carlson75f99362010-04-05 10:19:24 +000015266 offset = strlen(tp->fw_ver);
15267 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15268 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015269
15270 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015271 offset = strlen(tp->fw_ver);
15272 if (offset < TG3_VER_SIZE - 1)
15273 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015274 }
15275}
15276
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015277static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015278{
15279 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015280 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015281
15282 for (offset = TG3_NVM_DIR_START;
15283 offset < TG3_NVM_DIR_END;
15284 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015285 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015286 return;
15287
15288 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15289 break;
15290 }
15291
15292 if (offset == TG3_NVM_DIR_END)
15293 return;
15294
Joe Perches63c3a662011-04-26 08:12:10 +000015295 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015296 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015297 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015298 return;
15299
Matt Carlsone4f34112009-02-25 14:25:00 +000015300 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015301 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015302 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015303 return;
15304
15305 offset += val - start;
15306
Matt Carlsonacd9c112009-02-25 14:26:33 +000015307 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015308
Matt Carlsonacd9c112009-02-25 14:26:33 +000015309 tp->fw_ver[vlen++] = ',';
15310 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015311
15312 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015313 __be32 v;
15314 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015315 return;
15316
Al Virob9fc7dc2007-12-17 22:59:57 -080015317 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015318
Matt Carlsonacd9c112009-02-25 14:26:33 +000015319 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15320 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015321 break;
15322 }
15323
Matt Carlsonacd9c112009-02-25 14:26:33 +000015324 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15325 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015326 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015327}
15328
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015329static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015330{
Matt Carlson7fd76442009-02-25 14:27:20 +000015331 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015332
15333 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15334 if (apedata != APE_SEG_SIG_MAGIC)
15335 return;
15336
15337 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15338 if (!(apedata & APE_FW_STATUS_READY))
15339 return;
15340
Michael Chan165f4d12012-07-16 16:23:59 +000015341 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15342 tg3_flag_set(tp, APE_HAS_NCSI);
15343}
15344
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015345static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015346{
15347 int vlen;
15348 u32 apedata;
15349 char *fwtype;
15350
Matt Carlson7fd76442009-02-25 14:27:20 +000015351 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15352
Michael Chan165f4d12012-07-16 16:23:59 +000015353 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015354 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015355 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15356 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015357 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015358 fwtype = "DASH";
15359
Matt Carlson7fd76442009-02-25 14:27:20 +000015360 vlen = strlen(tp->fw_ver);
15361
Matt Carlsonecc79642010-08-02 11:26:01 +000015362 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15363 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015364 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15365 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15366 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15367 (apedata & APE_FW_VERSION_BLDMSK));
15368}
15369
Michael Chanc86a8562013-01-06 12:51:08 +000015370static void tg3_read_otp_ver(struct tg3 *tp)
15371{
15372 u32 val, val2;
15373
Joe Perches41535772013-02-16 11:20:04 +000015374 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015375 return;
15376
15377 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15378 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15379 TG3_OTP_MAGIC0_VALID(val)) {
15380 u64 val64 = (u64) val << 32 | val2;
15381 u32 ver = 0;
15382 int i, vlen;
15383
15384 for (i = 0; i < 7; i++) {
15385 if ((val64 & 0xff) == 0)
15386 break;
15387 ver = val64 & 0xff;
15388 val64 >>= 8;
15389 }
15390 vlen = strlen(tp->fw_ver);
15391 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15392 }
15393}
15394
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015395static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015396{
15397 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015398 bool vpd_vers = false;
15399
15400 if (tp->fw_ver[0] != 0)
15401 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015402
Joe Perches63c3a662011-04-26 08:12:10 +000015403 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015404 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015405 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015406 return;
15407 }
15408
Matt Carlsonacd9c112009-02-25 14:26:33 +000015409 if (tg3_nvram_read(tp, 0, &val))
15410 return;
15411
15412 if (val == TG3_EEPROM_MAGIC)
15413 tg3_read_bc_ver(tp);
15414 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15415 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015416 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15417 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015418
Michael Chan165f4d12012-07-16 16:23:59 +000015419 if (tg3_flag(tp, ENABLE_ASF)) {
15420 if (tg3_flag(tp, ENABLE_APE)) {
15421 tg3_probe_ncsi(tp);
15422 if (!vpd_vers)
15423 tg3_read_dash_ver(tp);
15424 } else if (!vpd_vers) {
15425 tg3_read_mgmtfw_ver(tp);
15426 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015427 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015428
15429 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015430}
15431
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015432static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15433{
Joe Perches63c3a662011-04-26 08:12:10 +000015434 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015435 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015436 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015437 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015438 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015439 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015440}
15441
Matt Carlson41434702011-03-09 16:58:22 +000015442static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015443 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15444 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15445 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15446 { },
15447};
15448
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015449static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015450{
15451 struct pci_dev *peer;
15452 unsigned int func, devnr = tp->pdev->devfn & ~7;
15453
15454 for (func = 0; func < 8; func++) {
15455 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15456 if (peer && peer != tp->pdev)
15457 break;
15458 pci_dev_put(peer);
15459 }
15460 /* 5704 can be configured in single-port mode, set peer to
15461 * tp->pdev in that case.
15462 */
15463 if (!peer) {
15464 peer = tp->pdev;
15465 return peer;
15466 }
15467
15468 /*
15469 * We don't need to keep the refcount elevated; there's no way
15470 * to remove one half of this device without removing the other
15471 */
15472 pci_dev_put(peer);
15473
15474 return peer;
15475}
15476
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015477static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015478{
15479 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015480 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015481 u32 reg;
15482
15483 /* All devices that use the alternate
15484 * ASIC REV location have a CPMU.
15485 */
15486 tg3_flag_set(tp, CPMU_PRESENT);
15487
15488 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015489 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015490 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15491 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015492 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15493 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15494 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15495 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015496 reg = TG3PCI_GEN2_PRODID_ASICREV;
15497 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15498 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15499 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15500 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15501 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15502 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15503 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15504 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15505 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15506 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15507 reg = TG3PCI_GEN15_PRODID_ASICREV;
15508 else
15509 reg = TG3PCI_PRODID_ASICREV;
15510
15511 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15512 }
15513
15514 /* Wrong chip ID in 5752 A0. This code can be removed later
15515 * as A0 is not in production.
15516 */
Joe Perches41535772013-02-16 11:20:04 +000015517 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015518 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15519
Joe Perches41535772013-02-16 11:20:04 +000015520 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015521 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15522
Joe Perches41535772013-02-16 11:20:04 +000015523 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15524 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15525 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015526 tg3_flag_set(tp, 5717_PLUS);
15527
Joe Perches41535772013-02-16 11:20:04 +000015528 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15529 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015530 tg3_flag_set(tp, 57765_CLASS);
15531
Michael Chanc65a17f2013-01-06 12:51:07 +000015532 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015533 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015534 tg3_flag_set(tp, 57765_PLUS);
15535
15536 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015537 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15538 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15539 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15540 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15541 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15542 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015543 tg3_flag(tp, 57765_PLUS))
15544 tg3_flag_set(tp, 5755_PLUS);
15545
Joe Perches41535772013-02-16 11:20:04 +000015546 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15547 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015548 tg3_flag_set(tp, 5780_CLASS);
15549
Joe Perches41535772013-02-16 11:20:04 +000015550 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15551 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15552 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015553 tg3_flag(tp, 5755_PLUS) ||
15554 tg3_flag(tp, 5780_CLASS))
15555 tg3_flag_set(tp, 5750_PLUS);
15556
Joe Perches41535772013-02-16 11:20:04 +000015557 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015558 tg3_flag(tp, 5750_PLUS))
15559 tg3_flag_set(tp, 5705_PLUS);
15560}
15561
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015562static bool tg3_10_100_only_device(struct tg3 *tp,
15563 const struct pci_device_id *ent)
15564{
15565 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15566
Joe Perches41535772013-02-16 11:20:04 +000015567 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15568 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015569 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15570 return true;
15571
15572 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015573 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015574 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15575 return true;
15576 } else {
15577 return true;
15578 }
15579 }
15580
15581 return false;
15582}
15583
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015584static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015586 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015587 u32 pci_state_reg, grc_misc_cfg;
15588 u32 val;
15589 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015590 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015591
Linus Torvalds1da177e2005-04-16 15:20:36 -070015592 /* Force memory write invalidate off. If we leave it on,
15593 * then on 5700_BX chips we have to enable a workaround.
15594 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15595 * to match the cacheline size. The Broadcom driver have this
15596 * workaround but turns MWI off all the times so never uses
15597 * it. This seems to suggest that the workaround is insufficient.
15598 */
15599 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15600 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15601 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15602
Matt Carlson16821282011-07-13 09:27:28 +000015603 /* Important! -- Make sure register accesses are byteswapped
15604 * correctly. Also, for those chips that require it, make
15605 * sure that indirect register accesses are enabled before
15606 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015607 */
15608 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15609 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015610 tp->misc_host_ctrl |= (misc_ctrl_reg &
15611 MISC_HOST_CTRL_CHIPREV);
15612 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15613 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015614
Matt Carlson42b123b2012-02-13 15:20:13 +000015615 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015616
Michael Chan68929142005-08-09 20:17:14 -070015617 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15618 * we need to disable memory and use config. cycles
15619 * only to access all registers. The 5702/03 chips
15620 * can mistakenly decode the special cycles from the
15621 * ICH chipsets as memory write cycles, causing corruption
15622 * of register and memory space. Only certain ICH bridges
15623 * will drive special cycles with non-zero data during the
15624 * address phase which can fall within the 5703's address
15625 * range. This is not an ICH bug as the PCI spec allows
15626 * non-zero address during special cycles. However, only
15627 * these ICH bridges are known to drive non-zero addresses
15628 * during special cycles.
15629 *
15630 * Since special cycles do not cross PCI bridges, we only
15631 * enable this workaround if the 5703 is on the secondary
15632 * bus of these ICH bridges.
15633 */
Joe Perches41535772013-02-16 11:20:04 +000015634 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15635 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015636 static struct tg3_dev_id {
15637 u32 vendor;
15638 u32 device;
15639 u32 rev;
15640 } ich_chipsets[] = {
15641 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15642 PCI_ANY_ID },
15643 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15644 PCI_ANY_ID },
15645 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15646 0xa },
15647 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15648 PCI_ANY_ID },
15649 { },
15650 };
15651 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15652 struct pci_dev *bridge = NULL;
15653
15654 while (pci_id->vendor != 0) {
15655 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15656 bridge);
15657 if (!bridge) {
15658 pci_id++;
15659 continue;
15660 }
15661 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015662 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015663 continue;
15664 }
15665 if (bridge->subordinate &&
15666 (bridge->subordinate->number ==
15667 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015668 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015669 pci_dev_put(bridge);
15670 break;
15671 }
15672 }
15673 }
15674
Joe Perches41535772013-02-16 11:20:04 +000015675 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015676 static struct tg3_dev_id {
15677 u32 vendor;
15678 u32 device;
15679 } bridge_chipsets[] = {
15680 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15681 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15682 { },
15683 };
15684 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15685 struct pci_dev *bridge = NULL;
15686
15687 while (pci_id->vendor != 0) {
15688 bridge = pci_get_device(pci_id->vendor,
15689 pci_id->device,
15690 bridge);
15691 if (!bridge) {
15692 pci_id++;
15693 continue;
15694 }
15695 if (bridge->subordinate &&
15696 (bridge->subordinate->number <=
15697 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015698 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015699 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015700 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015701 pci_dev_put(bridge);
15702 break;
15703 }
15704 }
15705 }
15706
Michael Chan4a29cc22006-03-19 13:21:12 -080015707 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15708 * DMA addresses > 40-bit. This bridge may have other additional
15709 * 57xx devices behind it in some 4-port NIC designs for example.
15710 * Any tg3 device found behind the bridge will also need the 40-bit
15711 * DMA workaround.
15712 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015713 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015714 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015715 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015716 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015717 struct pci_dev *bridge = NULL;
15718
15719 do {
15720 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15721 PCI_DEVICE_ID_SERVERWORKS_EPB,
15722 bridge);
15723 if (bridge && bridge->subordinate &&
15724 (bridge->subordinate->number <=
15725 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015726 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015727 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015728 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015729 pci_dev_put(bridge);
15730 break;
15731 }
15732 } while (bridge);
15733 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015734
Joe Perches41535772013-02-16 11:20:04 +000015735 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15736 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015737 tp->pdev_peer = tg3_find_peer(tp);
15738
Matt Carlson507399f2009-11-13 13:03:37 +000015739 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015740 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015741 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015742 else if (tg3_flag(tp, 57765_PLUS))
15743 tg3_flag_set(tp, HW_TSO_3);
15744 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015745 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015746 tg3_flag_set(tp, HW_TSO_2);
15747 else if (tg3_flag(tp, 5750_PLUS)) {
15748 tg3_flag_set(tp, HW_TSO_1);
15749 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015750 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15751 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015752 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015753 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15754 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15755 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000015756 tg3_flag_set(tp, FW_TSO);
15757 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015758 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015759 tp->fw_needed = FIRMWARE_TG3TSO5;
15760 else
15761 tp->fw_needed = FIRMWARE_TG3TSO;
15762 }
15763
Matt Carlsondabc5c62011-05-19 12:12:52 +000015764 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015765 if (tg3_flag(tp, HW_TSO_1) ||
15766 tg3_flag(tp, HW_TSO_2) ||
15767 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000015768 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015769 /* For firmware TSO, assume ASF is disabled.
15770 * We'll disable TSO later if we discover ASF
15771 * is enabled in tg3_get_eeprom_hw_cfg().
15772 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015773 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015774 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015775 tg3_flag_clear(tp, TSO_CAPABLE);
15776 tg3_flag_clear(tp, TSO_BUG);
15777 tp->fw_needed = NULL;
15778 }
15779
Joe Perches41535772013-02-16 11:20:04 +000015780 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015781 tp->fw_needed = FIRMWARE_TG3;
15782
Nithin Sujirc4dab502013-03-06 17:02:34 +000015783 if (tg3_asic_rev(tp) == ASIC_REV_57766)
15784 tp->fw_needed = FIRMWARE_TG357766;
15785
Matt Carlson507399f2009-11-13 13:03:37 +000015786 tp->irq_max = 1;
15787
Joe Perches63c3a662011-04-26 08:12:10 +000015788 if (tg3_flag(tp, 5750_PLUS)) {
15789 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015790 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15791 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15792 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15793 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015794 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015795 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015796
Joe Perches63c3a662011-04-26 08:12:10 +000015797 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015798 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015799 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015801
Joe Perches63c3a662011-04-26 08:12:10 +000015802 if (tg3_flag(tp, 57765_PLUS)) {
15803 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015804 tp->irq_max = TG3_IRQ_MAX_VECS;
15805 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015806 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015807
Michael Chan91024262012-09-28 07:12:38 +000015808 tp->txq_max = 1;
15809 tp->rxq_max = 1;
15810 if (tp->irq_max > 1) {
15811 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15812 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15813
Joe Perches41535772013-02-16 11:20:04 +000015814 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15815 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000015816 tp->txq_max = tp->irq_max - 1;
15817 }
15818
Matt Carlsonb7abee62012-06-07 12:56:54 +000015819 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015820 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015821 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015822
Joe Perches41535772013-02-16 11:20:04 +000015823 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015824 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015825
Joe Perches41535772013-02-16 11:20:04 +000015826 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15827 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15828 tg3_asic_rev(tp) == ASIC_REV_5720 ||
15829 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015830 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015831
Joe Perches63c3a662011-04-26 08:12:10 +000015832 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000015833 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015834 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015835
Joe Perches63c3a662011-04-26 08:12:10 +000015836 if (!tg3_flag(tp, 5705_PLUS) ||
15837 tg3_flag(tp, 5780_CLASS) ||
15838 tg3_flag(tp, USE_JUMBO_BDFLAG))
15839 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015840
Matt Carlson52f44902008-11-21 17:17:04 -080015841 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15842 &pci_state_reg);
15843
Jon Mason708ebb3a2011-06-27 12:56:50 +000015844 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015845 u16 lnkctl;
15846
Joe Perches63c3a662011-04-26 08:12:10 +000015847 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015848
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015849 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015850 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000015851 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015852 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015853 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015854 }
Joe Perches41535772013-02-16 11:20:04 +000015855 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
15856 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15857 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
15858 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015859 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015860 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015861 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015862 }
Joe Perches41535772013-02-16 11:20:04 +000015863 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015864 /* BCM5785 devices are effectively PCIe devices, and should
15865 * follow PCIe codepaths, but do not have a PCIe capabilities
15866 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015867 */
Joe Perches63c3a662011-04-26 08:12:10 +000015868 tg3_flag_set(tp, PCI_EXPRESS);
15869 } else if (!tg3_flag(tp, 5705_PLUS) ||
15870 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015871 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15872 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015873 dev_err(&tp->pdev->dev,
15874 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015875 return -EIO;
15876 }
15877
15878 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015879 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015881
Michael Chan399de502005-10-03 14:02:39 -070015882 /* If we have an AMD 762 or VIA K8T800 chipset, write
15883 * reordering to the mailbox registers done by the host
15884 * controller can cause major troubles. We read back from
15885 * every mailbox register write to force the writes to be
15886 * posted to the chip in order.
15887 */
Matt Carlson41434702011-03-09 16:58:22 +000015888 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015889 !tg3_flag(tp, PCI_EXPRESS))
15890 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015891
Matt Carlson69fc4052008-12-21 20:19:57 -080015892 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15893 &tp->pci_cacheline_sz);
15894 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15895 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000015896 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015897 tp->pci_lat_timer < 64) {
15898 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015899 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15900 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015901 }
15902
Matt Carlson16821282011-07-13 09:27:28 +000015903 /* Important! -- It is critical that the PCI-X hw workaround
15904 * situation is decided before the first MMIO register access.
15905 */
Joe Perches41535772013-02-16 11:20:04 +000015906 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080015907 /* 5700 BX chips need to have their TX producer index
15908 * mailboxes written twice to workaround a bug.
15909 */
Joe Perches63c3a662011-04-26 08:12:10 +000015910 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015911
Matt Carlson52f44902008-11-21 17:17:04 -080015912 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015913 *
15914 * The workaround is to use indirect register accesses
15915 * for all chip writes not to mailbox registers.
15916 */
Joe Perches63c3a662011-04-26 08:12:10 +000015917 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015918 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015919
Joe Perches63c3a662011-04-26 08:12:10 +000015920 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015921
15922 /* The chip can have it's power management PCI config
15923 * space registers clobbered due to this bug.
15924 * So explicitly force the chip into D0 here.
15925 */
Matt Carlson9974a352007-10-07 23:27:28 -070015926 pci_read_config_dword(tp->pdev,
15927 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015928 &pm_reg);
15929 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15930 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015931 pci_write_config_dword(tp->pdev,
15932 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015933 pm_reg);
15934
15935 /* Also, force SERR#/PERR# in PCI command. */
15936 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15937 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15938 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15939 }
15940 }
15941
Linus Torvalds1da177e2005-04-16 15:20:36 -070015942 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015943 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015944 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015945 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015946
15947 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000015948 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015949 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15950 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15951 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15952 }
15953
Michael Chan1ee582d2005-08-09 20:16:46 -070015954 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015955 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015956 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015957 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015958 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015959 tp->write32_tx_mbox = tg3_write32;
15960 tp->write32_rx_mbox = tg3_write32;
15961
15962 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015963 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015964 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000015965 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015966 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000015967 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070015968 /*
15969 * Back to back register writes can cause problems on these
15970 * chips, the workaround is to read back all reg writes
15971 * except those to mailbox regs.
15972 *
15973 * See tg3_write_indirect_reg32().
15974 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015975 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015976 }
15977
Joe Perches63c3a662011-04-26 08:12:10 +000015978 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015979 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015980 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015981 tp->write32_rx_mbox = tg3_write_flush_reg32;
15982 }
Michael Chan20094932005-08-09 20:16:32 -070015983
Joe Perches63c3a662011-04-26 08:12:10 +000015984 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015985 tp->read32 = tg3_read_indirect_reg32;
15986 tp->write32 = tg3_write_indirect_reg32;
15987 tp->read32_mbox = tg3_read_indirect_mbox;
15988 tp->write32_mbox = tg3_write_indirect_mbox;
15989 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15990 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15991
15992 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015993 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015994
15995 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15996 pci_cmd &= ~PCI_COMMAND_MEMORY;
15997 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15998 }
Joe Perches41535772013-02-16 11:20:04 +000015999 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016000 tp->read32_mbox = tg3_read32_mbox_5906;
16001 tp->write32_mbox = tg3_write32_mbox_5906;
16002 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16003 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16004 }
Michael Chan68929142005-08-09 20:17:14 -070016005
Michael Chanbbadf502006-04-06 21:46:34 -070016006 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016007 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016008 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16009 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016010 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016011
Matt Carlson16821282011-07-13 09:27:28 +000016012 /* The memory arbiter has to be enabled in order for SRAM accesses
16013 * to succeed. Normally on powerup the tg3 chip firmware will make
16014 * sure it is enabled, but other entities such as system netboot
16015 * code might disable it.
16016 */
16017 val = tr32(MEMARB_MODE);
16018 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16019
Matt Carlson9dc5e342011-11-04 09:15:02 +000016020 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016021 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016022 tg3_flag(tp, 5780_CLASS)) {
16023 if (tg3_flag(tp, PCIX_MODE)) {
16024 pci_read_config_dword(tp->pdev,
16025 tp->pcix_cap + PCI_X_STATUS,
16026 &val);
16027 tp->pci_fn = val & 0x7;
16028 }
Joe Perches41535772013-02-16 11:20:04 +000016029 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16030 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16031 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016032 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016033 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16034 val = tr32(TG3_CPMU_STATUS);
16035
Joe Perches41535772013-02-16 11:20:04 +000016036 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016037 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16038 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016039 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16040 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016041 }
16042
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016043 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16044 tp->write32_tx_mbox = tg3_write_flush_reg32;
16045 tp->write32_rx_mbox = tg3_write_flush_reg32;
16046 }
16047
Michael Chan7d0c41e2005-04-21 17:06:20 -070016048 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016049 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016050 * determined before calling tg3_set_power_state() so that
16051 * we know whether or not to switch out of Vaux power.
16052 * When the flag is set, it means that GPIO1 is used for eeprom
16053 * write protect and also implies that it is a LOM where GPIOs
16054 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016055 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016056 tg3_get_eeprom_hw_cfg(tp);
16057
Matt Carlson1caf13e2013-03-06 17:02:29 +000016058 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016059 tg3_flag_clear(tp, TSO_CAPABLE);
16060 tg3_flag_clear(tp, TSO_BUG);
16061 tp->fw_needed = NULL;
16062 }
16063
Joe Perches63c3a662011-04-26 08:12:10 +000016064 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016065 /* Allow reads and writes to the
16066 * APE register and memory space.
16067 */
16068 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016069 PCISTATE_ALLOW_APE_SHMEM_WR |
16070 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016071 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16072 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016073
16074 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016075 }
16076
Matt Carlson16821282011-07-13 09:27:28 +000016077 /* Set up tp->grc_local_ctrl before calling
16078 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16079 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016080 * It is also used as eeprom write protect on LOMs.
16081 */
16082 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016083 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016084 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016085 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16086 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016087 /* Unused GPIO3 must be driven as output on 5752 because there
16088 * are no pull-up resistors on unused GPIO pins.
16089 */
Joe Perches41535772013-02-16 11:20:04 +000016090 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016091 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016092
Joe Perches41535772013-02-16 11:20:04 +000016093 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16094 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016095 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016096 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16097
Matt Carlson8d519ab2009-04-20 06:58:01 +000016098 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16099 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016100 /* Turn off the debug UART. */
16101 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016102 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016103 /* Keep VMain power. */
16104 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16105 GRC_LCLCTRL_GPIO_OUTPUT0;
16106 }
16107
Joe Perches41535772013-02-16 11:20:04 +000016108 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016109 tp->grc_local_ctrl |=
16110 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16111
Matt Carlson16821282011-07-13 09:27:28 +000016112 /* Switch out of Vaux if it is a NIC */
16113 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016114
Linus Torvalds1da177e2005-04-16 15:20:36 -070016115 /* Derive initial jumbo mode from MTU assigned in
16116 * ether_setup() via the alloc_etherdev() call
16117 */
Joe Perches63c3a662011-04-26 08:12:10 +000016118 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16119 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016120
16121 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016122 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16123 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16124 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16125 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016126 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016127 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016128 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016129 }
16130
Joe Perches41535772013-02-16 11:20:04 +000016131 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016132 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016133
Linus Torvalds1da177e2005-04-16 15:20:36 -070016134 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016135 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16136 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16137 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16138 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016139 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16140 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16141 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016142
Joe Perches41535772013-02-16 11:20:04 +000016143 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16144 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016145 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016146 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016147 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016148
Joe Perches63c3a662011-04-26 08:12:10 +000016149 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016150 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016151 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16152 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016153 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016154 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16155 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16156 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16157 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016158 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16159 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016160 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016161 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016162 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016163 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016164 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016166
Joe Perches41535772013-02-16 11:20:04 +000016167 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16168 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016169 tp->phy_otp = tg3_read_otp_phycfg(tp);
16170 if (tp->phy_otp == 0)
16171 tp->phy_otp = TG3_OTP_DEFAULT;
16172 }
16173
Joe Perches63c3a662011-04-26 08:12:10 +000016174 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016175 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16176 else
16177 tp->mi_mode = MAC_MI_MODE_BASE;
16178
Linus Torvalds1da177e2005-04-16 15:20:36 -070016179 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016180 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16181 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016182 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16183
Matt Carlson4d958472011-04-20 07:57:35 +000016184 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016185 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16186 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16187 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016188 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16189 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16190 }
16191
Joe Perches41535772013-02-16 11:20:04 +000016192 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16193 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016194 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016195
Matt Carlson158d7ab2008-05-29 01:37:54 -070016196 err = tg3_mdio_init(tp);
16197 if (err)
16198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016199
16200 /* Initialize data/descriptor byte/word swapping. */
16201 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016202 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16203 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016204 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16205 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16206 GRC_MODE_B2HRX_ENABLE |
16207 GRC_MODE_HTX2B_ENABLE |
16208 GRC_MODE_HOST_STACKUP);
16209 else
16210 val &= GRC_MODE_HOST_STACKUP;
16211
Linus Torvalds1da177e2005-04-16 15:20:36 -070016212 tw32(GRC_MODE, val | tp->grc_mode);
16213
16214 tg3_switch_clocks(tp);
16215
16216 /* Clear this out for sanity. */
16217 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16218
16219 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16220 &pci_state_reg);
16221 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016222 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016223 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16224 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16225 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16226 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016227 void __iomem *sram_base;
16228
16229 /* Write some dummy words into the SRAM status block
16230 * area, see if it reads back correctly. If the return
16231 * value is bad, force enable the PCIX workaround.
16232 */
16233 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16234
16235 writel(0x00000000, sram_base);
16236 writel(0x00000000, sram_base + 4);
16237 writel(0xffffffff, sram_base + 4);
16238 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016239 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016240 }
16241 }
16242
16243 udelay(50);
16244 tg3_nvram_init(tp);
16245
Nithin Sujirc4dab502013-03-06 17:02:34 +000016246 /* If the device has an NVRAM, no need to load patch firmware */
16247 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16248 !tg3_flag(tp, NO_NVRAM))
16249 tp->fw_needed = NULL;
16250
Linus Torvalds1da177e2005-04-16 15:20:36 -070016251 grc_misc_cfg = tr32(GRC_MISC_CFG);
16252 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16253
Joe Perches41535772013-02-16 11:20:04 +000016254 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016255 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16256 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016257 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016258
Joe Perches63c3a662011-04-26 08:12:10 +000016259 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016260 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016261 tg3_flag_set(tp, TAGGED_STATUS);
16262 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016263 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16264 HOSTCC_MODE_CLRTICK_TXBD);
16265
16266 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16267 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16268 tp->misc_host_ctrl);
16269 }
16270
Matt Carlson3bda1252008-08-15 14:08:22 -070016271 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016272 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016273 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016274 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016275 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016276
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016277 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016278 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016279
16280 err = tg3_phy_probe(tp);
16281 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016282 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016283 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016284 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016285 }
16286
Matt Carlson184b8902010-04-05 10:19:25 +000016287 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016288 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016289
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016290 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16291 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016292 } else {
Joe Perches41535772013-02-16 11:20:04 +000016293 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016294 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016295 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016296 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016297 }
16298
16299 /* 5700 {AX,BX} chips have a broken status block link
16300 * change bit implementation, so we must use the
16301 * status register in those cases.
16302 */
Joe Perches41535772013-02-16 11:20:04 +000016303 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016304 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016305 else
Joe Perches63c3a662011-04-26 08:12:10 +000016306 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016307
16308 /* The led_ctrl is set during tg3_phy_probe, here we might
16309 * have to force the link status polling mechanism based
16310 * upon subsystem IDs.
16311 */
16312 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016313 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016314 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16315 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016316 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016317 }
16318
16319 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016320 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016321 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016322 else
Joe Perches63c3a662011-04-26 08:12:10 +000016323 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016324
Eric Dumazet9205fd92011-11-18 06:47:01 +000016325 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016326 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016327 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016328 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016329 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016330#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016331 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016332#endif
16333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016334
Matt Carlson2c49a442010-09-30 10:34:35 +000016335 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16336 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016337 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16338
Matt Carlson2c49a442010-09-30 10:34:35 +000016339 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016340
16341 /* Increment the rx prod index on the rx std ring by at most
16342 * 8 for these chips to workaround hw errata.
16343 */
Joe Perches41535772013-02-16 11:20:04 +000016344 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16345 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16346 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016347 tp->rx_std_max_post = 8;
16348
Joe Perches63c3a662011-04-26 08:12:10 +000016349 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016350 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16351 PCIE_PWR_MGMT_L1_THRESH_MSK;
16352
Linus Torvalds1da177e2005-04-16 15:20:36 -070016353 return err;
16354}
16355
David S. Miller49b6e95f2007-03-29 01:38:42 -070016356#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016357static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016358{
16359 struct net_device *dev = tp->dev;
16360 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016361 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016362 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016363 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016364
David S. Miller49b6e95f2007-03-29 01:38:42 -070016365 addr = of_get_property(dp, "local-mac-address", &len);
16366 if (addr && len == 6) {
16367 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016369 }
16370 return -ENODEV;
16371}
16372
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016373static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016374{
16375 struct net_device *dev = tp->dev;
16376
16377 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
16378 return 0;
16379}
16380#endif
16381
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016382static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016383{
16384 struct net_device *dev = tp->dev;
16385 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016386 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016387 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016388
David S. Miller49b6e95f2007-03-29 01:38:42 -070016389#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016390 if (!tg3_get_macaddr_sparc(tp))
16391 return 0;
16392#endif
16393
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016394 if (tg3_flag(tp, IS_SSB_CORE)) {
16395 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16396 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16397 return 0;
16398 }
16399
Linus Torvalds1da177e2005-04-16 15:20:36 -070016400 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016401 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016402 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016403 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16404 mac_offset = 0xcc;
16405 if (tg3_nvram_lock(tp))
16406 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16407 else
16408 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016409 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016410 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016411 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016412 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016413 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016414 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016415 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016416
16417 /* First try to get it from MAC address mailbox. */
16418 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16419 if ((hi >> 16) == 0x484b) {
16420 dev->dev_addr[0] = (hi >> 8) & 0xff;
16421 dev->dev_addr[1] = (hi >> 0) & 0xff;
16422
16423 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16424 dev->dev_addr[2] = (lo >> 24) & 0xff;
16425 dev->dev_addr[3] = (lo >> 16) & 0xff;
16426 dev->dev_addr[4] = (lo >> 8) & 0xff;
16427 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016428
Michael Chan008652b2006-03-27 23:14:53 -080016429 /* Some old bootcode may report a 0 MAC address in SRAM */
16430 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16431 }
16432 if (!addr_ok) {
16433 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016434 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016435 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016436 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016437 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16438 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016439 }
16440 /* Finally just fetch it out of the MAC control regs. */
16441 else {
16442 hi = tr32(MAC_ADDR_0_HIGH);
16443 lo = tr32(MAC_ADDR_0_LOW);
16444
16445 dev->dev_addr[5] = lo & 0xff;
16446 dev->dev_addr[4] = (lo >> 8) & 0xff;
16447 dev->dev_addr[3] = (lo >> 16) & 0xff;
16448 dev->dev_addr[2] = (lo >> 24) & 0xff;
16449 dev->dev_addr[1] = hi & 0xff;
16450 dev->dev_addr[0] = (hi >> 8) & 0xff;
16451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016452 }
16453
16454 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016455#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016456 if (!tg3_get_default_macaddr_sparc(tp))
16457 return 0;
16458#endif
16459 return -EINVAL;
16460 }
16461 return 0;
16462}
16463
David S. Miller59e6b432005-05-18 22:50:10 -070016464#define BOUNDARY_SINGLE_CACHELINE 1
16465#define BOUNDARY_MULTI_CACHELINE 2
16466
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016467static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016468{
16469 int cacheline_size;
16470 u8 byte;
16471 int goal;
16472
16473 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16474 if (byte == 0)
16475 cacheline_size = 1024;
16476 else
16477 cacheline_size = (int) byte * 4;
16478
16479 /* On 5703 and later chips, the boundary bits have no
16480 * effect.
16481 */
Joe Perches41535772013-02-16 11:20:04 +000016482 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16483 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016484 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016485 goto out;
16486
16487#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16488 goal = BOUNDARY_MULTI_CACHELINE;
16489#else
16490#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16491 goal = BOUNDARY_SINGLE_CACHELINE;
16492#else
16493 goal = 0;
16494#endif
16495#endif
16496
Joe Perches63c3a662011-04-26 08:12:10 +000016497 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016498 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16499 goto out;
16500 }
16501
David S. Miller59e6b432005-05-18 22:50:10 -070016502 if (!goal)
16503 goto out;
16504
16505 /* PCI controllers on most RISC systems tend to disconnect
16506 * when a device tries to burst across a cache-line boundary.
16507 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16508 *
16509 * Unfortunately, for PCI-E there are only limited
16510 * write-side controls for this, and thus for reads
16511 * we will still get the disconnects. We'll also waste
16512 * these PCI cycles for both read and write for chips
16513 * other than 5700 and 5701 which do not implement the
16514 * boundary bits.
16515 */
Joe Perches63c3a662011-04-26 08:12:10 +000016516 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016517 switch (cacheline_size) {
16518 case 16:
16519 case 32:
16520 case 64:
16521 case 128:
16522 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16523 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16524 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16525 } else {
16526 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16527 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16528 }
16529 break;
16530
16531 case 256:
16532 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16533 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16534 break;
16535
16536 default:
16537 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16538 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16539 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016540 }
Joe Perches63c3a662011-04-26 08:12:10 +000016541 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016542 switch (cacheline_size) {
16543 case 16:
16544 case 32:
16545 case 64:
16546 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16547 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16548 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16549 break;
16550 }
16551 /* fallthrough */
16552 case 128:
16553 default:
16554 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16555 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16556 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016557 }
David S. Miller59e6b432005-05-18 22:50:10 -070016558 } else {
16559 switch (cacheline_size) {
16560 case 16:
16561 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16562 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16563 DMA_RWCTRL_WRITE_BNDRY_16);
16564 break;
16565 }
16566 /* fallthrough */
16567 case 32:
16568 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16569 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16570 DMA_RWCTRL_WRITE_BNDRY_32);
16571 break;
16572 }
16573 /* fallthrough */
16574 case 64:
16575 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16576 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16577 DMA_RWCTRL_WRITE_BNDRY_64);
16578 break;
16579 }
16580 /* fallthrough */
16581 case 128:
16582 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16583 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16584 DMA_RWCTRL_WRITE_BNDRY_128);
16585 break;
16586 }
16587 /* fallthrough */
16588 case 256:
16589 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16590 DMA_RWCTRL_WRITE_BNDRY_256);
16591 break;
16592 case 512:
16593 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16594 DMA_RWCTRL_WRITE_BNDRY_512);
16595 break;
16596 case 1024:
16597 default:
16598 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16599 DMA_RWCTRL_WRITE_BNDRY_1024);
16600 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016601 }
David S. Miller59e6b432005-05-18 22:50:10 -070016602 }
16603
16604out:
16605 return val;
16606}
16607
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016608static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000016609 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016610{
16611 struct tg3_internal_buffer_desc test_desc;
16612 u32 sram_dma_descs;
16613 int i, ret;
16614
16615 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16616
16617 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16618 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16619 tw32(RDMAC_STATUS, 0);
16620 tw32(WDMAC_STATUS, 0);
16621
16622 tw32(BUFMGR_MODE, 0);
16623 tw32(FTQ_RESET, 0);
16624
16625 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16626 test_desc.addr_lo = buf_dma & 0xffffffff;
16627 test_desc.nic_mbuf = 0x00002100;
16628 test_desc.len = size;
16629
16630 /*
16631 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16632 * the *second* time the tg3 driver was getting loaded after an
16633 * initial scan.
16634 *
16635 * Broadcom tells me:
16636 * ...the DMA engine is connected to the GRC block and a DMA
16637 * reset may affect the GRC block in some unpredictable way...
16638 * The behavior of resets to individual blocks has not been tested.
16639 *
16640 * Broadcom noted the GRC reset will also reset all sub-components.
16641 */
16642 if (to_device) {
16643 test_desc.cqid_sqid = (13 << 8) | 2;
16644
16645 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16646 udelay(40);
16647 } else {
16648 test_desc.cqid_sqid = (16 << 8) | 7;
16649
16650 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16651 udelay(40);
16652 }
16653 test_desc.flags = 0x00000005;
16654
16655 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16656 u32 val;
16657
16658 val = *(((u32 *)&test_desc) + i);
16659 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16660 sram_dma_descs + (i * sizeof(u32)));
16661 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16662 }
16663 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16664
Matt Carlson859a588792010-04-05 10:19:28 +000016665 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016666 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016667 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016668 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016669
16670 ret = -ENODEV;
16671 for (i = 0; i < 40; i++) {
16672 u32 val;
16673
16674 if (to_device)
16675 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16676 else
16677 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16678 if ((val & 0xffff) == sram_dma_descs) {
16679 ret = 0;
16680 break;
16681 }
16682
16683 udelay(100);
16684 }
16685
16686 return ret;
16687}
16688
David S. Millerded73402005-05-23 13:59:47 -070016689#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016690
Matt Carlson41434702011-03-09 16:58:22 +000016691static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016692 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16693 { },
16694};
16695
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016696static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016697{
16698 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016699 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016700 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016701
Matt Carlson4bae65c2010-11-24 08:31:52 +000016702 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16703 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016704 if (!buf) {
16705 ret = -ENOMEM;
16706 goto out_nofree;
16707 }
16708
16709 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16710 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16711
David S. Miller59e6b432005-05-18 22:50:10 -070016712 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016713
Joe Perches63c3a662011-04-26 08:12:10 +000016714 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016715 goto out;
16716
Joe Perches63c3a662011-04-26 08:12:10 +000016717 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016718 /* DMA read watermark not used on PCIE */
16719 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016720 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016721 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16722 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016723 tp->dma_rwctrl |= 0x003f0000;
16724 else
16725 tp->dma_rwctrl |= 0x003f000f;
16726 } else {
Joe Perches41535772013-02-16 11:20:04 +000016727 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16728 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016729 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016730 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016731
Michael Chan4a29cc22006-03-19 13:21:12 -080016732 /* If the 5704 is behind the EPB bridge, we can
16733 * do the less restrictive ONE_DMA workaround for
16734 * better performance.
16735 */
Joe Perches63c3a662011-04-26 08:12:10 +000016736 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016737 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016738 tp->dma_rwctrl |= 0x8000;
16739 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016740 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16741
Joe Perches41535772013-02-16 11:20:04 +000016742 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016743 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016744 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016745 tp->dma_rwctrl |=
16746 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16747 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16748 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016749 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016750 /* 5780 always in PCIX mode */
16751 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016752 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016753 /* 5714 always in PCIX mode */
16754 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016755 } else {
16756 tp->dma_rwctrl |= 0x001b000f;
16757 }
16758 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016759 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16760 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016761
Joe Perches41535772013-02-16 11:20:04 +000016762 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16763 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016764 tp->dma_rwctrl &= 0xfffffff0;
16765
Joe Perches41535772013-02-16 11:20:04 +000016766 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16767 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016768 /* Remove this if it causes problems for some boards. */
16769 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16770
16771 /* On 5700/5701 chips, we need to set this bit.
16772 * Otherwise the chip will issue cacheline transactions
16773 * to streamable DMA memory with not all the byte
16774 * enables turned on. This is an error on several
16775 * RISC PCI controllers, in particular sparc64.
16776 *
16777 * On 5703/5704 chips, this bit has been reassigned
16778 * a different meaning. In particular, it is used
16779 * on those chips to enable a PCI-X workaround.
16780 */
16781 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16782 }
16783
16784 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16785
16786#if 0
16787 /* Unneeded, already done by tg3_get_invariants. */
16788 tg3_switch_clocks(tp);
16789#endif
16790
Joe Perches41535772013-02-16 11:20:04 +000016791 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16792 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016793 goto out;
16794
David S. Miller59e6b432005-05-18 22:50:10 -070016795 /* It is best to perform DMA test with maximum write burst size
16796 * to expose the 5700/5701 write DMA bug.
16797 */
16798 saved_dma_rwctrl = tp->dma_rwctrl;
16799 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16800 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16801
Linus Torvalds1da177e2005-04-16 15:20:36 -070016802 while (1) {
16803 u32 *p = buf, i;
16804
16805 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16806 p[i] = i;
16807
16808 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000016809 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016810 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016811 dev_err(&tp->pdev->dev,
16812 "%s: Buffer write failed. err = %d\n",
16813 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016814 break;
16815 }
16816
16817#if 0
16818 /* validate data reached card RAM correctly. */
16819 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16820 u32 val;
16821 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16822 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016823 dev_err(&tp->pdev->dev,
16824 "%s: Buffer corrupted on device! "
16825 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016826 /* ret = -ENODEV here? */
16827 }
16828 p[i] = 0;
16829 }
16830#endif
16831 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000016832 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016833 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016834 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16835 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016836 break;
16837 }
16838
16839 /* Verify it. */
16840 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16841 if (p[i] == i)
16842 continue;
16843
David S. Miller59e6b432005-05-18 22:50:10 -070016844 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16845 DMA_RWCTRL_WRITE_BNDRY_16) {
16846 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016847 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16848 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16849 break;
16850 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016851 dev_err(&tp->pdev->dev,
16852 "%s: Buffer corrupted on read back! "
16853 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016854 ret = -ENODEV;
16855 goto out;
16856 }
16857 }
16858
16859 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16860 /* Success. */
16861 ret = 0;
16862 break;
16863 }
16864 }
David S. Miller59e6b432005-05-18 22:50:10 -070016865 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16866 DMA_RWCTRL_WRITE_BNDRY_16) {
16867 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016868 * now look for chipsets that are known to expose the
16869 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016870 */
Matt Carlson41434702011-03-09 16:58:22 +000016871 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016872 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16873 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016874 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016875 /* Safe to use the calculated DMA boundary. */
16876 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016877 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016878
David S. Miller59e6b432005-05-18 22:50:10 -070016879 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016881
16882out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016883 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016884out_nofree:
16885 return ret;
16886}
16887
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016888static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016889{
Joe Perches63c3a662011-04-26 08:12:10 +000016890 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016891 tp->bufmgr_config.mbuf_read_dma_low_water =
16892 DEFAULT_MB_RDMA_LOW_WATER_5705;
16893 tp->bufmgr_config.mbuf_mac_rx_low_water =
16894 DEFAULT_MB_MACRX_LOW_WATER_57765;
16895 tp->bufmgr_config.mbuf_high_water =
16896 DEFAULT_MB_HIGH_WATER_57765;
16897
16898 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16899 DEFAULT_MB_RDMA_LOW_WATER_5705;
16900 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16901 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16902 tp->bufmgr_config.mbuf_high_water_jumbo =
16903 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016904 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016905 tp->bufmgr_config.mbuf_read_dma_low_water =
16906 DEFAULT_MB_RDMA_LOW_WATER_5705;
16907 tp->bufmgr_config.mbuf_mac_rx_low_water =
16908 DEFAULT_MB_MACRX_LOW_WATER_5705;
16909 tp->bufmgr_config.mbuf_high_water =
16910 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000016911 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016912 tp->bufmgr_config.mbuf_mac_rx_low_water =
16913 DEFAULT_MB_MACRX_LOW_WATER_5906;
16914 tp->bufmgr_config.mbuf_high_water =
16915 DEFAULT_MB_HIGH_WATER_5906;
16916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016917
Michael Chanfdfec1722005-07-25 12:31:48 -070016918 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16919 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16920 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16921 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16922 tp->bufmgr_config.mbuf_high_water_jumbo =
16923 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16924 } else {
16925 tp->bufmgr_config.mbuf_read_dma_low_water =
16926 DEFAULT_MB_RDMA_LOW_WATER;
16927 tp->bufmgr_config.mbuf_mac_rx_low_water =
16928 DEFAULT_MB_MACRX_LOW_WATER;
16929 tp->bufmgr_config.mbuf_high_water =
16930 DEFAULT_MB_HIGH_WATER;
16931
16932 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16933 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16934 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16935 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16936 tp->bufmgr_config.mbuf_high_water_jumbo =
16937 DEFAULT_MB_HIGH_WATER_JUMBO;
16938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016939
16940 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16941 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16942}
16943
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016944static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016945{
Matt Carlson79eb6902010-02-17 15:17:03 +000016946 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16947 case TG3_PHY_ID_BCM5400: return "5400";
16948 case TG3_PHY_ID_BCM5401: return "5401";
16949 case TG3_PHY_ID_BCM5411: return "5411";
16950 case TG3_PHY_ID_BCM5701: return "5701";
16951 case TG3_PHY_ID_BCM5703: return "5703";
16952 case TG3_PHY_ID_BCM5704: return "5704";
16953 case TG3_PHY_ID_BCM5705: return "5705";
16954 case TG3_PHY_ID_BCM5750: return "5750";
16955 case TG3_PHY_ID_BCM5752: return "5752";
16956 case TG3_PHY_ID_BCM5714: return "5714";
16957 case TG3_PHY_ID_BCM5780: return "5780";
16958 case TG3_PHY_ID_BCM5755: return "5755";
16959 case TG3_PHY_ID_BCM5787: return "5787";
16960 case TG3_PHY_ID_BCM5784: return "5784";
16961 case TG3_PHY_ID_BCM5756: return "5722/5756";
16962 case TG3_PHY_ID_BCM5906: return "5906";
16963 case TG3_PHY_ID_BCM5761: return "5761";
16964 case TG3_PHY_ID_BCM5718C: return "5718C";
16965 case TG3_PHY_ID_BCM5718S: return "5718S";
16966 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016967 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016968 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000016969 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016970 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016971 case 0: return "serdes";
16972 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016974}
16975
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016976static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016977{
Joe Perches63c3a662011-04-26 08:12:10 +000016978 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016979 strcpy(str, "PCI Express");
16980 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016981 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016982 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16983
16984 strcpy(str, "PCIX:");
16985
16986 if ((clock_ctrl == 7) ||
16987 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16988 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16989 strcat(str, "133MHz");
16990 else if (clock_ctrl == 0)
16991 strcat(str, "33MHz");
16992 else if (clock_ctrl == 2)
16993 strcat(str, "50MHz");
16994 else if (clock_ctrl == 4)
16995 strcat(str, "66MHz");
16996 else if (clock_ctrl == 6)
16997 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016998 } else {
16999 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017000 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017001 strcat(str, "66MHz");
17002 else
17003 strcat(str, "33MHz");
17004 }
Joe Perches63c3a662011-04-26 08:12:10 +000017005 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017006 strcat(str, ":32-bit");
17007 else
17008 strcat(str, ":64-bit");
17009 return str;
17010}
17011
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017012static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017013{
17014 struct ethtool_coalesce *ec = &tp->coal;
17015
17016 memset(ec, 0, sizeof(*ec));
17017 ec->cmd = ETHTOOL_GCOALESCE;
17018 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17019 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17020 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17021 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17022 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17023 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17024 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17025 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17026 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17027
17028 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17029 HOSTCC_MODE_CLRTICK_TXBD)) {
17030 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17031 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17032 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17033 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17034 }
Michael Chand244c892005-07-05 14:42:33 -070017035
Joe Perches63c3a662011-04-26 08:12:10 +000017036 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017037 ec->rx_coalesce_usecs_irq = 0;
17038 ec->tx_coalesce_usecs_irq = 0;
17039 ec->stats_block_coalesce_usecs = 0;
17040 }
David S. Miller15f98502005-05-18 22:49:26 -070017041}
17042
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017043static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017044 const struct pci_device_id *ent)
17045{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017046 struct net_device *dev;
17047 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017048 int i, err, pm_cap;
17049 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017050 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017051 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017052 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017053
Joe Perches05dbe002010-02-17 19:44:19 +000017054 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017055
17056 err = pci_enable_device(pdev);
17057 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017058 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017059 return err;
17060 }
17061
Linus Torvalds1da177e2005-04-16 15:20:36 -070017062 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17063 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017064 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017065 goto err_out_disable_pdev;
17066 }
17067
17068 pci_set_master(pdev);
17069
17070 /* Find power-management capability. */
17071 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
17072 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000017073 dev_err(&pdev->dev,
17074 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017075 err = -EIO;
17076 goto err_out_free_res;
17077 }
17078
Matt Carlson16821282011-07-13 09:27:28 +000017079 err = pci_set_power_state(pdev, PCI_D0);
17080 if (err) {
17081 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
17082 goto err_out_free_res;
17083 }
17084
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017085 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017086 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017087 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000017088 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017089 }
17090
Linus Torvalds1da177e2005-04-16 15:20:36 -070017091 SET_NETDEV_DEV(dev, &pdev->dev);
17092
Linus Torvalds1da177e2005-04-16 15:20:36 -070017093 tp = netdev_priv(dev);
17094 tp->pdev = pdev;
17095 tp->dev = dev;
17096 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017097 tp->rx_mode = TG3_DEF_RX_MODE;
17098 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017099 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017100
Linus Torvalds1da177e2005-04-16 15:20:36 -070017101 if (tg3_debug > 0)
17102 tp->msg_enable = tg3_debug;
17103 else
17104 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17105
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017106 if (pdev_is_ssb_gige_core(pdev)) {
17107 tg3_flag_set(tp, IS_SSB_CORE);
17108 if (ssb_gige_must_flush_posted_writes(pdev))
17109 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17110 if (ssb_gige_one_dma_at_once(pdev))
17111 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
17112 if (ssb_gige_have_roboswitch(pdev))
17113 tg3_flag_set(tp, ROBOSWITCH);
17114 if (ssb_gige_is_rgmii(pdev))
17115 tg3_flag_set(tp, RGMII_MODE);
17116 }
17117
Linus Torvalds1da177e2005-04-16 15:20:36 -070017118 /* The word/byte swap controls here control register access byte
17119 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17120 * setting below.
17121 */
17122 tp->misc_host_ctrl =
17123 MISC_HOST_CTRL_MASK_PCI_INT |
17124 MISC_HOST_CTRL_WORD_SWAP |
17125 MISC_HOST_CTRL_INDIR_ACCESS |
17126 MISC_HOST_CTRL_PCISTATE_RW;
17127
17128 /* The NONFRM (non-frame) byte/word swap controls take effect
17129 * on descriptor entries, anything which isn't packet data.
17130 *
17131 * The StrongARM chips on the board (one for tx, one for rx)
17132 * are running in big-endian mode.
17133 */
17134 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17135 GRC_MODE_WSWAP_NONFRM_DATA);
17136#ifdef __BIG_ENDIAN
17137 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17138#endif
17139 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017140 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017141 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017142
Matt Carlsond5fe4882008-11-21 17:20:32 -080017143 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017144 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017145 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017146 err = -ENOMEM;
17147 goto err_out_free_dev;
17148 }
17149
Matt Carlsonc9cab242011-07-13 09:27:27 +000017150 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17151 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17152 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17153 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17154 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017155 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017156 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17157 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017158 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
17159 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17160 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
17161 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017162 tg3_flag_set(tp, ENABLE_APE);
17163 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17164 if (!tp->aperegs) {
17165 dev_err(&pdev->dev,
17166 "Cannot map APE registers, aborting\n");
17167 err = -ENOMEM;
17168 goto err_out_iounmap;
17169 }
17170 }
17171
Linus Torvalds1da177e2005-04-16 15:20:36 -070017172 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17173 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017174
Linus Torvalds1da177e2005-04-16 15:20:36 -070017175 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017176 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017177 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017178 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017179
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017180 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017181 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017182 dev_err(&pdev->dev,
17183 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017184 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017185 }
17186
Michael Chan4a29cc22006-03-19 13:21:12 -080017187 /* The EPB bridge inside 5714, 5715, and 5780 and any
17188 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017189 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17190 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17191 * do DMA address check in tg3_start_xmit().
17192 */
Joe Perches63c3a662011-04-26 08:12:10 +000017193 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017194 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017195 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017196 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017197#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017198 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017199#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017200 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017201 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017202
17203 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017204 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017205 err = pci_set_dma_mask(pdev, dma_mask);
17206 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017207 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017208 err = pci_set_consistent_dma_mask(pdev,
17209 persist_dma_mask);
17210 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017211 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17212 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017213 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017214 }
17215 }
17216 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017217 if (err || dma_mask == DMA_BIT_MASK(32)) {
17218 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017219 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017220 dev_err(&pdev->dev,
17221 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017222 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017223 }
17224 }
17225
Michael Chanfdfec1722005-07-25 12:31:48 -070017226 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017227
Patrick McHardyf6469682013-04-19 02:04:27 +000017228 features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlson0da06062011-05-19 12:12:53 +000017229
17230 /* 5700 B0 chips do not support checksumming correctly due
17231 * to hardware bugs.
17232 */
Joe Perches41535772013-02-16 11:20:04 +000017233 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017234 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17235
17236 if (tg3_flag(tp, 5755_PLUS))
17237 features |= NETIF_F_IPV6_CSUM;
17238 }
17239
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017240 /* TSO is on by default on chips that support hardware TSO.
17241 * Firmware TSO on older chips gives lower performance, so it
17242 * is off by default, but can be enabled using ethtool.
17243 */
Joe Perches63c3a662011-04-26 08:12:10 +000017244 if ((tg3_flag(tp, HW_TSO_1) ||
17245 tg3_flag(tp, HW_TSO_2) ||
17246 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017247 (features & NETIF_F_IP_CSUM))
17248 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017249 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017250 if (features & NETIF_F_IPV6_CSUM)
17251 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017252 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017253 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17254 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17255 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17256 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17257 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017258 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017260
Matt Carlsond542fe22011-05-19 16:02:43 +000017261 dev->features |= features;
17262 dev->vlan_features |= features;
17263
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017264 /*
17265 * Add loopback capability only for a subset of devices that support
17266 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17267 * loopback for the remaining devices.
17268 */
Joe Perches41535772013-02-16 11:20:04 +000017269 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017270 !tg3_flag(tp, CPMU_PRESENT))
17271 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017272 features |= NETIF_F_LOOPBACK;
17273
Matt Carlson0da06062011-05-19 12:12:53 +000017274 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017275
Joe Perches41535772013-02-16 11:20:04 +000017276 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017277 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017278 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017279 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017280 tp->rx_pending = 63;
17281 }
17282
Linus Torvalds1da177e2005-04-16 15:20:36 -070017283 err = tg3_get_device_address(tp);
17284 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017285 dev_err(&pdev->dev,
17286 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017287 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017288 }
17289
Matt Carlsonc88864d2007-11-12 21:07:01 -080017290 /*
17291 * Reset chip in case UNDI or EFI driver did not shutdown
17292 * DMA self test will enable WDMAC and we'll see (spurious)
17293 * pending DMA on the PCI bus at that point.
17294 */
17295 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17296 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17297 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17298 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17299 }
17300
17301 err = tg3_test_dma(tp);
17302 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017303 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017304 goto err_out_apeunmap;
17305 }
17306
Matt Carlson78f90dc2009-11-13 13:03:42 +000017307 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17308 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17309 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017310 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017311 struct tg3_napi *tnapi = &tp->napi[i];
17312
17313 tnapi->tp = tp;
17314 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17315
17316 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017317 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017318 intmbx += 0x8;
17319 else
17320 intmbx += 0x4;
17321
17322 tnapi->consmbox = rcvmbx;
17323 tnapi->prodmbox = sndmbx;
17324
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017325 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017326 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017327 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017328 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017329
Joe Perches63c3a662011-04-26 08:12:10 +000017330 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017331 break;
17332
17333 /*
17334 * If we support MSIX, we'll be using RSS. If we're using
17335 * RSS, the first vector only handles link interrupts and the
17336 * remaining vectors handle rx and tx interrupts. Reuse the
17337 * mailbox values for the next iteration. The values we setup
17338 * above are still useful for the single vectored mode.
17339 */
17340 if (!i)
17341 continue;
17342
17343 rcvmbx += 0x8;
17344
17345 if (sndmbx & 0x4)
17346 sndmbx -= 0x4;
17347 else
17348 sndmbx += 0xc;
17349 }
17350
Matt Carlsonc88864d2007-11-12 21:07:01 -080017351 tg3_init_coal(tp);
17352
Michael Chanc49a1562006-12-17 17:07:29 -080017353 pci_set_drvdata(pdev, dev);
17354
Joe Perches41535772013-02-16 11:20:04 +000017355 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17356 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17357 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017358 tg3_flag_set(tp, PTP_CAPABLE);
17359
Matt Carlsoncd0d7222011-07-13 09:27:33 +000017360 if (tg3_flag(tp, 5717_PLUS)) {
17361 /* Resume a low-power mode */
17362 tg3_frob_aux_power(tp, false);
17363 }
17364
Matt Carlson21f76382012-02-22 12:35:21 +000017365 tg3_timer_init(tp);
17366
Michael Chan402e1392013-02-14 12:13:41 +000017367 tg3_carrier_off(tp);
17368
Linus Torvalds1da177e2005-04-16 15:20:36 -070017369 err = register_netdev(dev);
17370 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017371 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017372 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017373 }
17374
Joe Perches05dbe002010-02-17 19:44:19 +000017375 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17376 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017377 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017378 tg3_bus_string(tp, str),
17379 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017380
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017381 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017382 struct phy_device *phydev;
17383 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017384 netdev_info(dev,
17385 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017386 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017387 } else {
17388 char *ethtype;
17389
17390 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17391 ethtype = "10/100Base-TX";
17392 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17393 ethtype = "1000Base-SX";
17394 else
17395 ethtype = "10/100/1000Base-T";
17396
Matt Carlson5129c3a2010-04-05 10:19:23 +000017397 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017398 "(WireSpeed[%d], EEE[%d])\n",
17399 tg3_phy_string(tp), ethtype,
17400 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17401 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017402 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017403
Joe Perches05dbe002010-02-17 19:44:19 +000017404 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017405 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017406 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017407 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017408 tg3_flag(tp, ENABLE_ASF) != 0,
17409 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017410 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17411 tp->dma_rwctrl,
17412 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17413 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017414
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017415 pci_save_state(pdev);
17416
Linus Torvalds1da177e2005-04-16 15:20:36 -070017417 return 0;
17418
Matt Carlson0d3031d2007-10-10 18:02:43 -070017419err_out_apeunmap:
17420 if (tp->aperegs) {
17421 iounmap(tp->aperegs);
17422 tp->aperegs = NULL;
17423 }
17424
Linus Torvalds1da177e2005-04-16 15:20:36 -070017425err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017426 if (tp->regs) {
17427 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017428 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017430
17431err_out_free_dev:
17432 free_netdev(dev);
17433
Matt Carlson16821282011-07-13 09:27:28 +000017434err_out_power_down:
17435 pci_set_power_state(pdev, PCI_D3hot);
17436
Linus Torvalds1da177e2005-04-16 15:20:36 -070017437err_out_free_res:
17438 pci_release_regions(pdev);
17439
17440err_out_disable_pdev:
17441 pci_disable_device(pdev);
17442 pci_set_drvdata(pdev, NULL);
17443 return err;
17444}
17445
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017446static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017447{
17448 struct net_device *dev = pci_get_drvdata(pdev);
17449
17450 if (dev) {
17451 struct tg3 *tp = netdev_priv(dev);
17452
Jesper Juhle3c55302012-04-09 22:50:15 +020017453 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017454
Matt Carlsondb219972011-11-04 09:15:03 +000017455 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017456
David S. Miller1805b2f2011-10-24 18:18:09 -040017457 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017458 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017459 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017460 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017461
Linus Torvalds1da177e2005-04-16 15:20:36 -070017462 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017463 if (tp->aperegs) {
17464 iounmap(tp->aperegs);
17465 tp->aperegs = NULL;
17466 }
Michael Chan68929142005-08-09 20:17:14 -070017467 if (tp->regs) {
17468 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017469 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017471 free_netdev(dev);
17472 pci_release_regions(pdev);
17473 pci_disable_device(pdev);
17474 pci_set_drvdata(pdev, NULL);
17475 }
17476}
17477
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017478#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017479static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017480{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017481 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017482 struct net_device *dev = pci_get_drvdata(pdev);
17483 struct tg3 *tp = netdev_priv(dev);
17484 int err;
17485
17486 if (!netif_running(dev))
17487 return 0;
17488
Matt Carlsondb219972011-11-04 09:15:03 +000017489 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017490 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017491 tg3_netif_stop(tp);
17492
Matt Carlson21f76382012-02-22 12:35:21 +000017493 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017494
David S. Millerf47c11e2005-06-24 20:18:35 -070017495 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017496 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017497 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017498
17499 netif_device_detach(dev);
17500
David S. Millerf47c11e2005-06-24 20:18:35 -070017501 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017502 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017503 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017504 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017505
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017506 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017507 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017508 int err2;
17509
David S. Millerf47c11e2005-06-24 20:18:35 -070017510 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017511
Joe Perches63c3a662011-04-26 08:12:10 +000017512 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017513 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017514 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017515 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017516
Matt Carlson21f76382012-02-22 12:35:21 +000017517 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017518
17519 netif_device_attach(dev);
17520 tg3_netif_start(tp);
17521
Michael Chanb9ec6c12006-07-25 16:37:27 -070017522out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017523 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017524
17525 if (!err2)
17526 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017527 }
17528
17529 return err;
17530}
17531
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017532static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017533{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017534 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017535 struct net_device *dev = pci_get_drvdata(pdev);
17536 struct tg3 *tp = netdev_priv(dev);
17537 int err;
17538
17539 if (!netif_running(dev))
17540 return 0;
17541
Linus Torvalds1da177e2005-04-16 15:20:36 -070017542 netif_device_attach(dev);
17543
David S. Millerf47c11e2005-06-24 20:18:35 -070017544 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017545
Joe Perches63c3a662011-04-26 08:12:10 +000017546 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017547 err = tg3_restart_hw(tp,
17548 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017549 if (err)
17550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017551
Matt Carlson21f76382012-02-22 12:35:21 +000017552 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017553
Linus Torvalds1da177e2005-04-16 15:20:36 -070017554 tg3_netif_start(tp);
17555
Michael Chanb9ec6c12006-07-25 16:37:27 -070017556out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017557 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017558
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017559 if (!err)
17560 tg3_phy_start(tp);
17561
Michael Chanb9ec6c12006-07-25 16:37:27 -070017562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017563}
Fabio Estevam42df36a2013-04-16 09:28:29 +000017564#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017565
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017566static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
17567
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017568/**
17569 * tg3_io_error_detected - called when PCI error is detected
17570 * @pdev: Pointer to PCI device
17571 * @state: The current pci connection state
17572 *
17573 * This function is called after a PCI bus error affecting
17574 * this device has been detected.
17575 */
17576static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17577 pci_channel_state_t state)
17578{
17579 struct net_device *netdev = pci_get_drvdata(pdev);
17580 struct tg3 *tp = netdev_priv(netdev);
17581 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17582
17583 netdev_info(netdev, "PCI I/O error detected\n");
17584
17585 rtnl_lock();
17586
17587 if (!netif_running(netdev))
17588 goto done;
17589
17590 tg3_phy_stop(tp);
17591
17592 tg3_netif_stop(tp);
17593
Matt Carlson21f76382012-02-22 12:35:21 +000017594 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017595
17596 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017597 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017598
17599 netif_device_detach(netdev);
17600
17601 /* Clean up software state, even if MMIO is blocked */
17602 tg3_full_lock(tp, 0);
17603 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17604 tg3_full_unlock(tp);
17605
17606done:
17607 if (state == pci_channel_io_perm_failure)
17608 err = PCI_ERS_RESULT_DISCONNECT;
17609 else
17610 pci_disable_device(pdev);
17611
17612 rtnl_unlock();
17613
17614 return err;
17615}
17616
17617/**
17618 * tg3_io_slot_reset - called after the pci bus has been reset.
17619 * @pdev: Pointer to PCI device
17620 *
17621 * Restart the card from scratch, as if from a cold-boot.
17622 * At this point, the card has exprienced a hard reset,
17623 * followed by fixups by BIOS, and has its config space
17624 * set up identically to what it was at cold boot.
17625 */
17626static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17627{
17628 struct net_device *netdev = pci_get_drvdata(pdev);
17629 struct tg3 *tp = netdev_priv(netdev);
17630 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17631 int err;
17632
17633 rtnl_lock();
17634
17635 if (pci_enable_device(pdev)) {
17636 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17637 goto done;
17638 }
17639
17640 pci_set_master(pdev);
17641 pci_restore_state(pdev);
17642 pci_save_state(pdev);
17643
17644 if (!netif_running(netdev)) {
17645 rc = PCI_ERS_RESULT_RECOVERED;
17646 goto done;
17647 }
17648
17649 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017650 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017651 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017652
17653 rc = PCI_ERS_RESULT_RECOVERED;
17654
17655done:
17656 rtnl_unlock();
17657
17658 return rc;
17659}
17660
17661/**
17662 * tg3_io_resume - called when traffic can start flowing again.
17663 * @pdev: Pointer to PCI device
17664 *
17665 * This callback is called when the error recovery driver tells
17666 * us that its OK to resume normal operation.
17667 */
17668static void tg3_io_resume(struct pci_dev *pdev)
17669{
17670 struct net_device *netdev = pci_get_drvdata(pdev);
17671 struct tg3 *tp = netdev_priv(netdev);
17672 int err;
17673
17674 rtnl_lock();
17675
17676 if (!netif_running(netdev))
17677 goto done;
17678
17679 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000017680 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017681 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017682 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017683 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017684 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17685 goto done;
17686 }
17687
17688 netif_device_attach(netdev);
17689
Matt Carlson21f76382012-02-22 12:35:21 +000017690 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017691
17692 tg3_netif_start(tp);
17693
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017694 tg3_full_unlock(tp);
17695
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017696 tg3_phy_start(tp);
17697
17698done:
17699 rtnl_unlock();
17700}
17701
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017702static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017703 .error_detected = tg3_io_error_detected,
17704 .slot_reset = tg3_io_slot_reset,
17705 .resume = tg3_io_resume
17706};
17707
Linus Torvalds1da177e2005-04-16 15:20:36 -070017708static struct pci_driver tg3_driver = {
17709 .name = DRV_MODULE_NAME,
17710 .id_table = tg3_pci_tbl,
17711 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017712 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017713 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000017714 .driver.pm = &tg3_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017715};
17716
17717static int __init tg3_init(void)
17718{
Jeff Garzik29917622006-08-19 17:48:59 -040017719 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017720}
17721
17722static void __exit tg3_cleanup(void)
17723{
17724 pci_unregister_driver(&tg3_driver);
17725}
17726
17727module_init(tg3_init);
17728module_exit(tg3_cleanup);