blob: a9e068423ba0651414cc0c5b23f0eda6937a6c23 [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 Sujire4cb29f2013-09-20 16:47:02 -070097#define TG3_MIN_NUM 134
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Sujire4cb29f2013-09-20 16:47:02 -0700100#define DRV_MODULE_RELDATE "Sep 16, 2013"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000102#define RESET_KIND_SHUTDOWN 0
103#define RESET_KIND_INIT 1
104#define RESET_KIND_SUSPEND 2
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_MODE 0
107#define TG3_DEF_TX_MODE 0
108#define TG3_DEF_MSG_ENABLE \
109 (NETIF_MSG_DRV | \
110 NETIF_MSG_PROBE | \
111 NETIF_MSG_LINK | \
112 NETIF_MSG_TIMER | \
113 NETIF_MSG_IFDOWN | \
114 NETIF_MSG_IFUP | \
115 NETIF_MSG_RX_ERR | \
116 NETIF_MSG_TX_ERR)
117
Matt Carlson520b2752011-06-13 13:39:02 +0000118#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* length of time before we decide the hardware is borked,
121 * and dev->tx_timeout() should be called to fix the problem
122 */
Joe Perches63c3a662011-04-26 08:12:10 +0000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_TX_TIMEOUT (5 * HZ)
125
126/* hardware minimum and maximum for a single frame's data payload */
127#define TG3_MIN_MTU 60
128#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* These numbers seem to be hard coded in the NIC firmware somehow.
132 * You can't change the ring sizes, but you can change where you place
133 * them in the NIC onboard memory.
134 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000135#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000137 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000139#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000140 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000141 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define TG3_DEF_RX_JUMBO_RING_PENDING 100
143
144/* Do not place this n-ring entries value into the tp struct itself,
145 * we really want to expose these constants to GCC so that modulo et
146 * al. operations are done with shifts and masks instead of with
147 * hw multiply/modulo instructions. Another solution would be to
148 * replace things like '% foo' with '& (foo - 1)'.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define TG3_TX_RING_SIZE 512
152#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
153
Matt Carlson2c49a442010-09-30 10:34:35 +0000154#define TG3_RX_STD_RING_BYTES(tp) \
155 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
156#define TG3_RX_JMB_RING_BYTES(tp) \
157 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
158#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000159 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
161 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
163
Matt Carlson287be122009-08-28 13:58:46 +0000164#define TG3_DMA_BYTE_ENAB 64
165
166#define TG3_RX_STD_DMA_SZ 1536
167#define TG3_RX_JMB_DMA_SZ 9046
168
169#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
170
171#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
172#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Matt Carlson2c49a442010-09-30 10:34:35 +0000174#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
175 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000176
Matt Carlson2c49a442010-09-30 10:34:35 +0000177#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
178 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000179
Matt Carlsond2757fc2010-04-12 06:58:27 +0000180/* Due to a hardware bug, the 5701 can only DMA to memory addresses
181 * that are at least dword aligned when used in PCIX mode. The driver
182 * works around this bug by double copying the packet. This workaround
183 * is built into the normal double copy length check for efficiency.
184 *
185 * However, the double copy is only necessary on those architectures
186 * where unaligned memory accesses are inefficient. For those architectures
187 * where unaligned memory accesses incur little penalty, we can reintegrate
188 * the 5701 in the normal rx path. Doing so saves a device structure
189 * dereference by hardcoding the double copy threshold in place.
190 */
191#define TG3_RX_COPY_THRESHOLD 256
192#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
193 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
194#else
195 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
196#endif
197
Matt Carlson81389f52011-08-31 11:44:49 +0000198#if (NET_IP_ALIGN != 0)
199#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
200#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000201#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000202#endif
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000205#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000206#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000207#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Matt Carlsonad829262008-11-21 17:16:16 -0800209#define TG3_RAW_IP_ALIGN 2
210
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000211#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000212#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000213
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800214#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000215#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800216#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
217#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
218
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500219static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000220 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
223MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
224MODULE_LICENSE("GPL");
225MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800226MODULE_FIRMWARE(FIRMWARE_TG3);
227MODULE_FIRMWARE(FIRMWARE_TG3TSO);
228MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
231module_param(tg3_debug, int, 0);
232MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
233
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000234#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
235#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
236
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000237static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
257 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
258 TG3_DRV_DATA_FLAG_5705_10_100},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
260 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
261 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
264 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
265 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
272 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
278 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000286 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
287 PCI_VENDOR_ID_LENOVO,
288 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
289 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
292 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000311 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
312 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
313 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
314 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
315 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
316 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800317 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
320 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
330 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
332 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Nithin Sujir68273712013-09-20 16:46:56 -0700340 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
341 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
342 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
343 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
344 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700345 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
346 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
347 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
348 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
349 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
350 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
351 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000352 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700353 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
356MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
357
Andreas Mohr50da8592006-08-14 23:54:30 -0700358static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000360} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 { "rx_octets" },
362 { "rx_fragments" },
363 { "rx_ucast_packets" },
364 { "rx_mcast_packets" },
365 { "rx_bcast_packets" },
366 { "rx_fcs_errors" },
367 { "rx_align_errors" },
368 { "rx_xon_pause_rcvd" },
369 { "rx_xoff_pause_rcvd" },
370 { "rx_mac_ctrl_rcvd" },
371 { "rx_xoff_entered" },
372 { "rx_frame_too_long_errors" },
373 { "rx_jabbers" },
374 { "rx_undersize_packets" },
375 { "rx_in_length_errors" },
376 { "rx_out_length_errors" },
377 { "rx_64_or_less_octet_packets" },
378 { "rx_65_to_127_octet_packets" },
379 { "rx_128_to_255_octet_packets" },
380 { "rx_256_to_511_octet_packets" },
381 { "rx_512_to_1023_octet_packets" },
382 { "rx_1024_to_1522_octet_packets" },
383 { "rx_1523_to_2047_octet_packets" },
384 { "rx_2048_to_4095_octet_packets" },
385 { "rx_4096_to_8191_octet_packets" },
386 { "rx_8192_to_9022_octet_packets" },
387
388 { "tx_octets" },
389 { "tx_collisions" },
390
391 { "tx_xon_sent" },
392 { "tx_xoff_sent" },
393 { "tx_flow_control" },
394 { "tx_mac_errors" },
395 { "tx_single_collisions" },
396 { "tx_mult_collisions" },
397 { "tx_deferred" },
398 { "tx_excessive_collisions" },
399 { "tx_late_collisions" },
400 { "tx_collide_2times" },
401 { "tx_collide_3times" },
402 { "tx_collide_4times" },
403 { "tx_collide_5times" },
404 { "tx_collide_6times" },
405 { "tx_collide_7times" },
406 { "tx_collide_8times" },
407 { "tx_collide_9times" },
408 { "tx_collide_10times" },
409 { "tx_collide_11times" },
410 { "tx_collide_12times" },
411 { "tx_collide_13times" },
412 { "tx_collide_14times" },
413 { "tx_collide_15times" },
414 { "tx_ucast_packets" },
415 { "tx_mcast_packets" },
416 { "tx_bcast_packets" },
417 { "tx_carrier_sense_errors" },
418 { "tx_discards" },
419 { "tx_errors" },
420
421 { "dma_writeq_full" },
422 { "dma_write_prioq_full" },
423 { "rxbds_empty" },
424 { "rx_discards" },
425 { "rx_errors" },
426 { "rx_threshold_hit" },
427
428 { "dma_readq_full" },
429 { "dma_read_prioq_full" },
430 { "tx_comp_queue_full" },
431
432 { "ring_set_send_prod_index" },
433 { "ring_status_update" },
434 { "nic_irqs" },
435 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000436 { "nic_tx_threshold_hit" },
437
438 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
440
Matt Carlson48fa55a2011-04-13 11:05:06 +0000441#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000442#define TG3_NVRAM_TEST 0
443#define TG3_LINK_TEST 1
444#define TG3_REGISTER_TEST 2
445#define TG3_MEMORY_TEST 3
446#define TG3_MAC_LOOPB_TEST 4
447#define TG3_PHY_LOOPB_TEST 5
448#define TG3_EXT_LOOPB_TEST 6
449#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000450
451
Andreas Mohr50da8592006-08-14 23:54:30 -0700452static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700453 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000454} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000455 [TG3_NVRAM_TEST] = { "nvram test (online) " },
456 [TG3_LINK_TEST] = { "link test (online) " },
457 [TG3_REGISTER_TEST] = { "register test (offline)" },
458 [TG3_MEMORY_TEST] = { "memory test (offline)" },
459 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
460 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
461 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
462 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700463};
464
Matt Carlson48fa55a2011-04-13 11:05:06 +0000465#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
466
467
Michael Chanb401e9e2005-12-19 16:27:04 -0800468static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
469{
470 writel(val, tp->regs + off);
471}
472
473static u32 tg3_read32(struct tg3 *tp, u32 off)
474{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000475 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800476}
477
Matt Carlson0d3031d2007-10-10 18:02:43 -0700478static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
479{
480 writel(val, tp->aperegs + off);
481}
482
483static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
484{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000485 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
489{
Michael Chan68929142005-08-09 20:17:14 -0700490 unsigned long flags;
491
492 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700493 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
494 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700495 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700496}
497
498static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
499{
500 writel(val, tp->regs + off);
501 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Michael Chan68929142005-08-09 20:17:14 -0700504static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
505{
506 unsigned long flags;
507 u32 val;
508
509 spin_lock_irqsave(&tp->indirect_lock, flags);
510 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
511 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
512 spin_unlock_irqrestore(&tp->indirect_lock, flags);
513 return val;
514}
515
516static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
517{
518 unsigned long flags;
519
520 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
521 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
522 TG3_64BIT_REG_LOW, val);
523 return;
524 }
Matt Carlson66711e62009-11-13 13:03:49 +0000525 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700526 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
527 TG3_64BIT_REG_LOW, val);
528 return;
529 }
530
531 spin_lock_irqsave(&tp->indirect_lock, flags);
532 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
533 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
534 spin_unlock_irqrestore(&tp->indirect_lock, flags);
535
536 /* In indirect mode when disabling interrupts, we also need
537 * to clear the interrupt bit in the GRC local ctrl register.
538 */
539 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
540 (val == 0x1)) {
541 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
542 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
543 }
544}
545
546static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
547{
548 unsigned long flags;
549 u32 val;
550
551 spin_lock_irqsave(&tp->indirect_lock, flags);
552 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
553 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
554 spin_unlock_irqrestore(&tp->indirect_lock, flags);
555 return val;
556}
557
Michael Chanb401e9e2005-12-19 16:27:04 -0800558/* usec_wait specifies the wait time in usec when writing to certain registers
559 * where it is unsafe to read back the register without some delay.
560 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
561 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
562 */
563static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Joe Perches63c3a662011-04-26 08:12:10 +0000565 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800566 /* Non-posted methods */
567 tp->write32(tp, off, val);
568 else {
569 /* Posted method */
570 tg3_write32(tp, off, val);
571 if (usec_wait)
572 udelay(usec_wait);
573 tp->read32(tp, off);
574 }
575 /* Wait again after the read for the posted method to guarantee that
576 * the wait time is met.
577 */
578 if (usec_wait)
579 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Michael Chan09ee9292005-08-09 20:17:00 -0700582static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
583{
584 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000585 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
586 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
587 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700588 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700589}
590
Michael Chan20094932005-08-09 20:16:32 -0700591static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 void __iomem *mbox = tp->regs + off;
594 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000595 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000597 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
598 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 readl(mbox);
600}
601
Michael Chanb5d37722006-09-27 16:06:21 -0700602static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
603{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000604 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700605}
606
607static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
608{
609 writel(val, tp->regs + off + GRCMBOX_BASE);
610}
611
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000612#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700613#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000614#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
615#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
616#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700617
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000618#define tw32(reg, val) tp->write32(tp, reg, val)
619#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
620#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
621#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
624{
Michael Chan68929142005-08-09 20:17:14 -0700625 unsigned long flags;
626
Joe Perches41535772013-02-16 11:20:04 +0000627 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700628 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
629 return;
630
Michael Chan68929142005-08-09 20:17:14 -0700631 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000632 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700633 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
634 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Michael Chanbbadf502006-04-06 21:46:34 -0700636 /* Always leave this as zero. */
637 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
638 } else {
639 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
640 tw32_f(TG3PCI_MEM_WIN_DATA, val);
641
642 /* Always leave this as zero. */
643 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
644 }
Michael Chan68929142005-08-09 20:17:14 -0700645 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
649{
Michael Chan68929142005-08-09 20:17:14 -0700650 unsigned long flags;
651
Joe Perches41535772013-02-16 11:20:04 +0000652 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700653 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
654 *val = 0;
655 return;
656 }
657
Michael Chan68929142005-08-09 20:17:14 -0700658 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000659 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700660 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
661 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Michael Chanbbadf502006-04-06 21:46:34 -0700663 /* Always leave this as zero. */
664 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
665 } else {
666 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
667 *val = tr32(TG3PCI_MEM_WIN_DATA);
668
669 /* Always leave this as zero. */
670 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
671 }
Michael Chan68929142005-08-09 20:17:14 -0700672 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Matt Carlson0d3031d2007-10-10 18:02:43 -0700675static void tg3_ape_lock_init(struct tg3 *tp)
676{
677 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000678 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000679
Joe Perches41535772013-02-16 11:20:04 +0000680 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000681 regbase = TG3_APE_LOCK_GRANT;
682 else
683 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700684
685 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000686 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
687 switch (i) {
688 case TG3_APE_LOCK_PHY0:
689 case TG3_APE_LOCK_PHY1:
690 case TG3_APE_LOCK_PHY2:
691 case TG3_APE_LOCK_PHY3:
692 bit = APE_LOCK_GRANT_DRIVER;
693 break;
694 default:
695 if (!tp->pci_fn)
696 bit = APE_LOCK_GRANT_DRIVER;
697 else
698 bit = 1 << tp->pci_fn;
699 }
700 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000701 }
702
Matt Carlson0d3031d2007-10-10 18:02:43 -0700703}
704
705static int tg3_ape_lock(struct tg3 *tp, int locknum)
706{
707 int i, off;
708 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000709 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700710
Joe Perches63c3a662011-04-26 08:12:10 +0000711 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700712 return 0;
713
714 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000715 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000716 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000717 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000718 case TG3_APE_LOCK_GRC:
719 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000720 if (!tp->pci_fn)
721 bit = APE_LOCK_REQ_DRIVER;
722 else
723 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000724 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000725 case TG3_APE_LOCK_PHY0:
726 case TG3_APE_LOCK_PHY1:
727 case TG3_APE_LOCK_PHY2:
728 case TG3_APE_LOCK_PHY3:
729 bit = APE_LOCK_REQ_DRIVER;
730 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000731 default:
732 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700733 }
734
Joe Perches41535772013-02-16 11:20:04 +0000735 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000736 req = TG3_APE_LOCK_REQ;
737 gnt = TG3_APE_LOCK_GRANT;
738 } else {
739 req = TG3_APE_PER_LOCK_REQ;
740 gnt = TG3_APE_PER_LOCK_GRANT;
741 }
742
Matt Carlson0d3031d2007-10-10 18:02:43 -0700743 off = 4 * locknum;
744
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000745 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746
747 /* Wait for up to 1 millisecond to acquire lock. */
748 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000749 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000750 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700751 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800752 if (pci_channel_offline(tp->pdev))
753 break;
754
Matt Carlson0d3031d2007-10-10 18:02:43 -0700755 udelay(10);
756 }
757
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000758 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700759 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000760 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700761 ret = -EBUSY;
762 }
763
764 return ret;
765}
766
767static void tg3_ape_unlock(struct tg3 *tp, int locknum)
768{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000769 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700770
Joe Perches63c3a662011-04-26 08:12:10 +0000771 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700772 return;
773
774 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000775 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000776 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000777 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000778 case TG3_APE_LOCK_GRC:
779 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000780 if (!tp->pci_fn)
781 bit = APE_LOCK_GRANT_DRIVER;
782 else
783 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000784 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000785 case TG3_APE_LOCK_PHY0:
786 case TG3_APE_LOCK_PHY1:
787 case TG3_APE_LOCK_PHY2:
788 case TG3_APE_LOCK_PHY3:
789 bit = APE_LOCK_GRANT_DRIVER;
790 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000791 default:
792 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700793 }
794
Joe Perches41535772013-02-16 11:20:04 +0000795 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000796 gnt = TG3_APE_LOCK_GRANT;
797 else
798 gnt = TG3_APE_PER_LOCK_GRANT;
799
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000800 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700801}
802
Matt Carlsonb65a3722012-07-16 16:24:00 +0000803static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000804{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000805 u32 apedata;
806
Matt Carlsonb65a3722012-07-16 16:24:00 +0000807 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000808 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000809 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000810
811 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000812 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
813 break;
814
Matt Carlsonb65a3722012-07-16 16:24:00 +0000815 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
816
817 udelay(10);
818 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000819 }
820
Matt Carlsonb65a3722012-07-16 16:24:00 +0000821 return timeout_us ? 0 : -EBUSY;
822}
823
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000824static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
825{
826 u32 i, apedata;
827
828 for (i = 0; i < timeout_us / 10; i++) {
829 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
830
831 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
832 break;
833
834 udelay(10);
835 }
836
837 return i == timeout_us / 10;
838}
839
Michael Chan86449942012-10-02 20:31:14 -0700840static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
841 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000842{
843 int err;
844 u32 i, bufoff, msgoff, maxlen, apedata;
845
846 if (!tg3_flag(tp, APE_HAS_NCSI))
847 return 0;
848
849 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
850 if (apedata != APE_SEG_SIG_MAGIC)
851 return -ENODEV;
852
853 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
854 if (!(apedata & APE_FW_STATUS_READY))
855 return -EAGAIN;
856
857 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
858 TG3_APE_SHMEM_BASE;
859 msgoff = bufoff + 2 * sizeof(u32);
860 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
861
862 while (len) {
863 u32 length;
864
865 /* Cap xfer sizes to scratchpad limits. */
866 length = (len > maxlen) ? maxlen : len;
867 len -= length;
868
869 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
870 if (!(apedata & APE_FW_STATUS_READY))
871 return -EAGAIN;
872
873 /* Wait for up to 1 msec for APE to service previous event. */
874 err = tg3_ape_event_lock(tp, 1000);
875 if (err)
876 return err;
877
878 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
879 APE_EVENT_STATUS_SCRTCHPD_READ |
880 APE_EVENT_STATUS_EVENT_PENDING;
881 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
882
883 tg3_ape_write32(tp, bufoff, base_off);
884 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
885
886 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
887 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
888
889 base_off += length;
890
891 if (tg3_ape_wait_for_event(tp, 30000))
892 return -EAGAIN;
893
894 for (i = 0; length; i += 4, length -= 4) {
895 u32 val = tg3_ape_read32(tp, msgoff + i);
896 memcpy(data, &val, sizeof(u32));
897 data++;
898 }
899 }
900
901 return 0;
902}
903
Matt Carlsonb65a3722012-07-16 16:24:00 +0000904static int tg3_ape_send_event(struct tg3 *tp, u32 event)
905{
906 int err;
907 u32 apedata;
908
909 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
910 if (apedata != APE_SEG_SIG_MAGIC)
911 return -EAGAIN;
912
913 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
914 if (!(apedata & APE_FW_STATUS_READY))
915 return -EAGAIN;
916
917 /* Wait for up to 1 millisecond for APE to service previous event. */
918 err = tg3_ape_event_lock(tp, 1000);
919 if (err)
920 return err;
921
922 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
923 event | APE_EVENT_STATUS_EVENT_PENDING);
924
925 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
926 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
927
928 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000929}
930
931static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
932{
933 u32 event;
934 u32 apedata;
935
936 if (!tg3_flag(tp, ENABLE_APE))
937 return;
938
939 switch (kind) {
940 case RESET_KIND_INIT:
941 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
942 APE_HOST_SEG_SIG_MAGIC);
943 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
944 APE_HOST_SEG_LEN_MAGIC);
945 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
946 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
947 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
948 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
949 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
950 APE_HOST_BEHAV_NO_PHYLOCK);
951 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
952 TG3_APE_HOST_DRVR_STATE_START);
953
954 event = APE_EVENT_STATUS_STATE_START;
955 break;
956 case RESET_KIND_SHUTDOWN:
957 /* With the interface we are currently using,
958 * APE does not track driver state. Wiping
959 * out the HOST SEGMENT SIGNATURE forces
960 * the APE to assume OS absent status.
961 */
962 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
963
964 if (device_may_wakeup(&tp->pdev->dev) &&
965 tg3_flag(tp, WOL_ENABLE)) {
966 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
967 TG3_APE_HOST_WOL_SPEED_AUTO);
968 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
969 } else
970 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
971
972 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
973
974 event = APE_EVENT_STATUS_STATE_UNLOAD;
975 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000976 default:
977 return;
978 }
979
980 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
981
982 tg3_ape_send_event(tp, event);
983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985static void tg3_disable_ints(struct tg3 *tp)
986{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000987 int i;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 tw32(TG3PCI_MISC_HOST_CTRL,
990 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000991 for (i = 0; i < tp->irq_max; i++)
992 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static void tg3_enable_ints(struct tg3 *tp)
996{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000997 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000998
Michael Chanbbe832c2005-06-24 20:20:04 -0700999 tp->irq_sync = 0;
1000 wmb();
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 tw32(TG3PCI_MISC_HOST_CTRL,
1003 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001004
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001005 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001006 for (i = 0; i < tp->irq_cnt; i++) {
1007 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001008
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001010 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001011 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1012
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001013 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001014 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001015
1016 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001017 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001018 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1019 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1020 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001021 tw32(HOSTCC_MODE, tp->coal_now);
1022
1023 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Matt Carlson17375d22009-08-28 14:02:18 +00001026static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001027{
Matt Carlson17375d22009-08-28 14:02:18 +00001028 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001029 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001030 unsigned int work_exists = 0;
1031
1032 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001033 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001034 if (sblk->status & SD_STATUS_LINK_CHG)
1035 work_exists = 1;
1036 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001037
1038 /* check for TX work to do */
1039 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1040 work_exists = 1;
1041
1042 /* check for RX work to do */
1043 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001044 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001045 work_exists = 1;
1046
1047 return work_exists;
1048}
1049
Matt Carlson17375d22009-08-28 14:02:18 +00001050/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001051 * similar to tg3_enable_ints, but it accurately determines whether there
1052 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001053 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Matt Carlson17375d22009-08-28 14:02:18 +00001055static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Matt Carlson17375d22009-08-28 14:02:18 +00001057 struct tg3 *tp = tnapi->tp;
1058
Matt Carlson898a56f2009-08-28 14:02:40 +00001059 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 mmiowb();
1061
David S. Millerfac9b832005-05-18 22:46:34 -07001062 /* When doing tagged status, this work check is unnecessary.
1063 * The last_tag we write above tells the chip which piece of
1064 * work we've completed.
1065 */
Joe Perches63c3a662011-04-26 08:12:10 +00001066 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001067 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001068 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static void tg3_switch_clocks(struct tg3 *tp)
1072{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001073 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 u32 orig_clock_ctrl;
1075
Joe Perches63c3a662011-04-26 08:12:10 +00001076 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001077 return;
1078
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001079 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 orig_clock_ctrl = clock_ctrl;
1082 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1083 CLOCK_CTRL_CLKRUN_OENABLE |
1084 0x1f);
1085 tp->pci_clock_ctrl = clock_ctrl;
1086
Joe Perches63c3a662011-04-26 08:12:10 +00001087 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001089 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1090 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001093 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1094 clock_ctrl |
1095 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1096 40);
1097 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1098 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1099 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001101 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104#define PHY_BUSY_LOOPS 5000
1105
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001106static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1107 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 u32 frame_val;
1110 unsigned int loops;
1111 int ret;
1112
1113 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1114 tw32_f(MAC_MI_MODE,
1115 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1116 udelay(80);
1117 }
1118
Michael Chan8151ad52012-07-29 19:15:41 +00001119 tg3_ape_lock(tp, tp->phy_ape_lock);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 *val = 0x0;
1122
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001123 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 MI_COM_PHY_ADDR_MASK);
1125 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1126 MI_COM_REG_ADDR_MASK);
1127 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 tw32_f(MAC_MI_COM, frame_val);
1130
1131 loops = PHY_BUSY_LOOPS;
1132 while (loops != 0) {
1133 udelay(10);
1134 frame_val = tr32(MAC_MI_COM);
1135
1136 if ((frame_val & MI_COM_BUSY) == 0) {
1137 udelay(5);
1138 frame_val = tr32(MAC_MI_COM);
1139 break;
1140 }
1141 loops -= 1;
1142 }
1143
1144 ret = -EBUSY;
1145 if (loops != 0) {
1146 *val = frame_val & MI_COM_DATA_MASK;
1147 ret = 0;
1148 }
1149
1150 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1151 tw32_f(MAC_MI_MODE, tp->mi_mode);
1152 udelay(80);
1153 }
1154
Michael Chan8151ad52012-07-29 19:15:41 +00001155 tg3_ape_unlock(tp, tp->phy_ape_lock);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return ret;
1158}
1159
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001160static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1161{
1162 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1163}
1164
1165static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1166 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 u32 frame_val;
1169 unsigned int loops;
1170 int ret;
1171
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001172 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001173 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001174 return 0;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1177 tw32_f(MAC_MI_MODE,
1178 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1179 udelay(80);
1180 }
1181
Michael Chan8151ad52012-07-29 19:15:41 +00001182 tg3_ape_lock(tp, tp->phy_ape_lock);
1183
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001184 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 MI_COM_PHY_ADDR_MASK);
1186 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1187 MI_COM_REG_ADDR_MASK);
1188 frame_val |= (val & MI_COM_DATA_MASK);
1189 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 tw32_f(MAC_MI_COM, frame_val);
1192
1193 loops = PHY_BUSY_LOOPS;
1194 while (loops != 0) {
1195 udelay(10);
1196 frame_val = tr32(MAC_MI_COM);
1197 if ((frame_val & MI_COM_BUSY) == 0) {
1198 udelay(5);
1199 frame_val = tr32(MAC_MI_COM);
1200 break;
1201 }
1202 loops -= 1;
1203 }
1204
1205 ret = -EBUSY;
1206 if (loops != 0)
1207 ret = 0;
1208
1209 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1210 tw32_f(MAC_MI_MODE, tp->mi_mode);
1211 udelay(80);
1212 }
1213
Michael Chan8151ad52012-07-29 19:15:41 +00001214 tg3_ape_unlock(tp, tp->phy_ape_lock);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return ret;
1217}
1218
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001219static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1220{
1221 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1222}
1223
Matt Carlsonb0988c12011-04-20 07:57:39 +00001224static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1225{
1226 int err;
1227
1228 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1229 if (err)
1230 goto done;
1231
1232 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1233 if (err)
1234 goto done;
1235
1236 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1237 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1238 if (err)
1239 goto done;
1240
1241 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1242
1243done:
1244 return err;
1245}
1246
1247static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1248{
1249 int err;
1250
1251 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1252 if (err)
1253 goto done;
1254
1255 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1256 if (err)
1257 goto done;
1258
1259 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1260 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1261 if (err)
1262 goto done;
1263
1264 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1265
1266done:
1267 return err;
1268}
1269
1270static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1271{
1272 int err;
1273
1274 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1275 if (!err)
1276 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1277
1278 return err;
1279}
1280
1281static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1282{
1283 int err;
1284
1285 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1286 if (!err)
1287 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1288
1289 return err;
1290}
1291
Matt Carlson15ee95c2011-04-20 07:57:40 +00001292static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1293{
1294 int err;
1295
1296 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1297 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1298 MII_TG3_AUXCTL_SHDWSEL_MISC);
1299 if (!err)
1300 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1301
1302 return err;
1303}
1304
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001305static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1306{
1307 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1308 set |= MII_TG3_AUXCTL_MISC_WREN;
1309
1310 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1311}
1312
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001313static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1314{
1315 u32 val;
1316 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001317
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001318 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1319
1320 if (err)
1321 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001322
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001323 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001324 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1325 else
1326 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1327
1328 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1329 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1330
1331 return err;
1332}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001333
Nithin Sujir3ab71072013-09-20 16:46:55 -07001334static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1335{
1336 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1337 reg | val | MII_TG3_MISC_SHDW_WREN);
1338}
1339
Matt Carlson95e28692008-05-25 23:44:14 -07001340static int tg3_bmcr_reset(struct tg3 *tp)
1341{
1342 u32 phy_control;
1343 int limit, err;
1344
1345 /* OK, reset it, and poll the BMCR_RESET bit until it
1346 * clears or we time out.
1347 */
1348 phy_control = BMCR_RESET;
1349 err = tg3_writephy(tp, MII_BMCR, phy_control);
1350 if (err != 0)
1351 return -EBUSY;
1352
1353 limit = 5000;
1354 while (limit--) {
1355 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1356 if (err != 0)
1357 return -EBUSY;
1358
1359 if ((phy_control & BMCR_RESET) == 0) {
1360 udelay(40);
1361 break;
1362 }
1363 udelay(10);
1364 }
Roel Kluind4675b52009-02-12 16:33:27 -08001365 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001366 return -EBUSY;
1367
1368 return 0;
1369}
1370
Matt Carlson158d7ab2008-05-29 01:37:54 -07001371static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1372{
Francois Romieu3d165432009-01-19 16:56:50 -08001373 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001374 u32 val;
1375
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001376 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001377
Hauke Mehrtensead24022013-09-28 23:15:26 +02001378 if (__tg3_readphy(tp, mii_id, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001379 val = -EIO;
1380
1381 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001382
1383 return val;
1384}
1385
1386static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1387{
Francois Romieu3d165432009-01-19 16:56:50 -08001388 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001389 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001390
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001391 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001392
Hauke Mehrtensead24022013-09-28 23:15:26 +02001393 if (__tg3_writephy(tp, mii_id, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001394 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001395
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001396 spin_unlock_bh(&tp->lock);
1397
1398 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001399}
1400
1401static int tg3_mdio_reset(struct mii_bus *bp)
1402{
1403 return 0;
1404}
1405
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001406static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001407{
1408 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001409 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001410
Hauke Mehrtensead24022013-09-28 23:15:26 +02001411 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001412 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001413 case PHY_ID_BCM50610:
1414 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001415 val = MAC_PHYCFG2_50610_LED_MODES;
1416 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001417 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001418 val = MAC_PHYCFG2_AC131_LED_MODES;
1419 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001420 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001421 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1422 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001423 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001424 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1425 break;
1426 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001427 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001428 }
1429
1430 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1431 tw32(MAC_PHYCFG2, val);
1432
1433 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001434 val &= ~(MAC_PHYCFG1_RGMII_INT |
1435 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1436 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001437 tw32(MAC_PHYCFG1, val);
1438
1439 return;
1440 }
1441
Joe Perches63c3a662011-04-26 08:12:10 +00001442 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001443 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1444 MAC_PHYCFG2_FMODE_MASK_MASK |
1445 MAC_PHYCFG2_GMODE_MASK_MASK |
1446 MAC_PHYCFG2_ACT_MASK_MASK |
1447 MAC_PHYCFG2_QUAL_MASK_MASK |
1448 MAC_PHYCFG2_INBAND_ENABLE;
1449
1450 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001451
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001452 val = tr32(MAC_PHYCFG1);
1453 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1454 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001455 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1456 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001457 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001458 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001459 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1460 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001461 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1462 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1463 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001464
Matt Carlsona9daf362008-05-25 23:49:44 -07001465 val = tr32(MAC_EXT_RGMII_MODE);
1466 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1467 MAC_RGMII_MODE_RX_QUALITY |
1468 MAC_RGMII_MODE_RX_ACTIVITY |
1469 MAC_RGMII_MODE_RX_ENG_DET |
1470 MAC_RGMII_MODE_TX_ENABLE |
1471 MAC_RGMII_MODE_TX_LOWPWR |
1472 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001473 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1474 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001475 val |= MAC_RGMII_MODE_RX_INT_B |
1476 MAC_RGMII_MODE_RX_QUALITY |
1477 MAC_RGMII_MODE_RX_ACTIVITY |
1478 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001479 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001480 val |= MAC_RGMII_MODE_TX_ENABLE |
1481 MAC_RGMII_MODE_TX_LOWPWR |
1482 MAC_RGMII_MODE_TX_RESET;
1483 }
1484 tw32(MAC_EXT_RGMII_MODE, val);
1485}
1486
Matt Carlson158d7ab2008-05-29 01:37:54 -07001487static void tg3_mdio_start(struct tg3 *tp)
1488{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001489 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1490 tw32_f(MAC_MI_MODE, tp->mi_mode);
1491 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001492
Joe Perches63c3a662011-04-26 08:12:10 +00001493 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001494 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001495 tg3_mdio_config_5785(tp);
1496}
1497
1498static int tg3_mdio_init(struct tg3 *tp)
1499{
1500 int i;
1501 u32 reg;
1502 struct phy_device *phydev;
1503
Joe Perches63c3a662011-04-26 08:12:10 +00001504 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001505 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001506
Matt Carlson69f11c92011-07-13 09:27:30 +00001507 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001508
Joe Perches41535772013-02-16 11:20:04 +00001509 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001510 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1511 else
1512 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1513 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001514 if (is_serdes)
1515 tp->phy_addr += 7;
Hauke Mehrtensee002b62013-09-28 23:15:28 +02001516 } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
1517 int addr;
1518
1519 addr = ssb_gige_get_phyaddr(tp->pdev);
1520 if (addr < 0)
1521 return addr;
1522 tp->phy_addr = addr;
Matt Carlson882e9792009-09-01 13:21:36 +00001523 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001524 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001525
Matt Carlson158d7ab2008-05-29 01:37:54 -07001526 tg3_mdio_start(tp);
1527
Joe Perches63c3a662011-04-26 08:12:10 +00001528 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001529 return 0;
1530
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001531 tp->mdio_bus = mdiobus_alloc();
1532 if (tp->mdio_bus == NULL)
1533 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001534
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001535 tp->mdio_bus->name = "tg3 mdio bus";
1536 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001537 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001538 tp->mdio_bus->priv = tp;
1539 tp->mdio_bus->parent = &tp->pdev->dev;
1540 tp->mdio_bus->read = &tg3_mdio_read;
1541 tp->mdio_bus->write = &tg3_mdio_write;
1542 tp->mdio_bus->reset = &tg3_mdio_reset;
Hauke Mehrtensead24022013-09-28 23:15:26 +02001543 tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001544 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001545
1546 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001547 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001548
1549 /* The bus registration will look for all the PHYs on the mdio bus.
1550 * Unfortunately, it does not ensure the PHY is powered up before
1551 * accessing the PHY ID registers. A chip reset is the
1552 * quickest way to bring the device back to an operational state..
1553 */
1554 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1555 tg3_bmcr_reset(tp);
1556
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001557 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001558 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001559 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001560 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001561 return i;
1562 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001563
Hauke Mehrtensead24022013-09-28 23:15:26 +02001564 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsona9daf362008-05-25 23:49:44 -07001565
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001566 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001567 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001568 mdiobus_unregister(tp->mdio_bus);
1569 mdiobus_free(tp->mdio_bus);
1570 return -ENODEV;
1571 }
1572
1573 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001574 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001575 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001576 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001577 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001578 case PHY_ID_BCM50610:
1579 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001580 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001581 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001582 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001583 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001584 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001585 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001586 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001587 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001588 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001589 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001590 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001591 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001592 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001593 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001594 case PHY_ID_RTL8201E:
1595 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001596 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001597 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001598 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001599 break;
1600 }
1601
Joe Perches63c3a662011-04-26 08:12:10 +00001602 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001603
Joe Perches41535772013-02-16 11:20:04 +00001604 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001605 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001606
1607 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001608}
1609
1610static void tg3_mdio_fini(struct tg3 *tp)
1611{
Joe Perches63c3a662011-04-26 08:12:10 +00001612 if (tg3_flag(tp, MDIOBUS_INITED)) {
1613 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001614 mdiobus_unregister(tp->mdio_bus);
1615 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001616 }
1617}
1618
Matt Carlson95e28692008-05-25 23:44:14 -07001619/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001620static inline void tg3_generate_fw_event(struct tg3 *tp)
1621{
1622 u32 val;
1623
1624 val = tr32(GRC_RX_CPU_EVENT);
1625 val |= GRC_RX_CPU_DRIVER_EVENT;
1626 tw32_f(GRC_RX_CPU_EVENT, val);
1627
1628 tp->last_event_jiffies = jiffies;
1629}
1630
1631#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1632
1633/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001634static void tg3_wait_for_event_ack(struct tg3 *tp)
1635{
1636 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001637 unsigned int delay_cnt;
1638 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001639
Matt Carlson4ba526c2008-08-15 14:10:04 -07001640 /* If enough time has passed, no wait is necessary. */
1641 time_remain = (long)(tp->last_event_jiffies + 1 +
1642 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1643 (long)jiffies;
1644 if (time_remain < 0)
1645 return;
1646
1647 /* Check if we can shorten the wait time. */
1648 delay_cnt = jiffies_to_usecs(time_remain);
1649 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1650 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1651 delay_cnt = (delay_cnt >> 3) + 1;
1652
1653 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001654 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1655 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001656 if (pci_channel_offline(tp->pdev))
1657 break;
1658
Matt Carlson4ba526c2008-08-15 14:10:04 -07001659 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001660 }
1661}
1662
1663/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001664static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001665{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001666 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001667
1668 val = 0;
1669 if (!tg3_readphy(tp, MII_BMCR, &reg))
1670 val = reg << 16;
1671 if (!tg3_readphy(tp, MII_BMSR, &reg))
1672 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001673 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001674
1675 val = 0;
1676 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1677 val = reg << 16;
1678 if (!tg3_readphy(tp, MII_LPA, &reg))
1679 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001680 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001681
1682 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001683 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001684 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1685 val = reg << 16;
1686 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1687 val |= (reg & 0xffff);
1688 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001689 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001690
1691 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1692 val = reg << 16;
1693 else
1694 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001695 *data++ = val;
1696}
1697
1698/* tp->lock is held. */
1699static void tg3_ump_link_report(struct tg3 *tp)
1700{
1701 u32 data[4];
1702
1703 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1704 return;
1705
1706 tg3_phy_gather_ump_data(tp, data);
1707
1708 tg3_wait_for_event_ack(tp);
1709
1710 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1711 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1712 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1713 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1714 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1715 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001716
Matt Carlson4ba526c2008-08-15 14:10:04 -07001717 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001718}
1719
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001720/* tp->lock is held. */
1721static void tg3_stop_fw(struct tg3 *tp)
1722{
1723 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1724 /* Wait for RX cpu to ACK the previous event. */
1725 tg3_wait_for_event_ack(tp);
1726
1727 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1728
1729 tg3_generate_fw_event(tp);
1730
1731 /* Wait for RX cpu to ACK this event. */
1732 tg3_wait_for_event_ack(tp);
1733 }
1734}
1735
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001736/* tp->lock is held. */
1737static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1738{
1739 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1740 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1741
1742 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1743 switch (kind) {
1744 case RESET_KIND_INIT:
1745 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1746 DRV_STATE_START);
1747 break;
1748
1749 case RESET_KIND_SHUTDOWN:
1750 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1751 DRV_STATE_UNLOAD);
1752 break;
1753
1754 case RESET_KIND_SUSPEND:
1755 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1756 DRV_STATE_SUSPEND);
1757 break;
1758
1759 default:
1760 break;
1761 }
1762 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001763}
1764
1765/* tp->lock is held. */
1766static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1767{
1768 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1769 switch (kind) {
1770 case RESET_KIND_INIT:
1771 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1772 DRV_STATE_START_DONE);
1773 break;
1774
1775 case RESET_KIND_SHUTDOWN:
1776 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1777 DRV_STATE_UNLOAD_DONE);
1778 break;
1779
1780 default:
1781 break;
1782 }
1783 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001784}
1785
1786/* tp->lock is held. */
1787static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1788{
1789 if (tg3_flag(tp, ENABLE_ASF)) {
1790 switch (kind) {
1791 case RESET_KIND_INIT:
1792 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1793 DRV_STATE_START);
1794 break;
1795
1796 case RESET_KIND_SHUTDOWN:
1797 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1798 DRV_STATE_UNLOAD);
1799 break;
1800
1801 case RESET_KIND_SUSPEND:
1802 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1803 DRV_STATE_SUSPEND);
1804 break;
1805
1806 default:
1807 break;
1808 }
1809 }
1810}
1811
1812static int tg3_poll_fw(struct tg3 *tp)
1813{
1814 int i;
1815 u32 val;
1816
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001817 if (tg3_flag(tp, NO_FWARE_REPORTED))
1818 return 0;
1819
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001820 if (tg3_flag(tp, IS_SSB_CORE)) {
1821 /* We don't use firmware. */
1822 return 0;
1823 }
1824
Joe Perches41535772013-02-16 11:20:04 +00001825 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001826 /* Wait up to 20ms for init done. */
1827 for (i = 0; i < 200; i++) {
1828 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1829 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001830 if (pci_channel_offline(tp->pdev))
1831 return -ENODEV;
1832
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001833 udelay(100);
1834 }
1835 return -ENODEV;
1836 }
1837
1838 /* Wait for firmware initialization to complete. */
1839 for (i = 0; i < 100000; i++) {
1840 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1841 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1842 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001843 if (pci_channel_offline(tp->pdev)) {
1844 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1845 tg3_flag_set(tp, NO_FWARE_REPORTED);
1846 netdev_info(tp->dev, "No firmware running\n");
1847 }
1848
1849 break;
1850 }
1851
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001852 udelay(10);
1853 }
1854
1855 /* Chip might not be fitted with firmware. Some Sun onboard
1856 * parts are configured like that. So don't signal the timeout
1857 * of the above loop as an error, but do report the lack of
1858 * running firmware once.
1859 */
1860 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1861 tg3_flag_set(tp, NO_FWARE_REPORTED);
1862
1863 netdev_info(tp->dev, "No firmware running\n");
1864 }
1865
Joe Perches41535772013-02-16 11:20:04 +00001866 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001867 /* The 57765 A0 needs a little more
1868 * time to do some important work.
1869 */
1870 mdelay(10);
1871 }
1872
1873 return 0;
1874}
1875
Matt Carlson95e28692008-05-25 23:44:14 -07001876static void tg3_link_report(struct tg3 *tp)
1877{
1878 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001879 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001880 tg3_ump_link_report(tp);
1881 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001882 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1883 (tp->link_config.active_speed == SPEED_1000 ?
1884 1000 :
1885 (tp->link_config.active_speed == SPEED_100 ?
1886 100 : 10)),
1887 (tp->link_config.active_duplex == DUPLEX_FULL ?
1888 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001889
Joe Perches05dbe002010-02-17 19:44:19 +00001890 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1891 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1892 "on" : "off",
1893 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1894 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001895
1896 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1897 netdev_info(tp->dev, "EEE is %s\n",
1898 tp->setlpicnt ? "enabled" : "disabled");
1899
Matt Carlson95e28692008-05-25 23:44:14 -07001900 tg3_ump_link_report(tp);
1901 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001902
1903 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001904}
1905
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001906static u32 tg3_decode_flowctrl_1000T(u32 adv)
1907{
1908 u32 flowctrl = 0;
1909
1910 if (adv & ADVERTISE_PAUSE_CAP) {
1911 flowctrl |= FLOW_CTRL_RX;
1912 if (!(adv & ADVERTISE_PAUSE_ASYM))
1913 flowctrl |= FLOW_CTRL_TX;
1914 } else if (adv & ADVERTISE_PAUSE_ASYM)
1915 flowctrl |= FLOW_CTRL_TX;
1916
1917 return flowctrl;
1918}
1919
Matt Carlson95e28692008-05-25 23:44:14 -07001920static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1921{
1922 u16 miireg;
1923
Steve Glendinninge18ce342008-12-16 02:00:00 -08001924 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001925 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001926 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001927 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001928 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001929 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1930 else
1931 miireg = 0;
1932
1933 return miireg;
1934}
1935
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001936static u32 tg3_decode_flowctrl_1000X(u32 adv)
1937{
1938 u32 flowctrl = 0;
1939
1940 if (adv & ADVERTISE_1000XPAUSE) {
1941 flowctrl |= FLOW_CTRL_RX;
1942 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1943 flowctrl |= FLOW_CTRL_TX;
1944 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1945 flowctrl |= FLOW_CTRL_TX;
1946
1947 return flowctrl;
1948}
1949
Matt Carlson95e28692008-05-25 23:44:14 -07001950static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1951{
1952 u8 cap = 0;
1953
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001954 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1955 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1956 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1957 if (lcladv & ADVERTISE_1000XPAUSE)
1958 cap = FLOW_CTRL_RX;
1959 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001960 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001961 }
1962
1963 return cap;
1964}
1965
Matt Carlsonf51f3562008-05-25 23:45:08 -07001966static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001967{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001968 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001969 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001970 u32 old_rx_mode = tp->rx_mode;
1971 u32 old_tx_mode = tp->tx_mode;
1972
Joe Perches63c3a662011-04-26 08:12:10 +00001973 if (tg3_flag(tp, USE_PHYLIB))
Hauke Mehrtensead24022013-09-28 23:15:26 +02001974 autoneg = tp->mdio_bus->phy_map[tp->phy_addr]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001975 else
1976 autoneg = tp->link_config.autoneg;
1977
Joe Perches63c3a662011-04-26 08:12:10 +00001978 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001979 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001980 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001981 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001982 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001983 } else
1984 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001985
Matt Carlsonf51f3562008-05-25 23:45:08 -07001986 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001987
Steve Glendinninge18ce342008-12-16 02:00:00 -08001988 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001989 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1990 else
1991 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1992
Matt Carlsonf51f3562008-05-25 23:45:08 -07001993 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001994 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001995
Steve Glendinninge18ce342008-12-16 02:00:00 -08001996 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001997 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1998 else
1999 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
2000
Matt Carlsonf51f3562008-05-25 23:45:08 -07002001 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07002002 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07002003}
2004
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002005static void tg3_adjust_link(struct net_device *dev)
2006{
2007 u8 oldflowctrl, linkmesg = 0;
2008 u32 mac_mode, lcl_adv, rmt_adv;
2009 struct tg3 *tp = netdev_priv(dev);
Hauke Mehrtensead24022013-09-28 23:15:26 +02002010 struct phy_device *phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002011
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002012 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002013
2014 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2015 MAC_MODE_HALF_DUPLEX);
2016
2017 oldflowctrl = tp->link_config.active_flowctrl;
2018
2019 if (phydev->link) {
2020 lcl_adv = 0;
2021 rmt_adv = 0;
2022
2023 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2024 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002025 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002026 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002027 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002028 else
2029 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002030
2031 if (phydev->duplex == DUPLEX_HALF)
2032 mac_mode |= MAC_MODE_HALF_DUPLEX;
2033 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002034 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002035 tp->link_config.flowctrl);
2036
2037 if (phydev->pause)
2038 rmt_adv = LPA_PAUSE_CAP;
2039 if (phydev->asym_pause)
2040 rmt_adv |= LPA_PAUSE_ASYM;
2041 }
2042
2043 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2044 } else
2045 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2046
2047 if (mac_mode != tp->mac_mode) {
2048 tp->mac_mode = mac_mode;
2049 tw32_f(MAC_MODE, tp->mac_mode);
2050 udelay(40);
2051 }
2052
Joe Perches41535772013-02-16 11:20:04 +00002053 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002054 if (phydev->speed == SPEED_10)
2055 tw32(MAC_MI_STAT,
2056 MAC_MI_STAT_10MBPS_MODE |
2057 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2058 else
2059 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2060 }
2061
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002062 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2063 tw32(MAC_TX_LENGTHS,
2064 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2065 (6 << TX_LENGTHS_IPG_SHIFT) |
2066 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2067 else
2068 tw32(MAC_TX_LENGTHS,
2069 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2070 (6 << TX_LENGTHS_IPG_SHIFT) |
2071 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2072
Matt Carlson34655ad2012-02-22 12:35:18 +00002073 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002074 phydev->speed != tp->link_config.active_speed ||
2075 phydev->duplex != tp->link_config.active_duplex ||
2076 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002077 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002078
Matt Carlson34655ad2012-02-22 12:35:18 +00002079 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002080 tp->link_config.active_speed = phydev->speed;
2081 tp->link_config.active_duplex = phydev->duplex;
2082
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002083 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002084
2085 if (linkmesg)
2086 tg3_link_report(tp);
2087}
2088
2089static int tg3_phy_init(struct tg3 *tp)
2090{
2091 struct phy_device *phydev;
2092
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002093 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002094 return 0;
2095
2096 /* Bring the PHY back to a known state. */
2097 tg3_bmcr_reset(tp);
2098
Hauke Mehrtensead24022013-09-28 23:15:26 +02002099 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002100
2101 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002102 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2103 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002104 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002105 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002106 return PTR_ERR(phydev);
2107 }
2108
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002109 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002110 switch (phydev->interface) {
2111 case PHY_INTERFACE_MODE_GMII:
2112 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002113 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002114 phydev->supported &= (PHY_GBIT_FEATURES |
2115 SUPPORTED_Pause |
2116 SUPPORTED_Asym_Pause);
2117 break;
2118 }
2119 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002120 case PHY_INTERFACE_MODE_MII:
2121 phydev->supported &= (PHY_BASIC_FEATURES |
2122 SUPPORTED_Pause |
2123 SUPPORTED_Asym_Pause);
2124 break;
2125 default:
Hauke Mehrtensead24022013-09-28 23:15:26 +02002126 phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002127 return -EINVAL;
2128 }
2129
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002130 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002131
2132 phydev->advertising = phydev->supported;
2133
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002134 return 0;
2135}
2136
2137static void tg3_phy_start(struct tg3 *tp)
2138{
2139 struct phy_device *phydev;
2140
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002141 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002142 return;
2143
Hauke Mehrtensead24022013-09-28 23:15:26 +02002144 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002145
Matt Carlson80096062010-08-02 11:26:06 +00002146 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2147 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002148 phydev->speed = tp->link_config.speed;
2149 phydev->duplex = tp->link_config.duplex;
2150 phydev->autoneg = tp->link_config.autoneg;
2151 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002152 }
2153
2154 phy_start(phydev);
2155
2156 phy_start_aneg(phydev);
2157}
2158
2159static void tg3_phy_stop(struct tg3 *tp)
2160{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002161 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002162 return;
2163
Hauke Mehrtensead24022013-09-28 23:15:26 +02002164 phy_stop(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002165}
2166
2167static void tg3_phy_fini(struct tg3 *tp)
2168{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002169 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Hauke Mehrtensead24022013-09-28 23:15:26 +02002170 phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002171 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002172 }
2173}
2174
Matt Carlson941ec902011-08-19 13:58:23 +00002175static int tg3_phy_set_extloopbk(struct tg3 *tp)
2176{
2177 int err;
2178 u32 val;
2179
2180 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2181 return 0;
2182
2183 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2184 /* Cannot do read-modify-write on 5401 */
2185 err = tg3_phy_auxctl_write(tp,
2186 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2187 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2188 0x4c20);
2189 goto done;
2190 }
2191
2192 err = tg3_phy_auxctl_read(tp,
2193 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2194 if (err)
2195 return err;
2196
2197 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2198 err = tg3_phy_auxctl_write(tp,
2199 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2200
2201done:
2202 return err;
2203}
2204
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002205static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2206{
2207 u32 phytest;
2208
2209 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2210 u32 phy;
2211
2212 tg3_writephy(tp, MII_TG3_FET_TEST,
2213 phytest | MII_TG3_FET_SHADOW_EN);
2214 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2215 if (enable)
2216 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2217 else
2218 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2219 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2220 }
2221 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2222 }
2223}
2224
Matt Carlson6833c042008-11-21 17:18:59 -08002225static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2226{
2227 u32 reg;
2228
Joe Perches63c3a662011-04-26 08:12:10 +00002229 if (!tg3_flag(tp, 5705_PLUS) ||
2230 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002231 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002232 return;
2233
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002234 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002235 tg3_phy_fet_toggle_apd(tp, enable);
2236 return;
2237 }
2238
Nithin Sujir3ab71072013-09-20 16:46:55 -07002239 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002240 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2241 MII_TG3_MISC_SHDW_SCR5_SDTL |
2242 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002243 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002244 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2245
Nithin Sujir3ab71072013-09-20 16:46:55 -07002246 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002247
2248
Nithin Sujir3ab71072013-09-20 16:46:55 -07002249 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002250 if (enable)
2251 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2252
Nithin Sujir3ab71072013-09-20 16:46:55 -07002253 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002254}
2255
Joe Perches953c96e2013-04-09 10:18:14 +00002256static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002257{
2258 u32 phy;
2259
Joe Perches63c3a662011-04-26 08:12:10 +00002260 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002261 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002262 return;
2263
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002264 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002265 u32 ephy;
2266
Matt Carlson535ef6e2009-08-25 10:09:36 +00002267 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2268 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2269
2270 tg3_writephy(tp, MII_TG3_FET_TEST,
2271 ephy | MII_TG3_FET_SHADOW_EN);
2272 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002273 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002274 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002275 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002276 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2277 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002278 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002279 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002280 }
2281 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002282 int ret;
2283
2284 ret = tg3_phy_auxctl_read(tp,
2285 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2286 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002287 if (enable)
2288 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2289 else
2290 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002291 tg3_phy_auxctl_write(tp,
2292 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002293 }
2294 }
2295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297static void tg3_phy_set_wirespeed(struct tg3 *tp)
2298{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002299 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 u32 val;
2301
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002302 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return;
2304
Matt Carlson15ee95c2011-04-20 07:57:40 +00002305 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2306 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002307 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2308 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002311static void tg3_phy_apply_otp(struct tg3 *tp)
2312{
2313 u32 otp, phy;
2314
2315 if (!tp->phy_otp)
2316 return;
2317
2318 otp = tp->phy_otp;
2319
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002320 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002321 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002322
2323 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2324 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2325 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2326
2327 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2328 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2329 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2330
2331 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2332 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2333 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2334
2335 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2336 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2337
2338 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2339 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2340
2341 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2342 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2343 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2344
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002345 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002346}
2347
Nithin Sujir400dfba2013-05-18 06:26:53 +00002348static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2349{
2350 u32 val;
2351 struct ethtool_eee *dest = &tp->eee;
2352
2353 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2354 return;
2355
2356 if (eee)
2357 dest = eee;
2358
2359 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2360 return;
2361
2362 /* Pull eee_active */
2363 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2364 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2365 dest->eee_active = 1;
2366 } else
2367 dest->eee_active = 0;
2368
2369 /* Pull lp advertised settings */
2370 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2371 return;
2372 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2373
2374 /* Pull advertised and eee_enabled settings */
2375 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2376 return;
2377 dest->eee_enabled = !!val;
2378 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2379
2380 /* Pull tx_lpi_enabled */
2381 val = tr32(TG3_CPMU_EEE_MODE);
2382 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2383
2384 /* Pull lpi timer value */
2385 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2386}
2387
Joe Perches953c96e2013-04-09 10:18:14 +00002388static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002389{
2390 u32 val;
2391
2392 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2393 return;
2394
2395 tp->setlpicnt = 0;
2396
2397 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002398 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002399 tp->link_config.active_duplex == DUPLEX_FULL &&
2400 (tp->link_config.active_speed == SPEED_100 ||
2401 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002402 u32 eeectl;
2403
2404 if (tp->link_config.active_speed == SPEED_1000)
2405 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2406 else
2407 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2408
2409 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2410
Nithin Sujir400dfba2013-05-18 06:26:53 +00002411 tg3_eee_pull_config(tp, NULL);
2412 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002413 tp->setlpicnt = 2;
2414 }
2415
2416 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002417 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002418 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002419 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002420 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002421 }
2422
Matt Carlson52b02d02010-10-14 10:37:41 +00002423 val = tr32(TG3_CPMU_EEE_MODE);
2424 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2425 }
2426}
2427
Matt Carlsonb0c59432011-05-19 12:12:48 +00002428static void tg3_phy_eee_enable(struct tg3 *tp)
2429{
2430 u32 val;
2431
2432 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002433 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2434 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002435 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002436 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002437 val = MII_TG3_DSP_TAP26_ALNOKO |
2438 MII_TG3_DSP_TAP26_RMRXSTO;
2439 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002440 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002441 }
2442
2443 val = tr32(TG3_CPMU_EEE_MODE);
2444 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447static int tg3_wait_macro_done(struct tg3 *tp)
2448{
2449 int limit = 100;
2450
2451 while (limit--) {
2452 u32 tmp32;
2453
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002454 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if ((tmp32 & 0x1000) == 0)
2456 break;
2457 }
2458 }
Roel Kluind4675b52009-02-12 16:33:27 -08002459 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return -EBUSY;
2461
2462 return 0;
2463}
2464
2465static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2466{
2467 static const u32 test_pat[4][6] = {
2468 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2469 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2470 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2471 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2472 };
2473 int chan;
2474
2475 for (chan = 0; chan < 4; chan++) {
2476 int i;
2477
2478 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2479 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002480 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 for (i = 0; i < 6; i++)
2483 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2484 test_pat[chan][i]);
2485
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002486 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (tg3_wait_macro_done(tp)) {
2488 *resetp = 1;
2489 return -EBUSY;
2490 }
2491
2492 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2493 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002494 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (tg3_wait_macro_done(tp)) {
2496 *resetp = 1;
2497 return -EBUSY;
2498 }
2499
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002500 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (tg3_wait_macro_done(tp)) {
2502 *resetp = 1;
2503 return -EBUSY;
2504 }
2505
2506 for (i = 0; i < 6; i += 2) {
2507 u32 low, high;
2508
2509 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2510 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2511 tg3_wait_macro_done(tp)) {
2512 *resetp = 1;
2513 return -EBUSY;
2514 }
2515 low &= 0x7fff;
2516 high &= 0x000f;
2517 if (low != test_pat[chan][i] ||
2518 high != test_pat[chan][i+1]) {
2519 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2520 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2521 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2522
2523 return -EBUSY;
2524 }
2525 }
2526 }
2527
2528 return 0;
2529}
2530
2531static int tg3_phy_reset_chanpat(struct tg3 *tp)
2532{
2533 int chan;
2534
2535 for (chan = 0; chan < 4; chan++) {
2536 int i;
2537
2538 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2539 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002540 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 for (i = 0; i < 6; i++)
2542 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002543 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (tg3_wait_macro_done(tp))
2545 return -EBUSY;
2546 }
2547
2548 return 0;
2549}
2550
2551static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2552{
2553 u32 reg32, phy9_orig;
2554 int retries, do_phy_reset, err;
2555
2556 retries = 10;
2557 do_phy_reset = 1;
2558 do {
2559 if (do_phy_reset) {
2560 err = tg3_bmcr_reset(tp);
2561 if (err)
2562 return err;
2563 do_phy_reset = 0;
2564 }
2565
2566 /* Disable transmitter and interrupt. */
2567 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2568 continue;
2569
2570 reg32 |= 0x3000;
2571 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2572
2573 /* Set full-duplex, 1000 mbps. */
2574 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002575 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002578 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 continue;
2580
Matt Carlson221c5632011-06-13 13:39:01 +00002581 tg3_writephy(tp, MII_CTRL1000,
2582 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002584 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002585 if (err)
2586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002589 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2592 if (!err)
2593 break;
2594 } while (--retries);
2595
2596 err = tg3_phy_reset_chanpat(tp);
2597 if (err)
2598 return err;
2599
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002600 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002603 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002605 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Matt Carlson221c5632011-06-13 13:39:01 +00002607 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2610 reg32 &= ~0x3000;
2611 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2612 } else if (!err)
2613 err = -EBUSY;
2614
2615 return err;
2616}
2617
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002618static void tg3_carrier_off(struct tg3 *tp)
2619{
2620 netif_carrier_off(tp->dev);
2621 tp->link_up = false;
2622}
2623
Nithin Sujirce20f162013-04-09 08:48:04 +00002624static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2625{
2626 if (tg3_flag(tp, ENABLE_ASF))
2627 netdev_warn(tp->dev,
2628 "Management side-band traffic will be interrupted during phy settings change\n");
2629}
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631/* This will reset the tigon3 PHY if there is no valid
2632 * link unless the FORCE argument is non-zero.
2633 */
2634static int tg3_phy_reset(struct tg3 *tp)
2635{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002636 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 int err;
2638
Joe Perches41535772013-02-16 11:20:04 +00002639 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002640 val = tr32(GRC_MISC_CFG);
2641 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2642 udelay(40);
2643 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002644 err = tg3_readphy(tp, MII_BMSR, &val);
2645 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (err != 0)
2647 return -EBUSY;
2648
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002649 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002650 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002651 tg3_link_report(tp);
2652 }
2653
Joe Perches41535772013-02-16 11:20:04 +00002654 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2655 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2656 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 err = tg3_phy_reset_5703_4_5(tp);
2658 if (err)
2659 return err;
2660 goto out;
2661 }
2662
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002663 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002664 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2665 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002666 cpmuctrl = tr32(TG3_CPMU_CTRL);
2667 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2668 tw32(TG3_CPMU_CTRL,
2669 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2670 }
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 err = tg3_bmcr_reset(tp);
2673 if (err)
2674 return err;
2675
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002676 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002677 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2678 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002679
2680 tw32(TG3_CPMU_CTRL, cpmuctrl);
2681 }
2682
Joe Perches41535772013-02-16 11:20:04 +00002683 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2684 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002685 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2686 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2687 CPMU_LSPD_1000MB_MACCLK_12_5) {
2688 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2689 udelay(40);
2690 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2691 }
2692 }
2693
Joe Perches63c3a662011-04-26 08:12:10 +00002694 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002695 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002696 return 0;
2697
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002698 tg3_phy_apply_otp(tp);
2699
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002700 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002701 tg3_phy_toggle_apd(tp, true);
2702 else
2703 tg3_phy_toggle_apd(tp, false);
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002706 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002707 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002708 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2709 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002710 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002712
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002713 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002714 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2715 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002717
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002718 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002719 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002720 tg3_phydsp_write(tp, 0x000a, 0x310b);
2721 tg3_phydsp_write(tp, 0x201f, 0x9506);
2722 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002723 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002724 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002725 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002726 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002727 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2728 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2729 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2730 tg3_writephy(tp, MII_TG3_TEST1,
2731 MII_TG3_TEST1_TRIM_EN | 0x4);
2732 } else
2733 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2734
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002735 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002736 }
Michael Chanc424cb22006-04-29 18:56:34 -07002737 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 /* Set Extended packet length bit (bit 14) on all chips that */
2740 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002741 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002743 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002744 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002746 err = tg3_phy_auxctl_read(tp,
2747 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2748 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002749 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2750 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752
2753 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2754 * jumbo frames transmission.
2755 */
Joe Perches63c3a662011-04-26 08:12:10 +00002756 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002757 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002758 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002759 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
2761
Joe Perches41535772013-02-16 11:20:04 +00002762 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002763 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002764 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002765 }
2766
Joe Perches41535772013-02-16 11:20:04 +00002767 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002768 tg3_phydsp_write(tp, 0xffb, 0x4000);
2769
Joe Perches953c96e2013-04-09 10:18:14 +00002770 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 tg3_phy_set_wirespeed(tp);
2772 return 0;
2773}
2774
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002775#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2776#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2777#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2778 TG3_GPIO_MSG_NEED_VAUX)
2779#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2780 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2781 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2782 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2783 (TG3_GPIO_MSG_DRVR_PRES << 12))
2784
2785#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2786 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2787 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2788 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2789 (TG3_GPIO_MSG_NEED_VAUX << 12))
2790
2791static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2792{
2793 u32 status, shift;
2794
Joe Perches41535772013-02-16 11:20:04 +00002795 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2796 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002797 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2798 else
2799 status = tr32(TG3_CPMU_DRV_STATUS);
2800
2801 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2802 status &= ~(TG3_GPIO_MSG_MASK << shift);
2803 status |= (newstat << shift);
2804
Joe Perches41535772013-02-16 11:20:04 +00002805 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2806 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002807 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2808 else
2809 tw32(TG3_CPMU_DRV_STATUS, status);
2810
2811 return status >> TG3_APE_GPIO_MSG_SHIFT;
2812}
2813
Matt Carlson520b2752011-06-13 13:39:02 +00002814static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2815{
2816 if (!tg3_flag(tp, IS_NIC))
2817 return 0;
2818
Joe Perches41535772013-02-16 11:20:04 +00002819 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2820 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2821 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002822 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2823 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002824
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002825 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2826
2827 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2828 TG3_GRC_LCLCTL_PWRSW_DELAY);
2829
2830 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2831 } else {
2832 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2833 TG3_GRC_LCLCTL_PWRSW_DELAY);
2834 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002835
Matt Carlson520b2752011-06-13 13:39:02 +00002836 return 0;
2837}
2838
2839static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2840{
2841 u32 grc_local_ctrl;
2842
2843 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002844 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2845 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002846 return;
2847
2848 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2849
2850 tw32_wait_f(GRC_LOCAL_CTRL,
2851 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2852 TG3_GRC_LCLCTL_PWRSW_DELAY);
2853
2854 tw32_wait_f(GRC_LOCAL_CTRL,
2855 grc_local_ctrl,
2856 TG3_GRC_LCLCTL_PWRSW_DELAY);
2857
2858 tw32_wait_f(GRC_LOCAL_CTRL,
2859 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2860 TG3_GRC_LCLCTL_PWRSW_DELAY);
2861}
2862
2863static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2864{
2865 if (!tg3_flag(tp, IS_NIC))
2866 return;
2867
Joe Perches41535772013-02-16 11:20:04 +00002868 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2869 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002870 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2871 (GRC_LCLCTRL_GPIO_OE0 |
2872 GRC_LCLCTRL_GPIO_OE1 |
2873 GRC_LCLCTRL_GPIO_OE2 |
2874 GRC_LCLCTRL_GPIO_OUTPUT0 |
2875 GRC_LCLCTRL_GPIO_OUTPUT1),
2876 TG3_GRC_LCLCTL_PWRSW_DELAY);
2877 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2878 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2879 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2880 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2881 GRC_LCLCTRL_GPIO_OE1 |
2882 GRC_LCLCTRL_GPIO_OE2 |
2883 GRC_LCLCTRL_GPIO_OUTPUT0 |
2884 GRC_LCLCTRL_GPIO_OUTPUT1 |
2885 tp->grc_local_ctrl;
2886 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2887 TG3_GRC_LCLCTL_PWRSW_DELAY);
2888
2889 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2890 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2891 TG3_GRC_LCLCTL_PWRSW_DELAY);
2892
2893 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2894 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2895 TG3_GRC_LCLCTL_PWRSW_DELAY);
2896 } else {
2897 u32 no_gpio2;
2898 u32 grc_local_ctrl = 0;
2899
2900 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002901 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002902 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2903 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2904 grc_local_ctrl,
2905 TG3_GRC_LCLCTL_PWRSW_DELAY);
2906 }
2907
2908 /* On 5753 and variants, GPIO2 cannot be used. */
2909 no_gpio2 = tp->nic_sram_data_cfg &
2910 NIC_SRAM_DATA_CFG_NO_GPIO2;
2911
2912 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2913 GRC_LCLCTRL_GPIO_OE1 |
2914 GRC_LCLCTRL_GPIO_OE2 |
2915 GRC_LCLCTRL_GPIO_OUTPUT1 |
2916 GRC_LCLCTRL_GPIO_OUTPUT2;
2917 if (no_gpio2) {
2918 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2919 GRC_LCLCTRL_GPIO_OUTPUT2);
2920 }
2921 tw32_wait_f(GRC_LOCAL_CTRL,
2922 tp->grc_local_ctrl | grc_local_ctrl,
2923 TG3_GRC_LCLCTL_PWRSW_DELAY);
2924
2925 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2926
2927 tw32_wait_f(GRC_LOCAL_CTRL,
2928 tp->grc_local_ctrl | grc_local_ctrl,
2929 TG3_GRC_LCLCTL_PWRSW_DELAY);
2930
2931 if (!no_gpio2) {
2932 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2933 tw32_wait_f(GRC_LOCAL_CTRL,
2934 tp->grc_local_ctrl | grc_local_ctrl,
2935 TG3_GRC_LCLCTL_PWRSW_DELAY);
2936 }
2937 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002938}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002939
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002940static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002941{
2942 u32 msg = 0;
2943
2944 /* Serialize power state transitions */
2945 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2946 return;
2947
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002948 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002949 msg = TG3_GPIO_MSG_NEED_VAUX;
2950
2951 msg = tg3_set_function_status(tp, msg);
2952
2953 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2954 goto done;
2955
2956 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2957 tg3_pwrsrc_switch_to_vaux(tp);
2958 else
2959 tg3_pwrsrc_die_with_vmain(tp);
2960
2961done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002962 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002963}
2964
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002965static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
Matt Carlson683644b2011-03-09 16:58:23 +00002967 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Matt Carlson334355a2010-01-20 16:58:10 +00002969 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002970 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return;
2972
Joe Perches41535772013-02-16 11:20:04 +00002973 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2974 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2975 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002976 tg3_frob_aux_power_5717(tp, include_wol ?
2977 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002978 return;
2979 }
2980
2981 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002982 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002984 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002985
Michael Chanbc1c7562006-03-20 17:48:03 -08002986 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002987 if (dev_peer) {
2988 struct tg3 *tp_peer = netdev_priv(dev_peer);
2989
Joe Perches63c3a662011-04-26 08:12:10 +00002990 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002991 return;
2992
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002993 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002994 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002995 need_vaux = true;
2996 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002999 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
3000 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00003001 need_vaux = true;
3002
Matt Carlson520b2752011-06-13 13:39:02 +00003003 if (need_vaux)
3004 tg3_pwrsrc_switch_to_vaux(tp);
3005 else
3006 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003009static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3010{
3011 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3012 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003013 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003014 if (speed != SPEED_10)
3015 return 1;
3016 } else if (speed == SPEED_10)
3017 return 1;
3018
3019 return 0;
3020}
3021
Nithin Sujir44f3b502013-05-13 11:04:15 +00003022static bool tg3_phy_power_bug(struct tg3 *tp)
3023{
3024 switch (tg3_asic_rev(tp)) {
3025 case ASIC_REV_5700:
3026 case ASIC_REV_5704:
3027 return true;
3028 case ASIC_REV_5780:
3029 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3030 return true;
3031 return false;
3032 case ASIC_REV_5717:
3033 if (!tp->pci_fn)
3034 return true;
3035 return false;
3036 case ASIC_REV_5719:
3037 case ASIC_REV_5720:
3038 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3039 !tp->pci_fn)
3040 return true;
3041 return false;
3042 }
3043
3044 return false;
3045}
3046
Nithin Sujir989038e2013-08-30 17:01:36 -07003047static bool tg3_phy_led_bug(struct tg3 *tp)
3048{
3049 switch (tg3_asic_rev(tp)) {
3050 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003051 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003052 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3053 !tp->pci_fn)
3054 return true;
3055 return false;
3056 }
3057
3058 return false;
3059}
3060
Matt Carlson0a459aa2008-11-03 16:54:15 -08003061static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003062{
Matt Carlsonce057f02007-11-12 21:08:03 -08003063 u32 val;
3064
Nithin Sujir942d1af2013-04-09 08:48:07 +00003065 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3066 return;
3067
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003068 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003069 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003070 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3071 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3072
3073 sg_dig_ctrl |=
3074 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3075 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3076 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3077 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003078 return;
Michael Chan51297242007-02-13 12:17:57 -08003079 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003080
Joe Perches41535772013-02-16 11:20:04 +00003081 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003082 tg3_bmcr_reset(tp);
3083 val = tr32(GRC_MISC_CFG);
3084 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3085 udelay(40);
3086 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003087 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003088 u32 phytest;
3089 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3090 u32 phy;
3091
3092 tg3_writephy(tp, MII_ADVERTISE, 0);
3093 tg3_writephy(tp, MII_BMCR,
3094 BMCR_ANENABLE | BMCR_ANRESTART);
3095
3096 tg3_writephy(tp, MII_TG3_FET_TEST,
3097 phytest | MII_TG3_FET_SHADOW_EN);
3098 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3099 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3100 tg3_writephy(tp,
3101 MII_TG3_FET_SHDW_AUXMODE4,
3102 phy);
3103 }
3104 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3105 }
3106 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003107 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003108 if (!tg3_phy_led_bug(tp))
3109 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3110 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003111
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003112 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3113 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3114 MII_TG3_AUXCTL_PCTL_VREG_11V;
3115 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003116 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003117
Michael Chan15c3b692006-03-22 01:06:52 -08003118 /* The PHY should not be powered down on some chips because
3119 * of bugs.
3120 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003121 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003122 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003123
Joe Perches41535772013-02-16 11:20:04 +00003124 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3125 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003126 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3127 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3128 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3129 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3130 }
3131
Michael Chan15c3b692006-03-22 01:06:52 -08003132 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3133}
3134
Matt Carlson3f007892008-11-03 16:51:36 -08003135/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003136static int tg3_nvram_lock(struct tg3 *tp)
3137{
Joe Perches63c3a662011-04-26 08:12:10 +00003138 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003139 int i;
3140
3141 if (tp->nvram_lock_cnt == 0) {
3142 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3143 for (i = 0; i < 8000; i++) {
3144 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3145 break;
3146 udelay(20);
3147 }
3148 if (i == 8000) {
3149 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3150 return -ENODEV;
3151 }
3152 }
3153 tp->nvram_lock_cnt++;
3154 }
3155 return 0;
3156}
3157
3158/* tp->lock is held. */
3159static void tg3_nvram_unlock(struct tg3 *tp)
3160{
Joe Perches63c3a662011-04-26 08:12:10 +00003161 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003162 if (tp->nvram_lock_cnt > 0)
3163 tp->nvram_lock_cnt--;
3164 if (tp->nvram_lock_cnt == 0)
3165 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3166 }
3167}
3168
3169/* tp->lock is held. */
3170static void tg3_enable_nvram_access(struct tg3 *tp)
3171{
Joe Perches63c3a662011-04-26 08:12:10 +00003172 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003173 u32 nvaccess = tr32(NVRAM_ACCESS);
3174
3175 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3176 }
3177}
3178
3179/* tp->lock is held. */
3180static void tg3_disable_nvram_access(struct tg3 *tp)
3181{
Joe Perches63c3a662011-04-26 08:12:10 +00003182 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003183 u32 nvaccess = tr32(NVRAM_ACCESS);
3184
3185 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3186 }
3187}
3188
3189static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3190 u32 offset, u32 *val)
3191{
3192 u32 tmp;
3193 int i;
3194
3195 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3196 return -EINVAL;
3197
3198 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3199 EEPROM_ADDR_DEVID_MASK |
3200 EEPROM_ADDR_READ);
3201 tw32(GRC_EEPROM_ADDR,
3202 tmp |
3203 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3204 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3205 EEPROM_ADDR_ADDR_MASK) |
3206 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3207
3208 for (i = 0; i < 1000; i++) {
3209 tmp = tr32(GRC_EEPROM_ADDR);
3210
3211 if (tmp & EEPROM_ADDR_COMPLETE)
3212 break;
3213 msleep(1);
3214 }
3215 if (!(tmp & EEPROM_ADDR_COMPLETE))
3216 return -EBUSY;
3217
Matt Carlson62cedd12009-04-20 14:52:29 -07003218 tmp = tr32(GRC_EEPROM_DATA);
3219
3220 /*
3221 * The data will always be opposite the native endian
3222 * format. Perform a blind byteswap to compensate.
3223 */
3224 *val = swab32(tmp);
3225
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003226 return 0;
3227}
3228
3229#define NVRAM_CMD_TIMEOUT 10000
3230
3231static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3232{
3233 int i;
3234
3235 tw32(NVRAM_CMD, nvram_cmd);
3236 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3237 udelay(10);
3238 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3239 udelay(10);
3240 break;
3241 }
3242 }
3243
3244 if (i == NVRAM_CMD_TIMEOUT)
3245 return -EBUSY;
3246
3247 return 0;
3248}
3249
3250static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3251{
Joe Perches63c3a662011-04-26 08:12:10 +00003252 if (tg3_flag(tp, NVRAM) &&
3253 tg3_flag(tp, NVRAM_BUFFERED) &&
3254 tg3_flag(tp, FLASH) &&
3255 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003256 (tp->nvram_jedecnum == JEDEC_ATMEL))
3257
3258 addr = ((addr / tp->nvram_pagesize) <<
3259 ATMEL_AT45DB0X1B_PAGE_POS) +
3260 (addr % tp->nvram_pagesize);
3261
3262 return addr;
3263}
3264
3265static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3266{
Joe Perches63c3a662011-04-26 08:12:10 +00003267 if (tg3_flag(tp, NVRAM) &&
3268 tg3_flag(tp, NVRAM_BUFFERED) &&
3269 tg3_flag(tp, FLASH) &&
3270 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003271 (tp->nvram_jedecnum == JEDEC_ATMEL))
3272
3273 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3274 tp->nvram_pagesize) +
3275 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3276
3277 return addr;
3278}
3279
Matt Carlsone4f34112009-02-25 14:25:00 +00003280/* NOTE: Data read in from NVRAM is byteswapped according to
3281 * the byteswapping settings for all other register accesses.
3282 * tg3 devices are BE devices, so on a BE machine, the data
3283 * returned will be exactly as it is seen in NVRAM. On a LE
3284 * machine, the 32-bit value will be byteswapped.
3285 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003286static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3287{
3288 int ret;
3289
Joe Perches63c3a662011-04-26 08:12:10 +00003290 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003291 return tg3_nvram_read_using_eeprom(tp, offset, val);
3292
3293 offset = tg3_nvram_phys_addr(tp, offset);
3294
3295 if (offset > NVRAM_ADDR_MSK)
3296 return -EINVAL;
3297
3298 ret = tg3_nvram_lock(tp);
3299 if (ret)
3300 return ret;
3301
3302 tg3_enable_nvram_access(tp);
3303
3304 tw32(NVRAM_ADDR, offset);
3305 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3306 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3307
3308 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003309 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003310
3311 tg3_disable_nvram_access(tp);
3312
3313 tg3_nvram_unlock(tp);
3314
3315 return ret;
3316}
3317
Matt Carlsona9dc5292009-02-25 14:25:30 +00003318/* Ensures NVRAM data is in bytestream format. */
3319static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003320{
3321 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003322 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003323 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003324 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003325 return res;
3326}
3327
Matt Carlsondbe9b922012-02-13 10:20:09 +00003328static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3329 u32 offset, u32 len, u8 *buf)
3330{
3331 int i, j, rc = 0;
3332 u32 val;
3333
3334 for (i = 0; i < len; i += 4) {
3335 u32 addr;
3336 __be32 data;
3337
3338 addr = offset + i;
3339
3340 memcpy(&data, buf + i, 4);
3341
3342 /*
3343 * The SEEPROM interface expects the data to always be opposite
3344 * the native endian format. We accomplish this by reversing
3345 * all the operations that would have been performed on the
3346 * data from a call to tg3_nvram_read_be32().
3347 */
3348 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3349
3350 val = tr32(GRC_EEPROM_ADDR);
3351 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3352
3353 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3354 EEPROM_ADDR_READ);
3355 tw32(GRC_EEPROM_ADDR, val |
3356 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3357 (addr & EEPROM_ADDR_ADDR_MASK) |
3358 EEPROM_ADDR_START |
3359 EEPROM_ADDR_WRITE);
3360
3361 for (j = 0; j < 1000; j++) {
3362 val = tr32(GRC_EEPROM_ADDR);
3363
3364 if (val & EEPROM_ADDR_COMPLETE)
3365 break;
3366 msleep(1);
3367 }
3368 if (!(val & EEPROM_ADDR_COMPLETE)) {
3369 rc = -EBUSY;
3370 break;
3371 }
3372 }
3373
3374 return rc;
3375}
3376
3377/* offset and length are dword aligned */
3378static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3379 u8 *buf)
3380{
3381 int ret = 0;
3382 u32 pagesize = tp->nvram_pagesize;
3383 u32 pagemask = pagesize - 1;
3384 u32 nvram_cmd;
3385 u8 *tmp;
3386
3387 tmp = kmalloc(pagesize, GFP_KERNEL);
3388 if (tmp == NULL)
3389 return -ENOMEM;
3390
3391 while (len) {
3392 int j;
3393 u32 phy_addr, page_off, size;
3394
3395 phy_addr = offset & ~pagemask;
3396
3397 for (j = 0; j < pagesize; j += 4) {
3398 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3399 (__be32 *) (tmp + j));
3400 if (ret)
3401 break;
3402 }
3403 if (ret)
3404 break;
3405
3406 page_off = offset & pagemask;
3407 size = pagesize;
3408 if (len < size)
3409 size = len;
3410
3411 len -= size;
3412
3413 memcpy(tmp + page_off, buf, size);
3414
3415 offset = offset + (pagesize - page_off);
3416
3417 tg3_enable_nvram_access(tp);
3418
3419 /*
3420 * Before we can erase the flash page, we need
3421 * to issue a special "write enable" command.
3422 */
3423 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3424
3425 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3426 break;
3427
3428 /* Erase the target page */
3429 tw32(NVRAM_ADDR, phy_addr);
3430
3431 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3432 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3433
3434 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3435 break;
3436
3437 /* Issue another write enable to start the write. */
3438 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3439
3440 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3441 break;
3442
3443 for (j = 0; j < pagesize; j += 4) {
3444 __be32 data;
3445
3446 data = *((__be32 *) (tmp + j));
3447
3448 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3449
3450 tw32(NVRAM_ADDR, phy_addr + j);
3451
3452 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3453 NVRAM_CMD_WR;
3454
3455 if (j == 0)
3456 nvram_cmd |= NVRAM_CMD_FIRST;
3457 else if (j == (pagesize - 4))
3458 nvram_cmd |= NVRAM_CMD_LAST;
3459
3460 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3461 if (ret)
3462 break;
3463 }
3464 if (ret)
3465 break;
3466 }
3467
3468 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3469 tg3_nvram_exec_cmd(tp, nvram_cmd);
3470
3471 kfree(tmp);
3472
3473 return ret;
3474}
3475
3476/* offset and length are dword aligned */
3477static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3478 u8 *buf)
3479{
3480 int i, ret = 0;
3481
3482 for (i = 0; i < len; i += 4, offset += 4) {
3483 u32 page_off, phy_addr, nvram_cmd;
3484 __be32 data;
3485
3486 memcpy(&data, buf + i, 4);
3487 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3488
3489 page_off = offset % tp->nvram_pagesize;
3490
3491 phy_addr = tg3_nvram_phys_addr(tp, offset);
3492
Matt Carlsondbe9b922012-02-13 10:20:09 +00003493 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3494
3495 if (page_off == 0 || i == 0)
3496 nvram_cmd |= NVRAM_CMD_FIRST;
3497 if (page_off == (tp->nvram_pagesize - 4))
3498 nvram_cmd |= NVRAM_CMD_LAST;
3499
3500 if (i == (len - 4))
3501 nvram_cmd |= NVRAM_CMD_LAST;
3502
Matt Carlson42278222012-02-13 15:20:11 +00003503 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3504 !tg3_flag(tp, FLASH) ||
3505 !tg3_flag(tp, 57765_PLUS))
3506 tw32(NVRAM_ADDR, phy_addr);
3507
Joe Perches41535772013-02-16 11:20:04 +00003508 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003509 !tg3_flag(tp, 5755_PLUS) &&
3510 (tp->nvram_jedecnum == JEDEC_ST) &&
3511 (nvram_cmd & NVRAM_CMD_FIRST)) {
3512 u32 cmd;
3513
3514 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3515 ret = tg3_nvram_exec_cmd(tp, cmd);
3516 if (ret)
3517 break;
3518 }
3519 if (!tg3_flag(tp, FLASH)) {
3520 /* We always do complete word writes to eeprom. */
3521 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3522 }
3523
3524 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3525 if (ret)
3526 break;
3527 }
3528 return ret;
3529}
3530
3531/* offset and length are dword aligned */
3532static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3533{
3534 int ret;
3535
3536 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3537 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3538 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3539 udelay(40);
3540 }
3541
3542 if (!tg3_flag(tp, NVRAM)) {
3543 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3544 } else {
3545 u32 grc_mode;
3546
3547 ret = tg3_nvram_lock(tp);
3548 if (ret)
3549 return ret;
3550
3551 tg3_enable_nvram_access(tp);
3552 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3553 tw32(NVRAM_WRITE1, 0x406);
3554
3555 grc_mode = tr32(GRC_MODE);
3556 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3557
3558 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3559 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3560 buf);
3561 } else {
3562 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3563 buf);
3564 }
3565
3566 grc_mode = tr32(GRC_MODE);
3567 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3568
3569 tg3_disable_nvram_access(tp);
3570 tg3_nvram_unlock(tp);
3571 }
3572
3573 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3574 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3575 udelay(40);
3576 }
3577
3578 return ret;
3579}
3580
Matt Carlson997b4f12011-08-31 11:44:53 +00003581#define RX_CPU_SCRATCH_BASE 0x30000
3582#define RX_CPU_SCRATCH_SIZE 0x04000
3583#define TX_CPU_SCRATCH_BASE 0x34000
3584#define TX_CPU_SCRATCH_SIZE 0x04000
3585
3586/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003587static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003588{
3589 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003590 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003591
Nithin Sujir837c45b2013-03-06 17:02:30 +00003592 for (i = 0; i < iters; i++) {
3593 tw32(cpu_base + CPU_STATE, 0xffffffff);
3594 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3595 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3596 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003597 if (pci_channel_offline(tp->pdev))
3598 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003599 }
3600
3601 return (i == iters) ? -EBUSY : 0;
3602}
3603
3604/* tp->lock is held. */
3605static int tg3_rxcpu_pause(struct tg3 *tp)
3606{
3607 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3608
3609 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3610 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3611 udelay(10);
3612
3613 return rc;
3614}
3615
3616/* tp->lock is held. */
3617static int tg3_txcpu_pause(struct tg3 *tp)
3618{
3619 return tg3_pause_cpu(tp, TX_CPU_BASE);
3620}
3621
3622/* tp->lock is held. */
3623static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3624{
3625 tw32(cpu_base + CPU_STATE, 0xffffffff);
3626 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3627}
3628
3629/* tp->lock is held. */
3630static void tg3_rxcpu_resume(struct tg3 *tp)
3631{
3632 tg3_resume_cpu(tp, RX_CPU_BASE);
3633}
3634
3635/* tp->lock is held. */
3636static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3637{
3638 int rc;
3639
3640 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003641
Joe Perches41535772013-02-16 11:20:04 +00003642 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003643 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3644
3645 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3646 return 0;
3647 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003648 if (cpu_base == RX_CPU_BASE) {
3649 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003650 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003651 /*
3652 * There is only an Rx CPU for the 5750 derivative in the
3653 * BCM4785.
3654 */
3655 if (tg3_flag(tp, IS_SSB_CORE))
3656 return 0;
3657
Nithin Sujir837c45b2013-03-06 17:02:30 +00003658 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003659 }
3660
Nithin Sujir837c45b2013-03-06 17:02:30 +00003661 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003662 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003663 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003664 return -ENODEV;
3665 }
3666
3667 /* Clear firmware's nvram arbitration. */
3668 if (tg3_flag(tp, NVRAM))
3669 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3670 return 0;
3671}
3672
Nithin Sujir31f11a92013-03-06 17:02:33 +00003673static int tg3_fw_data_len(struct tg3 *tp,
3674 const struct tg3_firmware_hdr *fw_hdr)
3675{
3676 int fw_len;
3677
3678 /* Non fragmented firmware have one firmware header followed by a
3679 * contiguous chunk of data to be written. The length field in that
3680 * header is not the length of data to be written but the complete
3681 * length of the bss. The data length is determined based on
3682 * tp->fw->size minus headers.
3683 *
3684 * Fragmented firmware have a main header followed by multiple
3685 * fragments. Each fragment is identical to non fragmented firmware
3686 * with a firmware header followed by a contiguous chunk of data. In
3687 * the main header, the length field is unused and set to 0xffffffff.
3688 * In each fragment header the length is the entire size of that
3689 * fragment i.e. fragment data + header length. Data length is
3690 * therefore length field in the header minus TG3_FW_HDR_LEN.
3691 */
3692 if (tp->fw_len == 0xffffffff)
3693 fw_len = be32_to_cpu(fw_hdr->len);
3694 else
3695 fw_len = tp->fw->size;
3696
3697 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3698}
3699
Matt Carlson997b4f12011-08-31 11:44:53 +00003700/* tp->lock is held. */
3701static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3702 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003703 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003704{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003705 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003706 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003707 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003708
3709 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3710 netdev_err(tp->dev,
3711 "%s: Trying to load TX cpu firmware which is 5705\n",
3712 __func__);
3713 return -EINVAL;
3714 }
3715
Nithin Sujirc4dab502013-03-06 17:02:34 +00003716 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003717 write_op = tg3_write_mem;
3718 else
3719 write_op = tg3_write_indirect_reg32;
3720
Nithin Sujirc4dab502013-03-06 17:02:34 +00003721 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3722 /* It is possible that bootcode is still loading at this point.
3723 * Get the nvram lock first before halting the cpu.
3724 */
3725 int lock_err = tg3_nvram_lock(tp);
3726 err = tg3_halt_cpu(tp, cpu_base);
3727 if (!lock_err)
3728 tg3_nvram_unlock(tp);
3729 if (err)
3730 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003731
Nithin Sujirc4dab502013-03-06 17:02:34 +00003732 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3733 write_op(tp, cpu_scratch_base + i, 0);
3734 tw32(cpu_base + CPU_STATE, 0xffffffff);
3735 tw32(cpu_base + CPU_MODE,
3736 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3737 } else {
3738 /* Subtract additional main header for fragmented firmware and
3739 * advance to the first fragment
3740 */
3741 total_len -= TG3_FW_HDR_LEN;
3742 fw_hdr++;
3743 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003744
Nithin Sujir31f11a92013-03-06 17:02:33 +00003745 do {
3746 u32 *fw_data = (u32 *)(fw_hdr + 1);
3747 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3748 write_op(tp, cpu_scratch_base +
3749 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3750 (i * sizeof(u32)),
3751 be32_to_cpu(fw_data[i]));
3752
3753 total_len -= be32_to_cpu(fw_hdr->len);
3754
3755 /* Advance to next fragment */
3756 fw_hdr = (struct tg3_firmware_hdr *)
3757 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3758 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003759
3760 err = 0;
3761
3762out:
3763 return err;
3764}
3765
3766/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003767static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3768{
3769 int i;
3770 const int iters = 5;
3771
3772 tw32(cpu_base + CPU_STATE, 0xffffffff);
3773 tw32_f(cpu_base + CPU_PC, pc);
3774
3775 for (i = 0; i < iters; i++) {
3776 if (tr32(cpu_base + CPU_PC) == pc)
3777 break;
3778 tw32(cpu_base + CPU_STATE, 0xffffffff);
3779 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3780 tw32_f(cpu_base + CPU_PC, pc);
3781 udelay(1000);
3782 }
3783
3784 return (i == iters) ? -EBUSY : 0;
3785}
3786
3787/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003788static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3789{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003790 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003791 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003792
Nithin Sujir77997ea2013-03-06 17:02:32 +00003793 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003794
3795 /* Firmware blob starts with version numbers, followed by
3796 start address and length. We are setting complete length.
3797 length = end_address_of_bss - start_address_of_text.
3798 Remainder is the blob to be loaded contiguously
3799 from start address. */
3800
Matt Carlson997b4f12011-08-31 11:44:53 +00003801 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3802 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003803 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003804 if (err)
3805 return err;
3806
3807 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3808 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003809 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003810 if (err)
3811 return err;
3812
3813 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003814 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3815 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003816 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003817 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3818 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003819 tr32(RX_CPU_BASE + CPU_PC),
3820 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003821 return -ENODEV;
3822 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003823
3824 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003825
3826 return 0;
3827}
3828
Nithin Sujirc4dab502013-03-06 17:02:34 +00003829static int tg3_validate_rxcpu_state(struct tg3 *tp)
3830{
3831 const int iters = 1000;
3832 int i;
3833 u32 val;
3834
3835 /* Wait for boot code to complete initialization and enter service
3836 * loop. It is then safe to download service patches
3837 */
3838 for (i = 0; i < iters; i++) {
3839 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3840 break;
3841
3842 udelay(10);
3843 }
3844
3845 if (i == iters) {
3846 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3847 return -EBUSY;
3848 }
3849
3850 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3851 if (val & 0xff) {
3852 netdev_warn(tp->dev,
3853 "Other patches exist. Not downloading EEE patch\n");
3854 return -EEXIST;
3855 }
3856
3857 return 0;
3858}
3859
3860/* tp->lock is held. */
3861static void tg3_load_57766_firmware(struct tg3 *tp)
3862{
3863 struct tg3_firmware_hdr *fw_hdr;
3864
3865 if (!tg3_flag(tp, NO_NVRAM))
3866 return;
3867
3868 if (tg3_validate_rxcpu_state(tp))
3869 return;
3870
3871 if (!tp->fw)
3872 return;
3873
3874 /* This firmware blob has a different format than older firmware
3875 * releases as given below. The main difference is we have fragmented
3876 * data to be written to non-contiguous locations.
3877 *
3878 * In the beginning we have a firmware header identical to other
3879 * firmware which consists of version, base addr and length. The length
3880 * here is unused and set to 0xffffffff.
3881 *
3882 * This is followed by a series of firmware fragments which are
3883 * individually identical to previous firmware. i.e. they have the
3884 * firmware header and followed by data for that fragment. The version
3885 * field of the individual fragment header is unused.
3886 */
3887
3888 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3889 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3890 return;
3891
3892 if (tg3_rxcpu_pause(tp))
3893 return;
3894
3895 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3896 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3897
3898 tg3_rxcpu_resume(tp);
3899}
3900
Matt Carlson997b4f12011-08-31 11:44:53 +00003901/* tp->lock is held. */
3902static int tg3_load_tso_firmware(struct tg3 *tp)
3903{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003904 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003905 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003906 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003907
Matt Carlson1caf13e2013-03-06 17:02:29 +00003908 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003909 return 0;
3910
Nithin Sujir77997ea2013-03-06 17:02:32 +00003911 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003912
3913 /* Firmware blob starts with version numbers, followed by
3914 start address and length. We are setting complete length.
3915 length = end_address_of_bss - start_address_of_text.
3916 Remainder is the blob to be loaded contiguously
3917 from start address. */
3918
Matt Carlson997b4f12011-08-31 11:44:53 +00003919 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003920
Joe Perches41535772013-02-16 11:20:04 +00003921 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003922 cpu_base = RX_CPU_BASE;
3923 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3924 } else {
3925 cpu_base = TX_CPU_BASE;
3926 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3927 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3928 }
3929
3930 err = tg3_load_firmware_cpu(tp, cpu_base,
3931 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003932 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003933 if (err)
3934 return err;
3935
3936 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003937 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3938 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003939 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003940 netdev_err(tp->dev,
3941 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003942 __func__, tr32(cpu_base + CPU_PC),
3943 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003944 return -ENODEV;
3945 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003946
3947 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003948 return 0;
3949}
3950
3951
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003952/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003953static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003954{
3955 u32 addr_high, addr_low;
3956 int i;
3957
3958 addr_high = ((tp->dev->dev_addr[0] << 8) |
3959 tp->dev->dev_addr[1]);
3960 addr_low = ((tp->dev->dev_addr[2] << 24) |
3961 (tp->dev->dev_addr[3] << 16) |
3962 (tp->dev->dev_addr[4] << 8) |
3963 (tp->dev->dev_addr[5] << 0));
3964 for (i = 0; i < 4; i++) {
3965 if (i == 1 && skip_mac_1)
3966 continue;
3967 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3968 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3969 }
3970
Joe Perches41535772013-02-16 11:20:04 +00003971 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3972 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003973 for (i = 0; i < 12; i++) {
3974 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3975 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3976 }
3977 }
3978
3979 addr_high = (tp->dev->dev_addr[0] +
3980 tp->dev->dev_addr[1] +
3981 tp->dev->dev_addr[2] +
3982 tp->dev->dev_addr[3] +
3983 tp->dev->dev_addr[4] +
3984 tp->dev->dev_addr[5]) &
3985 TX_BACKOFF_SEED_MASK;
3986 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3987}
3988
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003989static void tg3_enable_register_access(struct tg3 *tp)
3990{
3991 /*
3992 * Make sure register accesses (indirect or otherwise) will function
3993 * correctly.
3994 */
3995 pci_write_config_dword(tp->pdev,
3996 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3997}
3998
3999static int tg3_power_up(struct tg3 *tp)
4000{
Matt Carlsonbed98292011-07-13 09:27:29 +00004001 int err;
4002
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004003 tg3_enable_register_access(tp);
4004
Matt Carlsonbed98292011-07-13 09:27:29 +00004005 err = pci_set_power_state(tp->pdev, PCI_D0);
4006 if (!err) {
4007 /* Switch out of Vaux if it is a NIC */
4008 tg3_pwrsrc_switch_to_vmain(tp);
4009 } else {
4010 netdev_err(tp->dev, "Transition to D0 failed\n");
4011 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004012
Matt Carlsonbed98292011-07-13 09:27:29 +00004013 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004014}
4015
Joe Perches953c96e2013-04-09 10:18:14 +00004016static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004017
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004018static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
4020 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004021 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004023 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004024
4025 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004026 if (tg3_flag(tp, CLKREQ_BUG))
4027 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4028 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4031 tw32(TG3PCI_MISC_HOST_CTRL,
4032 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4033
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004034 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004035 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004036
Joe Perches63c3a662011-04-26 08:12:10 +00004037 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004038 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004039 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00004040 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004041 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004042 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004043
Hauke Mehrtensead24022013-09-28 23:15:26 +02004044 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004045
Matt Carlson80096062010-08-02 11:26:06 +00004046 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004047
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004048 tp->link_config.speed = phydev->speed;
4049 tp->link_config.duplex = phydev->duplex;
4050 tp->link_config.autoneg = phydev->autoneg;
4051 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004052
4053 advertising = ADVERTISED_TP |
4054 ADVERTISED_Pause |
4055 ADVERTISED_Autoneg |
4056 ADVERTISED_10baseT_Half;
4057
Joe Perches63c3a662011-04-26 08:12:10 +00004058 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4059 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004060 advertising |=
4061 ADVERTISED_100baseT_Half |
4062 ADVERTISED_100baseT_Full |
4063 ADVERTISED_10baseT_Full;
4064 else
4065 advertising |= ADVERTISED_10baseT_Full;
4066 }
4067
4068 phydev->advertising = advertising;
4069
4070 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004071
4072 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004073 if (phyid != PHY_ID_BCMAC131) {
4074 phyid &= PHY_BCM_OUI_MASK;
4075 if (phyid == PHY_BCM_OUI_1 ||
4076 phyid == PHY_BCM_OUI_2 ||
4077 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004078 do_low_power = true;
4079 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004080 }
Matt Carlsondd477002008-05-25 23:45:58 -07004081 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004082 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004083
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004084 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004085 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Matt Carlson2855b9f2012-02-13 15:20:14 +00004087 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004088 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 }
4090
Joe Perches41535772013-02-16 11:20:04 +00004091 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004092 u32 val;
4093
4094 val = tr32(GRC_VCPU_EXT_CTRL);
4095 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004096 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004097 int i;
4098 u32 val;
4099
4100 for (i = 0; i < 200; i++) {
4101 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4102 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4103 break;
4104 msleep(1);
4105 }
4106 }
Joe Perches63c3a662011-04-26 08:12:10 +00004107 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004108 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4109 WOL_DRV_STATE_SHUTDOWN |
4110 WOL_DRV_WOL |
4111 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004112
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004113 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 u32 mac_mode;
4115
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004116 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004117 if (do_low_power &&
4118 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4119 tg3_phy_auxctl_write(tp,
4120 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4121 MII_TG3_AUXCTL_PCTL_WOL_EN |
4122 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4123 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004124 udelay(40);
4125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004127 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004128 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004129 else if (tp->phy_flags &
4130 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4131 if (tp->link_config.active_speed == SPEED_1000)
4132 mac_mode = MAC_MODE_PORT_MODE_GMII;
4133 else
4134 mac_mode = MAC_MODE_PORT_MODE_MII;
4135 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004136 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004138 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004139 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004140 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004141 SPEED_100 : SPEED_10;
4142 if (tg3_5700_link_polarity(tp, speed))
4143 mac_mode |= MAC_MODE_LINK_POLARITY;
4144 else
4145 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 } else {
4148 mac_mode = MAC_MODE_PORT_MODE_TBI;
4149 }
4150
Joe Perches63c3a662011-04-26 08:12:10 +00004151 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 tw32(MAC_LED_CTRL, tp->led_ctrl);
4153
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004154 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004155 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4156 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004157 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Joe Perches63c3a662011-04-26 08:12:10 +00004159 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004160 mac_mode |= MAC_MODE_APE_TX_EN |
4161 MAC_MODE_APE_RX_EN |
4162 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004163
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 tw32_f(MAC_MODE, mac_mode);
4165 udelay(100);
4166
4167 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4168 udelay(10);
4169 }
4170
Joe Perches63c3a662011-04-26 08:12:10 +00004171 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004172 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4173 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 u32 base_val;
4175
4176 base_val = tp->pci_clock_ctrl;
4177 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4178 CLOCK_CTRL_TXCLK_DISABLE);
4179
Michael Chanb401e9e2005-12-19 16:27:04 -08004180 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4181 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004182 } else if (tg3_flag(tp, 5780_CLASS) ||
4183 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004184 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004185 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004186 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 u32 newbits1, newbits2;
4188
Joe Perches41535772013-02-16 11:20:04 +00004189 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4190 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4192 CLOCK_CTRL_TXCLK_DISABLE |
4193 CLOCK_CTRL_ALTCLK);
4194 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004195 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 newbits1 = CLOCK_CTRL_625_CORE;
4197 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4198 } else {
4199 newbits1 = CLOCK_CTRL_ALTCLK;
4200 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4201 }
4202
Michael Chanb401e9e2005-12-19 16:27:04 -08004203 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4204 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Michael Chanb401e9e2005-12-19 16:27:04 -08004206 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4207 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Joe Perches63c3a662011-04-26 08:12:10 +00004209 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 u32 newbits3;
4211
Joe Perches41535772013-02-16 11:20:04 +00004212 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4213 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4215 CLOCK_CTRL_TXCLK_DISABLE |
4216 CLOCK_CTRL_44MHZ_CORE);
4217 } else {
4218 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4219 }
4220
Michael Chanb401e9e2005-12-19 16:27:04 -08004221 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4222 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 }
4224 }
4225
Joe Perches63c3a662011-04-26 08:12:10 +00004226 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004227 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004228
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004229 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
4231 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004232 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004233 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4234 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 u32 val = tr32(0x7d00);
4236
4237 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4238 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004239 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004240 int err;
4241
4242 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004244 if (!err)
4245 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 }
4248
Michael Chanbbadf502006-04-06 21:46:34 -07004249 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4250
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004251 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return 0;
4254}
4255
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004256static void tg3_power_down(struct tg3 *tp)
4257{
Joe Perches63c3a662011-04-26 08:12:10 +00004258 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004259 pci_set_power_state(tp->pdev, PCI_D3hot);
4260}
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4263{
4264 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4265 case MII_TG3_AUX_STAT_10HALF:
4266 *speed = SPEED_10;
4267 *duplex = DUPLEX_HALF;
4268 break;
4269
4270 case MII_TG3_AUX_STAT_10FULL:
4271 *speed = SPEED_10;
4272 *duplex = DUPLEX_FULL;
4273 break;
4274
4275 case MII_TG3_AUX_STAT_100HALF:
4276 *speed = SPEED_100;
4277 *duplex = DUPLEX_HALF;
4278 break;
4279
4280 case MII_TG3_AUX_STAT_100FULL:
4281 *speed = SPEED_100;
4282 *duplex = DUPLEX_FULL;
4283 break;
4284
4285 case MII_TG3_AUX_STAT_1000HALF:
4286 *speed = SPEED_1000;
4287 *duplex = DUPLEX_HALF;
4288 break;
4289
4290 case MII_TG3_AUX_STAT_1000FULL:
4291 *speed = SPEED_1000;
4292 *duplex = DUPLEX_FULL;
4293 break;
4294
4295 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004296 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004297 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4298 SPEED_10;
4299 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4300 DUPLEX_HALF;
4301 break;
4302 }
Matt Carlsone7405222012-02-13 15:20:16 +00004303 *speed = SPEED_UNKNOWN;
4304 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
Matt Carlson42b64a42011-05-19 12:12:49 +00004309static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Matt Carlson42b64a42011-05-19 12:12:49 +00004311 int err = 0;
4312 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Matt Carlson42b64a42011-05-19 12:12:49 +00004314 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004315 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004316 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Matt Carlson42b64a42011-05-19 12:12:49 +00004318 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4319 if (err)
4320 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Matt Carlson4f272092011-12-14 11:09:57 +00004322 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4323 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004324
Joe Perches41535772013-02-16 11:20:04 +00004325 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4326 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004327 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004328
Matt Carlson4f272092011-12-14 11:09:57 +00004329 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4330 if (err)
4331 goto done;
4332 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004333
Matt Carlson42b64a42011-05-19 12:12:49 +00004334 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4335 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Matt Carlson42b64a42011-05-19 12:12:49 +00004337 tw32(TG3_CPMU_EEE_MODE,
4338 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004339
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004340 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004341 if (!err) {
4342 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004343
Matt Carlsona6b68da2010-12-06 08:28:52 +00004344 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004345 /* Advertise 100-BaseTX EEE ability */
4346 if (advertise & ADVERTISED_100baseT_Full)
4347 val |= MDIO_AN_EEE_ADV_100TX;
4348 /* Advertise 1000-BaseT EEE ability */
4349 if (advertise & ADVERTISED_1000baseT_Full)
4350 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004351
4352 if (!tp->eee.eee_enabled) {
4353 val = 0;
4354 tp->eee.advertised = 0;
4355 } else {
4356 tp->eee.advertised = advertise &
4357 (ADVERTISED_100baseT_Full |
4358 ADVERTISED_1000baseT_Full);
4359 }
4360
Matt Carlson42b64a42011-05-19 12:12:49 +00004361 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004362 if (err)
4363 val = 0;
4364
Joe Perches41535772013-02-16 11:20:04 +00004365 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004366 case ASIC_REV_5717:
4367 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004368 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004369 case ASIC_REV_5719:
4370 /* If we advertised any eee advertisements above... */
4371 if (val)
4372 val = MII_TG3_DSP_TAP26_ALNOKO |
4373 MII_TG3_DSP_TAP26_RMRXSTO |
4374 MII_TG3_DSP_TAP26_OPCSINPT;
4375 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4376 /* Fall through */
4377 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004378 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004379 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4380 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4381 MII_TG3_DSP_CH34TP2_HIBW01);
4382 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004383
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004384 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004385 if (!err)
4386 err = err2;
4387 }
4388
4389done:
4390 return err;
4391}
4392
4393static void tg3_phy_copper_begin(struct tg3 *tp)
4394{
Matt Carlsond13ba512012-02-22 12:35:19 +00004395 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4396 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4397 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004398
Nithin Sujir942d1af2013-04-09 08:48:07 +00004399 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4400 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004401 adv = ADVERTISED_10baseT_Half |
4402 ADVERTISED_10baseT_Full;
4403 if (tg3_flag(tp, WOL_SPEED_100MB))
4404 adv |= ADVERTISED_100baseT_Half |
4405 ADVERTISED_100baseT_Full;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004406 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK)
4407 adv |= ADVERTISED_1000baseT_Half |
4408 ADVERTISED_1000baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004409
Matt Carlsond13ba512012-02-22 12:35:19 +00004410 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004411 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004412 adv = tp->link_config.advertising;
4413 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4414 adv &= ~(ADVERTISED_1000baseT_Half |
4415 ADVERTISED_1000baseT_Full);
4416
4417 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004418 }
4419
Matt Carlsond13ba512012-02-22 12:35:19 +00004420 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004421
Nithin Sujir942d1af2013-04-09 08:48:07 +00004422 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4423 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4424 /* Normally during power down we want to autonegotiate
4425 * the lowest possible speed for WOL. However, to avoid
4426 * link flap, we leave it untouched.
4427 */
4428 return;
4429 }
4430
Matt Carlsond13ba512012-02-22 12:35:19 +00004431 tg3_writephy(tp, MII_BMCR,
4432 BMCR_ANENABLE | BMCR_ANRESTART);
4433 } else {
4434 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 u32 bmcr, orig_bmcr;
4436
4437 tp->link_config.active_speed = tp->link_config.speed;
4438 tp->link_config.active_duplex = tp->link_config.duplex;
4439
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004440 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4441 /* With autoneg disabled, 5715 only links up when the
4442 * advertisement register has the configured speed
4443 * enabled.
4444 */
4445 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4446 }
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 bmcr = 0;
4449 switch (tp->link_config.speed) {
4450 default:
4451 case SPEED_10:
4452 break;
4453
4454 case SPEED_100:
4455 bmcr |= BMCR_SPEED100;
4456 break;
4457
4458 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004459 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 if (tp->link_config.duplex == DUPLEX_FULL)
4464 bmcr |= BMCR_FULLDPLX;
4465
4466 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4467 (bmcr != orig_bmcr)) {
4468 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4469 for (i = 0; i < 1500; i++) {
4470 u32 tmp;
4471
4472 udelay(10);
4473 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4474 tg3_readphy(tp, MII_BMSR, &tmp))
4475 continue;
4476 if (!(tmp & BMSR_LSTATUS)) {
4477 udelay(40);
4478 break;
4479 }
4480 }
4481 tg3_writephy(tp, MII_BMCR, bmcr);
4482 udelay(40);
4483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 }
4485}
4486
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004487static int tg3_phy_pull_config(struct tg3 *tp)
4488{
4489 int err;
4490 u32 val;
4491
4492 err = tg3_readphy(tp, MII_BMCR, &val);
4493 if (err)
4494 goto done;
4495
4496 if (!(val & BMCR_ANENABLE)) {
4497 tp->link_config.autoneg = AUTONEG_DISABLE;
4498 tp->link_config.advertising = 0;
4499 tg3_flag_clear(tp, PAUSE_AUTONEG);
4500
4501 err = -EIO;
4502
4503 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4504 case 0:
4505 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4506 goto done;
4507
4508 tp->link_config.speed = SPEED_10;
4509 break;
4510 case BMCR_SPEED100:
4511 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4512 goto done;
4513
4514 tp->link_config.speed = SPEED_100;
4515 break;
4516 case BMCR_SPEED1000:
4517 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4518 tp->link_config.speed = SPEED_1000;
4519 break;
4520 }
4521 /* Fall through */
4522 default:
4523 goto done;
4524 }
4525
4526 if (val & BMCR_FULLDPLX)
4527 tp->link_config.duplex = DUPLEX_FULL;
4528 else
4529 tp->link_config.duplex = DUPLEX_HALF;
4530
4531 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4532
4533 err = 0;
4534 goto done;
4535 }
4536
4537 tp->link_config.autoneg = AUTONEG_ENABLE;
4538 tp->link_config.advertising = ADVERTISED_Autoneg;
4539 tg3_flag_set(tp, PAUSE_AUTONEG);
4540
4541 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4542 u32 adv;
4543
4544 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4545 if (err)
4546 goto done;
4547
4548 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4549 tp->link_config.advertising |= adv | ADVERTISED_TP;
4550
4551 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4552 } else {
4553 tp->link_config.advertising |= ADVERTISED_FIBRE;
4554 }
4555
4556 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4557 u32 adv;
4558
4559 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4560 err = tg3_readphy(tp, MII_CTRL1000, &val);
4561 if (err)
4562 goto done;
4563
4564 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4565 } else {
4566 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4567 if (err)
4568 goto done;
4569
4570 adv = tg3_decode_flowctrl_1000X(val);
4571 tp->link_config.flowctrl = adv;
4572
4573 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4574 adv = mii_adv_to_ethtool_adv_x(val);
4575 }
4576
4577 tp->link_config.advertising |= adv;
4578 }
4579
4580done:
4581 return err;
4582}
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584static int tg3_init_5401phy_dsp(struct tg3 *tp)
4585{
4586 int err;
4587
4588 /* Turn off tap power management. */
4589 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004590 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004592 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4593 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4594 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4595 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4596 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
4598 udelay(40);
4599
4600 return err;
4601}
4602
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004603static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4604{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004605 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004606
4607 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4608 return true;
4609
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004610 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004611
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004612 if (tp->eee.eee_enabled) {
4613 if (tp->eee.advertised != eee.advertised ||
4614 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4615 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4616 return false;
4617 } else {
4618 /* EEE is disabled but we're advertising */
4619 if (eee.advertised)
4620 return false;
4621 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004622
4623 return true;
4624}
4625
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004626static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004628 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004629
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004630 advertising = tp->link_config.advertising;
4631 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004633 advmsk = ADVERTISE_ALL;
4634 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004635 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004636 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004639 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4640 return false;
4641
4642 if ((*lcladv & advmsk) != tgtadv)
4643 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004644
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004645 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 u32 tg3_ctrl;
4647
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004648 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004649
Matt Carlson221c5632011-06-13 13:39:01 +00004650 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004651 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Matt Carlson3198e072012-02-13 15:20:10 +00004653 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004654 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4655 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004656 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4657 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4658 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4659 } else {
4660 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4661 }
4662
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004663 if (tg3_ctrl != tgtadv)
4664 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004666
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004667 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004668}
4669
Matt Carlson859edb22011-12-08 14:40:16 +00004670static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4671{
4672 u32 lpeth = 0;
4673
4674 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4675 u32 val;
4676
4677 if (tg3_readphy(tp, MII_STAT1000, &val))
4678 return false;
4679
4680 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4681 }
4682
4683 if (tg3_readphy(tp, MII_LPA, rmtadv))
4684 return false;
4685
4686 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4687 tp->link_config.rmt_adv = lpeth;
4688
4689 return true;
4690}
4691
Joe Perches953c96e2013-04-09 10:18:14 +00004692static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004693{
4694 if (curr_link_up != tp->link_up) {
4695 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004696 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004697 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004698 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004699 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4700 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4701 }
4702
4703 tg3_link_report(tp);
4704 return true;
4705 }
4706
4707 return false;
4708}
4709
Michael Chan3310e242013-04-09 08:48:05 +00004710static void tg3_clear_mac_status(struct tg3 *tp)
4711{
4712 tw32(MAC_EVENT, 0);
4713
4714 tw32_f(MAC_STATUS,
4715 MAC_STATUS_SYNC_CHANGED |
4716 MAC_STATUS_CFG_CHANGED |
4717 MAC_STATUS_MI_COMPLETION |
4718 MAC_STATUS_LNKSTATE_CHANGED);
4719 udelay(40);
4720}
4721
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004722static void tg3_setup_eee(struct tg3 *tp)
4723{
4724 u32 val;
4725
4726 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4727 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4728 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4729 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4730
4731 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4732
4733 tw32_f(TG3_CPMU_EEE_CTRL,
4734 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4735
4736 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4737 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4738 TG3_CPMU_EEEMD_LPI_IN_RX |
4739 TG3_CPMU_EEEMD_EEE_ENABLE;
4740
4741 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4742 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4743
4744 if (tg3_flag(tp, ENABLE_APE))
4745 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4746
4747 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4748
4749 tw32_f(TG3_CPMU_EEE_DBTMR1,
4750 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4751 (tp->eee.tx_lpi_timer & 0xffff));
4752
4753 tw32_f(TG3_CPMU_EEE_DBTMR2,
4754 TG3_CPMU_DBTMR2_APE_TX_2047US |
4755 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4756}
4757
Joe Perches953c96e2013-04-09 10:18:14 +00004758static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Joe Perches953c96e2013-04-09 10:18:14 +00004760 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004761 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004762 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 u16 current_speed;
4764 u8 current_duplex;
4765 int i, err;
4766
Michael Chan3310e242013-04-09 08:48:05 +00004767 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Matt Carlson8ef21422008-05-02 16:47:53 -07004769 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4770 tw32_f(MAC_MI_MODE,
4771 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4772 udelay(80);
4773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004775 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 /* Some third-party PHYs need to be reset on link going
4778 * down.
4779 */
Joe Perches41535772013-02-16 11:20:04 +00004780 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4781 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4782 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004783 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 tg3_readphy(tp, MII_BMSR, &bmsr);
4785 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4786 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004787 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 }
4789 if (force_reset)
4790 tg3_phy_reset(tp);
4791
Matt Carlson79eb6902010-02-17 15:17:03 +00004792 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 tg3_readphy(tp, MII_BMSR, &bmsr);
4794 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004795 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 bmsr = 0;
4797
4798 if (!(bmsr & BMSR_LSTATUS)) {
4799 err = tg3_init_5401phy_dsp(tp);
4800 if (err)
4801 return err;
4802
4803 tg3_readphy(tp, MII_BMSR, &bmsr);
4804 for (i = 0; i < 1000; i++) {
4805 udelay(10);
4806 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4807 (bmsr & BMSR_LSTATUS)) {
4808 udelay(40);
4809 break;
4810 }
4811 }
4812
Matt Carlson79eb6902010-02-17 15:17:03 +00004813 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4814 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 !(bmsr & BMSR_LSTATUS) &&
4816 tp->link_config.active_speed == SPEED_1000) {
4817 err = tg3_phy_reset(tp);
4818 if (!err)
4819 err = tg3_init_5401phy_dsp(tp);
4820 if (err)
4821 return err;
4822 }
4823 }
Joe Perches41535772013-02-16 11:20:04 +00004824 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4825 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 /* 5701 {A0,B0} CRC bug workaround */
4827 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004828 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4829 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4830 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 }
4832
4833 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004834 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4835 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004837 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004839 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4841
Joe Perches41535772013-02-16 11:20:04 +00004842 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4843 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4845 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4846 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4847 else
4848 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4849 }
4850
Joe Perches953c96e2013-04-09 10:18:14 +00004851 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004852 current_speed = SPEED_UNKNOWN;
4853 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004854 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004855 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004857 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004858 err = tg3_phy_auxctl_read(tp,
4859 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4860 &val);
4861 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004862 tg3_phy_auxctl_write(tp,
4863 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4864 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 goto relink;
4866 }
4867 }
4868
4869 bmsr = 0;
4870 for (i = 0; i < 100; i++) {
4871 tg3_readphy(tp, MII_BMSR, &bmsr);
4872 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4873 (bmsr & BMSR_LSTATUS))
4874 break;
4875 udelay(40);
4876 }
4877
4878 if (bmsr & BMSR_LSTATUS) {
4879 u32 aux_stat, bmcr;
4880
4881 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4882 for (i = 0; i < 2000; i++) {
4883 udelay(10);
4884 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4885 aux_stat)
4886 break;
4887 }
4888
4889 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4890 &current_speed,
4891 &current_duplex);
4892
4893 bmcr = 0;
4894 for (i = 0; i < 200; i++) {
4895 tg3_readphy(tp, MII_BMCR, &bmcr);
4896 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4897 continue;
4898 if (bmcr && bmcr != 0x7fff)
4899 break;
4900 udelay(10);
4901 }
4902
Matt Carlsonef167e22007-12-20 20:10:01 -08004903 lcl_adv = 0;
4904 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Matt Carlsonef167e22007-12-20 20:10:01 -08004906 tp->link_config.active_speed = current_speed;
4907 tp->link_config.active_duplex = current_duplex;
4908
4909 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004910 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4911
Matt Carlsonef167e22007-12-20 20:10:01 -08004912 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004913 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004914 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004915 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004916 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004917
4918 /* EEE settings changes take effect only after a phy
4919 * reset. If we have skipped a reset due to Link Flap
4920 * Avoidance being enabled, do it now.
4921 */
4922 if (!eee_config_ok &&
4923 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004924 !force_reset) {
4925 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004926 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 } else {
4929 if (!(bmcr & BMCR_ANENABLE) &&
4930 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004931 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004932 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
4934 }
4935
Joe Perches953c96e2013-04-09 10:18:14 +00004936 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004937 tp->link_config.active_duplex == DUPLEX_FULL) {
4938 u32 reg, bit;
4939
4940 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4941 reg = MII_TG3_FET_GEN_STAT;
4942 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4943 } else {
4944 reg = MII_TG3_EXT_STAT;
4945 bit = MII_TG3_EXT_STAT_MDIX;
4946 }
4947
4948 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4949 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4950
Matt Carlsonef167e22007-12-20 20:10:01 -08004951 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 }
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004956 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 tg3_phy_copper_begin(tp);
4958
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004959 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004960 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004961 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4962 current_speed = SPEED_1000;
4963 current_duplex = DUPLEX_FULL;
4964 tp->link_config.active_speed = current_speed;
4965 tp->link_config.active_duplex = current_duplex;
4966 }
4967
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004968 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004969 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4970 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004971 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 }
4973
4974 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004975 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 if (tp->link_config.active_speed == SPEED_100 ||
4977 tp->link_config.active_speed == SPEED_10)
4978 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4979 else
4980 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004981 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004982 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4983 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4985
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004986 /* In order for the 5750 core in BCM4785 chip to work properly
4987 * in RGMII mode, the Led Control Register must be set up.
4988 */
4989 if (tg3_flag(tp, RGMII_MODE)) {
4990 u32 led_ctrl = tr32(MAC_LED_CTRL);
4991 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4992
4993 if (tp->link_config.active_speed == SPEED_10)
4994 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4995 else if (tp->link_config.active_speed == SPEED_100)
4996 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4997 LED_CTRL_100MBPS_ON);
4998 else if (tp->link_config.active_speed == SPEED_1000)
4999 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5000 LED_CTRL_1000MBPS_ON);
5001
5002 tw32(MAC_LED_CTRL, led_ctrl);
5003 udelay(40);
5004 }
5005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5007 if (tp->link_config.active_duplex == DUPLEX_HALF)
5008 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5009
Joe Perches41535772013-02-16 11:20:04 +00005010 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005011 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005012 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005014 else
5015 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 }
5017
5018 /* ??? Without this setting Netgear GA302T PHY does not
5019 * ??? send/receive packets...
5020 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005021 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005022 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5024 tw32_f(MAC_MI_MODE, tp->mi_mode);
5025 udelay(80);
5026 }
5027
5028 tw32_f(MAC_MODE, tp->mac_mode);
5029 udelay(40);
5030
Matt Carlson52b02d02010-10-14 10:37:41 +00005031 tg3_phy_eee_adjust(tp, current_link_up);
5032
Joe Perches63c3a662011-04-26 08:12:10 +00005033 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 /* Polled via timer. */
5035 tw32_f(MAC_EVENT, 0);
5036 } else {
5037 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5038 }
5039 udelay(40);
5040
Joe Perches41535772013-02-16 11:20:04 +00005041 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005042 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005044 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 udelay(120);
5046 tw32_f(MAC_STATUS,
5047 (MAC_STATUS_SYNC_CHANGED |
5048 MAC_STATUS_CFG_CHANGED));
5049 udelay(40);
5050 tg3_write_mem(tp,
5051 NIC_SRAM_FIRMWARE_MBOX,
5052 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5053 }
5054
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005055 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005056 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005057 if (tp->link_config.active_speed == SPEED_100 ||
5058 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005059 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5060 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005061 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005062 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5063 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005064 }
5065
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005066 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 return 0;
5069}
5070
5071struct tg3_fiber_aneginfo {
5072 int state;
5073#define ANEG_STATE_UNKNOWN 0
5074#define ANEG_STATE_AN_ENABLE 1
5075#define ANEG_STATE_RESTART_INIT 2
5076#define ANEG_STATE_RESTART 3
5077#define ANEG_STATE_DISABLE_LINK_OK 4
5078#define ANEG_STATE_ABILITY_DETECT_INIT 5
5079#define ANEG_STATE_ABILITY_DETECT 6
5080#define ANEG_STATE_ACK_DETECT_INIT 7
5081#define ANEG_STATE_ACK_DETECT 8
5082#define ANEG_STATE_COMPLETE_ACK_INIT 9
5083#define ANEG_STATE_COMPLETE_ACK 10
5084#define ANEG_STATE_IDLE_DETECT_INIT 11
5085#define ANEG_STATE_IDLE_DETECT 12
5086#define ANEG_STATE_LINK_OK 13
5087#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5088#define ANEG_STATE_NEXT_PAGE_WAIT 15
5089
5090 u32 flags;
5091#define MR_AN_ENABLE 0x00000001
5092#define MR_RESTART_AN 0x00000002
5093#define MR_AN_COMPLETE 0x00000004
5094#define MR_PAGE_RX 0x00000008
5095#define MR_NP_LOADED 0x00000010
5096#define MR_TOGGLE_TX 0x00000020
5097#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5098#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5099#define MR_LP_ADV_SYM_PAUSE 0x00000100
5100#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5101#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5102#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5103#define MR_LP_ADV_NEXT_PAGE 0x00001000
5104#define MR_TOGGLE_RX 0x00002000
5105#define MR_NP_RX 0x00004000
5106
5107#define MR_LINK_OK 0x80000000
5108
5109 unsigned long link_time, cur_time;
5110
5111 u32 ability_match_cfg;
5112 int ability_match_count;
5113
5114 char ability_match, idle_match, ack_match;
5115
5116 u32 txconfig, rxconfig;
5117#define ANEG_CFG_NP 0x00000080
5118#define ANEG_CFG_ACK 0x00000040
5119#define ANEG_CFG_RF2 0x00000020
5120#define ANEG_CFG_RF1 0x00000010
5121#define ANEG_CFG_PS2 0x00000001
5122#define ANEG_CFG_PS1 0x00008000
5123#define ANEG_CFG_HD 0x00004000
5124#define ANEG_CFG_FD 0x00002000
5125#define ANEG_CFG_INVAL 0x00001f06
5126
5127};
5128#define ANEG_OK 0
5129#define ANEG_DONE 1
5130#define ANEG_TIMER_ENAB 2
5131#define ANEG_FAILED -1
5132
5133#define ANEG_STATE_SETTLE_TIME 10000
5134
5135static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5136 struct tg3_fiber_aneginfo *ap)
5137{
Matt Carlson5be73b42007-12-20 20:09:29 -08005138 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 unsigned long delta;
5140 u32 rx_cfg_reg;
5141 int ret;
5142
5143 if (ap->state == ANEG_STATE_UNKNOWN) {
5144 ap->rxconfig = 0;
5145 ap->link_time = 0;
5146 ap->cur_time = 0;
5147 ap->ability_match_cfg = 0;
5148 ap->ability_match_count = 0;
5149 ap->ability_match = 0;
5150 ap->idle_match = 0;
5151 ap->ack_match = 0;
5152 }
5153 ap->cur_time++;
5154
5155 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5156 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5157
5158 if (rx_cfg_reg != ap->ability_match_cfg) {
5159 ap->ability_match_cfg = rx_cfg_reg;
5160 ap->ability_match = 0;
5161 ap->ability_match_count = 0;
5162 } else {
5163 if (++ap->ability_match_count > 1) {
5164 ap->ability_match = 1;
5165 ap->ability_match_cfg = rx_cfg_reg;
5166 }
5167 }
5168 if (rx_cfg_reg & ANEG_CFG_ACK)
5169 ap->ack_match = 1;
5170 else
5171 ap->ack_match = 0;
5172
5173 ap->idle_match = 0;
5174 } else {
5175 ap->idle_match = 1;
5176 ap->ability_match_cfg = 0;
5177 ap->ability_match_count = 0;
5178 ap->ability_match = 0;
5179 ap->ack_match = 0;
5180
5181 rx_cfg_reg = 0;
5182 }
5183
5184 ap->rxconfig = rx_cfg_reg;
5185 ret = ANEG_OK;
5186
Matt Carlson33f401a2010-04-05 10:19:27 +00005187 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 case ANEG_STATE_UNKNOWN:
5189 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5190 ap->state = ANEG_STATE_AN_ENABLE;
5191
5192 /* fallthru */
5193 case ANEG_STATE_AN_ENABLE:
5194 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5195 if (ap->flags & MR_AN_ENABLE) {
5196 ap->link_time = 0;
5197 ap->cur_time = 0;
5198 ap->ability_match_cfg = 0;
5199 ap->ability_match_count = 0;
5200 ap->ability_match = 0;
5201 ap->idle_match = 0;
5202 ap->ack_match = 0;
5203
5204 ap->state = ANEG_STATE_RESTART_INIT;
5205 } else {
5206 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5207 }
5208 break;
5209
5210 case ANEG_STATE_RESTART_INIT:
5211 ap->link_time = ap->cur_time;
5212 ap->flags &= ~(MR_NP_LOADED);
5213 ap->txconfig = 0;
5214 tw32(MAC_TX_AUTO_NEG, 0);
5215 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5216 tw32_f(MAC_MODE, tp->mac_mode);
5217 udelay(40);
5218
5219 ret = ANEG_TIMER_ENAB;
5220 ap->state = ANEG_STATE_RESTART;
5221
5222 /* fallthru */
5223 case ANEG_STATE_RESTART:
5224 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005225 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005227 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 break;
5230
5231 case ANEG_STATE_DISABLE_LINK_OK:
5232 ret = ANEG_DONE;
5233 break;
5234
5235 case ANEG_STATE_ABILITY_DETECT_INIT:
5236 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005237 ap->txconfig = ANEG_CFG_FD;
5238 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5239 if (flowctrl & ADVERTISE_1000XPAUSE)
5240 ap->txconfig |= ANEG_CFG_PS1;
5241 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5242 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5244 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5245 tw32_f(MAC_MODE, tp->mac_mode);
5246 udelay(40);
5247
5248 ap->state = ANEG_STATE_ABILITY_DETECT;
5249 break;
5250
5251 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005252 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 break;
5255
5256 case ANEG_STATE_ACK_DETECT_INIT:
5257 ap->txconfig |= ANEG_CFG_ACK;
5258 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5259 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5260 tw32_f(MAC_MODE, tp->mac_mode);
5261 udelay(40);
5262
5263 ap->state = ANEG_STATE_ACK_DETECT;
5264
5265 /* fallthru */
5266 case ANEG_STATE_ACK_DETECT:
5267 if (ap->ack_match != 0) {
5268 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5269 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5270 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5271 } else {
5272 ap->state = ANEG_STATE_AN_ENABLE;
5273 }
5274 } else if (ap->ability_match != 0 &&
5275 ap->rxconfig == 0) {
5276 ap->state = ANEG_STATE_AN_ENABLE;
5277 }
5278 break;
5279
5280 case ANEG_STATE_COMPLETE_ACK_INIT:
5281 if (ap->rxconfig & ANEG_CFG_INVAL) {
5282 ret = ANEG_FAILED;
5283 break;
5284 }
5285 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5286 MR_LP_ADV_HALF_DUPLEX |
5287 MR_LP_ADV_SYM_PAUSE |
5288 MR_LP_ADV_ASYM_PAUSE |
5289 MR_LP_ADV_REMOTE_FAULT1 |
5290 MR_LP_ADV_REMOTE_FAULT2 |
5291 MR_LP_ADV_NEXT_PAGE |
5292 MR_TOGGLE_RX |
5293 MR_NP_RX);
5294 if (ap->rxconfig & ANEG_CFG_FD)
5295 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5296 if (ap->rxconfig & ANEG_CFG_HD)
5297 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5298 if (ap->rxconfig & ANEG_CFG_PS1)
5299 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5300 if (ap->rxconfig & ANEG_CFG_PS2)
5301 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5302 if (ap->rxconfig & ANEG_CFG_RF1)
5303 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5304 if (ap->rxconfig & ANEG_CFG_RF2)
5305 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5306 if (ap->rxconfig & ANEG_CFG_NP)
5307 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5308
5309 ap->link_time = ap->cur_time;
5310
5311 ap->flags ^= (MR_TOGGLE_TX);
5312 if (ap->rxconfig & 0x0008)
5313 ap->flags |= MR_TOGGLE_RX;
5314 if (ap->rxconfig & ANEG_CFG_NP)
5315 ap->flags |= MR_NP_RX;
5316 ap->flags |= MR_PAGE_RX;
5317
5318 ap->state = ANEG_STATE_COMPLETE_ACK;
5319 ret = ANEG_TIMER_ENAB;
5320 break;
5321
5322 case ANEG_STATE_COMPLETE_ACK:
5323 if (ap->ability_match != 0 &&
5324 ap->rxconfig == 0) {
5325 ap->state = ANEG_STATE_AN_ENABLE;
5326 break;
5327 }
5328 delta = ap->cur_time - ap->link_time;
5329 if (delta > ANEG_STATE_SETTLE_TIME) {
5330 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5331 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5332 } else {
5333 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5334 !(ap->flags & MR_NP_RX)) {
5335 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5336 } else {
5337 ret = ANEG_FAILED;
5338 }
5339 }
5340 }
5341 break;
5342
5343 case ANEG_STATE_IDLE_DETECT_INIT:
5344 ap->link_time = ap->cur_time;
5345 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5346 tw32_f(MAC_MODE, tp->mac_mode);
5347 udelay(40);
5348
5349 ap->state = ANEG_STATE_IDLE_DETECT;
5350 ret = ANEG_TIMER_ENAB;
5351 break;
5352
5353 case ANEG_STATE_IDLE_DETECT:
5354 if (ap->ability_match != 0 &&
5355 ap->rxconfig == 0) {
5356 ap->state = ANEG_STATE_AN_ENABLE;
5357 break;
5358 }
5359 delta = ap->cur_time - ap->link_time;
5360 if (delta > ANEG_STATE_SETTLE_TIME) {
5361 /* XXX another gem from the Broadcom driver :( */
5362 ap->state = ANEG_STATE_LINK_OK;
5363 }
5364 break;
5365
5366 case ANEG_STATE_LINK_OK:
5367 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5368 ret = ANEG_DONE;
5369 break;
5370
5371 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5372 /* ??? unimplemented */
5373 break;
5374
5375 case ANEG_STATE_NEXT_PAGE_WAIT:
5376 /* ??? unimplemented */
5377 break;
5378
5379 default:
5380 ret = ANEG_FAILED;
5381 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
5384 return ret;
5385}
5386
Matt Carlson5be73b42007-12-20 20:09:29 -08005387static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388{
5389 int res = 0;
5390 struct tg3_fiber_aneginfo aninfo;
5391 int status = ANEG_FAILED;
5392 unsigned int tick;
5393 u32 tmp;
5394
5395 tw32_f(MAC_TX_AUTO_NEG, 0);
5396
5397 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5398 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5399 udelay(40);
5400
5401 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5402 udelay(40);
5403
5404 memset(&aninfo, 0, sizeof(aninfo));
5405 aninfo.flags |= MR_AN_ENABLE;
5406 aninfo.state = ANEG_STATE_UNKNOWN;
5407 aninfo.cur_time = 0;
5408 tick = 0;
5409 while (++tick < 195000) {
5410 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5411 if (status == ANEG_DONE || status == ANEG_FAILED)
5412 break;
5413
5414 udelay(1);
5415 }
5416
5417 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5418 tw32_f(MAC_MODE, tp->mac_mode);
5419 udelay(40);
5420
Matt Carlson5be73b42007-12-20 20:09:29 -08005421 *txflags = aninfo.txconfig;
5422 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
5424 if (status == ANEG_DONE &&
5425 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5426 MR_LP_ADV_FULL_DUPLEX)))
5427 res = 1;
5428
5429 return res;
5430}
5431
5432static void tg3_init_bcm8002(struct tg3 *tp)
5433{
5434 u32 mac_status = tr32(MAC_STATUS);
5435 int i;
5436
5437 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005438 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 !(mac_status & MAC_STATUS_PCS_SYNCED))
5440 return;
5441
5442 /* Set PLL lock range. */
5443 tg3_writephy(tp, 0x16, 0x8007);
5444
5445 /* SW reset */
5446 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5447
5448 /* Wait for reset to complete. */
5449 /* XXX schedule_timeout() ... */
5450 for (i = 0; i < 500; i++)
5451 udelay(10);
5452
5453 /* Config mode; select PMA/Ch 1 regs. */
5454 tg3_writephy(tp, 0x10, 0x8411);
5455
5456 /* Enable auto-lock and comdet, select txclk for tx. */
5457 tg3_writephy(tp, 0x11, 0x0a10);
5458
5459 tg3_writephy(tp, 0x18, 0x00a0);
5460 tg3_writephy(tp, 0x16, 0x41ff);
5461
5462 /* Assert and deassert POR. */
5463 tg3_writephy(tp, 0x13, 0x0400);
5464 udelay(40);
5465 tg3_writephy(tp, 0x13, 0x0000);
5466
5467 tg3_writephy(tp, 0x11, 0x0a50);
5468 udelay(40);
5469 tg3_writephy(tp, 0x11, 0x0a10);
5470
5471 /* Wait for signal to stabilize */
5472 /* XXX schedule_timeout() ... */
5473 for (i = 0; i < 15000; i++)
5474 udelay(10);
5475
5476 /* Deselect the channel register so we can read the PHYID
5477 * later.
5478 */
5479 tg3_writephy(tp, 0x10, 0x8011);
5480}
5481
Joe Perches953c96e2013-04-09 10:18:14 +00005482static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005484 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005485 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 u32 sg_dig_ctrl, sg_dig_status;
5487 u32 serdes_cfg, expected_sg_dig_ctrl;
5488 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 serdes_cfg = 0;
5491 expected_sg_dig_ctrl = 0;
5492 workaround = 0;
5493 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005494 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
Joe Perches41535772013-02-16 11:20:04 +00005496 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5497 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 workaround = 1;
5499 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5500 port_a = 0;
5501
5502 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5503 /* preserve bits 20-23 for voltage regulator */
5504 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5505 }
5506
5507 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5508
5509 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005510 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 if (workaround) {
5512 u32 val = serdes_cfg;
5513
5514 if (port_a)
5515 val |= 0xc010000;
5516 else
5517 val |= 0x4010000;
5518 tw32_f(MAC_SERDES_CFG, val);
5519 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005520
5521 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 }
5523 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5524 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005525 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 }
5527 goto out;
5528 }
5529
5530 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005531 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
Matt Carlson82cd3d12007-12-20 20:09:00 -08005533 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5534 if (flowctrl & ADVERTISE_1000XPAUSE)
5535 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5536 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5537 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
5539 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005540 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005541 tp->serdes_counter &&
5542 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5543 MAC_STATUS_RCVD_CFG)) ==
5544 MAC_STATUS_PCS_SYNCED)) {
5545 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005546 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005547 goto out;
5548 }
5549restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 if (workaround)
5551 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005552 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 udelay(5);
5554 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5555
Michael Chan3d3ebe72006-09-27 15:59:15 -07005556 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005557 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5559 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005560 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 mac_status = tr32(MAC_STATUS);
5562
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005563 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005565 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Matt Carlson82cd3d12007-12-20 20:09:00 -08005567 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5568 local_adv |= ADVERTISE_1000XPAUSE;
5569 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5570 local_adv |= ADVERTISE_1000XPSE_ASYM;
5571
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005572 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005573 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005574 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005575 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Matt Carlson859edb22011-12-08 14:40:16 +00005577 tp->link_config.rmt_adv =
5578 mii_adv_to_ethtool_adv_x(remote_adv);
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005581 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005582 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005583 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005584 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005585 if (tp->serdes_counter)
5586 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 else {
5588 if (workaround) {
5589 u32 val = serdes_cfg;
5590
5591 if (port_a)
5592 val |= 0xc010000;
5593 else
5594 val |= 0x4010000;
5595
5596 tw32_f(MAC_SERDES_CFG, val);
5597 }
5598
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005599 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 udelay(40);
5601
5602 /* Link parallel detection - link is up */
5603 /* only if we have PCS_SYNC and not */
5604 /* receiving config code words */
5605 mac_status = tr32(MAC_STATUS);
5606 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5607 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5608 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005609 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005610 tp->phy_flags |=
5611 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005612 tp->serdes_counter =
5613 SERDES_PARALLEL_DET_TIMEOUT;
5614 } else
5615 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 }
5617 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005618 } else {
5619 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005620 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 }
5622
5623out:
5624 return current_link_up;
5625}
5626
Joe Perches953c96e2013-04-09 10:18:14 +00005627static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628{
Joe Perches953c96e2013-04-09 10:18:14 +00005629 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005631 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
5634 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005635 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005637
Matt Carlson5be73b42007-12-20 20:09:29 -08005638 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5639 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
Matt Carlson5be73b42007-12-20 20:09:29 -08005641 if (txflags & ANEG_CFG_PS1)
5642 local_adv |= ADVERTISE_1000XPAUSE;
5643 if (txflags & ANEG_CFG_PS2)
5644 local_adv |= ADVERTISE_1000XPSE_ASYM;
5645
5646 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5647 remote_adv |= LPA_1000XPAUSE;
5648 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5649 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Matt Carlson859edb22011-12-08 14:40:16 +00005651 tp->link_config.rmt_adv =
5652 mii_adv_to_ethtool_adv_x(remote_adv);
5653
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 tg3_setup_flow_control(tp, local_adv, remote_adv);
5655
Joe Perches953c96e2013-04-09 10:18:14 +00005656 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 }
5658 for (i = 0; i < 30; i++) {
5659 udelay(20);
5660 tw32_f(MAC_STATUS,
5661 (MAC_STATUS_SYNC_CHANGED |
5662 MAC_STATUS_CFG_CHANGED));
5663 udelay(40);
5664 if ((tr32(MAC_STATUS) &
5665 (MAC_STATUS_SYNC_CHANGED |
5666 MAC_STATUS_CFG_CHANGED)) == 0)
5667 break;
5668 }
5669
5670 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005671 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5673 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005674 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005676 tg3_setup_flow_control(tp, 0, 0);
5677
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005679 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
5681 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5682 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005683
5684 tw32_f(MAC_MODE, tp->mac_mode);
5685 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 }
5687
5688out:
5689 return current_link_up;
5690}
5691
Joe Perches953c96e2013-04-09 10:18:14 +00005692static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693{
5694 u32 orig_pause_cfg;
5695 u16 orig_active_speed;
5696 u8 orig_active_duplex;
5697 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005698 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 int i;
5700
Matt Carlson8d018622007-12-20 20:05:44 -08005701 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 orig_active_speed = tp->link_config.active_speed;
5703 orig_active_duplex = tp->link_config.active_duplex;
5704
Joe Perches63c3a662011-04-26 08:12:10 +00005705 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005706 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005707 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 mac_status = tr32(MAC_STATUS);
5709 mac_status &= (MAC_STATUS_PCS_SYNCED |
5710 MAC_STATUS_SIGNAL_DET |
5711 MAC_STATUS_CFG_CHANGED |
5712 MAC_STATUS_RCVD_CFG);
5713 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5714 MAC_STATUS_SIGNAL_DET)) {
5715 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5716 MAC_STATUS_CFG_CHANGED));
5717 return 0;
5718 }
5719 }
5720
5721 tw32_f(MAC_TX_AUTO_NEG, 0);
5722
5723 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5724 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5725 tw32_f(MAC_MODE, tp->mac_mode);
5726 udelay(40);
5727
Matt Carlson79eb6902010-02-17 15:17:03 +00005728 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 tg3_init_bcm8002(tp);
5730
5731 /* Enable link change event even when serdes polling. */
5732 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5733 udelay(40);
5734
Joe Perches953c96e2013-04-09 10:18:14 +00005735 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005736 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 mac_status = tr32(MAC_STATUS);
5738
Joe Perches63c3a662011-04-26 08:12:10 +00005739 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5741 else
5742 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5743
Matt Carlson898a56f2009-08-28 14:02:40 +00005744 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005746 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 for (i = 0; i < 100; i++) {
5749 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5750 MAC_STATUS_CFG_CHANGED));
5751 udelay(5);
5752 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005753 MAC_STATUS_CFG_CHANGED |
5754 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 break;
5756 }
5757
5758 mac_status = tr32(MAC_STATUS);
5759 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005760 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005761 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5762 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 tw32_f(MAC_MODE, (tp->mac_mode |
5764 MAC_MODE_SEND_CONFIGS));
5765 udelay(1);
5766 tw32_f(MAC_MODE, tp->mac_mode);
5767 }
5768 }
5769
Joe Perches953c96e2013-04-09 10:18:14 +00005770 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 tp->link_config.active_speed = SPEED_1000;
5772 tp->link_config.active_duplex = DUPLEX_FULL;
5773 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5774 LED_CTRL_LNKLED_OVERRIDE |
5775 LED_CTRL_1000MBPS_ON));
5776 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005777 tp->link_config.active_speed = SPEED_UNKNOWN;
5778 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5780 LED_CTRL_LNKLED_OVERRIDE |
5781 LED_CTRL_TRAFFIC_OVERRIDE));
5782 }
5783
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005784 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005785 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 if (orig_pause_cfg != now_pause_cfg ||
5787 orig_active_speed != tp->link_config.active_speed ||
5788 orig_active_duplex != tp->link_config.active_duplex)
5789 tg3_link_report(tp);
5790 }
5791
5792 return 0;
5793}
5794
Joe Perches953c96e2013-04-09 10:18:14 +00005795static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005796{
Joe Perches953c96e2013-04-09 10:18:14 +00005797 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005798 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005799 u16 current_speed = SPEED_UNKNOWN;
5800 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005801 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005802 u32 local_adv, remote_adv, sgsr;
5803
5804 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5805 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5806 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5807 (sgsr & SERDES_TG3_SGMII_MODE)) {
5808
5809 if (force_reset)
5810 tg3_phy_reset(tp);
5811
5812 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5813
5814 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5815 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5816 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005817 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005818 if (sgsr & SERDES_TG3_SPEED_1000) {
5819 current_speed = SPEED_1000;
5820 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5821 } else if (sgsr & SERDES_TG3_SPEED_100) {
5822 current_speed = SPEED_100;
5823 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5824 } else {
5825 current_speed = SPEED_10;
5826 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5827 }
5828
5829 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5830 current_duplex = DUPLEX_FULL;
5831 else
5832 current_duplex = DUPLEX_HALF;
5833 }
5834
5835 tw32_f(MAC_MODE, tp->mac_mode);
5836 udelay(40);
5837
5838 tg3_clear_mac_status(tp);
5839
5840 goto fiber_setup_done;
5841 }
Michael Chan747e8f82005-07-25 12:33:22 -07005842
5843 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5844 tw32_f(MAC_MODE, tp->mac_mode);
5845 udelay(40);
5846
Michael Chan3310e242013-04-09 08:48:05 +00005847 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005848
5849 if (force_reset)
5850 tg3_phy_reset(tp);
5851
Matt Carlson859edb22011-12-08 14:40:16 +00005852 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005853
5854 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5855 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005856 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005857 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5858 bmsr |= BMSR_LSTATUS;
5859 else
5860 bmsr &= ~BMSR_LSTATUS;
5861 }
Michael Chan747e8f82005-07-25 12:33:22 -07005862
5863 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5864
5865 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005866 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005867 /* do nothing, just check for link up at the end */
5868 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005869 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005870
5871 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005872 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5873 ADVERTISE_1000XPAUSE |
5874 ADVERTISE_1000XPSE_ASYM |
5875 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005876
Matt Carlson28011cf2011-11-16 18:36:59 -05005877 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005878 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005879
Matt Carlson28011cf2011-11-16 18:36:59 -05005880 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5881 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005882 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5883 tg3_writephy(tp, MII_BMCR, bmcr);
5884
5885 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005886 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005887 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005888
5889 return err;
5890 }
5891 } else {
5892 u32 new_bmcr;
5893
5894 bmcr &= ~BMCR_SPEED1000;
5895 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5896
5897 if (tp->link_config.duplex == DUPLEX_FULL)
5898 new_bmcr |= BMCR_FULLDPLX;
5899
5900 if (new_bmcr != bmcr) {
5901 /* BMCR_SPEED1000 is a reserved bit that needs
5902 * to be set on write.
5903 */
5904 new_bmcr |= BMCR_SPEED1000;
5905
5906 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005907 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005908 u32 adv;
5909
5910 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5911 adv &= ~(ADVERTISE_1000XFULL |
5912 ADVERTISE_1000XHALF |
5913 ADVERTISE_SLCT);
5914 tg3_writephy(tp, MII_ADVERTISE, adv);
5915 tg3_writephy(tp, MII_BMCR, bmcr |
5916 BMCR_ANRESTART |
5917 BMCR_ANENABLE);
5918 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005919 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005920 }
5921 tg3_writephy(tp, MII_BMCR, new_bmcr);
5922 bmcr = new_bmcr;
5923 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5924 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005925 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005926 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5927 bmsr |= BMSR_LSTATUS;
5928 else
5929 bmsr &= ~BMSR_LSTATUS;
5930 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005931 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005932 }
5933 }
5934
5935 if (bmsr & BMSR_LSTATUS) {
5936 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005937 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005938 if (bmcr & BMCR_FULLDPLX)
5939 current_duplex = DUPLEX_FULL;
5940 else
5941 current_duplex = DUPLEX_HALF;
5942
Matt Carlsonef167e22007-12-20 20:10:01 -08005943 local_adv = 0;
5944 remote_adv = 0;
5945
Michael Chan747e8f82005-07-25 12:33:22 -07005946 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005947 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005948
5949 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5950 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5951 common = local_adv & remote_adv;
5952 if (common & (ADVERTISE_1000XHALF |
5953 ADVERTISE_1000XFULL)) {
5954 if (common & ADVERTISE_1000XFULL)
5955 current_duplex = DUPLEX_FULL;
5956 else
5957 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005958
5959 tp->link_config.rmt_adv =
5960 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005961 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005962 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005963 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005964 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005965 }
Michael Chan747e8f82005-07-25 12:33:22 -07005966 }
5967 }
5968
Michael Chan85730a62013-04-09 08:48:06 +00005969fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005970 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005971 tg3_setup_flow_control(tp, local_adv, remote_adv);
5972
Michael Chan747e8f82005-07-25 12:33:22 -07005973 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5974 if (tp->link_config.active_duplex == DUPLEX_HALF)
5975 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5976
5977 tw32_f(MAC_MODE, tp->mac_mode);
5978 udelay(40);
5979
5980 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5981
5982 tp->link_config.active_speed = current_speed;
5983 tp->link_config.active_duplex = current_duplex;
5984
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005985 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005986 return err;
5987}
5988
5989static void tg3_serdes_parallel_detect(struct tg3 *tp)
5990{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005991 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005992 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005993 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005994 return;
5995 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005996
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005997 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005998 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5999 u32 bmcr;
6000
6001 tg3_readphy(tp, MII_BMCR, &bmcr);
6002 if (bmcr & BMCR_ANENABLE) {
6003 u32 phy1, phy2;
6004
6005 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006006 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6007 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006008
6009 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006010 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6011 MII_TG3_DSP_EXP1_INT_STAT);
6012 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6013 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006014
6015 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6016 /* We have signal detect and not receiving
6017 * config code words, link is up by parallel
6018 * detection.
6019 */
6020
6021 bmcr &= ~BMCR_ANENABLE;
6022 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6023 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006024 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006025 }
6026 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006027 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006028 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006029 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006030 u32 phy2;
6031
6032 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006033 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6034 MII_TG3_DSP_EXP1_INT_STAT);
6035 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006036 if (phy2 & 0x20) {
6037 u32 bmcr;
6038
6039 /* Config code words received, turn on autoneg. */
6040 tg3_readphy(tp, MII_BMCR, &bmcr);
6041 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6042
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006043 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006044
6045 }
6046 }
6047}
6048
Joe Perches953c96e2013-04-09 10:18:14 +00006049static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006051 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 int err;
6053
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006054 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006056 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006057 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006058 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Joe Perches41535772013-02-16 11:20:04 +00006061 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006062 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006063
6064 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6065 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6066 scale = 65;
6067 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6068 scale = 6;
6069 else
6070 scale = 12;
6071
6072 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6073 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6074 tw32(GRC_MISC_CFG, val);
6075 }
6076
Matt Carlsonf2096f92011-04-05 14:22:48 +00006077 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6078 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006079 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6080 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006081 val |= tr32(MAC_TX_LENGTHS) &
6082 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6083 TX_LENGTHS_CNT_DWN_VAL_MSK);
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 if (tp->link_config.active_speed == SPEED_1000 &&
6086 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006087 tw32(MAC_TX_LENGTHS, val |
6088 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006090 tw32(MAC_TX_LENGTHS, val |
6091 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
Joe Perches63c3a662011-04-26 08:12:10 +00006093 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006094 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006096 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 } else {
6098 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6099 }
6100 }
6101
Joe Perches63c3a662011-04-26 08:12:10 +00006102 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006103 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006104 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006105 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6106 tp->pwrmgmt_thresh;
6107 else
6108 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6109 tw32(PCIE_PWR_MGMT_THRESH, val);
6110 }
6111
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 return err;
6113}
6114
Matt Carlsonbe947302012-12-03 19:36:57 +00006115/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006116static u64 tg3_refclk_read(struct tg3 *tp)
6117{
6118 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6119 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6120}
6121
6122/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006123static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6124{
Nithin Sujir92e64572013-07-29 13:58:38 -07006125 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6126
6127 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006128 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6129 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006130 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006131}
6132
Matt Carlson7d41e492012-12-03 19:36:58 +00006133static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6134static inline void tg3_full_unlock(struct tg3 *tp);
6135static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6136{
6137 struct tg3 *tp = netdev_priv(dev);
6138
6139 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6140 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006141 SOF_TIMESTAMPING_SOFTWARE;
6142
6143 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006144 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006145 SOF_TIMESTAMPING_RX_HARDWARE |
6146 SOF_TIMESTAMPING_RAW_HARDWARE;
6147 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006148
6149 if (tp->ptp_clock)
6150 info->phc_index = ptp_clock_index(tp->ptp_clock);
6151 else
6152 info->phc_index = -1;
6153
6154 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6155
6156 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6157 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6158 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6159 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6160 return 0;
6161}
6162
6163static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6164{
6165 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6166 bool neg_adj = false;
6167 u32 correction = 0;
6168
6169 if (ppb < 0) {
6170 neg_adj = true;
6171 ppb = -ppb;
6172 }
6173
6174 /* Frequency adjustment is performed using hardware with a 24 bit
6175 * accumulator and a programmable correction value. On each clk, the
6176 * correction value gets added to the accumulator and when it
6177 * overflows, the time counter is incremented/decremented.
6178 *
6179 * So conversion from ppb to correction value is
6180 * ppb * (1 << 24) / 1000000000
6181 */
6182 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6183 TG3_EAV_REF_CLK_CORRECT_MASK;
6184
6185 tg3_full_lock(tp, 0);
6186
6187 if (correction)
6188 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6189 TG3_EAV_REF_CLK_CORRECT_EN |
6190 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6191 else
6192 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6193
6194 tg3_full_unlock(tp);
6195
6196 return 0;
6197}
6198
6199static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6200{
6201 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6202
6203 tg3_full_lock(tp, 0);
6204 tp->ptp_adjust += delta;
6205 tg3_full_unlock(tp);
6206
6207 return 0;
6208}
6209
6210static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6211{
6212 u64 ns;
6213 u32 remainder;
6214 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6215
6216 tg3_full_lock(tp, 0);
6217 ns = tg3_refclk_read(tp);
6218 ns += tp->ptp_adjust;
6219 tg3_full_unlock(tp);
6220
6221 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6222 ts->tv_nsec = remainder;
6223
6224 return 0;
6225}
6226
6227static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6228 const struct timespec *ts)
6229{
6230 u64 ns;
6231 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6232
6233 ns = timespec_to_ns(ts);
6234
6235 tg3_full_lock(tp, 0);
6236 tg3_refclk_write(tp, ns);
6237 tp->ptp_adjust = 0;
6238 tg3_full_unlock(tp);
6239
6240 return 0;
6241}
6242
6243static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6244 struct ptp_clock_request *rq, int on)
6245{
Nithin Sujir92e64572013-07-29 13:58:38 -07006246 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6247 u32 clock_ctl;
6248 int rval = 0;
6249
6250 switch (rq->type) {
6251 case PTP_CLK_REQ_PEROUT:
6252 if (rq->perout.index != 0)
6253 return -EINVAL;
6254
6255 tg3_full_lock(tp, 0);
6256 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6257 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6258
6259 if (on) {
6260 u64 nsec;
6261
6262 nsec = rq->perout.start.sec * 1000000000ULL +
6263 rq->perout.start.nsec;
6264
6265 if (rq->perout.period.sec || rq->perout.period.nsec) {
6266 netdev_warn(tp->dev,
6267 "Device supports only a one-shot timesync output, period must be 0\n");
6268 rval = -EINVAL;
6269 goto err_out;
6270 }
6271
6272 if (nsec & (1ULL << 63)) {
6273 netdev_warn(tp->dev,
6274 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6275 rval = -EINVAL;
6276 goto err_out;
6277 }
6278
6279 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6280 tw32(TG3_EAV_WATCHDOG0_MSB,
6281 TG3_EAV_WATCHDOG0_EN |
6282 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6283
6284 tw32(TG3_EAV_REF_CLCK_CTL,
6285 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6286 } else {
6287 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6288 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6289 }
6290
6291err_out:
6292 tg3_full_unlock(tp);
6293 return rval;
6294
6295 default:
6296 break;
6297 }
6298
Matt Carlson7d41e492012-12-03 19:36:58 +00006299 return -EOPNOTSUPP;
6300}
6301
6302static const struct ptp_clock_info tg3_ptp_caps = {
6303 .owner = THIS_MODULE,
6304 .name = "tg3 clock",
6305 .max_adj = 250000000,
6306 .n_alarm = 0,
6307 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006308 .n_per_out = 1,
Matt Carlson7d41e492012-12-03 19:36:58 +00006309 .pps = 0,
6310 .adjfreq = tg3_ptp_adjfreq,
6311 .adjtime = tg3_ptp_adjtime,
6312 .gettime = tg3_ptp_gettime,
6313 .settime = tg3_ptp_settime,
6314 .enable = tg3_ptp_enable,
6315};
6316
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006317static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6318 struct skb_shared_hwtstamps *timestamp)
6319{
6320 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6321 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6322 tp->ptp_adjust);
6323}
6324
Matt Carlsonbe947302012-12-03 19:36:57 +00006325/* tp->lock must be held */
6326static void tg3_ptp_init(struct tg3 *tp)
6327{
6328 if (!tg3_flag(tp, PTP_CAPABLE))
6329 return;
6330
6331 /* Initialize the hardware clock to the system time. */
6332 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6333 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006334 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006335}
6336
6337/* tp->lock must be held */
6338static void tg3_ptp_resume(struct tg3 *tp)
6339{
6340 if (!tg3_flag(tp, PTP_CAPABLE))
6341 return;
6342
6343 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6344 tp->ptp_adjust = 0;
6345}
6346
6347static void tg3_ptp_fini(struct tg3 *tp)
6348{
6349 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6350 return;
6351
Matt Carlson7d41e492012-12-03 19:36:58 +00006352 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006353 tp->ptp_clock = NULL;
6354 tp->ptp_adjust = 0;
6355}
6356
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006357static inline int tg3_irq_sync(struct tg3 *tp)
6358{
6359 return tp->irq_sync;
6360}
6361
Matt Carlson97bd8e42011-04-13 11:05:04 +00006362static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6363{
6364 int i;
6365
6366 dst = (u32 *)((u8 *)dst + off);
6367 for (i = 0; i < len; i += sizeof(u32))
6368 *dst++ = tr32(off + i);
6369}
6370
6371static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6372{
6373 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6374 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6375 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6376 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6377 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6378 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6379 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6380 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6381 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6382 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6383 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6384 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6385 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6386 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6387 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6388 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6389 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6390 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6391 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6392
Joe Perches63c3a662011-04-26 08:12:10 +00006393 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006394 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6395
6396 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6397 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6398 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6399 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6400 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6401 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6402 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6403 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6404
Joe Perches63c3a662011-04-26 08:12:10 +00006405 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006406 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6407 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6408 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6409 }
6410
6411 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6412 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6413 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6414 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6415 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6416
Joe Perches63c3a662011-04-26 08:12:10 +00006417 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006418 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6419}
6420
6421static void tg3_dump_state(struct tg3 *tp)
6422{
6423 int i;
6424 u32 *regs;
6425
6426 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006427 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006428 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006429
Joe Perches63c3a662011-04-26 08:12:10 +00006430 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006431 /* Read up to but not including private PCI registers */
6432 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6433 regs[i / sizeof(u32)] = tr32(i);
6434 } else
6435 tg3_dump_legacy_regs(tp, regs);
6436
6437 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6438 if (!regs[i + 0] && !regs[i + 1] &&
6439 !regs[i + 2] && !regs[i + 3])
6440 continue;
6441
6442 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6443 i * 4,
6444 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6445 }
6446
6447 kfree(regs);
6448
6449 for (i = 0; i < tp->irq_cnt; i++) {
6450 struct tg3_napi *tnapi = &tp->napi[i];
6451
6452 /* SW status block */
6453 netdev_err(tp->dev,
6454 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6455 i,
6456 tnapi->hw_status->status,
6457 tnapi->hw_status->status_tag,
6458 tnapi->hw_status->rx_jumbo_consumer,
6459 tnapi->hw_status->rx_consumer,
6460 tnapi->hw_status->rx_mini_consumer,
6461 tnapi->hw_status->idx[0].rx_producer,
6462 tnapi->hw_status->idx[0].tx_consumer);
6463
6464 netdev_err(tp->dev,
6465 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6466 i,
6467 tnapi->last_tag, tnapi->last_irq_tag,
6468 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6469 tnapi->rx_rcb_ptr,
6470 tnapi->prodring.rx_std_prod_idx,
6471 tnapi->prodring.rx_std_cons_idx,
6472 tnapi->prodring.rx_jmb_prod_idx,
6473 tnapi->prodring.rx_jmb_cons_idx);
6474 }
6475}
6476
Michael Chandf3e6542006-05-26 17:48:07 -07006477/* This is called whenever we suspect that the system chipset is re-
6478 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6479 * is bogus tx completions. We try to recover by setting the
6480 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6481 * in the workqueue.
6482 */
6483static void tg3_tx_recover(struct tg3 *tp)
6484{
Joe Perches63c3a662011-04-26 08:12:10 +00006485 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006486 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6487
Matt Carlson5129c3a2010-04-05 10:19:23 +00006488 netdev_warn(tp->dev,
6489 "The system may be re-ordering memory-mapped I/O "
6490 "cycles to the network device, attempting to recover. "
6491 "Please report the problem to the driver maintainer "
6492 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006493
Joe Perches63c3a662011-04-26 08:12:10 +00006494 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006495}
6496
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006497static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006498{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006499 /* Tell compiler to fetch tx indices from memory. */
6500 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006501 return tnapi->tx_pending -
6502 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006503}
6504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505/* Tigon3 never reports partial packet sends. So we do not
6506 * need special logic to handle SKBs that have not had all
6507 * of their frags sent yet, like SunGEM does.
6508 */
Matt Carlson17375d22009-08-28 14:02:18 +00006509static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510{
Matt Carlson17375d22009-08-28 14:02:18 +00006511 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006512 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006513 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006514 struct netdev_queue *txq;
6515 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006516 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006517
Joe Perches63c3a662011-04-26 08:12:10 +00006518 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006519 index--;
6520
6521 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522
6523 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006524 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006526 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
Michael Chandf3e6542006-05-26 17:48:07 -07006528 if (unlikely(skb == NULL)) {
6529 tg3_tx_recover(tp);
6530 return;
6531 }
6532
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006533 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6534 struct skb_shared_hwtstamps timestamp;
6535 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6536 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6537
6538 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6539
6540 skb_tstamp_tx(skb, &timestamp);
6541 }
6542
Alexander Duyckf4188d82009-12-02 16:48:38 +00006543 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006544 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006545 skb_headlen(skb),
6546 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
6548 ri->skb = NULL;
6549
Matt Carlsone01ee142011-07-27 14:20:50 +00006550 while (ri->fragmented) {
6551 ri->fragmented = false;
6552 sw_idx = NEXT_TX(sw_idx);
6553 ri = &tnapi->tx_buffers[sw_idx];
6554 }
6555
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 sw_idx = NEXT_TX(sw_idx);
6557
6558 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006559 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006560 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6561 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006562
6563 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006564 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006565 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006566 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006567
6568 while (ri->fragmented) {
6569 ri->fragmented = false;
6570 sw_idx = NEXT_TX(sw_idx);
6571 ri = &tnapi->tx_buffers[sw_idx];
6572 }
6573
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 sw_idx = NEXT_TX(sw_idx);
6575 }
6576
Tom Herbert298376d2011-11-28 16:33:30 +00006577 pkts_compl++;
6578 bytes_compl += skb->len;
6579
David S. Millerf47c11e2005-06-24 20:18:35 -07006580 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006581
6582 if (unlikely(tx_bug)) {
6583 tg3_tx_recover(tp);
6584 return;
6585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 }
6587
Tom Herbert5cb917b2012-03-05 19:53:50 +00006588 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006589
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006590 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591
Michael Chan1b2a7202006-08-07 21:46:02 -07006592 /* Need to make the tx_cons update visible to tg3_start_xmit()
6593 * before checking for netif_queue_stopped(). Without the
6594 * memory barrier, there is a small possibility that tg3_start_xmit()
6595 * will miss it and cause the queue to be stopped forever.
6596 */
6597 smp_mb();
6598
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006599 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006600 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006601 __netif_tx_lock(txq, smp_processor_id());
6602 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006603 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006604 netif_tx_wake_queue(txq);
6605 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607}
6608
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006609static void tg3_frag_free(bool is_frag, void *data)
6610{
6611 if (is_frag)
6612 put_page(virt_to_head_page(data));
6613 else
6614 kfree(data);
6615}
6616
Eric Dumazet9205fd92011-11-18 06:47:01 +00006617static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006618{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006619 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6620 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6621
Eric Dumazet9205fd92011-11-18 06:47:01 +00006622 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006623 return;
6624
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006625 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006626 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006627 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006628 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006629}
6630
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006631
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632/* Returns size of skb allocated or < 0 on error.
6633 *
6634 * We only need to fill in the address because the other members
6635 * of the RX descriptor are invariant, see tg3_init_rings.
6636 *
6637 * Note the purposeful assymetry of cpu vs. chip accesses. For
6638 * posting buffers we only dirty the first cache line of the RX
6639 * descriptor (containing the address). Whereas for the RX status
6640 * buffers the cpu only reads the last cacheline of the RX descriptor
6641 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6642 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006643static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006644 u32 opaque_key, u32 dest_idx_unmasked,
6645 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646{
6647 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006648 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006649 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006651 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 switch (opaque_key) {
6654 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006655 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006656 desc = &tpr->rx_std[dest_idx];
6657 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006658 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 break;
6660
6661 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006662 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006663 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006664 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006665 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 break;
6667
6668 default:
6669 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
6672 /* Do not overwrite any of the map or rp information
6673 * until we are sure we can commit to a new buffer.
6674 *
6675 * Callers depend upon this behavior and assume that
6676 * we leave everything unchanged if we fail.
6677 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006678 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6679 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006680 if (skb_size <= PAGE_SIZE) {
6681 data = netdev_alloc_frag(skb_size);
6682 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006683 } else {
6684 data = kmalloc(skb_size, GFP_ATOMIC);
6685 *frag_size = 0;
6686 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006687 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 return -ENOMEM;
6689
Eric Dumazet9205fd92011-11-18 06:47:01 +00006690 mapping = pci_map_single(tp->pdev,
6691 data + TG3_RX_OFFSET(tp),
6692 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006694 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006695 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006696 return -EIO;
6697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698
Eric Dumazet9205fd92011-11-18 06:47:01 +00006699 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006700 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 desc->addr_hi = ((u64)mapping >> 32);
6703 desc->addr_lo = ((u64)mapping & 0xffffffff);
6704
Eric Dumazet9205fd92011-11-18 06:47:01 +00006705 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706}
6707
6708/* We only need to move over in the address because the other
6709 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006710 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 */
Matt Carlsona3896162009-11-13 13:03:44 +00006712static void tg3_recycle_rx(struct tg3_napi *tnapi,
6713 struct tg3_rx_prodring_set *dpr,
6714 u32 opaque_key, int src_idx,
6715 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716{
Matt Carlson17375d22009-08-28 14:02:18 +00006717 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6719 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006720 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006721 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722
6723 switch (opaque_key) {
6724 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006725 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006726 dest_desc = &dpr->rx_std[dest_idx];
6727 dest_map = &dpr->rx_std_buffers[dest_idx];
6728 src_desc = &spr->rx_std[src_idx];
6729 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 break;
6731
6732 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006733 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006734 dest_desc = &dpr->rx_jmb[dest_idx].std;
6735 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6736 src_desc = &spr->rx_jmb[src_idx].std;
6737 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 break;
6739
6740 default:
6741 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
Eric Dumazet9205fd92011-11-18 06:47:01 +00006744 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006745 dma_unmap_addr_set(dest_map, mapping,
6746 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 dest_desc->addr_hi = src_desc->addr_hi;
6748 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006749
6750 /* Ensure that the update to the skb happens after the physical
6751 * addresses have been transferred to the new BD location.
6752 */
6753 smp_wmb();
6754
Eric Dumazet9205fd92011-11-18 06:47:01 +00006755 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756}
6757
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758/* The RX ring scheme is composed of multiple rings which post fresh
6759 * buffers to the chip, and one special ring the chip uses to report
6760 * status back to the host.
6761 *
6762 * The special ring reports the status of received packets to the
6763 * host. The chip does not write into the original descriptor the
6764 * RX buffer was obtained from. The chip simply takes the original
6765 * descriptor as provided by the host, updates the status and length
6766 * field, then writes this into the next status ring entry.
6767 *
6768 * Each ring the host uses to post buffers to the chip is described
6769 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6770 * it is first placed into the on-chip ram. When the packet's length
6771 * is known, it walks down the TG3_BDINFO entries to select the ring.
6772 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6773 * which is within the range of the new packet's length is chosen.
6774 *
6775 * The "separate ring for rx status" scheme may sound queer, but it makes
6776 * sense from a cache coherency perspective. If only the host writes
6777 * to the buffer post rings, and only the chip writes to the rx status
6778 * rings, then cache lines never move beyond shared-modified state.
6779 * If both the host and chip were to write into the same ring, cache line
6780 * eviction could occur since both entities want it in an exclusive state.
6781 */
Matt Carlson17375d22009-08-28 14:02:18 +00006782static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783{
Matt Carlson17375d22009-08-28 14:02:18 +00006784 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006785 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006786 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006787 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006788 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006790 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006792 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 /*
6794 * We need to order the read of hw_idx and the read of
6795 * the opaque cookie.
6796 */
6797 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 work_mask = 0;
6799 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006800 std_prod_idx = tpr->rx_std_prod_idx;
6801 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006803 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006804 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 unsigned int len;
6806 struct sk_buff *skb;
6807 dma_addr_t dma_addr;
6808 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006809 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006810 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811
6812 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6813 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6814 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006815 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006816 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006817 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006818 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006819 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006821 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006822 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006823 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006824 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006825 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
6828 work_mask |= opaque_key;
6829
6830 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6831 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6832 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006833 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 desc_idx, *post_ptr);
6835 drop_it_no_recycle:
6836 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006837 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 goto next_pkt;
6839 }
6840
Eric Dumazet9205fd92011-11-18 06:47:01 +00006841 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006842 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6843 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006845 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6846 RXD_FLAG_PTPSTAT_PTPV1 ||
6847 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6848 RXD_FLAG_PTPSTAT_PTPV2) {
6849 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6850 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6851 }
6852
Matt Carlsond2757fc2010-04-12 06:58:27 +00006853 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006855 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
Eric Dumazet9205fd92011-11-18 06:47:01 +00006857 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006858 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 if (skb_size < 0)
6860 goto drop_it;
6861
Matt Carlson287be122009-08-28 13:58:46 +00006862 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 PCI_DMA_FROMDEVICE);
6864
Eric Dumazet9205fd92011-11-18 06:47:01 +00006865 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006866 * after the usage of the old DMA mapping.
6867 */
6868 smp_wmb();
6869
Eric Dumazet9205fd92011-11-18 06:47:01 +00006870 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006871
Ivan Vecera85aec732013-11-06 14:02:36 +01006872 skb = build_skb(data, frag_size);
6873 if (!skb) {
6874 tg3_frag_free(frag_size != 0, data);
6875 goto drop_it_no_recycle;
6876 }
6877 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006879 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 desc_idx, *post_ptr);
6881
Eric Dumazet9205fd92011-11-18 06:47:01 +00006882 skb = netdev_alloc_skb(tp->dev,
6883 len + TG3_RAW_IP_ALIGN);
6884 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 goto drop_it_no_recycle;
6886
Eric Dumazet9205fd92011-11-18 06:47:01 +00006887 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006889 memcpy(skb->data,
6890 data + TG3_RX_OFFSET(tp),
6891 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 }
6894
Eric Dumazet9205fd92011-11-18 06:47:01 +00006895 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006896 if (tstamp)
6897 tg3_hwclock_to_timestamp(tp, tstamp,
6898 skb_hwtstamps(skb));
6899
Michał Mirosławdc668912011-04-07 03:35:07 +00006900 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6902 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6903 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6904 skb->ip_summed = CHECKSUM_UNNECESSARY;
6905 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006906 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907
6908 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006909
6910 if (len > (tp->dev->mtu + ETH_HLEN) &&
6911 skb->protocol != htons(ETH_P_8021Q)) {
6912 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006913 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006914 }
6915
Matt Carlson9dc7a112010-04-12 06:58:28 +00006916 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006917 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006918 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006919 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006920
Matt Carlsonbf933c82011-01-25 15:58:49 +00006921 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 received++;
6924 budget--;
6925
6926next_pkt:
6927 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006928
6929 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006930 tpr->rx_std_prod_idx = std_prod_idx &
6931 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006932 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6933 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006934 work_mask &= ~RXD_OPAQUE_RING_STD;
6935 rx_std_posted = 0;
6936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006938 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006939 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006940
6941 /* Refresh hw_idx to see if there is new work */
6942 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006943 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006944 rmb();
6945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 }
6947
6948 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006949 tnapi->rx_rcb_ptr = sw_idx;
6950 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951
6952 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006953 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006954 /* Sync BD data before updating mailbox */
6955 wmb();
6956
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006957 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006958 tpr->rx_std_prod_idx = std_prod_idx &
6959 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006960 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6961 tpr->rx_std_prod_idx);
6962 }
6963 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006964 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6965 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006966 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6967 tpr->rx_jmb_prod_idx);
6968 }
6969 mmiowb();
6970 } else if (work_mask) {
6971 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6972 * updated before the producer indices can be updated.
6973 */
6974 smp_wmb();
6975
Matt Carlson2c49a442010-09-30 10:34:35 +00006976 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6977 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006978
Michael Chan7ae52892012-03-21 15:38:33 +00006979 if (tnapi != &tp->napi[1]) {
6980 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006981 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
6985 return received;
6986}
6987
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006988static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006991 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006992 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6993
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 if (sblk->status & SD_STATUS_LINK_CHG) {
6995 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006996 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006997 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006998 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006999 tw32_f(MAC_STATUS,
7000 (MAC_STATUS_SYNC_CHANGED |
7001 MAC_STATUS_CFG_CHANGED |
7002 MAC_STATUS_MI_COMPLETION |
7003 MAC_STATUS_LNKSTATE_CHANGED));
7004 udelay(40);
7005 } else
Joe Perches953c96e2013-04-09 10:18:14 +00007006 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007007 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 }
7009 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007010}
7011
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007012static int tg3_rx_prodring_xfer(struct tg3 *tp,
7013 struct tg3_rx_prodring_set *dpr,
7014 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007015{
7016 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007017 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007018
7019 while (1) {
7020 src_prod_idx = spr->rx_std_prod_idx;
7021
7022 /* Make sure updates to the rx_std_buffers[] entries and the
7023 * standard producer index are seen in the correct order.
7024 */
7025 smp_rmb();
7026
7027 if (spr->rx_std_cons_idx == src_prod_idx)
7028 break;
7029
7030 if (spr->rx_std_cons_idx < src_prod_idx)
7031 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7032 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007033 cpycnt = tp->rx_std_ring_mask + 1 -
7034 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007035
Matt Carlson2c49a442010-09-30 10:34:35 +00007036 cpycnt = min(cpycnt,
7037 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007038
7039 si = spr->rx_std_cons_idx;
7040 di = dpr->rx_std_prod_idx;
7041
Matt Carlsone92967b2010-02-12 14:47:06 +00007042 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007043 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007044 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007045 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007046 break;
7047 }
7048 }
7049
7050 if (!cpycnt)
7051 break;
7052
7053 /* Ensure that updates to the rx_std_buffers ring and the
7054 * shadowed hardware producer ring from tg3_recycle_skb() are
7055 * ordered correctly WRT the skb check above.
7056 */
7057 smp_rmb();
7058
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007059 memcpy(&dpr->rx_std_buffers[di],
7060 &spr->rx_std_buffers[si],
7061 cpycnt * sizeof(struct ring_info));
7062
7063 for (i = 0; i < cpycnt; i++, di++, si++) {
7064 struct tg3_rx_buffer_desc *sbd, *dbd;
7065 sbd = &spr->rx_std[si];
7066 dbd = &dpr->rx_std[di];
7067 dbd->addr_hi = sbd->addr_hi;
7068 dbd->addr_lo = sbd->addr_lo;
7069 }
7070
Matt Carlson2c49a442010-09-30 10:34:35 +00007071 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7072 tp->rx_std_ring_mask;
7073 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7074 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007075 }
7076
7077 while (1) {
7078 src_prod_idx = spr->rx_jmb_prod_idx;
7079
7080 /* Make sure updates to the rx_jmb_buffers[] entries and
7081 * the jumbo producer index are seen in the correct order.
7082 */
7083 smp_rmb();
7084
7085 if (spr->rx_jmb_cons_idx == src_prod_idx)
7086 break;
7087
7088 if (spr->rx_jmb_cons_idx < src_prod_idx)
7089 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7090 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007091 cpycnt = tp->rx_jmb_ring_mask + 1 -
7092 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007093
7094 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007095 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007096
7097 si = spr->rx_jmb_cons_idx;
7098 di = dpr->rx_jmb_prod_idx;
7099
Matt Carlsone92967b2010-02-12 14:47:06 +00007100 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007101 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007102 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007103 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007104 break;
7105 }
7106 }
7107
7108 if (!cpycnt)
7109 break;
7110
7111 /* Ensure that updates to the rx_jmb_buffers ring and the
7112 * shadowed hardware producer ring from tg3_recycle_skb() are
7113 * ordered correctly WRT the skb check above.
7114 */
7115 smp_rmb();
7116
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007117 memcpy(&dpr->rx_jmb_buffers[di],
7118 &spr->rx_jmb_buffers[si],
7119 cpycnt * sizeof(struct ring_info));
7120
7121 for (i = 0; i < cpycnt; i++, di++, si++) {
7122 struct tg3_rx_buffer_desc *sbd, *dbd;
7123 sbd = &spr->rx_jmb[si].std;
7124 dbd = &dpr->rx_jmb[di].std;
7125 dbd->addr_hi = sbd->addr_hi;
7126 dbd->addr_lo = sbd->addr_lo;
7127 }
7128
Matt Carlson2c49a442010-09-30 10:34:35 +00007129 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7130 tp->rx_jmb_ring_mask;
7131 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7132 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007133 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007134
7135 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007136}
7137
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007138static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7139{
7140 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141
7142 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007143 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007144 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007145 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007146 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 }
7148
Matt Carlsonf891ea12012-04-24 13:37:01 +00007149 if (!tnapi->rx_rcb_prod_idx)
7150 return work_done;
7151
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 /* run RX thread, within the bounds set by NAPI.
7153 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007154 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007156 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007157 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158
Joe Perches63c3a662011-04-26 08:12:10 +00007159 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007160 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007161 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007162 u32 std_prod_idx = dpr->rx_std_prod_idx;
7163 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007164
Michael Chan7ae52892012-03-21 15:38:33 +00007165 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007166 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007167 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007168 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007169
7170 wmb();
7171
Matt Carlsone4af1af2010-02-12 14:47:05 +00007172 if (std_prod_idx != dpr->rx_std_prod_idx)
7173 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7174 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007175
Matt Carlsone4af1af2010-02-12 14:47:05 +00007176 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7177 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7178 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007179
7180 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007181
7182 if (err)
7183 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007184 }
7185
David S. Miller6f535762007-10-11 18:08:29 -07007186 return work_done;
7187}
David S. Millerf7383c22005-05-18 22:50:53 -07007188
Matt Carlsondb219972011-11-04 09:15:03 +00007189static inline void tg3_reset_task_schedule(struct tg3 *tp)
7190{
7191 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7192 schedule_work(&tp->reset_task);
7193}
7194
7195static inline void tg3_reset_task_cancel(struct tg3 *tp)
7196{
7197 cancel_work_sync(&tp->reset_task);
7198 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007199 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007200}
7201
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007202static int tg3_poll_msix(struct napi_struct *napi, int budget)
7203{
7204 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7205 struct tg3 *tp = tnapi->tp;
7206 int work_done = 0;
7207 struct tg3_hw_status *sblk = tnapi->hw_status;
7208
7209 while (1) {
7210 work_done = tg3_poll_work(tnapi, work_done, budget);
7211
Joe Perches63c3a662011-04-26 08:12:10 +00007212 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007213 goto tx_recovery;
7214
7215 if (unlikely(work_done >= budget))
7216 break;
7217
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007218 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007219 * to tell the hw how much work has been processed,
7220 * so we must read it before checking for more work.
7221 */
7222 tnapi->last_tag = sblk->status_tag;
7223 tnapi->last_irq_tag = tnapi->last_tag;
7224 rmb();
7225
7226 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007227 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7228 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007229
7230 /* This test here is not race free, but will reduce
7231 * the number of interrupts by looping again.
7232 */
7233 if (tnapi == &tp->napi[1] && tp->rx_refill)
7234 continue;
7235
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007236 napi_complete(napi);
7237 /* Reenable interrupts. */
7238 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007239
7240 /* This test here is synchronized by napi_schedule()
7241 * and napi_complete() to close the race condition.
7242 */
7243 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7244 tw32(HOSTCC_MODE, tp->coalesce_mode |
7245 HOSTCC_MODE_ENABLE |
7246 tnapi->coal_now);
7247 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007248 mmiowb();
7249 break;
7250 }
7251 }
7252
7253 return work_done;
7254
7255tx_recovery:
7256 /* work_done is guaranteed to be less than budget. */
7257 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007258 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007259 return work_done;
7260}
7261
Matt Carlsone64de4e2011-04-13 11:05:05 +00007262static void tg3_process_error(struct tg3 *tp)
7263{
7264 u32 val;
7265 bool real_error = false;
7266
Joe Perches63c3a662011-04-26 08:12:10 +00007267 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007268 return;
7269
7270 /* Check Flow Attention register */
7271 val = tr32(HOSTCC_FLOW_ATTN);
7272 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7273 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7274 real_error = true;
7275 }
7276
7277 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7278 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7279 real_error = true;
7280 }
7281
7282 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7283 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7284 real_error = true;
7285 }
7286
7287 if (!real_error)
7288 return;
7289
7290 tg3_dump_state(tp);
7291
Joe Perches63c3a662011-04-26 08:12:10 +00007292 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007293 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007294}
7295
David S. Miller6f535762007-10-11 18:08:29 -07007296static int tg3_poll(struct napi_struct *napi, int budget)
7297{
Matt Carlson8ef04422009-08-28 14:01:37 +00007298 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7299 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007300 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007301 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007302
7303 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007304 if (sblk->status & SD_STATUS_ERROR)
7305 tg3_process_error(tp);
7306
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007307 tg3_poll_link(tp);
7308
Matt Carlson17375d22009-08-28 14:02:18 +00007309 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007310
Joe Perches63c3a662011-04-26 08:12:10 +00007311 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007312 goto tx_recovery;
7313
7314 if (unlikely(work_done >= budget))
7315 break;
7316
Joe Perches63c3a662011-04-26 08:12:10 +00007317 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007318 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007319 * to tell the hw how much work has been processed,
7320 * so we must read it before checking for more work.
7321 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007322 tnapi->last_tag = sblk->status_tag;
7323 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007324 rmb();
7325 } else
7326 sblk->status &= ~SD_STATUS_UPDATED;
7327
Matt Carlson17375d22009-08-28 14:02:18 +00007328 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007329 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007330 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007331 break;
7332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 }
7334
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007335 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007336
7337tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007338 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007339 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007340 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007341 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342}
7343
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007344static void tg3_napi_disable(struct tg3 *tp)
7345{
7346 int i;
7347
7348 for (i = tp->irq_cnt - 1; i >= 0; i--)
7349 napi_disable(&tp->napi[i].napi);
7350}
7351
7352static void tg3_napi_enable(struct tg3 *tp)
7353{
7354 int i;
7355
7356 for (i = 0; i < tp->irq_cnt; i++)
7357 napi_enable(&tp->napi[i].napi);
7358}
7359
7360static void tg3_napi_init(struct tg3 *tp)
7361{
7362 int i;
7363
7364 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7365 for (i = 1; i < tp->irq_cnt; i++)
7366 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7367}
7368
7369static void tg3_napi_fini(struct tg3 *tp)
7370{
7371 int i;
7372
7373 for (i = 0; i < tp->irq_cnt; i++)
7374 netif_napi_del(&tp->napi[i].napi);
7375}
7376
7377static inline void tg3_netif_stop(struct tg3 *tp)
7378{
7379 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7380 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007381 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007382 netif_tx_disable(tp->dev);
7383}
7384
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007385/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007386static inline void tg3_netif_start(struct tg3 *tp)
7387{
Matt Carlsonbe947302012-12-03 19:36:57 +00007388 tg3_ptp_resume(tp);
7389
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007390 /* NOTE: unconditional netif_tx_wake_all_queues is only
7391 * appropriate so long as all callers are assured to
7392 * have free tx slots (such as after tg3_init_hw)
7393 */
7394 netif_tx_wake_all_queues(tp->dev);
7395
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007396 if (tp->link_up)
7397 netif_carrier_on(tp->dev);
7398
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007399 tg3_napi_enable(tp);
7400 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7401 tg3_enable_ints(tp);
7402}
7403
David S. Millerf47c11e2005-06-24 20:18:35 -07007404static void tg3_irq_quiesce(struct tg3 *tp)
7405{
Matt Carlson4f125f42009-09-01 12:55:02 +00007406 int i;
7407
David S. Millerf47c11e2005-06-24 20:18:35 -07007408 BUG_ON(tp->irq_sync);
7409
7410 tp->irq_sync = 1;
7411 smp_mb();
7412
Matt Carlson4f125f42009-09-01 12:55:02 +00007413 for (i = 0; i < tp->irq_cnt; i++)
7414 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007415}
7416
David S. Millerf47c11e2005-06-24 20:18:35 -07007417/* Fully shutdown all tg3 driver activity elsewhere in the system.
7418 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7419 * with as well. Most of the time, this is not necessary except when
7420 * shutting down the device.
7421 */
7422static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7423{
Michael Chan46966542007-07-11 19:47:19 -07007424 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007425 if (irq_sync)
7426 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007427}
7428
7429static inline void tg3_full_unlock(struct tg3 *tp)
7430{
David S. Millerf47c11e2005-06-24 20:18:35 -07007431 spin_unlock_bh(&tp->lock);
7432}
7433
Michael Chanfcfa0a32006-03-20 22:28:41 -08007434/* One-shot MSI handler - Chip automatically disables interrupt
7435 * after sending MSI so driver doesn't have to do it.
7436 */
David Howells7d12e782006-10-05 14:55:46 +01007437static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007438{
Matt Carlson09943a12009-08-28 14:01:57 +00007439 struct tg3_napi *tnapi = dev_id;
7440 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007441
Matt Carlson898a56f2009-08-28 14:02:40 +00007442 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007443 if (tnapi->rx_rcb)
7444 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007445
7446 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007447 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007448
7449 return IRQ_HANDLED;
7450}
7451
Michael Chan88b06bc22005-04-21 17:13:25 -07007452/* MSI ISR - No need to check for interrupt sharing and no need to
7453 * flush status block and interrupt mailbox. PCI ordering rules
7454 * guarantee that MSI will arrive after the status block.
7455 */
David Howells7d12e782006-10-05 14:55:46 +01007456static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007457{
Matt Carlson09943a12009-08-28 14:01:57 +00007458 struct tg3_napi *tnapi = dev_id;
7459 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007460
Matt Carlson898a56f2009-08-28 14:02:40 +00007461 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007462 if (tnapi->rx_rcb)
7463 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007464 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007465 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007466 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007467 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007468 * NIC to stop sending us irqs, engaging "in-intr-handler"
7469 * event coalescing.
7470 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007471 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007472 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007473 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007474
Michael Chan88b06bc22005-04-21 17:13:25 -07007475 return IRQ_RETVAL(1);
7476}
7477
David Howells7d12e782006-10-05 14:55:46 +01007478static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479{
Matt Carlson09943a12009-08-28 14:01:57 +00007480 struct tg3_napi *tnapi = dev_id;
7481 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007482 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 unsigned int handled = 1;
7484
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 /* In INTx mode, it is possible for the interrupt to arrive at
7486 * the CPU before the status block posted prior to the interrupt.
7487 * Reading the PCI State register will confirm whether the
7488 * interrupt is ours and will flush the status block.
7489 */
Michael Chand18edcb2007-03-24 20:57:11 -07007490 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007491 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007492 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7493 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007494 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007495 }
Michael Chand18edcb2007-03-24 20:57:11 -07007496 }
7497
7498 /*
7499 * Writing any value to intr-mbox-0 clears PCI INTA# and
7500 * chip-internal interrupt pending events.
7501 * Writing non-zero to intr-mbox-0 additional tells the
7502 * NIC to stop sending us irqs, engaging "in-intr-handler"
7503 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007504 *
7505 * Flush the mailbox to de-assert the IRQ immediately to prevent
7506 * spurious interrupts. The flush impacts performance but
7507 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007508 */
Michael Chanc04cb342007-05-07 00:26:15 -07007509 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007510 if (tg3_irq_sync(tp))
7511 goto out;
7512 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007513 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007514 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007515 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007516 } else {
7517 /* No work, shared interrupt perhaps? re-enable
7518 * interrupts, and flush that PCI write
7519 */
7520 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7521 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007522 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007523out:
David S. Millerfac9b832005-05-18 22:46:34 -07007524 return IRQ_RETVAL(handled);
7525}
7526
David Howells7d12e782006-10-05 14:55:46 +01007527static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007528{
Matt Carlson09943a12009-08-28 14:01:57 +00007529 struct tg3_napi *tnapi = dev_id;
7530 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007531 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007532 unsigned int handled = 1;
7533
David S. Millerfac9b832005-05-18 22:46:34 -07007534 /* In INTx mode, it is possible for the interrupt to arrive at
7535 * the CPU before the status block posted prior to the interrupt.
7536 * Reading the PCI State register will confirm whether the
7537 * interrupt is ours and will flush the status block.
7538 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007539 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007540 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007541 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7542 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007543 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 }
Michael Chand18edcb2007-03-24 20:57:11 -07007545 }
7546
7547 /*
7548 * writing any value to intr-mbox-0 clears PCI INTA# and
7549 * chip-internal interrupt pending events.
7550 * writing non-zero to intr-mbox-0 additional tells the
7551 * NIC to stop sending us irqs, engaging "in-intr-handler"
7552 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007553 *
7554 * Flush the mailbox to de-assert the IRQ immediately to prevent
7555 * spurious interrupts. The flush impacts performance but
7556 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007557 */
Michael Chanc04cb342007-05-07 00:26:15 -07007558 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007559
7560 /*
7561 * In a shared interrupt configuration, sometimes other devices'
7562 * interrupts will scream. We record the current status tag here
7563 * so that the above check can report that the screaming interrupts
7564 * are unhandled. Eventually they will be silenced.
7565 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007566 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007567
Michael Chand18edcb2007-03-24 20:57:11 -07007568 if (tg3_irq_sync(tp))
7569 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007570
Matt Carlson72334482009-08-28 14:03:01 +00007571 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007572
Matt Carlson09943a12009-08-28 14:01:57 +00007573 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007574
David S. Millerf47c11e2005-06-24 20:18:35 -07007575out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 return IRQ_RETVAL(handled);
7577}
7578
Michael Chan79381092005-04-21 17:13:59 -07007579/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007580static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007581{
Matt Carlson09943a12009-08-28 14:01:57 +00007582 struct tg3_napi *tnapi = dev_id;
7583 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007584 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007585
Michael Chanf9804dd2005-09-27 12:13:10 -07007586 if ((sblk->status & SD_STATUS_UPDATED) ||
7587 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007588 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007589 return IRQ_RETVAL(1);
7590 }
7591 return IRQ_RETVAL(0);
7592}
7593
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594#ifdef CONFIG_NET_POLL_CONTROLLER
7595static void tg3_poll_controller(struct net_device *dev)
7596{
Matt Carlson4f125f42009-09-01 12:55:02 +00007597 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007598 struct tg3 *tp = netdev_priv(dev);
7599
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007600 if (tg3_irq_sync(tp))
7601 return;
7602
Matt Carlson4f125f42009-09-01 12:55:02 +00007603 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007604 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605}
7606#endif
7607
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608static void tg3_tx_timeout(struct net_device *dev)
7609{
7610 struct tg3 *tp = netdev_priv(dev);
7611
Michael Chanb0408752007-02-13 12:18:30 -08007612 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007613 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007614 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616
Matt Carlsondb219972011-11-04 09:15:03 +00007617 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618}
7619
Michael Chanc58ec932005-09-17 00:46:27 -07007620/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7621static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7622{
7623 u32 base = (u32) mapping & 0xffffffff;
7624
Eric Dumazet807540b2010-09-23 05:40:09 +00007625 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007626}
7627
Michael Chan0f0d1512013-05-13 11:04:16 +00007628/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7629 * of any 4GB boundaries: 4G, 8G, etc
7630 */
7631static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7632 u32 len, u32 mss)
7633{
7634 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7635 u32 base = (u32) mapping & 0xffffffff;
7636
7637 return ((base + len + (mss & 0x3fff)) < base);
7638 }
7639 return 0;
7640}
7641
Michael Chan72f2afb2006-03-06 19:28:35 -08007642/* Test for DMA addresses > 40-bit */
7643static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7644 int len)
7645{
7646#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007647 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007648 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007649 return 0;
7650#else
7651 return 0;
7652#endif
7653}
7654
Matt Carlsond1a3b732011-07-27 14:20:51 +00007655static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007656 dma_addr_t mapping, u32 len, u32 flags,
7657 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007658{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007659 txbd->addr_hi = ((u64) mapping >> 32);
7660 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7661 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7662 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007663}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
Matt Carlson84b67b22011-07-27 14:20:52 +00007665static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007666 dma_addr_t map, u32 len, u32 flags,
7667 u32 mss, u32 vlan)
7668{
7669 struct tg3 *tp = tnapi->tp;
7670 bool hwbug = false;
7671
7672 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007673 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007674
7675 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007676 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007677
Michael Chan0f0d1512013-05-13 11:04:16 +00007678 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7679 hwbug = true;
7680
Matt Carlsond1a3b732011-07-27 14:20:51 +00007681 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007682 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007683
Matt Carlsona4cb4282011-12-14 11:09:58 +00007684 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007685 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007686 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007687 while (len > tp->dma_limit && *budget) {
7688 u32 frag_len = tp->dma_limit;
7689 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007690
Matt Carlsonb9e45482011-11-04 09:14:59 +00007691 /* Avoid the 8byte DMA problem */
7692 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007693 len += tp->dma_limit / 2;
7694 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007695 }
7696
Matt Carlsonb9e45482011-11-04 09:14:59 +00007697 tnapi->tx_buffers[*entry].fragmented = true;
7698
7699 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7700 frag_len, tmp_flag, mss, vlan);
7701 *budget -= 1;
7702 prvidx = *entry;
7703 *entry = NEXT_TX(*entry);
7704
Matt Carlsone31aa982011-07-27 14:20:53 +00007705 map += frag_len;
7706 }
7707
7708 if (len) {
7709 if (*budget) {
7710 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7711 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007712 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007713 *entry = NEXT_TX(*entry);
7714 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007715 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007716 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007717 }
7718 }
7719 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007720 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7721 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007722 *entry = NEXT_TX(*entry);
7723 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007724
7725 return hwbug;
7726}
7727
Matt Carlson0d681b22011-07-27 14:20:49 +00007728static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007729{
7730 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007731 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007732 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007733
Matt Carlson0d681b22011-07-27 14:20:49 +00007734 skb = txb->skb;
7735 txb->skb = NULL;
7736
Matt Carlson432aa7e2011-05-19 12:12:45 +00007737 pci_unmap_single(tnapi->tp->pdev,
7738 dma_unmap_addr(txb, mapping),
7739 skb_headlen(skb),
7740 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007741
7742 while (txb->fragmented) {
7743 txb->fragmented = false;
7744 entry = NEXT_TX(entry);
7745 txb = &tnapi->tx_buffers[entry];
7746 }
7747
Matt Carlsonba1142e2011-11-04 09:15:00 +00007748 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007749 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007750
7751 entry = NEXT_TX(entry);
7752 txb = &tnapi->tx_buffers[entry];
7753
7754 pci_unmap_page(tnapi->tp->pdev,
7755 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007756 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007757
7758 while (txb->fragmented) {
7759 txb->fragmented = false;
7760 entry = NEXT_TX(entry);
7761 txb = &tnapi->tx_buffers[entry];
7762 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007763 }
7764}
7765
Michael Chan72f2afb2006-03-06 19:28:35 -08007766/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007767static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007768 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007769 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007770 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007772 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007773 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007774 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007775 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776
Joe Perches41535772013-02-16 11:20:04 +00007777 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007778 new_skb = skb_copy(skb, GFP_ATOMIC);
7779 else {
7780 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7781
7782 new_skb = skb_copy_expand(skb,
7783 skb_headroom(skb) + more_headroom,
7784 skb_tailroom(skb), GFP_ATOMIC);
7785 }
7786
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007788 ret = -1;
7789 } else {
7790 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007791 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7792 PCI_DMA_TODEVICE);
7793 /* Make sure the mapping succeeded */
7794 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007795 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007796 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007797 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007798 u32 save_entry = *entry;
7799
Matt Carlson92cd3a12011-07-27 14:20:47 +00007800 base_flags |= TXD_FLAG_END;
7801
Matt Carlson84b67b22011-07-27 14:20:52 +00007802 tnapi->tx_buffers[*entry].skb = new_skb;
7803 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007804 mapping, new_addr);
7805
Matt Carlson84b67b22011-07-27 14:20:52 +00007806 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007807 new_skb->len, base_flags,
7808 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007809 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007810 dev_kfree_skb(new_skb);
7811 ret = -1;
7812 }
Michael Chanc58ec932005-09-17 00:46:27 -07007813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 }
7815
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007817 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819}
7820
Matt Carlson2ffcc982011-05-19 12:12:44 +00007821static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007822
7823/* Use GSO to workaround a rare TSO bug that may be triggered when the
7824 * TSO header is greater than 80 bytes.
7825 */
7826static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7827{
7828 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007829 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007830
7831 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007832 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007833 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007834
7835 /* netif_tx_stop_queue() must be done before checking
7836 * checking tx index in tg3_tx_avail() below, because in
7837 * tg3_tx(), we update tx index before checking for
7838 * netif_tx_queue_stopped().
7839 */
7840 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007841 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007842 return NETDEV_TX_BUSY;
7843
7844 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007845 }
7846
7847 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007848 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007849 goto tg3_tso_bug_end;
7850
7851 do {
7852 nskb = segs;
7853 segs = segs->next;
7854 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007855 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007856 } while (segs);
7857
7858tg3_tso_bug_end:
7859 dev_kfree_skb(skb);
7860
7861 return NETDEV_TX_OK;
7862}
Michael Chan52c0fd82006-06-29 20:15:54 -07007863
Michael Chan5a6f3072006-03-20 22:28:05 -08007864/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007865 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007866 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007867static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007868{
7869 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007870 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007871 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007872 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007873 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007874 struct tg3_napi *tnapi;
7875 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007876 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007877
Matt Carlson24f4efd2009-11-13 13:03:35 +00007878 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7879 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007880 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007881 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882
Matt Carlson84b67b22011-07-27 14:20:52 +00007883 budget = tg3_tx_avail(tnapi);
7884
Michael Chan00b70502006-06-17 21:58:45 -07007885 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007886 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007887 * interrupt. Furthermore, IRQ processing runs lockless so we have
7888 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007890 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007891 if (!netif_tx_queue_stopped(txq)) {
7892 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007893
7894 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007895 netdev_err(dev,
7896 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 return NETDEV_TX_BUSY;
7899 }
7900
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007901 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007903 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007905
Matt Carlsonbe98da62010-07-11 09:31:46 +00007906 mss = skb_shinfo(skb)->gso_size;
7907 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007908 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007909 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910
7911 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007912 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7913 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914
Matt Carlson34195c32010-07-11 09:31:42 +00007915 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007916 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
Eric Dumazeta5a11952012-01-23 01:22:09 +00007918 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007919
Eric Dumazeta5a11952012-01-23 01:22:09 +00007920 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007921 iph->check = 0;
7922 iph->tot_len = htons(mss + hdr_len);
7923 }
7924
Michael Chan52c0fd82006-06-29 20:15:54 -07007925 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007926 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007927 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007928
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7930 TXD_FLAG_CPU_POST_DMA);
7931
Joe Perches63c3a662011-04-26 08:12:10 +00007932 if (tg3_flag(tp, HW_TSO_1) ||
7933 tg3_flag(tp, HW_TSO_2) ||
7934 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007935 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007937 } else
7938 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7939 iph->daddr, 0,
7940 IPPROTO_TCP,
7941 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942
Joe Perches63c3a662011-04-26 08:12:10 +00007943 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007944 mss |= (hdr_len & 0xc) << 12;
7945 if (hdr_len & 0x10)
7946 base_flags |= 0x00000010;
7947 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007948 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007949 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007950 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007951 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007952 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 int tsflags;
7954
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007955 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956 mss |= (tsflags << 11);
7957 }
7958 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007959 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960 int tsflags;
7961
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007962 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963 base_flags |= tsflags << 12;
7964 }
7965 }
7966 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007967
Matt Carlson93a700a2011-08-31 11:44:54 +00007968 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7969 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7970 base_flags |= TXD_FLAG_JMB_PKT;
7971
Matt Carlson92cd3a12011-07-27 14:20:47 +00007972 if (vlan_tx_tag_present(skb)) {
7973 base_flags |= TXD_FLAG_VLAN;
7974 vlan = vlan_tx_tag_get(skb);
7975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007977 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7978 tg3_flag(tp, TX_TSTAMP_EN)) {
7979 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7980 base_flags |= TXD_FLAG_HWTSTAMP;
7981 }
7982
Alexander Duyckf4188d82009-12-02 16:48:38 +00007983 len = skb_headlen(skb);
7984
7985 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007986 if (pci_dma_mapping_error(tp->pdev, mapping))
7987 goto drop;
7988
David S. Miller90079ce2008-09-11 04:52:51 -07007989
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007990 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007991 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992
7993 would_hit_hwbug = 0;
7994
Joe Perches63c3a662011-04-26 08:12:10 +00007995 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007996 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997
Matt Carlson84b67b22011-07-27 14:20:52 +00007998 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007999 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008000 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008001 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008002 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008003 u32 tmp_mss = mss;
8004
8005 if (!tg3_flag(tp, HW_TSO_1) &&
8006 !tg3_flag(tp, HW_TSO_2) &&
8007 !tg3_flag(tp, HW_TSO_3))
8008 tmp_mss = 0;
8009
Matt Carlsonc5665a52012-02-13 10:20:12 +00008010 /* Now loop through additional data
8011 * fragments, and queue them.
8012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013 last = skb_shinfo(skb)->nr_frags - 1;
8014 for (i = 0; i <= last; i++) {
8015 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8016
Eric Dumazet9e903e02011-10-18 21:00:24 +00008017 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008018 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008019 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008021 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008022 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008023 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008024 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008025 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026
Matt Carlsonb9e45482011-11-04 09:14:59 +00008027 if (!budget ||
8028 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008029 len, base_flags |
8030 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008031 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008032 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008033 break;
8034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035 }
8036 }
8037
8038 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008039 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040
8041 /* If the workaround fails due to memory/mapping
8042 * failure, silently drop this packet.
8043 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008044 entry = tnapi->tx_prod;
8045 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008046 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008047 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008048 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 }
8050
Richard Cochrand515b452011-06-19 03:31:41 +00008051 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008052 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008053
Michael Chan6541b802012-03-04 14:48:14 +00008054 /* Sync BD data before updating mailbox */
8055 wmb();
8056
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00008058 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008060 tnapi->tx_prod = entry;
8061 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008062 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008063
8064 /* netif_tx_stop_queue() must be done before checking
8065 * checking tx index in tg3_tx_avail() below, because in
8066 * tg3_tx(), we update tx index before checking for
8067 * netif_tx_queue_stopped().
8068 */
8069 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008070 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008071 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073
Eric Dumazetcdd0db02009-05-28 00:00:41 +00008074 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008076
8077dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008078 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008079 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008080drop:
8081 dev_kfree_skb(skb);
8082drop_nofree:
8083 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008084 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085}
8086
Matt Carlson6e01b202011-08-19 13:58:20 +00008087static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8088{
8089 if (enable) {
8090 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8091 MAC_MODE_PORT_MODE_MASK);
8092
8093 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8094
8095 if (!tg3_flag(tp, 5705_PLUS))
8096 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8097
8098 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8099 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8100 else
8101 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8102 } else {
8103 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8104
8105 if (tg3_flag(tp, 5705_PLUS) ||
8106 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008107 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008108 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8109 }
8110
8111 tw32(MAC_MODE, tp->mac_mode);
8112 udelay(40);
8113}
8114
Matt Carlson941ec902011-08-19 13:58:23 +00008115static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008116{
Matt Carlson941ec902011-08-19 13:58:23 +00008117 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008118
8119 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008120 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008121
Matt Carlson941ec902011-08-19 13:58:23 +00008122 if (extlpbk && tg3_phy_set_extloopbk(tp))
8123 return -EIO;
8124
8125 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008126 switch (speed) {
8127 case SPEED_10:
8128 break;
8129 case SPEED_100:
8130 bmcr |= BMCR_SPEED100;
8131 break;
8132 case SPEED_1000:
8133 default:
8134 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8135 speed = SPEED_100;
8136 bmcr |= BMCR_SPEED100;
8137 } else {
8138 speed = SPEED_1000;
8139 bmcr |= BMCR_SPEED1000;
8140 }
8141 }
8142
Matt Carlson941ec902011-08-19 13:58:23 +00008143 if (extlpbk) {
8144 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8145 tg3_readphy(tp, MII_CTRL1000, &val);
8146 val |= CTL1000_AS_MASTER |
8147 CTL1000_ENABLE_MASTER;
8148 tg3_writephy(tp, MII_CTRL1000, val);
8149 } else {
8150 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8151 MII_TG3_FET_PTEST_TRIM_2;
8152 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8153 }
8154 } else
8155 bmcr |= BMCR_LOOPBACK;
8156
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008157 tg3_writephy(tp, MII_BMCR, bmcr);
8158
8159 /* The write needs to be flushed for the FETs */
8160 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8161 tg3_readphy(tp, MII_BMCR, &bmcr);
8162
8163 udelay(40);
8164
8165 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008166 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008167 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008168 MII_TG3_FET_PTEST_FRC_TX_LINK |
8169 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8170
8171 /* The write needs to be flushed for the AC131 */
8172 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8173 }
8174
8175 /* Reset to prevent losing 1st rx packet intermittently */
8176 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8177 tg3_flag(tp, 5780_CLASS)) {
8178 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8179 udelay(10);
8180 tw32_f(MAC_RX_MODE, tp->rx_mode);
8181 }
8182
8183 mac_mode = tp->mac_mode &
8184 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8185 if (speed == SPEED_1000)
8186 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8187 else
8188 mac_mode |= MAC_MODE_PORT_MODE_MII;
8189
Joe Perches41535772013-02-16 11:20:04 +00008190 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008191 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8192
8193 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8194 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8195 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8196 mac_mode |= MAC_MODE_LINK_POLARITY;
8197
8198 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8199 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8200 }
8201
8202 tw32(MAC_MODE, mac_mode);
8203 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008204
8205 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008206}
8207
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008208static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008209{
8210 struct tg3 *tp = netdev_priv(dev);
8211
8212 if (features & NETIF_F_LOOPBACK) {
8213 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8214 return;
8215
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008216 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008217 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008218 netif_carrier_on(tp->dev);
8219 spin_unlock_bh(&tp->lock);
8220 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8221 } else {
8222 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8223 return;
8224
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008225 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008226 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008227 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008228 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008229 spin_unlock_bh(&tp->lock);
8230 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8231 }
8232}
8233
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008234static netdev_features_t tg3_fix_features(struct net_device *dev,
8235 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008236{
8237 struct tg3 *tp = netdev_priv(dev);
8238
Joe Perches63c3a662011-04-26 08:12:10 +00008239 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008240 features &= ~NETIF_F_ALL_TSO;
8241
8242 return features;
8243}
8244
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008245static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008246{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008247 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008248
8249 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8250 tg3_set_loopback(dev, features);
8251
8252 return 0;
8253}
8254
Matt Carlson21f581a2009-08-28 14:00:25 +00008255static void tg3_rx_prodring_free(struct tg3 *tp,
8256 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258 int i;
8259
Matt Carlson8fea32b2010-09-15 08:59:58 +00008260 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008261 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008262 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008263 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008264 tp->rx_pkt_map_sz);
8265
Joe Perches63c3a662011-04-26 08:12:10 +00008266 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008267 for (i = tpr->rx_jmb_cons_idx;
8268 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008269 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008270 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008271 TG3_RX_JMB_MAP_SZ);
8272 }
8273 }
8274
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008275 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277
Matt Carlson2c49a442010-09-30 10:34:35 +00008278 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008279 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008280 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281
Joe Perches63c3a662011-04-26 08:12:10 +00008282 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008283 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008284 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008285 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286 }
8287}
8288
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008289/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 *
8291 * The chip has been shut down and the driver detached from
8292 * the networking, so no interrupts or new tx packets will
8293 * end up in the driver. tp->{tx,}lock are held and thus
8294 * we may not sleep.
8295 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008296static int tg3_rx_prodring_alloc(struct tg3 *tp,
8297 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298{
Matt Carlson287be122009-08-28 13:58:46 +00008299 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008301 tpr->rx_std_cons_idx = 0;
8302 tpr->rx_std_prod_idx = 0;
8303 tpr->rx_jmb_cons_idx = 0;
8304 tpr->rx_jmb_prod_idx = 0;
8305
Matt Carlson8fea32b2010-09-15 08:59:58 +00008306 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008307 memset(&tpr->rx_std_buffers[0], 0,
8308 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008309 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008310 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008311 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008312 goto done;
8313 }
8314
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008316 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317
Matt Carlson287be122009-08-28 13:58:46 +00008318 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008319 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008320 tp->dev->mtu > ETH_DATA_LEN)
8321 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8322 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008323
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 /* Initialize invariants of the rings, we only set this
8325 * stuff once. This works because the card does not
8326 * write into the rx buffer posting rings.
8327 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008328 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329 struct tg3_rx_buffer_desc *rxd;
8330
Matt Carlson21f581a2009-08-28 14:00:25 +00008331 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008332 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8334 rxd->opaque = (RXD_OPAQUE_RING_STD |
8335 (i << RXD_OPAQUE_INDEX_SHIFT));
8336 }
8337
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008338 /* Now allocate fresh SKBs for each rx ring. */
8339 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008340 unsigned int frag_size;
8341
8342 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8343 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008344 netdev_warn(tp->dev,
8345 "Using a smaller RX standard ring. Only "
8346 "%d out of %d buffers were allocated "
8347 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008348 if (i == 0)
8349 goto initfail;
8350 tp->rx_pending = i;
8351 break;
8352 }
8353 }
8354
Joe Perches63c3a662011-04-26 08:12:10 +00008355 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008356 goto done;
8357
Matt Carlson2c49a442010-09-30 10:34:35 +00008358 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008359
Joe Perches63c3a662011-04-26 08:12:10 +00008360 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008361 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362
Matt Carlson2c49a442010-09-30 10:34:35 +00008363 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008364 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365
Matt Carlson0d86df82010-02-17 15:17:00 +00008366 rxd = &tpr->rx_jmb[i].std;
8367 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8368 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8369 RXD_FLAG_JUMBO;
8370 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8371 (i << RXD_OPAQUE_INDEX_SHIFT));
8372 }
8373
8374 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008375 unsigned int frag_size;
8376
8377 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8378 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008379 netdev_warn(tp->dev,
8380 "Using a smaller RX jumbo ring. Only %d "
8381 "out of %d buffers were allocated "
8382 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008383 if (i == 0)
8384 goto initfail;
8385 tp->rx_jumbo_pending = i;
8386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387 }
8388 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008389
8390done:
Michael Chan32d8c572006-07-25 16:38:29 -07008391 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008392
8393initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008394 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008395 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396}
8397
Matt Carlson21f581a2009-08-28 14:00:25 +00008398static void tg3_rx_prodring_fini(struct tg3 *tp,
8399 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400{
Matt Carlson21f581a2009-08-28 14:00:25 +00008401 kfree(tpr->rx_std_buffers);
8402 tpr->rx_std_buffers = NULL;
8403 kfree(tpr->rx_jmb_buffers);
8404 tpr->rx_jmb_buffers = NULL;
8405 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008406 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8407 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008408 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008410 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008411 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8412 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008413 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008415}
8416
Matt Carlson21f581a2009-08-28 14:00:25 +00008417static int tg3_rx_prodring_init(struct tg3 *tp,
8418 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008419{
Matt Carlson2c49a442010-09-30 10:34:35 +00008420 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8421 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008422 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008423 return -ENOMEM;
8424
Matt Carlson4bae65c2010-11-24 08:31:52 +00008425 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8426 TG3_RX_STD_RING_BYTES(tp),
8427 &tpr->rx_std_mapping,
8428 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008429 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008430 goto err_out;
8431
Joe Perches63c3a662011-04-26 08:12:10 +00008432 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008433 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008434 GFP_KERNEL);
8435 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008436 goto err_out;
8437
Matt Carlson4bae65c2010-11-24 08:31:52 +00008438 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8439 TG3_RX_JMB_RING_BYTES(tp),
8440 &tpr->rx_jmb_mapping,
8441 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008442 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008443 goto err_out;
8444 }
8445
8446 return 0;
8447
8448err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008449 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008450 return -ENOMEM;
8451}
8452
8453/* Free up pending packets in all rx/tx rings.
8454 *
8455 * The chip has been shut down and the driver detached from
8456 * the networking, so no interrupts or new tx packets will
8457 * end up in the driver. tp->{tx,}lock is not held and we are not
8458 * in an interrupt context and thus may sleep.
8459 */
8460static void tg3_free_rings(struct tg3 *tp)
8461{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008462 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008463
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008464 for (j = 0; j < tp->irq_cnt; j++) {
8465 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008466
Matt Carlson8fea32b2010-09-15 08:59:58 +00008467 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008468
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008469 if (!tnapi->tx_buffers)
8470 continue;
8471
Matt Carlson0d681b22011-07-27 14:20:49 +00008472 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8473 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008474
Matt Carlson0d681b22011-07-27 14:20:49 +00008475 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008476 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008477
Matt Carlsonba1142e2011-11-04 09:15:00 +00008478 tg3_tx_skb_unmap(tnapi, i,
8479 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008480
8481 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008482 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008483 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008484 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008485}
8486
8487/* Initialize tx/rx rings for packet processing.
8488 *
8489 * The chip has been shut down and the driver detached from
8490 * the networking, so no interrupts or new tx packets will
8491 * end up in the driver. tp->{tx,}lock are held and thus
8492 * we may not sleep.
8493 */
8494static int tg3_init_rings(struct tg3 *tp)
8495{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008496 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008497
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008498 /* Free up all the SKBs. */
8499 tg3_free_rings(tp);
8500
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008501 for (i = 0; i < tp->irq_cnt; i++) {
8502 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008503
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008504 tnapi->last_tag = 0;
8505 tnapi->last_irq_tag = 0;
8506 tnapi->hw_status->status = 0;
8507 tnapi->hw_status->status_tag = 0;
8508 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8509
8510 tnapi->tx_prod = 0;
8511 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008512 if (tnapi->tx_ring)
8513 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008514
8515 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008516 if (tnapi->rx_rcb)
8517 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008518
Matt Carlson8fea32b2010-09-15 08:59:58 +00008519 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008520 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008521 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008522 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008523 }
Matt Carlson72334482009-08-28 14:03:01 +00008524
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008525 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008526}
8527
Michael Chan49a359e2012-09-28 07:12:37 +00008528static void tg3_mem_tx_release(struct tg3 *tp)
8529{
8530 int i;
8531
8532 for (i = 0; i < tp->irq_max; i++) {
8533 struct tg3_napi *tnapi = &tp->napi[i];
8534
8535 if (tnapi->tx_ring) {
8536 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8537 tnapi->tx_ring, tnapi->tx_desc_mapping);
8538 tnapi->tx_ring = NULL;
8539 }
8540
8541 kfree(tnapi->tx_buffers);
8542 tnapi->tx_buffers = NULL;
8543 }
8544}
8545
8546static int tg3_mem_tx_acquire(struct tg3 *tp)
8547{
8548 int i;
8549 struct tg3_napi *tnapi = &tp->napi[0];
8550
8551 /* If multivector TSS is enabled, vector 0 does not handle
8552 * tx interrupts. Don't allocate any resources for it.
8553 */
8554 if (tg3_flag(tp, ENABLE_TSS))
8555 tnapi++;
8556
8557 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8558 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8559 TG3_TX_RING_SIZE, GFP_KERNEL);
8560 if (!tnapi->tx_buffers)
8561 goto err_out;
8562
8563 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8564 TG3_TX_RING_BYTES,
8565 &tnapi->tx_desc_mapping,
8566 GFP_KERNEL);
8567 if (!tnapi->tx_ring)
8568 goto err_out;
8569 }
8570
8571 return 0;
8572
8573err_out:
8574 tg3_mem_tx_release(tp);
8575 return -ENOMEM;
8576}
8577
8578static void tg3_mem_rx_release(struct tg3 *tp)
8579{
8580 int i;
8581
8582 for (i = 0; i < tp->irq_max; i++) {
8583 struct tg3_napi *tnapi = &tp->napi[i];
8584
8585 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8586
8587 if (!tnapi->rx_rcb)
8588 continue;
8589
8590 dma_free_coherent(&tp->pdev->dev,
8591 TG3_RX_RCB_RING_BYTES(tp),
8592 tnapi->rx_rcb,
8593 tnapi->rx_rcb_mapping);
8594 tnapi->rx_rcb = NULL;
8595 }
8596}
8597
8598static int tg3_mem_rx_acquire(struct tg3 *tp)
8599{
8600 unsigned int i, limit;
8601
8602 limit = tp->rxq_cnt;
8603
8604 /* If RSS is enabled, we need a (dummy) producer ring
8605 * set on vector zero. This is the true hw prodring.
8606 */
8607 if (tg3_flag(tp, ENABLE_RSS))
8608 limit++;
8609
8610 for (i = 0; i < limit; i++) {
8611 struct tg3_napi *tnapi = &tp->napi[i];
8612
8613 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8614 goto err_out;
8615
8616 /* If multivector RSS is enabled, vector 0
8617 * does not handle rx or tx interrupts.
8618 * Don't allocate any resources for it.
8619 */
8620 if (!i && tg3_flag(tp, ENABLE_RSS))
8621 continue;
8622
Joe Perchesede23fa82013-08-26 22:45:23 -07008623 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8624 TG3_RX_RCB_RING_BYTES(tp),
8625 &tnapi->rx_rcb_mapping,
8626 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008627 if (!tnapi->rx_rcb)
8628 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008629 }
8630
8631 return 0;
8632
8633err_out:
8634 tg3_mem_rx_release(tp);
8635 return -ENOMEM;
8636}
8637
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008638/*
8639 * Must not be invoked with interrupt sources disabled and
8640 * the hardware shutdown down.
8641 */
8642static void tg3_free_consistent(struct tg3 *tp)
8643{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008644 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008645
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008646 for (i = 0; i < tp->irq_cnt; i++) {
8647 struct tg3_napi *tnapi = &tp->napi[i];
8648
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008649 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008650 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8651 tnapi->hw_status,
8652 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008653 tnapi->hw_status = NULL;
8654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008656
Michael Chan49a359e2012-09-28 07:12:37 +00008657 tg3_mem_rx_release(tp);
8658 tg3_mem_tx_release(tp);
8659
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008661 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8662 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663 tp->hw_stats = NULL;
8664 }
8665}
8666
8667/*
8668 * Must not be invoked with interrupt sources disabled and
8669 * the hardware shutdown down. Can sleep.
8670 */
8671static int tg3_alloc_consistent(struct tg3 *tp)
8672{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008673 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008674
Joe Perchesede23fa82013-08-26 22:45:23 -07008675 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8676 sizeof(struct tg3_hw_stats),
8677 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678 if (!tp->hw_stats)
8679 goto err_out;
8680
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008681 for (i = 0; i < tp->irq_cnt; i++) {
8682 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008683 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008684
Joe Perchesede23fa82013-08-26 22:45:23 -07008685 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8686 TG3_HW_STATUS_SIZE,
8687 &tnapi->status_mapping,
8688 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008689 if (!tnapi->hw_status)
8690 goto err_out;
8691
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008692 sblk = tnapi->hw_status;
8693
Michael Chan49a359e2012-09-28 07:12:37 +00008694 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008695 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008696
Michael Chan49a359e2012-09-28 07:12:37 +00008697 /*
8698 * When RSS is enabled, the status block format changes
8699 * slightly. The "rx_jumbo_consumer", "reserved",
8700 * and "rx_mini_consumer" members get mapped to the
8701 * other three rx return ring producer indexes.
8702 */
8703 switch (i) {
8704 case 1:
8705 prodptr = &sblk->idx[0].rx_producer;
8706 break;
8707 case 2:
8708 prodptr = &sblk->rx_jumbo_consumer;
8709 break;
8710 case 3:
8711 prodptr = &sblk->reserved;
8712 break;
8713 case 4:
8714 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008715 break;
8716 }
Michael Chan49a359e2012-09-28 07:12:37 +00008717 tnapi->rx_rcb_prod_idx = prodptr;
8718 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008719 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008720 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008721 }
8722
Michael Chan49a359e2012-09-28 07:12:37 +00008723 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8724 goto err_out;
8725
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 return 0;
8727
8728err_out:
8729 tg3_free_consistent(tp);
8730 return -ENOMEM;
8731}
8732
8733#define MAX_WAIT_CNT 1000
8734
8735/* To stop a block, clear the enable bit and poll till it
8736 * clears. tp->lock is held.
8737 */
Joe Perches953c96e2013-04-09 10:18:14 +00008738static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739{
8740 unsigned int i;
8741 u32 val;
8742
Joe Perches63c3a662011-04-26 08:12:10 +00008743 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 switch (ofs) {
8745 case RCVLSC_MODE:
8746 case DMAC_MODE:
8747 case MBFREE_MODE:
8748 case BUFMGR_MODE:
8749 case MEMARB_MODE:
8750 /* We can't enable/disable these bits of the
8751 * 5705/5750, just say success.
8752 */
8753 return 0;
8754
8755 default:
8756 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 }
8759
8760 val = tr32(ofs);
8761 val &= ~enable_bit;
8762 tw32_f(ofs, val);
8763
8764 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008765 if (pci_channel_offline(tp->pdev)) {
8766 dev_err(&tp->pdev->dev,
8767 "tg3_stop_block device offline, "
8768 "ofs=%lx enable_bit=%x\n",
8769 ofs, enable_bit);
8770 return -ENODEV;
8771 }
8772
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 udelay(100);
8774 val = tr32(ofs);
8775 if ((val & enable_bit) == 0)
8776 break;
8777 }
8778
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008779 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008780 dev_err(&tp->pdev->dev,
8781 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8782 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783 return -ENODEV;
8784 }
8785
8786 return 0;
8787}
8788
8789/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008790static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008791{
8792 int i, err;
8793
8794 tg3_disable_ints(tp);
8795
Gavin Shan6d446ec2013-06-25 15:24:32 +08008796 if (pci_channel_offline(tp->pdev)) {
8797 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8798 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8799 err = -ENODEV;
8800 goto err_no_dev;
8801 }
8802
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803 tp->rx_mode &= ~RX_MODE_ENABLE;
8804 tw32_f(MAC_RX_MODE, tp->rx_mode);
8805 udelay(10);
8806
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008807 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8808 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8809 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8810 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8811 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8812 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008814 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8815 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8816 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8817 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8818 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8819 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8820 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821
8822 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8823 tw32_f(MAC_MODE, tp->mac_mode);
8824 udelay(40);
8825
8826 tp->tx_mode &= ~TX_MODE_ENABLE;
8827 tw32_f(MAC_TX_MODE, tp->tx_mode);
8828
8829 for (i = 0; i < MAX_WAIT_CNT; i++) {
8830 udelay(100);
8831 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8832 break;
8833 }
8834 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008835 dev_err(&tp->pdev->dev,
8836 "%s timed out, TX_MODE_ENABLE will not clear "
8837 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008838 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839 }
8840
Michael Chane6de8ad2005-05-05 14:42:41 -07008841 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008842 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8843 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844
8845 tw32(FTQ_RESET, 0xffffffff);
8846 tw32(FTQ_RESET, 0x00000000);
8847
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008848 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8849 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
Gavin Shan6d446ec2013-06-25 15:24:32 +08008851err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008852 for (i = 0; i < tp->irq_cnt; i++) {
8853 struct tg3_napi *tnapi = &tp->napi[i];
8854 if (tnapi->hw_status)
8855 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008857
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858 return err;
8859}
8860
Michael Chanee6a99b2007-07-18 21:49:10 -07008861/* Save PCI command register before chip reset */
8862static void tg3_save_pci_state(struct tg3 *tp)
8863{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008864 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008865}
8866
8867/* Restore PCI state after chip reset */
8868static void tg3_restore_pci_state(struct tg3 *tp)
8869{
8870 u32 val;
8871
8872 /* Re-enable indirect register accesses. */
8873 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8874 tp->misc_host_ctrl);
8875
8876 /* Set MAX PCI retry to zero. */
8877 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008878 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008879 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008880 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008881 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008882 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008883 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008884 PCISTATE_ALLOW_APE_SHMEM_WR |
8885 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008886 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8887
Matt Carlson8a6eac92007-10-21 16:17:55 -07008888 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008889
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008890 if (!tg3_flag(tp, PCI_EXPRESS)) {
8891 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8892 tp->pci_cacheline_sz);
8893 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8894 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008895 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008896
Michael Chanee6a99b2007-07-18 21:49:10 -07008897 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008898 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008899 u16 pcix_cmd;
8900
8901 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8902 &pcix_cmd);
8903 pcix_cmd &= ~PCI_X_CMD_ERO;
8904 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8905 pcix_cmd);
8906 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008907
Joe Perches63c3a662011-04-26 08:12:10 +00008908 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008909
8910 /* Chip reset on 5780 will reset MSI enable bit,
8911 * so need to restore it.
8912 */
Joe Perches63c3a662011-04-26 08:12:10 +00008913 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008914 u16 ctrl;
8915
8916 pci_read_config_word(tp->pdev,
8917 tp->msi_cap + PCI_MSI_FLAGS,
8918 &ctrl);
8919 pci_write_config_word(tp->pdev,
8920 tp->msi_cap + PCI_MSI_FLAGS,
8921 ctrl | PCI_MSI_FLAGS_ENABLE);
8922 val = tr32(MSGINT_MODE);
8923 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8924 }
8925 }
8926}
8927
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928/* tp->lock is held. */
8929static int tg3_chip_reset(struct tg3 *tp)
8930{
8931 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008932 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008933 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934
David S. Millerf49639e2006-06-09 11:58:36 -07008935 tg3_nvram_lock(tp);
8936
Matt Carlson77b483f2008-08-15 14:07:24 -07008937 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8938
David S. Millerf49639e2006-06-09 11:58:36 -07008939 /* No matching tg3_nvram_unlock() after this because
8940 * chip reset below will undo the nvram lock.
8941 */
8942 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943
Michael Chanee6a99b2007-07-18 21:49:10 -07008944 /* GRC_MISC_CFG core clock reset will clear the memory
8945 * enable bit in PCI register 4 and the MSI enable bit
8946 * on some chips, so we save relevant registers here.
8947 */
8948 tg3_save_pci_state(tp);
8949
Joe Perches41535772013-02-16 11:20:04 +00008950 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008951 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008952 tw32(GRC_FASTBOOT_PC, 0);
8953
Linus Torvalds1da177e2005-04-16 15:20:36 -07008954 /*
8955 * We must avoid the readl() that normally takes place.
8956 * It locks machines, causes machine checks, and other
8957 * fun things. So, temporarily disable the 5701
8958 * hardware workaround, while we do the reset.
8959 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008960 write_op = tp->write32;
8961 if (write_op == tg3_write_flush_reg32)
8962 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963
Michael Chand18edcb2007-03-24 20:57:11 -07008964 /* Prevent the irq handler from reading or writing PCI registers
8965 * during chip reset when the memory enable bit in the PCI command
8966 * register may be cleared. The chip does not generate interrupt
8967 * at this time, but the irq handler may still be called due to irq
8968 * sharing or irqpoll.
8969 */
Joe Perches63c3a662011-04-26 08:12:10 +00008970 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008971 for (i = 0; i < tp->irq_cnt; i++) {
8972 struct tg3_napi *tnapi = &tp->napi[i];
8973 if (tnapi->hw_status) {
8974 tnapi->hw_status->status = 0;
8975 tnapi->hw_status->status_tag = 0;
8976 }
8977 tnapi->last_tag = 0;
8978 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008979 }
Michael Chand18edcb2007-03-24 20:57:11 -07008980 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008981
8982 for (i = 0; i < tp->irq_cnt; i++)
8983 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008984
Joe Perches41535772013-02-16 11:20:04 +00008985 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008986 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8987 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8988 }
8989
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 /* do the reset */
8991 val = GRC_MISC_CFG_CORECLK_RESET;
8992
Joe Perches63c3a662011-04-26 08:12:10 +00008993 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008994 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008995 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008996 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008997 tr32(TG3_PCIE_PHY_TSTCTL) ==
8998 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8999 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9000
Joe Perches41535772013-02-16 11:20:04 +00009001 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009002 tw32(GRC_MISC_CFG, (1 << 29));
9003 val |= (1 << 29);
9004 }
9005 }
9006
Joe Perches41535772013-02-16 11:20:04 +00009007 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009008 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9009 tw32(GRC_VCPU_EXT_CTRL,
9010 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9011 }
9012
Matt Carlsonf37500d2010-08-02 11:25:59 +00009013 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009014 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009016
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017 tw32(GRC_MISC_CFG, val);
9018
Michael Chan1ee582d2005-08-09 20:16:46 -07009019 /* restore 5701 hardware bug workaround write method */
9020 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021
9022 /* Unfortunately, we have to delay before the PCI read back.
9023 * Some 575X chips even will not respond to a PCI cfg access
9024 * when the reset command is given to the chip.
9025 *
9026 * How do these hardware designers expect things to work
9027 * properly if the PCI write is posted for a long period
9028 * of time? It is always necessary to have some method by
9029 * which a register read back can occur to push the write
9030 * out which does the reset.
9031 *
9032 * For most tg3 variants the trick below was working.
9033 * Ho hum...
9034 */
9035 udelay(120);
9036
9037 /* Flush PCI posted writes. The normal MMIO registers
9038 * are inaccessible at this time so this is the only
9039 * way to make this reliably (actually, this is no longer
9040 * the case, see above). I tried to use indirect
9041 * register read/write but this upset some 5701 variants.
9042 */
9043 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9044
9045 udelay(120);
9046
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009047 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009048 u16 val16;
9049
Joe Perches41535772013-02-16 11:20:04 +00009050 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009051 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 u32 cfg_val;
9053
9054 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009055 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056 udelay(100);
9057
9058 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9059 pci_write_config_dword(tp->pdev, 0xc4,
9060 cfg_val | (1 << 15));
9061 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009062
Matt Carlsone7126992009-08-25 10:08:16 +00009063 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009064 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009065 /*
9066 * Older PCIe devices only support the 128 byte
9067 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009068 */
Joe Perches63c3a662011-04-26 08:12:10 +00009069 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009070 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9071 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009072
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009073 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009074 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009075 PCI_EXP_DEVSTA_CED |
9076 PCI_EXP_DEVSTA_NFED |
9077 PCI_EXP_DEVSTA_FED |
9078 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009079 }
9080
Michael Chanee6a99b2007-07-18 21:49:10 -07009081 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082
Joe Perches63c3a662011-04-26 08:12:10 +00009083 tg3_flag_clear(tp, CHIP_RESETTING);
9084 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009085
Michael Chanee6a99b2007-07-18 21:49:10 -07009086 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009087 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009088 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009089 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090
Joe Perches41535772013-02-16 11:20:04 +00009091 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092 tg3_stop_fw(tp);
9093 tw32(0x5000, 0x400);
9094 }
9095
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009096 if (tg3_flag(tp, IS_SSB_CORE)) {
9097 /*
9098 * BCM4785: In order to avoid repercussions from using
9099 * potentially defective internal ROM, stop the Rx RISC CPU,
9100 * which is not required.
9101 */
9102 tg3_stop_fw(tp);
9103 tg3_halt_cpu(tp, RX_CPU_BASE);
9104 }
9105
Nithin Sujirfb03a432013-05-21 12:57:32 +00009106 err = tg3_poll_fw(tp);
9107 if (err)
9108 return err;
9109
Linus Torvalds1da177e2005-04-16 15:20:36 -07009110 tw32(GRC_MODE, tp->grc_mode);
9111
Joe Perches41535772013-02-16 11:20:04 +00009112 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009113 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009114
9115 tw32(0xc4, val | (1 << 15));
9116 }
9117
9118 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009119 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009121 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009122 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9123 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9124 }
9125
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009126 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009127 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009128 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009129 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009130 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009131 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009132 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009133 val = 0;
9134
9135 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136 udelay(40);
9137
Matt Carlson77b483f2008-08-15 14:07:24 -07009138 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9139
Matt Carlson0a9140c2009-08-28 12:27:50 +00009140 tg3_mdio_start(tp);
9141
Joe Perches63c3a662011-04-26 08:12:10 +00009142 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009143 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9144 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009145 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009146 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009147
9148 tw32(0x7c00, val | (1 << 25));
9149 }
9150
Joe Perches41535772013-02-16 11:20:04 +00009151 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00009152 val = tr32(TG3_CPMU_CLCK_ORIDE);
9153 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9154 }
9155
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009157 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009158 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9159 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9160
Joe Perches63c3a662011-04-26 08:12:10 +00009161 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009162 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9163 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9164 u32 nic_cfg;
9165
9166 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9167 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009168 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009169 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009170 if (tg3_flag(tp, 5750_PLUS))
9171 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009172
9173 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9174 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9175 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9176 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9177 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178 }
9179 }
9180
9181 return 0;
9182}
9183
Matt Carlson65ec6982012-02-28 23:33:37 +00009184static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9185static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009186
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009188static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189{
9190 int err;
9191
9192 tg3_stop_fw(tp);
9193
Michael Chan944d9802005-05-29 14:57:48 -07009194 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009196 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197 err = tg3_chip_reset(tp);
9198
Joe Perches953c96e2013-04-09 10:18:14 +00009199 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009200
Michael Chan944d9802005-05-29 14:57:48 -07009201 tg3_write_sig_legacy(tp, kind);
9202 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203
Matt Carlson92feeab2011-12-08 14:40:14 +00009204 if (tp->hw_stats) {
9205 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009206 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009207 tg3_get_estats(tp, &tp->estats_prev);
9208
9209 /* And make sure the next sample is new data */
9210 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9211 }
9212
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009214}
9215
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216static int tg3_set_mac_addr(struct net_device *dev, void *p)
9217{
9218 struct tg3 *tp = netdev_priv(dev);
9219 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009220 int err = 0;
9221 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222
Michael Chanf9804dd2005-09-27 12:13:10 -07009223 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009224 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009225
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9227
Michael Chane75f7c92006-03-20 21:33:26 -08009228 if (!netif_running(dev))
9229 return 0;
9230
Joe Perches63c3a662011-04-26 08:12:10 +00009231 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009232 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009233
Michael Chan986e0ae2007-05-05 12:10:20 -07009234 addr0_high = tr32(MAC_ADDR_0_HIGH);
9235 addr0_low = tr32(MAC_ADDR_0_LOW);
9236 addr1_high = tr32(MAC_ADDR_1_HIGH);
9237 addr1_low = tr32(MAC_ADDR_1_LOW);
9238
9239 /* Skip MAC addr 1 if ASF is using it. */
9240 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9241 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009242 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009243 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009244 spin_lock_bh(&tp->lock);
9245 __tg3_set_mac_addr(tp, skip_mac_1);
9246 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247
Michael Chanb9ec6c12006-07-25 16:37:27 -07009248 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249}
9250
9251/* tp->lock is held. */
9252static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9253 dma_addr_t mapping, u32 maxlen_flags,
9254 u32 nic_addr)
9255{
9256 tg3_write_mem(tp,
9257 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9258 ((u64) mapping >> 32));
9259 tg3_write_mem(tp,
9260 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9261 ((u64) mapping & 0xffffffff));
9262 tg3_write_mem(tp,
9263 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9264 maxlen_flags);
9265
Joe Perches63c3a662011-04-26 08:12:10 +00009266 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267 tg3_write_mem(tp,
9268 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9269 nic_addr);
9270}
9271
Michael Chana489b6d2012-09-28 07:12:39 +00009272
9273static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009274{
Michael Chana489b6d2012-09-28 07:12:39 +00009275 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009276
Joe Perches63c3a662011-04-26 08:12:10 +00009277 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009278 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9279 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9280 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009281 } else {
9282 tw32(HOSTCC_TXCOL_TICKS, 0);
9283 tw32(HOSTCC_TXMAX_FRAMES, 0);
9284 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009285
9286 for (; i < tp->txq_cnt; i++) {
9287 u32 reg;
9288
9289 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9290 tw32(reg, ec->tx_coalesce_usecs);
9291 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9292 tw32(reg, ec->tx_max_coalesced_frames);
9293 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9294 tw32(reg, ec->tx_max_coalesced_frames_irq);
9295 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009296 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009297
Michael Chana489b6d2012-09-28 07:12:39 +00009298 for (; i < tp->irq_max - 1; i++) {
9299 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9300 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9301 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9302 }
9303}
9304
9305static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9306{
9307 int i = 0;
9308 u32 limit = tp->rxq_cnt;
9309
Joe Perches63c3a662011-04-26 08:12:10 +00009310 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009311 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9312 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9313 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009314 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009315 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009316 tw32(HOSTCC_RXCOL_TICKS, 0);
9317 tw32(HOSTCC_RXMAX_FRAMES, 0);
9318 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009319 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009320
Michael Chana489b6d2012-09-28 07:12:39 +00009321 for (; i < limit; i++) {
9322 u32 reg;
9323
9324 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9325 tw32(reg, ec->rx_coalesce_usecs);
9326 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9327 tw32(reg, ec->rx_max_coalesced_frames);
9328 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9329 tw32(reg, ec->rx_max_coalesced_frames_irq);
9330 }
9331
9332 for (; i < tp->irq_max - 1; i++) {
9333 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9334 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9335 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9336 }
9337}
9338
9339static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9340{
9341 tg3_coal_tx_init(tp, ec);
9342 tg3_coal_rx_init(tp, ec);
9343
Joe Perches63c3a662011-04-26 08:12:10 +00009344 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009345 u32 val = ec->stats_block_coalesce_usecs;
9346
Matt Carlsonb6080e12009-09-01 13:12:00 +00009347 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9348 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9349
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009350 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009351 val = 0;
9352
9353 tw32(HOSTCC_STAT_COAL_TICKS, val);
9354 }
9355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356
9357/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009358static void tg3_tx_rcbs_disable(struct tg3 *tp)
9359{
9360 u32 txrcb, limit;
9361
9362 /* Disable all transmit rings but the first. */
9363 if (!tg3_flag(tp, 5705_PLUS))
9364 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9365 else if (tg3_flag(tp, 5717_PLUS))
9366 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9367 else if (tg3_flag(tp, 57765_CLASS) ||
9368 tg3_asic_rev(tp) == ASIC_REV_5762)
9369 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9370 else
9371 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9372
9373 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9374 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9375 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9376 BDINFO_FLAGS_DISABLED);
9377}
9378
9379/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009380static void tg3_tx_rcbs_init(struct tg3 *tp)
9381{
9382 int i = 0;
9383 u32 txrcb = NIC_SRAM_SEND_RCB;
9384
9385 if (tg3_flag(tp, ENABLE_TSS))
9386 i++;
9387
9388 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9389 struct tg3_napi *tnapi = &tp->napi[i];
9390
9391 if (!tnapi->tx_ring)
9392 continue;
9393
9394 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9395 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9396 NIC_SRAM_TX_BUFFER_DESC);
9397 }
9398}
9399
9400/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009401static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9402{
9403 u32 rxrcb, limit;
9404
9405 /* Disable all receive return rings but the first. */
9406 if (tg3_flag(tp, 5717_PLUS))
9407 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9408 else if (!tg3_flag(tp, 5705_PLUS))
9409 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9410 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9411 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9412 tg3_flag(tp, 57765_CLASS))
9413 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9414 else
9415 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9416
9417 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9418 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9419 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9420 BDINFO_FLAGS_DISABLED);
9421}
9422
9423/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009424static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9425{
9426 int i = 0;
9427 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9428
9429 if (tg3_flag(tp, ENABLE_RSS))
9430 i++;
9431
9432 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9433 struct tg3_napi *tnapi = &tp->napi[i];
9434
9435 if (!tnapi->rx_rcb)
9436 continue;
9437
9438 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9439 (tp->rx_ret_ring_mask + 1) <<
9440 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9441 }
9442}
9443
9444/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009445static void tg3_rings_reset(struct tg3 *tp)
9446{
9447 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009448 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009449 struct tg3_napi *tnapi = &tp->napi[0];
9450
Nithin Sujir328947f2013-05-23 11:11:24 +00009451 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009452
Nithin Sujir328947f2013-05-23 11:11:24 +00009453 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009454
9455 /* Disable interrupts */
9456 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009457 tp->napi[0].chk_msi_cnt = 0;
9458 tp->napi[0].last_rx_cons = 0;
9459 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009460
9461 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009462 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009463 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009464 tp->napi[i].tx_prod = 0;
9465 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009466 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009467 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009468 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9469 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009470 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009471 tp->napi[i].last_rx_cons = 0;
9472 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009473 }
Joe Perches63c3a662011-04-26 08:12:10 +00009474 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009475 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009476 } else {
9477 tp->napi[0].tx_prod = 0;
9478 tp->napi[0].tx_cons = 0;
9479 tw32_mailbox(tp->napi[0].prodmbox, 0);
9480 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9481 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009482
9483 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009484 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009485 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9486 for (i = 0; i < 16; i++)
9487 tw32_tx_mbox(mbox + i * 8, 0);
9488 }
9489
Matt Carlson2d31eca2009-09-01 12:53:31 +00009490 /* Clear status block in ram. */
9491 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9492
9493 /* Set status block DMA address */
9494 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9495 ((u64) tnapi->status_mapping >> 32));
9496 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9497 ((u64) tnapi->status_mapping & 0xffffffff));
9498
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009499 stblk = HOSTCC_STATBLCK_RING1;
9500
9501 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9502 u64 mapping = (u64)tnapi->status_mapping;
9503 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9504 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009505 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009506
9507 /* Clear status block in ram. */
9508 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009509 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009510
9511 tg3_tx_rcbs_init(tp);
9512 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009513}
9514
Matt Carlsoneb07a942011-04-20 07:57:36 +00009515static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9516{
9517 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9518
Joe Perches63c3a662011-04-26 08:12:10 +00009519 if (!tg3_flag(tp, 5750_PLUS) ||
9520 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009521 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9522 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009523 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009524 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009525 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9526 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009527 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9528 else
9529 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9530
9531 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9532 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9533
9534 val = min(nic_rep_thresh, host_rep_thresh);
9535 tw32(RCVBDI_STD_THRESH, val);
9536
Joe Perches63c3a662011-04-26 08:12:10 +00009537 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009538 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9539
Joe Perches63c3a662011-04-26 08:12:10 +00009540 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009541 return;
9542
Matt Carlson513aa6e2011-11-21 15:01:18 +00009543 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009544
9545 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9546
9547 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9548 tw32(RCVBDI_JUMBO_THRESH, val);
9549
Joe Perches63c3a662011-04-26 08:12:10 +00009550 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009551 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9552}
9553
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009554static inline u32 calc_crc(unsigned char *buf, int len)
9555{
9556 u32 reg;
9557 u32 tmp;
9558 int j, k;
9559
9560 reg = 0xffffffff;
9561
9562 for (j = 0; j < len; j++) {
9563 reg ^= buf[j];
9564
9565 for (k = 0; k < 8; k++) {
9566 tmp = reg & 0x01;
9567
9568 reg >>= 1;
9569
9570 if (tmp)
9571 reg ^= 0xedb88320;
9572 }
9573 }
9574
9575 return ~reg;
9576}
9577
9578static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9579{
9580 /* accept or reject all multicast frames */
9581 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9582 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9583 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9584 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9585}
9586
9587static void __tg3_set_rx_mode(struct net_device *dev)
9588{
9589 struct tg3 *tp = netdev_priv(dev);
9590 u32 rx_mode;
9591
9592 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9593 RX_MODE_KEEP_VLAN_TAG);
9594
9595#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9596 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9597 * flag clear.
9598 */
9599 if (!tg3_flag(tp, ENABLE_ASF))
9600 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9601#endif
9602
9603 if (dev->flags & IFF_PROMISC) {
9604 /* Promiscuous mode. */
9605 rx_mode |= RX_MODE_PROMISC;
9606 } else if (dev->flags & IFF_ALLMULTI) {
9607 /* Accept all multicast. */
9608 tg3_set_multi(tp, 1);
9609 } else if (netdev_mc_empty(dev)) {
9610 /* Reject all multicast. */
9611 tg3_set_multi(tp, 0);
9612 } else {
9613 /* Accept one or more multicast(s). */
9614 struct netdev_hw_addr *ha;
9615 u32 mc_filter[4] = { 0, };
9616 u32 regidx;
9617 u32 bit;
9618 u32 crc;
9619
9620 netdev_for_each_mc_addr(ha, dev) {
9621 crc = calc_crc(ha->addr, ETH_ALEN);
9622 bit = ~crc & 0x7f;
9623 regidx = (bit & 0x60) >> 5;
9624 bit &= 0x1f;
9625 mc_filter[regidx] |= (1 << bit);
9626 }
9627
9628 tw32(MAC_HASH_REG_0, mc_filter[0]);
9629 tw32(MAC_HASH_REG_1, mc_filter[1]);
9630 tw32(MAC_HASH_REG_2, mc_filter[2]);
9631 tw32(MAC_HASH_REG_3, mc_filter[3]);
9632 }
9633
9634 if (rx_mode != tp->rx_mode) {
9635 tp->rx_mode = rx_mode;
9636 tw32_f(MAC_RX_MODE, rx_mode);
9637 udelay(10);
9638 }
9639}
9640
Michael Chan91024262012-09-28 07:12:38 +00009641static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009642{
9643 int i;
9644
9645 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009646 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009647}
9648
9649static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009650{
9651 int i;
9652
9653 if (!tg3_flag(tp, SUPPORT_MSIX))
9654 return;
9655
Michael Chan0b3ba052012-11-14 14:44:29 +00009656 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009657 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009658 return;
9659 }
9660
9661 /* Validate table against current IRQ count */
9662 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009663 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009664 break;
9665 }
9666
9667 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009668 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009669}
9670
Matt Carlson90415472011-12-16 13:33:23 +00009671static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009672{
9673 int i = 0;
9674 u32 reg = MAC_RSS_INDIR_TBL_0;
9675
9676 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9677 u32 val = tp->rss_ind_tbl[i];
9678 i++;
9679 for (; i % 8; i++) {
9680 val <<= 4;
9681 val |= tp->rss_ind_tbl[i];
9682 }
9683 tw32(reg, val);
9684 reg += 4;
9685 }
9686}
9687
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009688static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9689{
9690 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9691 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9692 else
9693 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9694}
9695
Matt Carlson2d31eca2009-09-01 12:53:31 +00009696/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009697static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698{
9699 u32 val, rdmac_mode;
9700 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009701 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702
9703 tg3_disable_ints(tp);
9704
9705 tg3_stop_fw(tp);
9706
9707 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9708
Joe Perches63c3a662011-04-26 08:12:10 +00009709 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009710 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009712 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9713 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9714 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009715 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009716 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9717 }
9718
Nithin Sujir400dfba2013-05-18 06:26:53 +00009719 /* Enable MAC control of LPI */
9720 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9721 tg3_setup_eee(tp);
9722
Matt Carlson603f1172010-02-12 14:47:10 +00009723 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009724 tg3_phy_reset(tp);
9725
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726 err = tg3_chip_reset(tp);
9727 if (err)
9728 return err;
9729
9730 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9731
Joe Perches41535772013-02-16 11:20:04 +00009732 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009733 val = tr32(TG3_CPMU_CTRL);
9734 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9735 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009736
9737 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9738 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9739 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9740 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9741
9742 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9743 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9744 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9745 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9746
9747 val = tr32(TG3_CPMU_HST_ACC);
9748 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9749 val |= CPMU_HST_ACC_MACCLK_6_25;
9750 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009751 }
9752
Joe Perches41535772013-02-16 11:20:04 +00009753 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009754 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9755 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9756 PCIE_PWR_MGMT_L1_THRESH_4MS;
9757 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009758
9759 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9760 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9761
9762 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009763
Matt Carlsonf40386c2009-11-02 14:24:02 +00009764 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9765 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009766 }
9767
Joe Perches63c3a662011-04-26 08:12:10 +00009768 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009769 u32 grc_mode = tr32(GRC_MODE);
9770
9771 /* Access the lower 1K of PL PCIE block registers. */
9772 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9773 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9774
9775 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9776 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9777 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9778
9779 tw32(GRC_MODE, grc_mode);
9780 }
9781
Matt Carlson55086ad2011-12-14 11:09:59 +00009782 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009783 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009784 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009785
Matt Carlson5093eed2010-11-24 08:31:45 +00009786 /* Access the lower 1K of PL PCIE block registers. */
9787 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9788 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009789
Matt Carlson5093eed2010-11-24 08:31:45 +00009790 val = tr32(TG3_PCIE_TLDLPL_PORT +
9791 TG3_PCIE_PL_LO_PHYCTL5);
9792 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9793 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009794
Matt Carlson5093eed2010-11-24 08:31:45 +00009795 tw32(GRC_MODE, grc_mode);
9796 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009797
Joe Perches41535772013-02-16 11:20:04 +00009798 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009799 u32 grc_mode;
9800
9801 /* Fix transmit hangs */
9802 val = tr32(TG3_CPMU_PADRNG_CTL);
9803 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9804 tw32(TG3_CPMU_PADRNG_CTL, val);
9805
9806 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009807
9808 /* Access the lower 1K of DL PCIE block registers. */
9809 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9810 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9811
9812 val = tr32(TG3_PCIE_TLDLPL_PORT +
9813 TG3_PCIE_DL_LO_FTSMAX);
9814 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9815 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9816 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9817
9818 tw32(GRC_MODE, grc_mode);
9819 }
9820
Matt Carlsona977dbe2010-04-12 06:58:26 +00009821 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9822 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9823 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9824 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009825 }
9826
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827 /* This works around an issue with Athlon chipsets on
9828 * B3 tigon3 silicon. This bit has no effect on any
9829 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009830 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831 */
Joe Perches63c3a662011-04-26 08:12:10 +00009832 if (!tg3_flag(tp, CPMU_PRESENT)) {
9833 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009834 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9835 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837
Joe Perches41535772013-02-16 11:20:04 +00009838 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009839 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 val = tr32(TG3PCI_PCISTATE);
9841 val |= PCISTATE_RETRY_SAME_DMA;
9842 tw32(TG3PCI_PCISTATE, val);
9843 }
9844
Joe Perches63c3a662011-04-26 08:12:10 +00009845 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009846 /* Allow reads and writes to the
9847 * APE register and memory space.
9848 */
9849 val = tr32(TG3PCI_PCISTATE);
9850 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009851 PCISTATE_ALLOW_APE_SHMEM_WR |
9852 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009853 tw32(TG3PCI_PCISTATE, val);
9854 }
9855
Joe Perches41535772013-02-16 11:20:04 +00009856 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009857 /* Enable some hw fixes. */
9858 val = tr32(TG3PCI_MSI_DATA);
9859 val |= (1 << 26) | (1 << 28) | (1 << 29);
9860 tw32(TG3PCI_MSI_DATA, val);
9861 }
9862
9863 /* Descriptor ring init may make accesses to the
9864 * NIC SRAM area to setup the TX descriptors, so we
9865 * can only do this after the hardware has been
9866 * successfully reset.
9867 */
Michael Chan32d8c572006-07-25 16:38:29 -07009868 err = tg3_init_rings(tp);
9869 if (err)
9870 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871
Joe Perches63c3a662011-04-26 08:12:10 +00009872 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009873 val = tr32(TG3PCI_DMA_RW_CTRL) &
9874 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009875 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009876 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009877 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009878 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9879 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009880 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009881 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009882 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9883 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009884 /* This value is determined during the probe time DMA
9885 * engine test, tg3_test_dma.
9886 */
9887 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009889
9890 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9891 GRC_MODE_4X_NIC_SEND_RINGS |
9892 GRC_MODE_NO_TX_PHDR_CSUM |
9893 GRC_MODE_NO_RX_PHDR_CSUM);
9894 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009895
9896 /* Pseudo-header checksum is done by hardware logic and not
9897 * the offload processers, so make the chip do the pseudo-
9898 * header checksums on receive. For transmit it is more
9899 * convenient to do the pseudo-header checksum in software
9900 * as Linux does that on transmit for us in all cases.
9901 */
9902 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009903
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009904 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9905 if (tp->rxptpctl)
9906 tw32(TG3_RX_PTP_CTL,
9907 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9908
9909 if (tg3_flag(tp, PTP_CAPABLE))
9910 val |= GRC_MODE_TIME_SYNC_ENABLE;
9911
9912 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913
9914 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9915 val = tr32(GRC_MISC_CFG);
9916 val &= ~0xff;
9917 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9918 tw32(GRC_MISC_CFG, val);
9919
9920 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009921 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009922 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009923 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009925 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009926 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9927 else
9928 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9929 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9930 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009931 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932 int fw_len;
9933
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009934 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009935 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9936 tw32(BUFMGR_MB_POOL_ADDR,
9937 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9938 tw32(BUFMGR_MB_POOL_SIZE,
9939 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009941
Michael Chan0f893dc2005-07-25 12:30:38 -07009942 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9944 tp->bufmgr_config.mbuf_read_dma_low_water);
9945 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9946 tp->bufmgr_config.mbuf_mac_rx_low_water);
9947 tw32(BUFMGR_MB_HIGH_WATER,
9948 tp->bufmgr_config.mbuf_high_water);
9949 } else {
9950 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9951 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9952 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9953 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9954 tw32(BUFMGR_MB_HIGH_WATER,
9955 tp->bufmgr_config.mbuf_high_water_jumbo);
9956 }
9957 tw32(BUFMGR_DMA_LOW_WATER,
9958 tp->bufmgr_config.dma_low_water);
9959 tw32(BUFMGR_DMA_HIGH_WATER,
9960 tp->bufmgr_config.dma_high_water);
9961
Matt Carlsond309a462010-09-30 10:34:31 +00009962 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009963 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009964 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009965 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9966 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9967 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009968 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009969 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970 for (i = 0; i < 2000; i++) {
9971 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9972 break;
9973 udelay(10);
9974 }
9975 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009976 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977 return -ENODEV;
9978 }
9979
Joe Perches41535772013-02-16 11:20:04 +00009980 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009981 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009982
Matt Carlsoneb07a942011-04-20 07:57:36 +00009983 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984
9985 /* Initialize TG3_BDINFO's at:
9986 * RCVDBDI_STD_BD: standard eth size rx ring
9987 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9988 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9989 *
9990 * like so:
9991 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9992 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9993 * ring attribute flags
9994 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9995 *
9996 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9997 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9998 *
9999 * The size of each ring is fixed in the firmware, but the location is
10000 * configurable.
10001 */
10002 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010003 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010004 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010005 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010006 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010007 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10008 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010010 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010011 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10013 BDINFO_FLAGS_DISABLED);
10014
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010015 /* Program the jumbo buffer descriptor ring control
10016 * blocks on those devices that have them.
10017 */
Joe Perches41535772013-02-16 11:20:04 +000010018 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010019 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010020
Joe Perches63c3a662011-04-26 08:12:10 +000010021 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010023 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010024 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010025 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010026 val = TG3_RX_JMB_RING_SIZE(tp) <<
10027 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010029 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010030 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010031 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010032 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010033 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10034 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035 } else {
10036 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10037 BDINFO_FLAGS_DISABLED);
10038 }
10039
Joe Perches63c3a662011-04-26 08:12:10 +000010040 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010041 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010042 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10043 val |= (TG3_RX_STD_DMA_SZ << 2);
10044 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010045 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010046 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010047 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010048
10049 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010050
Matt Carlson411da642009-11-13 13:03:46 +000010051 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +000010052 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010053
Joe Perches63c3a662011-04-26 08:12:10 +000010054 tpr->rx_jmb_prod_idx =
10055 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +000010056 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057
Matt Carlson2d31eca2009-09-01 12:53:31 +000010058 tg3_rings_reset(tp);
10059
Linus Torvalds1da177e2005-04-16 15:20:36 -070010060 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010061 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062
10063 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010064 tw32(MAC_RX_MTU_SIZE,
10065 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066
10067 /* The slot time is changed by tg3_setup_phy if we
10068 * run at gigabit with half duplex.
10069 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010070 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10071 (6 << TX_LENGTHS_IPG_SHIFT) |
10072 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10073
Joe Perches41535772013-02-16 11:20:04 +000010074 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10075 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010076 val |= tr32(MAC_TX_LENGTHS) &
10077 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10078 TX_LENGTHS_CNT_DWN_VAL_MSK);
10079
10080 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010081
10082 /* Receive rules. */
10083 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10084 tw32(RCVLPC_CONFIG, 0x0181);
10085
10086 /* Calculate RDMAC_MODE setting early, we need it to determine
10087 * the RCVLPC_STATE_ENABLE mask.
10088 */
10089 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10090 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10091 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10092 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10093 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010094
Joe Perches41535772013-02-16 11:20:04 +000010095 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010096 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10097
Joe Perches41535772013-02-16 11:20:04 +000010098 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10099 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10100 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010101 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10102 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10103 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10104
Joe Perches41535772013-02-16 11:20:04 +000010105 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10106 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010107 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010108 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10110 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010111 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10113 }
10114 }
10115
Joe Perches63c3a662011-04-26 08:12:10 +000010116 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010117 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10118
Joe Perches41535772013-02-16 11:20:04 +000010119 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010120 tp->dma_limit = 0;
10121 if (tp->dev->mtu <= ETH_DATA_LEN) {
10122 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10123 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10124 }
10125 }
10126
Joe Perches63c3a662011-04-26 08:12:10 +000010127 if (tg3_flag(tp, HW_TSO_1) ||
10128 tg3_flag(tp, HW_TSO_2) ||
10129 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010130 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10131
Matt Carlson108a6c12011-05-19 12:12:47 +000010132 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010133 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10134 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010135 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136
Joe Perches41535772013-02-16 11:20:04 +000010137 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10138 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010139 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10140
Joe Perches41535772013-02-16 11:20:04 +000010141 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10142 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10143 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10144 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010145 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010146 u32 tgtreg;
10147
Joe Perches41535772013-02-16 11:20:04 +000010148 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010149 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10150 else
10151 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10152
10153 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010154 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10155 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010156 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10157 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10158 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10159 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10160 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10161 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010162 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010163 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010164 }
10165
Joe Perches41535772013-02-16 11:20:04 +000010166 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10167 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10168 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010169 u32 tgtreg;
10170
Joe Perches41535772013-02-16 11:20:04 +000010171 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010172 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10173 else
10174 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10175
10176 val = tr32(tgtreg);
10177 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010178 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10179 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10180 }
10181
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010183 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010184 val = tr32(RCVLPC_STATS_ENABLE);
10185 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10186 tw32(RCVLPC_STATS_ENABLE, val);
10187 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010188 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 val = tr32(RCVLPC_STATS_ENABLE);
10190 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10191 tw32(RCVLPC_STATS_ENABLE, val);
10192 } else {
10193 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10194 }
10195 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10196 tw32(SNDDATAI_STATSENAB, 0xffffff);
10197 tw32(SNDDATAI_STATSCTRL,
10198 (SNDDATAI_SCTRL_ENABLE |
10199 SNDDATAI_SCTRL_FASTUPD));
10200
10201 /* Setup host coalescing engine. */
10202 tw32(HOSTCC_MODE, 0);
10203 for (i = 0; i < 2000; i++) {
10204 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10205 break;
10206 udelay(10);
10207 }
10208
Michael Chand244c892005-07-05 14:42:33 -070010209 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010210
Joe Perches63c3a662011-04-26 08:12:10 +000010211 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010212 /* Status/statistics block address. See tg3_timer,
10213 * the tg3_periodic_fetch_stats call there, and
10214 * tg3_get_stats to see how this works for 5705/5750 chips.
10215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010216 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10217 ((u64) tp->stats_mapping >> 32));
10218 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10219 ((u64) tp->stats_mapping & 0xffffffff));
10220 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010221
Linus Torvalds1da177e2005-04-16 15:20:36 -070010222 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010223
10224 /* Clear statistics and status block memory areas */
10225 for (i = NIC_SRAM_STATS_BLK;
10226 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10227 i += sizeof(u32)) {
10228 tg3_write_mem(tp, i, 0);
10229 udelay(40);
10230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231 }
10232
10233 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10234
10235 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10236 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010237 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10239
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010240 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10241 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010242 /* reset to prevent losing 1st rx packet intermittently */
10243 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10244 udelay(10);
10245 }
10246
Matt Carlson3bda1252008-08-15 14:08:22 -070010247 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010248 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10249 MAC_MODE_FHDE_ENABLE;
10250 if (tg3_flag(tp, ENABLE_APE))
10251 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010252 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010253 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010254 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010255 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010256 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10257 udelay(40);
10258
Michael Chan314fba32005-04-21 17:07:04 -070010259 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010260 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010261 * register to preserve the GPIO settings for LOMs. The GPIOs,
10262 * whether used as inputs or outputs, are set by boot code after
10263 * reset.
10264 */
Joe Perches63c3a662011-04-26 08:12:10 +000010265 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010266 u32 gpio_mask;
10267
Michael Chan9d26e212006-12-07 00:21:14 -080010268 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10269 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10270 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010271
Joe Perches41535772013-02-16 11:20:04 +000010272 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010273 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10274 GRC_LCLCTRL_GPIO_OUTPUT3;
10275
Joe Perches41535772013-02-16 11:20:04 +000010276 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010277 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10278
Gary Zambranoaaf84462007-05-05 11:51:45 -070010279 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010280 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10281
10282 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010283 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010284 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10285 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10288 udelay(100);
10289
Matt Carlsonc3b50032012-01-17 15:27:23 +000010290 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010291 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010292 val |= MSGINT_MODE_ENABLE;
10293 if (tp->irq_cnt > 1)
10294 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010295 if (!tg3_flag(tp, 1SHOT_MSI))
10296 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010297 tw32(MSGINT_MODE, val);
10298 }
10299
Joe Perches63c3a662011-04-26 08:12:10 +000010300 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010301 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10302 udelay(40);
10303 }
10304
10305 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10306 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10307 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10308 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10309 WDMAC_MODE_LNGREAD_ENAB);
10310
Joe Perches41535772013-02-16 11:20:04 +000010311 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10312 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010313 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010314 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10315 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316 /* nothing */
10317 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010318 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010319 val |= WDMAC_MODE_RX_ACCEL;
10320 }
10321 }
10322
Michael Chand9ab5ad12006-03-20 22:27:35 -080010323 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010324 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010325 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010326
Joe Perches41535772013-02-16 11:20:04 +000010327 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010328 val |= WDMAC_MODE_BURST_ALL_DATA;
10329
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330 tw32_f(WDMAC_MODE, val);
10331 udelay(40);
10332
Joe Perches63c3a662011-04-26 08:12:10 +000010333 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010334 u16 pcix_cmd;
10335
10336 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10337 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010338 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010339 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10340 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010341 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010342 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10343 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010344 }
Matt Carlson9974a352007-10-07 23:27:28 -070010345 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10346 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010347 }
10348
10349 tw32_f(RDMAC_MODE, rdmac_mode);
10350 udelay(40);
10351
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010352 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10353 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010354 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10355 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10356 break;
10357 }
10358 if (i < TG3_NUM_RDMA_CHANNELS) {
10359 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010360 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010361 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010362 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010363 }
10364 }
10365
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010367 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010368 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010369
Joe Perches41535772013-02-16 11:20:04 +000010370 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010371 tw32(SNDDATAC_MODE,
10372 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10373 else
10374 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10375
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10377 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010378 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010379 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010380 val |= RCVDBDI_MODE_LRG_RING_SZ;
10381 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010382 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010383 if (tg3_flag(tp, HW_TSO_1) ||
10384 tg3_flag(tp, HW_TSO_2) ||
10385 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010386 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010387 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010388 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010389 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10390 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010391 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10392
Joe Perches41535772013-02-16 11:20:04 +000010393 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394 err = tg3_load_5701_a0_firmware_fix(tp);
10395 if (err)
10396 return err;
10397 }
10398
Nithin Sujirc4dab502013-03-06 17:02:34 +000010399 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10400 /* Ignore any errors for the firmware download. If download
10401 * fails, the device will operate with EEE disabled
10402 */
10403 tg3_load_57766_firmware(tp);
10404 }
10405
Joe Perches63c3a662011-04-26 08:12:10 +000010406 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407 err = tg3_load_tso_firmware(tp);
10408 if (err)
10409 return err;
10410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010411
10412 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010413
Joe Perches63c3a662011-04-26 08:12:10 +000010414 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010415 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010416 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010417
Joe Perches41535772013-02-16 11:20:04 +000010418 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10419 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010420 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10421 tp->tx_mode &= ~val;
10422 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10423 }
10424
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 tw32_f(MAC_TX_MODE, tp->tx_mode);
10426 udelay(100);
10427
Joe Perches63c3a662011-04-26 08:12:10 +000010428 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010429 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010430
10431 /* Setup the "secret" hash key. */
10432 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10433 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10434 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10435 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10436 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10437 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10438 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10439 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10440 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10441 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10442 }
10443
Linus Torvalds1da177e2005-04-16 15:20:36 -070010444 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010445 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010446 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10447
Nithin Sujir378b72c2013-07-29 13:58:39 -070010448 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10449 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10450
Joe Perches63c3a662011-04-26 08:12:10 +000010451 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010452 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10453 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10454 RX_MODE_RSS_IPV6_HASH_EN |
10455 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10456 RX_MODE_RSS_IPV4_HASH_EN |
10457 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10458
Linus Torvalds1da177e2005-04-16 15:20:36 -070010459 tw32_f(MAC_RX_MODE, tp->rx_mode);
10460 udelay(10);
10461
Linus Torvalds1da177e2005-04-16 15:20:36 -070010462 tw32(MAC_LED_CTRL, tp->led_ctrl);
10463
10464 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010465 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10467 udelay(10);
10468 }
10469 tw32_f(MAC_RX_MODE, tp->rx_mode);
10470 udelay(10);
10471
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010472 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010473 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10474 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010475 /* Set drive transmission level to 1.2V */
10476 /* only if the signal pre-emphasis bit is not set */
10477 val = tr32(MAC_SERDES_CFG);
10478 val &= 0xfffff000;
10479 val |= 0x880;
10480 tw32(MAC_SERDES_CFG, val);
10481 }
Joe Perches41535772013-02-16 11:20:04 +000010482 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010483 tw32(MAC_SERDES_CFG, 0x616000);
10484 }
10485
10486 /* Prevent chip from dropping frames when flow control
10487 * is enabled.
10488 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010489 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010490 val = 1;
10491 else
10492 val = 2;
10493 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010494
Joe Perches41535772013-02-16 11:20:04 +000010495 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010496 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010497 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010498 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499 }
10500
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010501 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010502 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010503 u32 tmp;
10504
10505 tmp = tr32(SERDES_RX_CTRL);
10506 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10507 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10508 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10509 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10510 }
10511
Joe Perches63c3a662011-04-26 08:12:10 +000010512 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010513 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010514 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010515
Joe Perches953c96e2013-04-09 10:18:14 +000010516 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010517 if (err)
10518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010519
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010520 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10521 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010522 u32 tmp;
10523
10524 /* Clear CRC stats. */
10525 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10526 tg3_writephy(tp, MII_TG3_TEST1,
10527 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010528 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530 }
10531 }
10532
10533 __tg3_set_rx_mode(tp->dev);
10534
10535 /* Initialize receive rules. */
10536 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10537 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10538 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10539 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10540
Joe Perches63c3a662011-04-26 08:12:10 +000010541 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542 limit = 8;
10543 else
10544 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010545 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010546 limit -= 4;
10547 switch (limit) {
10548 case 16:
10549 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10550 case 15:
10551 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10552 case 14:
10553 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10554 case 13:
10555 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10556 case 12:
10557 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10558 case 11:
10559 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10560 case 10:
10561 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10562 case 9:
10563 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10564 case 8:
10565 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10566 case 7:
10567 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10568 case 6:
10569 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10570 case 5:
10571 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10572 case 4:
10573 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10574 case 3:
10575 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10576 case 2:
10577 case 1:
10578
10579 default:
10580 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010582
Joe Perches63c3a662011-04-26 08:12:10 +000010583 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010584 /* Write our heartbeat update interval to APE. */
10585 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10586 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010587
Linus Torvalds1da177e2005-04-16 15:20:36 -070010588 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10589
Linus Torvalds1da177e2005-04-16 15:20:36 -070010590 return 0;
10591}
10592
10593/* Called at device open time to get the chip ready for
10594 * packet processing. Invoked with tp->lock held.
10595 */
Joe Perches953c96e2013-04-09 10:18:14 +000010596static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010598 /* Chip may have been just powered on. If so, the boot code may still
10599 * be running initialization. Wait for it to finish to avoid races in
10600 * accessing the hardware.
10601 */
10602 tg3_enable_register_access(tp);
10603 tg3_poll_fw(tp);
10604
Linus Torvalds1da177e2005-04-16 15:20:36 -070010605 tg3_switch_clocks(tp);
10606
10607 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10608
Matt Carlson2f751b62008-08-04 23:17:34 -070010609 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010610}
10611
Michael Chanaed93e02012-07-16 16:24:02 +000010612static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10613{
10614 int i;
10615
10616 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10617 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10618
10619 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10620 off += len;
10621
10622 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10623 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10624 memset(ocir, 0, TG3_OCIR_LEN);
10625 }
10626}
10627
10628/* sysfs attributes for hwmon */
10629static ssize_t tg3_show_temp(struct device *dev,
10630 struct device_attribute *devattr, char *buf)
10631{
10632 struct pci_dev *pdev = to_pci_dev(dev);
10633 struct net_device *netdev = pci_get_drvdata(pdev);
10634 struct tg3 *tp = netdev_priv(netdev);
10635 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10636 u32 temperature;
10637
10638 spin_lock_bh(&tp->lock);
10639 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10640 sizeof(temperature));
10641 spin_unlock_bh(&tp->lock);
10642 return sprintf(buf, "%u\n", temperature);
10643}
10644
10645
10646static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10647 TG3_TEMP_SENSOR_OFFSET);
10648static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10649 TG3_TEMP_CAUTION_OFFSET);
10650static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10651 TG3_TEMP_MAX_OFFSET);
10652
10653static struct attribute *tg3_attributes[] = {
10654 &sensor_dev_attr_temp1_input.dev_attr.attr,
10655 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10656 &sensor_dev_attr_temp1_max.dev_attr.attr,
10657 NULL
10658};
10659
10660static const struct attribute_group tg3_group = {
10661 .attrs = tg3_attributes,
10662};
10663
Michael Chanaed93e02012-07-16 16:24:02 +000010664static void tg3_hwmon_close(struct tg3 *tp)
10665{
Michael Chanaed93e02012-07-16 16:24:02 +000010666 if (tp->hwmon_dev) {
10667 hwmon_device_unregister(tp->hwmon_dev);
10668 tp->hwmon_dev = NULL;
10669 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10670 }
Michael Chanaed93e02012-07-16 16:24:02 +000010671}
10672
10673static void tg3_hwmon_open(struct tg3 *tp)
10674{
Michael Chanaed93e02012-07-16 16:24:02 +000010675 int i, err;
10676 u32 size = 0;
10677 struct pci_dev *pdev = tp->pdev;
10678 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10679
10680 tg3_sd_scan_scratchpad(tp, ocirs);
10681
10682 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10683 if (!ocirs[i].src_data_length)
10684 continue;
10685
10686 size += ocirs[i].src_hdr_length;
10687 size += ocirs[i].src_data_length;
10688 }
10689
10690 if (!size)
10691 return;
10692
10693 /* Register hwmon sysfs hooks */
10694 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10695 if (err) {
10696 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10697 return;
10698 }
10699
10700 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10701 if (IS_ERR(tp->hwmon_dev)) {
10702 tp->hwmon_dev = NULL;
10703 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10704 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10705 }
Michael Chanaed93e02012-07-16 16:24:02 +000010706}
10707
10708
Linus Torvalds1da177e2005-04-16 15:20:36 -070010709#define TG3_STAT_ADD32(PSTAT, REG) \
10710do { u32 __val = tr32(REG); \
10711 (PSTAT)->low += __val; \
10712 if ((PSTAT)->low < __val) \
10713 (PSTAT)->high += 1; \
10714} while (0)
10715
10716static void tg3_periodic_fetch_stats(struct tg3 *tp)
10717{
10718 struct tg3_hw_stats *sp = tp->hw_stats;
10719
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010720 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010721 return;
10722
10723 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10724 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10725 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10726 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10727 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10728 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10729 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10730 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10731 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10732 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10733 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10734 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10735 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010736 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010737 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10738 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10739 u32 val;
10740
10741 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010742 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010743 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010744 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010746
10747 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10748 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10749 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10750 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10751 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10752 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10753 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10754 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10755 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10756 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10757 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10758 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10759 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10760 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010761
10762 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010763 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10764 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10765 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010766 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10767 } else {
10768 u32 val = tr32(HOSTCC_FLOW_ATTN);
10769 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10770 if (val) {
10771 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10772 sp->rx_discards.low += val;
10773 if (sp->rx_discards.low < val)
10774 sp->rx_discards.high += 1;
10775 }
10776 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10777 }
Michael Chan463d3052006-05-22 16:36:27 -070010778 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010779}
10780
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010781static void tg3_chk_missed_msi(struct tg3 *tp)
10782{
10783 u32 i;
10784
10785 for (i = 0; i < tp->irq_cnt; i++) {
10786 struct tg3_napi *tnapi = &tp->napi[i];
10787
10788 if (tg3_has_work(tnapi)) {
10789 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10790 tnapi->last_tx_cons == tnapi->tx_cons) {
10791 if (tnapi->chk_msi_cnt < 1) {
10792 tnapi->chk_msi_cnt++;
10793 return;
10794 }
Matt Carlson7f230732011-08-31 11:44:48 +000010795 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010796 }
10797 }
10798 tnapi->chk_msi_cnt = 0;
10799 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10800 tnapi->last_tx_cons = tnapi->tx_cons;
10801 }
10802}
10803
Linus Torvalds1da177e2005-04-16 15:20:36 -070010804static void tg3_timer(unsigned long __opaque)
10805{
10806 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010807
Matt Carlson5b190622011-11-04 09:15:04 +000010808 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010809 goto restart_timer;
10810
David S. Millerf47c11e2005-06-24 20:18:35 -070010811 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010812
Joe Perches41535772013-02-16 11:20:04 +000010813 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010814 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010815 tg3_chk_missed_msi(tp);
10816
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010817 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10818 /* BCM4785: Flush posted writes from GbE to host memory. */
10819 tr32(HOSTCC_MODE);
10820 }
10821
Joe Perches63c3a662011-04-26 08:12:10 +000010822 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010823 /* All of this garbage is because when using non-tagged
10824 * IRQ status the mailbox/status_block protocol the chip
10825 * uses with the cpu is race prone.
10826 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010827 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010828 tw32(GRC_LOCAL_CTRL,
10829 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10830 } else {
10831 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010832 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010834
David S. Millerfac9b832005-05-18 22:46:34 -070010835 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010836 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010837 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010838 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010840 }
10841
Linus Torvalds1da177e2005-04-16 15:20:36 -070010842 /* This part only runs once per second. */
10843 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010844 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010845 tg3_periodic_fetch_stats(tp);
10846
Matt Carlsonb0c59432011-05-19 12:12:48 +000010847 if (tp->setlpicnt && !--tp->setlpicnt)
10848 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010849
Joe Perches63c3a662011-04-26 08:12:10 +000010850 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010851 u32 mac_stat;
10852 int phy_event;
10853
10854 mac_stat = tr32(MAC_STATUS);
10855
10856 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010857 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010858 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10859 phy_event = 1;
10860 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10861 phy_event = 1;
10862
10863 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010864 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010865 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010866 u32 mac_stat = tr32(MAC_STATUS);
10867 int need_setup = 0;
10868
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010869 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010870 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10871 need_setup = 1;
10872 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010873 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010874 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10875 MAC_STATUS_SIGNAL_DET))) {
10876 need_setup = 1;
10877 }
10878 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010879 if (!tp->serdes_counter) {
10880 tw32_f(MAC_MODE,
10881 (tp->mac_mode &
10882 ~MAC_MODE_PORT_MODE_MASK));
10883 udelay(40);
10884 tw32_f(MAC_MODE, tp->mac_mode);
10885 udelay(40);
10886 }
Joe Perches953c96e2013-04-09 10:18:14 +000010887 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010888 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010889 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010890 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010891 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010893
10894 tp->timer_counter = tp->timer_multiplier;
10895 }
10896
Michael Chan130b8e42006-09-27 16:00:40 -070010897 /* Heartbeat is only sent once every 2 seconds.
10898 *
10899 * The heartbeat is to tell the ASF firmware that the host
10900 * driver is still alive. In the event that the OS crashes,
10901 * ASF needs to reset the hardware to free up the FIFO space
10902 * that may be filled with rx packets destined for the host.
10903 * If the FIFO is full, ASF will no longer function properly.
10904 *
10905 * Unintended resets have been reported on real time kernels
10906 * where the timer doesn't run on time. Netpoll will also have
10907 * same problem.
10908 *
10909 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10910 * to check the ring condition when the heartbeat is expiring
10911 * before doing the reset. This will prevent most unintended
10912 * resets.
10913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010914 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010915 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010916 tg3_wait_for_event_ack(tp);
10917
Michael Chanbbadf502006-04-06 21:46:34 -070010918 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010919 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010920 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010921 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10922 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010923
10924 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010925 }
10926 tp->asf_counter = tp->asf_multiplier;
10927 }
10928
David S. Millerf47c11e2005-06-24 20:18:35 -070010929 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010930
Michael Chanf475f162006-03-27 23:20:14 -080010931restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010932 tp->timer.expires = jiffies + tp->timer_offset;
10933 add_timer(&tp->timer);
10934}
10935
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010936static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010937{
10938 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010939 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010940 !tg3_flag(tp, 57765_CLASS))
10941 tp->timer_offset = HZ;
10942 else
10943 tp->timer_offset = HZ / 10;
10944
10945 BUG_ON(tp->timer_offset > HZ);
10946
10947 tp->timer_multiplier = (HZ / tp->timer_offset);
10948 tp->asf_multiplier = (HZ / tp->timer_offset) *
10949 TG3_FW_UPDATE_FREQ_SEC;
10950
10951 init_timer(&tp->timer);
10952 tp->timer.data = (unsigned long) tp;
10953 tp->timer.function = tg3_timer;
10954}
10955
10956static void tg3_timer_start(struct tg3 *tp)
10957{
10958 tp->asf_counter = tp->asf_multiplier;
10959 tp->timer_counter = tp->timer_multiplier;
10960
10961 tp->timer.expires = jiffies + tp->timer_offset;
10962 add_timer(&tp->timer);
10963}
10964
10965static void tg3_timer_stop(struct tg3 *tp)
10966{
10967 del_timer_sync(&tp->timer);
10968}
10969
10970/* Restart hardware after configuration changes, self-test, etc.
10971 * Invoked with tp->lock held.
10972 */
Joe Perches953c96e2013-04-09 10:18:14 +000010973static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000010974 __releases(tp->lock)
10975 __acquires(tp->lock)
10976{
10977 int err;
10978
10979 err = tg3_init_hw(tp, reset_phy);
10980 if (err) {
10981 netdev_err(tp->dev,
10982 "Failed to re-initialize device, aborting\n");
10983 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10984 tg3_full_unlock(tp);
10985 tg3_timer_stop(tp);
10986 tp->irq_sync = 0;
10987 tg3_napi_enable(tp);
10988 dev_close(tp->dev);
10989 tg3_full_lock(tp, 0);
10990 }
10991 return err;
10992}
10993
10994static void tg3_reset_task(struct work_struct *work)
10995{
10996 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10997 int err;
10998
10999 tg3_full_lock(tp, 0);
11000
11001 if (!netif_running(tp->dev)) {
11002 tg3_flag_clear(tp, RESET_TASK_PENDING);
11003 tg3_full_unlock(tp);
11004 return;
11005 }
11006
11007 tg3_full_unlock(tp);
11008
11009 tg3_phy_stop(tp);
11010
11011 tg3_netif_stop(tp);
11012
11013 tg3_full_lock(tp, 1);
11014
11015 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11016 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11017 tp->write32_rx_mbox = tg3_write_flush_reg32;
11018 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11019 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11020 }
11021
11022 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011023 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011024 if (err)
11025 goto out;
11026
11027 tg3_netif_start(tp);
11028
11029out:
11030 tg3_full_unlock(tp);
11031
11032 if (!err)
11033 tg3_phy_start(tp);
11034
11035 tg3_flag_clear(tp, RESET_TASK_PENDING);
11036}
11037
Matt Carlson4f125f42009-09-01 12:55:02 +000011038static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011039{
David Howells7d12e782006-10-05 14:55:46 +010011040 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011041 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011042 char *name;
11043 struct tg3_napi *tnapi = &tp->napi[irq_num];
11044
11045 if (tp->irq_cnt == 1)
11046 name = tp->dev->name;
11047 else {
11048 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011049 if (tnapi->tx_buffers && tnapi->rx_rcb)
11050 snprintf(name, IFNAMSIZ,
11051 "%s-txrx-%d", tp->dev->name, irq_num);
11052 else if (tnapi->tx_buffers)
11053 snprintf(name, IFNAMSIZ,
11054 "%s-tx-%d", tp->dev->name, irq_num);
11055 else if (tnapi->rx_rcb)
11056 snprintf(name, IFNAMSIZ,
11057 "%s-rx-%d", tp->dev->name, irq_num);
11058 else
11059 snprintf(name, IFNAMSIZ,
11060 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011061 name[IFNAMSIZ-1] = 0;
11062 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011063
Joe Perches63c3a662011-04-26 08:12:10 +000011064 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011065 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011066 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011067 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011068 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011069 } else {
11070 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011071 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011072 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011073 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011074 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011075
11076 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011077}
11078
Michael Chan79381092005-04-21 17:13:59 -070011079static int tg3_test_interrupt(struct tg3 *tp)
11080{
Matt Carlson09943a12009-08-28 14:01:57 +000011081 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011082 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011083 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011084 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011085
Michael Chand4bc3922005-05-29 14:59:20 -070011086 if (!netif_running(dev))
11087 return -ENODEV;
11088
Michael Chan79381092005-04-21 17:13:59 -070011089 tg3_disable_ints(tp);
11090
Matt Carlson4f125f42009-09-01 12:55:02 +000011091 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011092
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011093 /*
11094 * Turn off MSI one shot mode. Otherwise this test has no
11095 * observable way to know whether the interrupt was delivered.
11096 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011097 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011098 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11099 tw32(MSGINT_MODE, val);
11100 }
11101
Matt Carlson4f125f42009-09-01 12:55:02 +000011102 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011103 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011104 if (err)
11105 return err;
11106
Matt Carlson898a56f2009-08-28 14:02:40 +000011107 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011108 tg3_enable_ints(tp);
11109
11110 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011111 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011112
11113 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011114 u32 int_mbox, misc_host_ctrl;
11115
Matt Carlson898a56f2009-08-28 14:02:40 +000011116 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011117 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11118
11119 if ((int_mbox != 0) ||
11120 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11121 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011122 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011123 }
11124
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011125 if (tg3_flag(tp, 57765_PLUS) &&
11126 tnapi->hw_status->status_tag != tnapi->last_tag)
11127 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11128
Michael Chan79381092005-04-21 17:13:59 -070011129 msleep(10);
11130 }
11131
11132 tg3_disable_ints(tp);
11133
Matt Carlson4f125f42009-09-01 12:55:02 +000011134 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011135
Matt Carlson4f125f42009-09-01 12:55:02 +000011136 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011137
11138 if (err)
11139 return err;
11140
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011141 if (intr_ok) {
11142 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011143 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011144 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11145 tw32(MSGINT_MODE, val);
11146 }
Michael Chan79381092005-04-21 17:13:59 -070011147 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011148 }
Michael Chan79381092005-04-21 17:13:59 -070011149
11150 return -EIO;
11151}
11152
11153/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11154 * successfully restored
11155 */
11156static int tg3_test_msi(struct tg3 *tp)
11157{
Michael Chan79381092005-04-21 17:13:59 -070011158 int err;
11159 u16 pci_cmd;
11160
Joe Perches63c3a662011-04-26 08:12:10 +000011161 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011162 return 0;
11163
11164 /* Turn off SERR reporting in case MSI terminates with Master
11165 * Abort.
11166 */
11167 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11168 pci_write_config_word(tp->pdev, PCI_COMMAND,
11169 pci_cmd & ~PCI_COMMAND_SERR);
11170
11171 err = tg3_test_interrupt(tp);
11172
11173 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11174
11175 if (!err)
11176 return 0;
11177
11178 /* other failures */
11179 if (err != -EIO)
11180 return err;
11181
11182 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011183 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11184 "to INTx mode. Please report this failure to the PCI "
11185 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011186
Matt Carlson4f125f42009-09-01 12:55:02 +000011187 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011188
Michael Chan79381092005-04-21 17:13:59 -070011189 pci_disable_msi(tp->pdev);
11190
Joe Perches63c3a662011-04-26 08:12:10 +000011191 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011192 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011193
Matt Carlson4f125f42009-09-01 12:55:02 +000011194 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011195 if (err)
11196 return err;
11197
11198 /* Need to reset the chip because the MSI cycle may have terminated
11199 * with Master Abort.
11200 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011201 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011202
Michael Chan944d9802005-05-29 14:57:48 -070011203 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011204 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011205
David S. Millerf47c11e2005-06-24 20:18:35 -070011206 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011207
11208 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011209 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011210
11211 return err;
11212}
11213
Matt Carlson9e9fd122009-01-19 16:57:45 -080011214static int tg3_request_firmware(struct tg3 *tp)
11215{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011216 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011217
11218 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011219 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11220 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011221 return -ENOENT;
11222 }
11223
Nithin Sujir77997ea2013-03-06 17:02:32 +000011224 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011225
11226 /* Firmware blob starts with version numbers, followed by
11227 * start address and _full_ length including BSS sections
11228 * (which must be longer than the actual data, of course
11229 */
11230
Nithin Sujir77997ea2013-03-06 17:02:32 +000011231 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11232 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011233 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11234 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011235 release_firmware(tp->fw);
11236 tp->fw = NULL;
11237 return -EINVAL;
11238 }
11239
11240 /* We no longer need firmware; we have it. */
11241 tp->fw_needed = NULL;
11242 return 0;
11243}
11244
Michael Chan91024262012-09-28 07:12:38 +000011245static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011246{
Michael Chan91024262012-09-28 07:12:38 +000011247 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011248
Michael Chan91024262012-09-28 07:12:38 +000011249 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011250 /* We want as many rx rings enabled as there are cpus.
11251 * In multiqueue MSI-X mode, the first MSI-X vector
11252 * only deals with link interrupts, etc, so we add
11253 * one to the number of vectors we are requesting.
11254 */
Michael Chan91024262012-09-28 07:12:38 +000011255 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011256 }
Matt Carlson679563f2009-09-01 12:55:46 +000011257
Michael Chan91024262012-09-28 07:12:38 +000011258 return irq_cnt;
11259}
11260
11261static bool tg3_enable_msix(struct tg3 *tp)
11262{
11263 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011264 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011265
Michael Chan09681692012-09-28 07:12:42 +000011266 tp->txq_cnt = tp->txq_req;
11267 tp->rxq_cnt = tp->rxq_req;
11268 if (!tp->rxq_cnt)
11269 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011270 if (tp->rxq_cnt > tp->rxq_max)
11271 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011272
11273 /* Disable multiple TX rings by default. Simple round-robin hardware
11274 * scheduling of the TX rings can cause starvation of rings with
11275 * small packets when other rings have TSO or jumbo packets.
11276 */
11277 if (!tp->txq_req)
11278 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011279
11280 tp->irq_cnt = tg3_irq_count(tp);
11281
Matt Carlson679563f2009-09-01 12:55:46 +000011282 for (i = 0; i < tp->irq_max; i++) {
11283 msix_ent[i].entry = i;
11284 msix_ent[i].vector = 0;
11285 }
11286
11287 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011288 if (rc < 0) {
11289 return false;
11290 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011291 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11292 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011293 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11294 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011295 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011296 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011297 if (tp->txq_cnt)
11298 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011299 }
11300
11301 for (i = 0; i < tp->irq_max; i++)
11302 tp->napi[i].irq_vec = msix_ent[i].vector;
11303
Michael Chan49a359e2012-09-28 07:12:37 +000011304 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011305 pci_disable_msix(tp->pdev);
11306 return false;
11307 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011308
Michael Chan91024262012-09-28 07:12:38 +000011309 if (tp->irq_cnt == 1)
11310 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011311
Michael Chan91024262012-09-28 07:12:38 +000011312 tg3_flag_set(tp, ENABLE_RSS);
11313
11314 if (tp->txq_cnt > 1)
11315 tg3_flag_set(tp, ENABLE_TSS);
11316
11317 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011318
Matt Carlson679563f2009-09-01 12:55:46 +000011319 return true;
11320}
11321
Matt Carlson07b01732009-08-28 14:01:15 +000011322static void tg3_ints_init(struct tg3 *tp)
11323{
Joe Perches63c3a662011-04-26 08:12:10 +000011324 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11325 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011326 /* All MSI supporting chips should support tagged
11327 * status. Assert that this is the case.
11328 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011329 netdev_warn(tp->dev,
11330 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011331 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011332 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011333
Joe Perches63c3a662011-04-26 08:12:10 +000011334 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11335 tg3_flag_set(tp, USING_MSIX);
11336 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11337 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011338
Joe Perches63c3a662011-04-26 08:12:10 +000011339 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011340 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011341 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011342 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011343 if (!tg3_flag(tp, 1SHOT_MSI))
11344 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011345 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11346 }
11347defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011348 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011349 tp->irq_cnt = 1;
11350 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011351 }
11352
11353 if (tp->irq_cnt == 1) {
11354 tp->txq_cnt = 1;
11355 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011356 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011357 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011358 }
Matt Carlson07b01732009-08-28 14:01:15 +000011359}
11360
11361static void tg3_ints_fini(struct tg3 *tp)
11362{
Joe Perches63c3a662011-04-26 08:12:10 +000011363 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011364 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011365 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011366 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011367 tg3_flag_clear(tp, USING_MSI);
11368 tg3_flag_clear(tp, USING_MSIX);
11369 tg3_flag_clear(tp, ENABLE_RSS);
11370 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011371}
11372
Matt Carlsonbe947302012-12-03 19:36:57 +000011373static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11374 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011375{
Michael Chand8f4cd32012-09-28 07:12:40 +000011376 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011377 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011378
Matt Carlson679563f2009-09-01 12:55:46 +000011379 /*
11380 * Setup interrupts first so we know how
11381 * many NAPI resources to allocate
11382 */
11383 tg3_ints_init(tp);
11384
Matt Carlson90415472011-12-16 13:33:23 +000011385 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011386
Linus Torvalds1da177e2005-04-16 15:20:36 -070011387 /* The placement of this call is tied
11388 * to the setup and use of Host TX descriptors.
11389 */
11390 err = tg3_alloc_consistent(tp);
11391 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011392 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011393
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011394 tg3_napi_init(tp);
11395
Matt Carlsonfed97812009-09-01 13:10:19 +000011396 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011397
Matt Carlson4f125f42009-09-01 12:55:02 +000011398 for (i = 0; i < tp->irq_cnt; i++) {
11399 struct tg3_napi *tnapi = &tp->napi[i];
11400 err = tg3_request_irq(tp, i);
11401 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011402 for (i--; i >= 0; i--) {
11403 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011404 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011405 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011406 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011407 }
11408 }
Matt Carlson07b01732009-08-28 14:01:15 +000011409
David S. Millerf47c11e2005-06-24 20:18:35 -070011410 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011411
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011412 if (init)
11413 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11414
Michael Chand8f4cd32012-09-28 07:12:40 +000011415 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011416 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011417 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011418 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011419 }
11420
David S. Millerf47c11e2005-06-24 20:18:35 -070011421 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011422
Matt Carlson07b01732009-08-28 14:01:15 +000011423 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011424 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011425
Michael Chand8f4cd32012-09-28 07:12:40 +000011426 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011427 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011428
Michael Chan79381092005-04-21 17:13:59 -070011429 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011430 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011431 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011432 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011433 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011434
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011435 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011436 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011437
Joe Perches63c3a662011-04-26 08:12:10 +000011438 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011439 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011440
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011441 tw32(PCIE_TRANSACTION_CFG,
11442 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011443 }
Michael Chan79381092005-04-21 17:13:59 -070011444 }
11445
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011446 tg3_phy_start(tp);
11447
Michael Chanaed93e02012-07-16 16:24:02 +000011448 tg3_hwmon_open(tp);
11449
David S. Millerf47c11e2005-06-24 20:18:35 -070011450 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011451
Matt Carlson21f76382012-02-22 12:35:21 +000011452 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011453 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011454 tg3_enable_ints(tp);
11455
Matt Carlsonbe947302012-12-03 19:36:57 +000011456 if (init)
11457 tg3_ptp_init(tp);
11458 else
11459 tg3_ptp_resume(tp);
11460
11461
David S. Millerf47c11e2005-06-24 20:18:35 -070011462 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011463
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011464 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011465
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011466 /*
11467 * Reset loopback feature if it was turned on while the device was down
11468 * make sure that it's installed properly now.
11469 */
11470 if (dev->features & NETIF_F_LOOPBACK)
11471 tg3_set_loopback(dev, dev->features);
11472
Linus Torvalds1da177e2005-04-16 15:20:36 -070011473 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011474
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011475out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011476 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11477 struct tg3_napi *tnapi = &tp->napi[i];
11478 free_irq(tnapi->irq_vec, tnapi);
11479 }
Matt Carlson07b01732009-08-28 14:01:15 +000011480
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011481out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011482 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011483 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011484 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011485
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011486out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011487 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011488
Matt Carlson07b01732009-08-28 14:01:15 +000011489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011490}
11491
Michael Chan65138592012-09-28 07:12:41 +000011492static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011493{
Matt Carlson4f125f42009-09-01 12:55:02 +000011494 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011495
Matt Carlsondb219972011-11-04 09:15:03 +000011496 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011497 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011498
Matt Carlson21f76382012-02-22 12:35:21 +000011499 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011500
Michael Chanaed93e02012-07-16 16:24:02 +000011501 tg3_hwmon_close(tp);
11502
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011503 tg3_phy_stop(tp);
11504
David S. Millerf47c11e2005-06-24 20:18:35 -070011505 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011506
11507 tg3_disable_ints(tp);
11508
Michael Chan944d9802005-05-29 14:57:48 -070011509 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011510 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011511 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011512
David S. Millerf47c11e2005-06-24 20:18:35 -070011513 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011514
Matt Carlson4f125f42009-09-01 12:55:02 +000011515 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11516 struct tg3_napi *tnapi = &tp->napi[i];
11517 free_irq(tnapi->irq_vec, tnapi);
11518 }
Matt Carlson07b01732009-08-28 14:01:15 +000011519
11520 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011521
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011522 tg3_napi_fini(tp);
11523
Linus Torvalds1da177e2005-04-16 15:20:36 -070011524 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011525}
11526
Michael Chand8f4cd32012-09-28 07:12:40 +000011527static int tg3_open(struct net_device *dev)
11528{
11529 struct tg3 *tp = netdev_priv(dev);
11530 int err;
11531
11532 if (tp->fw_needed) {
11533 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011534 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11535 if (err) {
11536 netdev_warn(tp->dev, "EEE capability disabled\n");
11537 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11538 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11539 netdev_warn(tp->dev, "EEE capability restored\n");
11540 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11541 }
11542 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011543 if (err)
11544 return err;
11545 } else if (err) {
11546 netdev_warn(tp->dev, "TSO capability disabled\n");
11547 tg3_flag_clear(tp, TSO_CAPABLE);
11548 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11549 netdev_notice(tp->dev, "TSO capability restored\n");
11550 tg3_flag_set(tp, TSO_CAPABLE);
11551 }
11552 }
11553
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011554 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011555
11556 err = tg3_power_up(tp);
11557 if (err)
11558 return err;
11559
11560 tg3_full_lock(tp, 0);
11561
11562 tg3_disable_ints(tp);
11563 tg3_flag_clear(tp, INIT_COMPLETE);
11564
11565 tg3_full_unlock(tp);
11566
Nithin Sujir942d1af2013-04-09 08:48:07 +000011567 err = tg3_start(tp,
11568 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11569 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011570 if (err) {
11571 tg3_frob_aux_power(tp, false);
11572 pci_set_power_state(tp->pdev, PCI_D3hot);
11573 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011574
Matt Carlson7d41e492012-12-03 19:36:58 +000011575 if (tg3_flag(tp, PTP_CAPABLE)) {
11576 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11577 &tp->pdev->dev);
11578 if (IS_ERR(tp->ptp_clock))
11579 tp->ptp_clock = NULL;
11580 }
11581
Linus Torvalds1da177e2005-04-16 15:20:36 -070011582 return err;
11583}
11584
11585static int tg3_close(struct net_device *dev)
11586{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011587 struct tg3 *tp = netdev_priv(dev);
11588
Matt Carlsonbe947302012-12-03 19:36:57 +000011589 tg3_ptp_fini(tp);
11590
Michael Chan65138592012-09-28 07:12:41 +000011591 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011592
11593 /* Clear stats across close / open calls */
11594 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11595 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011596
Nithin Sujir5137a2e2013-07-29 13:58:36 -070011597 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011599 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011600
11601 return 0;
11602}
11603
11604static inline u64 get_stat64(tg3_stat64_t *val)
11605{
11606 return ((u64)val->high << 32) | ((u64)val->low);
11607}
11608
11609static u64 tg3_calc_crc_errors(struct tg3 *tp)
11610{
11611 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11612
11613 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011614 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11615 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011616 u32 val;
11617
11618 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11619 tg3_writephy(tp, MII_TG3_TEST1,
11620 val | MII_TG3_TEST1_CRC_EN);
11621 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11622 } else
11623 val = 0;
11624
11625 tp->phy_crc_errors += val;
11626
11627 return tp->phy_crc_errors;
11628 }
11629
11630 return get_stat64(&hw_stats->rx_fcs_errors);
11631}
11632
11633#define ESTAT_ADD(member) \
11634 estats->member = old_estats->member + \
11635 get_stat64(&hw_stats->member)
11636
11637static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11638{
11639 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11640 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11641
11642 ESTAT_ADD(rx_octets);
11643 ESTAT_ADD(rx_fragments);
11644 ESTAT_ADD(rx_ucast_packets);
11645 ESTAT_ADD(rx_mcast_packets);
11646 ESTAT_ADD(rx_bcast_packets);
11647 ESTAT_ADD(rx_fcs_errors);
11648 ESTAT_ADD(rx_align_errors);
11649 ESTAT_ADD(rx_xon_pause_rcvd);
11650 ESTAT_ADD(rx_xoff_pause_rcvd);
11651 ESTAT_ADD(rx_mac_ctrl_rcvd);
11652 ESTAT_ADD(rx_xoff_entered);
11653 ESTAT_ADD(rx_frame_too_long_errors);
11654 ESTAT_ADD(rx_jabbers);
11655 ESTAT_ADD(rx_undersize_packets);
11656 ESTAT_ADD(rx_in_length_errors);
11657 ESTAT_ADD(rx_out_length_errors);
11658 ESTAT_ADD(rx_64_or_less_octet_packets);
11659 ESTAT_ADD(rx_65_to_127_octet_packets);
11660 ESTAT_ADD(rx_128_to_255_octet_packets);
11661 ESTAT_ADD(rx_256_to_511_octet_packets);
11662 ESTAT_ADD(rx_512_to_1023_octet_packets);
11663 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11664 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11665 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11666 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11667 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11668
11669 ESTAT_ADD(tx_octets);
11670 ESTAT_ADD(tx_collisions);
11671 ESTAT_ADD(tx_xon_sent);
11672 ESTAT_ADD(tx_xoff_sent);
11673 ESTAT_ADD(tx_flow_control);
11674 ESTAT_ADD(tx_mac_errors);
11675 ESTAT_ADD(tx_single_collisions);
11676 ESTAT_ADD(tx_mult_collisions);
11677 ESTAT_ADD(tx_deferred);
11678 ESTAT_ADD(tx_excessive_collisions);
11679 ESTAT_ADD(tx_late_collisions);
11680 ESTAT_ADD(tx_collide_2times);
11681 ESTAT_ADD(tx_collide_3times);
11682 ESTAT_ADD(tx_collide_4times);
11683 ESTAT_ADD(tx_collide_5times);
11684 ESTAT_ADD(tx_collide_6times);
11685 ESTAT_ADD(tx_collide_7times);
11686 ESTAT_ADD(tx_collide_8times);
11687 ESTAT_ADD(tx_collide_9times);
11688 ESTAT_ADD(tx_collide_10times);
11689 ESTAT_ADD(tx_collide_11times);
11690 ESTAT_ADD(tx_collide_12times);
11691 ESTAT_ADD(tx_collide_13times);
11692 ESTAT_ADD(tx_collide_14times);
11693 ESTAT_ADD(tx_collide_15times);
11694 ESTAT_ADD(tx_ucast_packets);
11695 ESTAT_ADD(tx_mcast_packets);
11696 ESTAT_ADD(tx_bcast_packets);
11697 ESTAT_ADD(tx_carrier_sense_errors);
11698 ESTAT_ADD(tx_discards);
11699 ESTAT_ADD(tx_errors);
11700
11701 ESTAT_ADD(dma_writeq_full);
11702 ESTAT_ADD(dma_write_prioq_full);
11703 ESTAT_ADD(rxbds_empty);
11704 ESTAT_ADD(rx_discards);
11705 ESTAT_ADD(rx_errors);
11706 ESTAT_ADD(rx_threshold_hit);
11707
11708 ESTAT_ADD(dma_readq_full);
11709 ESTAT_ADD(dma_read_prioq_full);
11710 ESTAT_ADD(tx_comp_queue_full);
11711
11712 ESTAT_ADD(ring_set_send_prod_index);
11713 ESTAT_ADD(ring_status_update);
11714 ESTAT_ADD(nic_irqs);
11715 ESTAT_ADD(nic_avoided_irqs);
11716 ESTAT_ADD(nic_tx_threshold_hit);
11717
Matt Carlson4452d092011-05-19 12:12:51 +000011718 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011719}
11720
Matt Carlson65ec6982012-02-28 23:33:37 +000011721static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011722{
Eric Dumazet511d2222010-07-07 20:44:24 +000011723 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011724 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11725
Linus Torvalds1da177e2005-04-16 15:20:36 -070011726 stats->rx_packets = old_stats->rx_packets +
11727 get_stat64(&hw_stats->rx_ucast_packets) +
11728 get_stat64(&hw_stats->rx_mcast_packets) +
11729 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011730
Linus Torvalds1da177e2005-04-16 15:20:36 -070011731 stats->tx_packets = old_stats->tx_packets +
11732 get_stat64(&hw_stats->tx_ucast_packets) +
11733 get_stat64(&hw_stats->tx_mcast_packets) +
11734 get_stat64(&hw_stats->tx_bcast_packets);
11735
11736 stats->rx_bytes = old_stats->rx_bytes +
11737 get_stat64(&hw_stats->rx_octets);
11738 stats->tx_bytes = old_stats->tx_bytes +
11739 get_stat64(&hw_stats->tx_octets);
11740
11741 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011742 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011743 stats->tx_errors = old_stats->tx_errors +
11744 get_stat64(&hw_stats->tx_errors) +
11745 get_stat64(&hw_stats->tx_mac_errors) +
11746 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11747 get_stat64(&hw_stats->tx_discards);
11748
11749 stats->multicast = old_stats->multicast +
11750 get_stat64(&hw_stats->rx_mcast_packets);
11751 stats->collisions = old_stats->collisions +
11752 get_stat64(&hw_stats->tx_collisions);
11753
11754 stats->rx_length_errors = old_stats->rx_length_errors +
11755 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11756 get_stat64(&hw_stats->rx_undersize_packets);
11757
11758 stats->rx_over_errors = old_stats->rx_over_errors +
11759 get_stat64(&hw_stats->rxbds_empty);
11760 stats->rx_frame_errors = old_stats->rx_frame_errors +
11761 get_stat64(&hw_stats->rx_align_errors);
11762 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11763 get_stat64(&hw_stats->tx_discards);
11764 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11765 get_stat64(&hw_stats->tx_carrier_sense_errors);
11766
11767 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011768 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011769
John W. Linville4f63b872005-09-12 14:43:18 -070011770 stats->rx_missed_errors = old_stats->rx_missed_errors +
11771 get_stat64(&hw_stats->rx_discards);
11772
Eric Dumazetb0057c52010-10-10 19:55:52 +000011773 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011774 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775}
11776
Linus Torvalds1da177e2005-04-16 15:20:36 -070011777static int tg3_get_regs_len(struct net_device *dev)
11778{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011779 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011780}
11781
11782static void tg3_get_regs(struct net_device *dev,
11783 struct ethtool_regs *regs, void *_p)
11784{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011785 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011786
11787 regs->version = 0;
11788
Matt Carlson97bd8e42011-04-13 11:05:04 +000011789 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011790
Matt Carlson80096062010-08-02 11:26:06 +000011791 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011792 return;
11793
David S. Millerf47c11e2005-06-24 20:18:35 -070011794 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011795
Matt Carlson97bd8e42011-04-13 11:05:04 +000011796 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011797
David S. Millerf47c11e2005-06-24 20:18:35 -070011798 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011799}
11800
11801static int tg3_get_eeprom_len(struct net_device *dev)
11802{
11803 struct tg3 *tp = netdev_priv(dev);
11804
11805 return tp->nvram_size;
11806}
11807
Linus Torvalds1da177e2005-04-16 15:20:36 -070011808static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11809{
11810 struct tg3 *tp = netdev_priv(dev);
11811 int ret;
11812 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011813 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011814 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011815
Joe Perches63c3a662011-04-26 08:12:10 +000011816 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011817 return -EINVAL;
11818
Linus Torvalds1da177e2005-04-16 15:20:36 -070011819 offset = eeprom->offset;
11820 len = eeprom->len;
11821 eeprom->len = 0;
11822
11823 eeprom->magic = TG3_EEPROM_MAGIC;
11824
11825 if (offset & 3) {
11826 /* adjustments to start on required 4 byte boundary */
11827 b_offset = offset & 3;
11828 b_count = 4 - b_offset;
11829 if (b_count > len) {
11830 /* i.e. offset=1 len=2 */
11831 b_count = len;
11832 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011833 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011834 if (ret)
11835 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011836 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011837 len -= b_count;
11838 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011839 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011840 }
11841
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011842 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011843 pd = &data[eeprom->len];
11844 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011845 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011846 if (ret) {
11847 eeprom->len += i;
11848 return ret;
11849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011850 memcpy(pd + i, &val, 4);
11851 }
11852 eeprom->len += i;
11853
11854 if (len & 3) {
11855 /* read last bytes not ending on 4 byte boundary */
11856 pd = &data[eeprom->len];
11857 b_count = len & 3;
11858 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011859 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011860 if (ret)
11861 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011862 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011863 eeprom->len += b_count;
11864 }
11865 return 0;
11866}
11867
Linus Torvalds1da177e2005-04-16 15:20:36 -070011868static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11869{
11870 struct tg3 *tp = netdev_priv(dev);
11871 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011872 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011873 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011874 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011875
Joe Perches63c3a662011-04-26 08:12:10 +000011876 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011877 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011878 return -EINVAL;
11879
11880 offset = eeprom->offset;
11881 len = eeprom->len;
11882
11883 if ((b_offset = (offset & 3))) {
11884 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011885 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011886 if (ret)
11887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011888 len += b_offset;
11889 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011890 if (len < 4)
11891 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011892 }
11893
11894 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011895 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896 /* adjustments to end on required 4 byte boundary */
11897 odd_len = 1;
11898 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011899 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011900 if (ret)
11901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011902 }
11903
11904 buf = data;
11905 if (b_offset || odd_len) {
11906 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011907 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011908 return -ENOMEM;
11909 if (b_offset)
11910 memcpy(buf, &start, 4);
11911 if (odd_len)
11912 memcpy(buf+len-4, &end, 4);
11913 memcpy(buf + b_offset, data, eeprom->len);
11914 }
11915
11916 ret = tg3_nvram_write_block(tp, offset, len, buf);
11917
11918 if (buf != data)
11919 kfree(buf);
11920
11921 return ret;
11922}
11923
11924static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11925{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011926 struct tg3 *tp = netdev_priv(dev);
11927
Joe Perches63c3a662011-04-26 08:12:10 +000011928 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011929 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011930 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011931 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020011932 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011933 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011934 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011935
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936 cmd->supported = (SUPPORTED_Autoneg);
11937
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011938 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011939 cmd->supported |= (SUPPORTED_1000baseT_Half |
11940 SUPPORTED_1000baseT_Full);
11941
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011942 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011943 cmd->supported |= (SUPPORTED_100baseT_Half |
11944 SUPPORTED_100baseT_Full |
11945 SUPPORTED_10baseT_Half |
11946 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011947 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011948 cmd->port = PORT_TP;
11949 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011950 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011951 cmd->port = PORT_FIBRE;
11952 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011953
Linus Torvalds1da177e2005-04-16 15:20:36 -070011954 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011955 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11956 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11957 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11958 cmd->advertising |= ADVERTISED_Pause;
11959 } else {
11960 cmd->advertising |= ADVERTISED_Pause |
11961 ADVERTISED_Asym_Pause;
11962 }
11963 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11964 cmd->advertising |= ADVERTISED_Asym_Pause;
11965 }
11966 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011967 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011968 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011969 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011970 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011971 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11972 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11973 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11974 else
11975 cmd->eth_tp_mdix = ETH_TP_MDI;
11976 }
Matt Carlson64c22182010-10-14 10:37:44 +000011977 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011978 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11979 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011980 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011981 }
Matt Carlson882e9792009-09-01 13:21:36 +000011982 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011983 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011984 cmd->autoneg = tp->link_config.autoneg;
11985 cmd->maxtxpkt = 0;
11986 cmd->maxrxpkt = 0;
11987 return 0;
11988}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011989
Linus Torvalds1da177e2005-04-16 15:20:36 -070011990static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11991{
11992 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011993 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011994
Joe Perches63c3a662011-04-26 08:12:10 +000011995 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011996 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011997 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011998 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020011999 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012000 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012001 }
12002
Matt Carlson7e5856b2009-02-25 14:23:01 +000012003 if (cmd->autoneg != AUTONEG_ENABLE &&
12004 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012005 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012006
12007 if (cmd->autoneg == AUTONEG_DISABLE &&
12008 cmd->duplex != DUPLEX_FULL &&
12009 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012010 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012011
Matt Carlson7e5856b2009-02-25 14:23:01 +000012012 if (cmd->autoneg == AUTONEG_ENABLE) {
12013 u32 mask = ADVERTISED_Autoneg |
12014 ADVERTISED_Pause |
12015 ADVERTISED_Asym_Pause;
12016
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012017 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012018 mask |= ADVERTISED_1000baseT_Half |
12019 ADVERTISED_1000baseT_Full;
12020
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012021 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012022 mask |= ADVERTISED_100baseT_Half |
12023 ADVERTISED_100baseT_Full |
12024 ADVERTISED_10baseT_Half |
12025 ADVERTISED_10baseT_Full |
12026 ADVERTISED_TP;
12027 else
12028 mask |= ADVERTISED_FIBRE;
12029
12030 if (cmd->advertising & ~mask)
12031 return -EINVAL;
12032
12033 mask &= (ADVERTISED_1000baseT_Half |
12034 ADVERTISED_1000baseT_Full |
12035 ADVERTISED_100baseT_Half |
12036 ADVERTISED_100baseT_Full |
12037 ADVERTISED_10baseT_Half |
12038 ADVERTISED_10baseT_Full);
12039
12040 cmd->advertising &= mask;
12041 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012042 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012043 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012044 return -EINVAL;
12045
12046 if (cmd->duplex != DUPLEX_FULL)
12047 return -EINVAL;
12048 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012049 if (speed != SPEED_100 &&
12050 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012051 return -EINVAL;
12052 }
12053 }
12054
David S. Millerf47c11e2005-06-24 20:18:35 -070012055 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012056
12057 tp->link_config.autoneg = cmd->autoneg;
12058 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012059 tp->link_config.advertising = (cmd->advertising |
12060 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012061 tp->link_config.speed = SPEED_UNKNOWN;
12062 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012063 } else {
12064 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012065 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012066 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012067 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012068
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012069 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12070
Nithin Sujirce20f162013-04-09 08:48:04 +000012071 tg3_warn_mgmt_link_flap(tp);
12072
Linus Torvalds1da177e2005-04-16 15:20:36 -070012073 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012074 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012075
David S. Millerf47c11e2005-06-24 20:18:35 -070012076 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012077
Linus Torvalds1da177e2005-04-16 15:20:36 -070012078 return 0;
12079}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012080
Linus Torvalds1da177e2005-04-16 15:20:36 -070012081static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12082{
12083 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012084
Rick Jones68aad782011-11-07 13:29:27 +000012085 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12086 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12087 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12088 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012089}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012090
Linus Torvalds1da177e2005-04-16 15:20:36 -070012091static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12092{
12093 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012094
Joe Perches63c3a662011-04-26 08:12:10 +000012095 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012096 wol->supported = WAKE_MAGIC;
12097 else
12098 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012099 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012100 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012101 wol->wolopts = WAKE_MAGIC;
12102 memset(&wol->sopass, 0, sizeof(wol->sopass));
12103}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012104
Linus Torvalds1da177e2005-04-16 15:20:36 -070012105static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12106{
12107 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012108 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012109
Linus Torvalds1da177e2005-04-16 15:20:36 -070012110 if (wol->wolopts & ~WAKE_MAGIC)
12111 return -EINVAL;
12112 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012113 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012114 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012115
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012116 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12117
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012118 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012119 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012120 else
Joe Perches63c3a662011-04-26 08:12:10 +000012121 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012122
Linus Torvalds1da177e2005-04-16 15:20:36 -070012123 return 0;
12124}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012125
Linus Torvalds1da177e2005-04-16 15:20:36 -070012126static u32 tg3_get_msglevel(struct net_device *dev)
12127{
12128 struct tg3 *tp = netdev_priv(dev);
12129 return tp->msg_enable;
12130}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012131
Linus Torvalds1da177e2005-04-16 15:20:36 -070012132static void tg3_set_msglevel(struct net_device *dev, u32 value)
12133{
12134 struct tg3 *tp = netdev_priv(dev);
12135 tp->msg_enable = value;
12136}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012137
Linus Torvalds1da177e2005-04-16 15:20:36 -070012138static int tg3_nway_reset(struct net_device *dev)
12139{
12140 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012141 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012142
Linus Torvalds1da177e2005-04-16 15:20:36 -070012143 if (!netif_running(dev))
12144 return -EAGAIN;
12145
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012146 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012147 return -EINVAL;
12148
Nithin Sujirce20f162013-04-09 08:48:04 +000012149 tg3_warn_mgmt_link_flap(tp);
12150
Joe Perches63c3a662011-04-26 08:12:10 +000012151 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012152 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012153 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012154 r = phy_start_aneg(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012155 } else {
12156 u32 bmcr;
12157
12158 spin_lock_bh(&tp->lock);
12159 r = -EINVAL;
12160 tg3_readphy(tp, MII_BMCR, &bmcr);
12161 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12162 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012163 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012164 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12165 BMCR_ANENABLE);
12166 r = 0;
12167 }
12168 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012169 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012170
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 return r;
12172}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012173
Linus Torvalds1da177e2005-04-16 15:20:36 -070012174static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12175{
12176 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012177
Matt Carlson2c49a442010-09-30 10:34:35 +000012178 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012179 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012180 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012181 else
12182 ering->rx_jumbo_max_pending = 0;
12183
12184 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012185
12186 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012187 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012188 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12189 else
12190 ering->rx_jumbo_pending = 0;
12191
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012192 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012193}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012194
Linus Torvalds1da177e2005-04-16 15:20:36 -070012195static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12196{
12197 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012198 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012199
Matt Carlson2c49a442010-09-30 10:34:35 +000012200 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12201 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012202 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12203 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012204 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012205 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012206 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012207
Michael Chanbbe832c2005-06-24 20:20:04 -070012208 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012209 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012210 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012211 irq_sync = 1;
12212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012213
Michael Chanbbe832c2005-06-24 20:20:04 -070012214 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012215
Linus Torvalds1da177e2005-04-16 15:20:36 -070012216 tp->rx_pending = ering->rx_pending;
12217
Joe Perches63c3a662011-04-26 08:12:10 +000012218 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012219 tp->rx_pending > 63)
12220 tp->rx_pending = 63;
12221 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012222
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012223 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012224 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012225
12226 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012227 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012228 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012229 if (!err)
12230 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012231 }
12232
David S. Millerf47c11e2005-06-24 20:18:35 -070012233 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012234
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012235 if (irq_sync && !err)
12236 tg3_phy_start(tp);
12237
Michael Chanb9ec6c12006-07-25 16:37:27 -070012238 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012239}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012240
Linus Torvalds1da177e2005-04-16 15:20:36 -070012241static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12242{
12243 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012244
Joe Perches63c3a662011-04-26 08:12:10 +000012245 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012246
Matt Carlson4a2db502011-12-08 14:40:17 +000012247 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012248 epause->rx_pause = 1;
12249 else
12250 epause->rx_pause = 0;
12251
Matt Carlson4a2db502011-12-08 14:40:17 +000012252 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012253 epause->tx_pause = 1;
12254 else
12255 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012256}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012257
Linus Torvalds1da177e2005-04-16 15:20:36 -070012258static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12259{
12260 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012261 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012262
Nithin Sujirce20f162013-04-09 08:48:04 +000012263 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12264 tg3_warn_mgmt_link_flap(tp);
12265
Joe Perches63c3a662011-04-26 08:12:10 +000012266 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012267 u32 newadv;
12268 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012269
Hauke Mehrtensead24022013-09-28 23:15:26 +020012270 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012271
Matt Carlson27121682010-02-17 15:16:57 +000012272 if (!(phydev->supported & SUPPORTED_Pause) ||
12273 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012274 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012275 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012276
Matt Carlson27121682010-02-17 15:16:57 +000012277 tp->link_config.flowctrl = 0;
12278 if (epause->rx_pause) {
12279 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012280
Matt Carlson27121682010-02-17 15:16:57 +000012281 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012282 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012283 newadv = ADVERTISED_Pause;
12284 } else
12285 newadv = ADVERTISED_Pause |
12286 ADVERTISED_Asym_Pause;
12287 } else if (epause->tx_pause) {
12288 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12289 newadv = ADVERTISED_Asym_Pause;
12290 } else
12291 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012292
Matt Carlson27121682010-02-17 15:16:57 +000012293 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012294 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012295 else
Joe Perches63c3a662011-04-26 08:12:10 +000012296 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012297
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012298 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012299 u32 oldadv = phydev->advertising &
12300 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12301 if (oldadv != newadv) {
12302 phydev->advertising &=
12303 ~(ADVERTISED_Pause |
12304 ADVERTISED_Asym_Pause);
12305 phydev->advertising |= newadv;
12306 if (phydev->autoneg) {
12307 /*
12308 * Always renegotiate the link to
12309 * inform our link partner of our
12310 * flow control settings, even if the
12311 * flow control is forced. Let
12312 * tg3_adjust_link() do the final
12313 * flow control setup.
12314 */
12315 return phy_start_aneg(phydev);
12316 }
12317 }
12318
12319 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012320 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012321 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012322 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012323 ~(ADVERTISED_Pause |
12324 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012325 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012326 }
12327 } else {
12328 int irq_sync = 0;
12329
12330 if (netif_running(dev)) {
12331 tg3_netif_stop(tp);
12332 irq_sync = 1;
12333 }
12334
12335 tg3_full_lock(tp, irq_sync);
12336
12337 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012338 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012339 else
Joe Perches63c3a662011-04-26 08:12:10 +000012340 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012341 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012342 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012343 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012344 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012345 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012346 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012347 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012348 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012349
12350 if (netif_running(dev)) {
12351 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012352 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012353 if (!err)
12354 tg3_netif_start(tp);
12355 }
12356
12357 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012359
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012360 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12361
Michael Chanb9ec6c12006-07-25 16:37:27 -070012362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012363}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012364
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012365static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012366{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012367 switch (sset) {
12368 case ETH_SS_TEST:
12369 return TG3_NUM_TEST;
12370 case ETH_SS_STATS:
12371 return TG3_NUM_STATS;
12372 default:
12373 return -EOPNOTSUPP;
12374 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012375}
12376
Matt Carlson90415472011-12-16 13:33:23 +000012377static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12378 u32 *rules __always_unused)
12379{
12380 struct tg3 *tp = netdev_priv(dev);
12381
12382 if (!tg3_flag(tp, SUPPORT_MSIX))
12383 return -EOPNOTSUPP;
12384
12385 switch (info->cmd) {
12386 case ETHTOOL_GRXRINGS:
12387 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012388 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012389 else {
12390 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012391 if (info->data > TG3_RSS_MAX_NUM_QS)
12392 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012393 }
12394
12395 /* The first interrupt vector only
12396 * handles link interrupts.
12397 */
12398 info->data -= 1;
12399 return 0;
12400
12401 default:
12402 return -EOPNOTSUPP;
12403 }
12404}
12405
12406static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12407{
12408 u32 size = 0;
12409 struct tg3 *tp = netdev_priv(dev);
12410
12411 if (tg3_flag(tp, SUPPORT_MSIX))
12412 size = TG3_RSS_INDIR_TBL_SIZE;
12413
12414 return size;
12415}
12416
12417static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12418{
12419 struct tg3 *tp = netdev_priv(dev);
12420 int i;
12421
12422 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12423 indir[i] = tp->rss_ind_tbl[i];
12424
12425 return 0;
12426}
12427
12428static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12429{
12430 struct tg3 *tp = netdev_priv(dev);
12431 size_t i;
12432
12433 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12434 tp->rss_ind_tbl[i] = indir[i];
12435
12436 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12437 return 0;
12438
12439 /* It is legal to write the indirection
12440 * table while the device is running.
12441 */
12442 tg3_full_lock(tp, 0);
12443 tg3_rss_write_indir_tbl(tp);
12444 tg3_full_unlock(tp);
12445
12446 return 0;
12447}
12448
Michael Chan09681692012-09-28 07:12:42 +000012449static void tg3_get_channels(struct net_device *dev,
12450 struct ethtool_channels *channel)
12451{
12452 struct tg3 *tp = netdev_priv(dev);
12453 u32 deflt_qs = netif_get_num_default_rss_queues();
12454
12455 channel->max_rx = tp->rxq_max;
12456 channel->max_tx = tp->txq_max;
12457
12458 if (netif_running(dev)) {
12459 channel->rx_count = tp->rxq_cnt;
12460 channel->tx_count = tp->txq_cnt;
12461 } else {
12462 if (tp->rxq_req)
12463 channel->rx_count = tp->rxq_req;
12464 else
12465 channel->rx_count = min(deflt_qs, tp->rxq_max);
12466
12467 if (tp->txq_req)
12468 channel->tx_count = tp->txq_req;
12469 else
12470 channel->tx_count = min(deflt_qs, tp->txq_max);
12471 }
12472}
12473
12474static int tg3_set_channels(struct net_device *dev,
12475 struct ethtool_channels *channel)
12476{
12477 struct tg3 *tp = netdev_priv(dev);
12478
12479 if (!tg3_flag(tp, SUPPORT_MSIX))
12480 return -EOPNOTSUPP;
12481
12482 if (channel->rx_count > tp->rxq_max ||
12483 channel->tx_count > tp->txq_max)
12484 return -EINVAL;
12485
12486 tp->rxq_req = channel->rx_count;
12487 tp->txq_req = channel->tx_count;
12488
12489 if (!netif_running(dev))
12490 return 0;
12491
12492 tg3_stop(tp);
12493
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012494 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012495
Matt Carlsonbe947302012-12-03 19:36:57 +000012496 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012497
12498 return 0;
12499}
12500
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012501static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012502{
12503 switch (stringset) {
12504 case ETH_SS_STATS:
12505 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12506 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012507 case ETH_SS_TEST:
12508 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012510 default:
12511 WARN_ON(1); /* we need a WARN() */
12512 break;
12513 }
12514}
12515
stephen hemminger81b87092011-04-04 08:43:50 +000012516static int tg3_set_phys_id(struct net_device *dev,
12517 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012518{
12519 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012520
12521 if (!netif_running(tp->dev))
12522 return -EAGAIN;
12523
stephen hemminger81b87092011-04-04 08:43:50 +000012524 switch (state) {
12525 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012526 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012527
stephen hemminger81b87092011-04-04 08:43:50 +000012528 case ETHTOOL_ID_ON:
12529 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12530 LED_CTRL_1000MBPS_ON |
12531 LED_CTRL_100MBPS_ON |
12532 LED_CTRL_10MBPS_ON |
12533 LED_CTRL_TRAFFIC_OVERRIDE |
12534 LED_CTRL_TRAFFIC_BLINK |
12535 LED_CTRL_TRAFFIC_LED);
12536 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012537
stephen hemminger81b87092011-04-04 08:43:50 +000012538 case ETHTOOL_ID_OFF:
12539 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12540 LED_CTRL_TRAFFIC_OVERRIDE);
12541 break;
Michael Chan4009a932005-09-05 17:52:54 -070012542
stephen hemminger81b87092011-04-04 08:43:50 +000012543 case ETHTOOL_ID_INACTIVE:
12544 tw32(MAC_LED_CTRL, tp->led_ctrl);
12545 break;
Michael Chan4009a932005-09-05 17:52:54 -070012546 }
stephen hemminger81b87092011-04-04 08:43:50 +000012547
Michael Chan4009a932005-09-05 17:52:54 -070012548 return 0;
12549}
12550
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012551static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012552 struct ethtool_stats *estats, u64 *tmp_stats)
12553{
12554 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012555
Matt Carlsonb546e462012-02-13 15:20:09 +000012556 if (tp->hw_stats)
12557 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12558 else
12559 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012560}
12561
Matt Carlson535a4902011-07-20 10:20:56 +000012562static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012563{
12564 int i;
12565 __be32 *buf;
12566 u32 offset = 0, len = 0;
12567 u32 magic, val;
12568
Joe Perches63c3a662011-04-26 08:12:10 +000012569 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012570 return NULL;
12571
12572 if (magic == TG3_EEPROM_MAGIC) {
12573 for (offset = TG3_NVM_DIR_START;
12574 offset < TG3_NVM_DIR_END;
12575 offset += TG3_NVM_DIRENT_SIZE) {
12576 if (tg3_nvram_read(tp, offset, &val))
12577 return NULL;
12578
12579 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12580 TG3_NVM_DIRTYPE_EXTVPD)
12581 break;
12582 }
12583
12584 if (offset != TG3_NVM_DIR_END) {
12585 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12586 if (tg3_nvram_read(tp, offset + 4, &offset))
12587 return NULL;
12588
12589 offset = tg3_nvram_logical_addr(tp, offset);
12590 }
12591 }
12592
12593 if (!offset || !len) {
12594 offset = TG3_NVM_VPD_OFF;
12595 len = TG3_NVM_VPD_LEN;
12596 }
12597
12598 buf = kmalloc(len, GFP_KERNEL);
12599 if (buf == NULL)
12600 return NULL;
12601
12602 if (magic == TG3_EEPROM_MAGIC) {
12603 for (i = 0; i < len; i += 4) {
12604 /* The data is in little-endian format in NVRAM.
12605 * Use the big-endian read routines to preserve
12606 * the byte order as it exists in NVRAM.
12607 */
12608 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12609 goto error;
12610 }
12611 } else {
12612 u8 *ptr;
12613 ssize_t cnt;
12614 unsigned int pos = 0;
12615
12616 ptr = (u8 *)&buf[0];
12617 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12618 cnt = pci_read_vpd(tp->pdev, pos,
12619 len - pos, ptr);
12620 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12621 cnt = 0;
12622 else if (cnt < 0)
12623 goto error;
12624 }
12625 if (pos != len)
12626 goto error;
12627 }
12628
Matt Carlson535a4902011-07-20 10:20:56 +000012629 *vpdlen = len;
12630
Matt Carlsonc3e94502011-04-13 11:05:08 +000012631 return buf;
12632
12633error:
12634 kfree(buf);
12635 return NULL;
12636}
12637
Michael Chan566f86a2005-05-29 14:56:58 -070012638#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012639#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12640#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12641#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012642#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12643#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012644#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012645#define NVRAM_SELFBOOT_HW_SIZE 0x20
12646#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012647
12648static int tg3_test_nvram(struct tg3 *tp)
12649{
Matt Carlson535a4902011-07-20 10:20:56 +000012650 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012651 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012652 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012653
Joe Perches63c3a662011-04-26 08:12:10 +000012654 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012655 return 0;
12656
Matt Carlsone4f34112009-02-25 14:25:00 +000012657 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012658 return -EIO;
12659
Michael Chan1b277772006-03-20 22:27:48 -080012660 if (magic == TG3_EEPROM_MAGIC)
12661 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012662 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012663 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12664 TG3_EEPROM_SB_FORMAT_1) {
12665 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12666 case TG3_EEPROM_SB_REVISION_0:
12667 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12668 break;
12669 case TG3_EEPROM_SB_REVISION_2:
12670 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12671 break;
12672 case TG3_EEPROM_SB_REVISION_3:
12673 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12674 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012675 case TG3_EEPROM_SB_REVISION_4:
12676 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12677 break;
12678 case TG3_EEPROM_SB_REVISION_5:
12679 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12680 break;
12681 case TG3_EEPROM_SB_REVISION_6:
12682 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12683 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012684 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012685 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012686 }
12687 } else
Michael Chan1b277772006-03-20 22:27:48 -080012688 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012689 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12690 size = NVRAM_SELFBOOT_HW_SIZE;
12691 else
Michael Chan1b277772006-03-20 22:27:48 -080012692 return -EIO;
12693
12694 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012695 if (buf == NULL)
12696 return -ENOMEM;
12697
Michael Chan1b277772006-03-20 22:27:48 -080012698 err = -EIO;
12699 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012700 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12701 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012702 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012703 }
Michael Chan1b277772006-03-20 22:27:48 -080012704 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012705 goto out;
12706
Michael Chan1b277772006-03-20 22:27:48 -080012707 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012708 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012709 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012710 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012711 u8 *buf8 = (u8 *) buf, csum8 = 0;
12712
Al Virob9fc7dc2007-12-17 22:59:57 -080012713 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012714 TG3_EEPROM_SB_REVISION_2) {
12715 /* For rev 2, the csum doesn't include the MBA. */
12716 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12717 csum8 += buf8[i];
12718 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12719 csum8 += buf8[i];
12720 } else {
12721 for (i = 0; i < size; i++)
12722 csum8 += buf8[i];
12723 }
Michael Chan1b277772006-03-20 22:27:48 -080012724
Adrian Bunkad96b482006-04-05 22:21:04 -070012725 if (csum8 == 0) {
12726 err = 0;
12727 goto out;
12728 }
12729
12730 err = -EIO;
12731 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012732 }
Michael Chan566f86a2005-05-29 14:56:58 -070012733
Al Virob9fc7dc2007-12-17 22:59:57 -080012734 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012735 TG3_EEPROM_MAGIC_HW) {
12736 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012737 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012738 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012739
12740 /* Separate the parity bits and the data bytes. */
12741 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12742 if ((i == 0) || (i == 8)) {
12743 int l;
12744 u8 msk;
12745
12746 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12747 parity[k++] = buf8[i] & msk;
12748 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012749 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012750 int l;
12751 u8 msk;
12752
12753 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12754 parity[k++] = buf8[i] & msk;
12755 i++;
12756
12757 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12758 parity[k++] = buf8[i] & msk;
12759 i++;
12760 }
12761 data[j++] = buf8[i];
12762 }
12763
12764 err = -EIO;
12765 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12766 u8 hw8 = hweight8(data[i]);
12767
12768 if ((hw8 & 0x1) && parity[i])
12769 goto out;
12770 else if (!(hw8 & 0x1) && !parity[i])
12771 goto out;
12772 }
12773 err = 0;
12774 goto out;
12775 }
12776
Matt Carlson01c3a392011-03-09 16:58:20 +000012777 err = -EIO;
12778
Michael Chan566f86a2005-05-29 14:56:58 -070012779 /* Bootstrap checksum at offset 0x10 */
12780 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012781 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012782 goto out;
12783
12784 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12785 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012786 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012787 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012788
Matt Carlsonc3e94502011-04-13 11:05:08 +000012789 kfree(buf);
12790
Matt Carlson535a4902011-07-20 10:20:56 +000012791 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012792 if (!buf)
12793 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012794
Matt Carlson535a4902011-07-20 10:20:56 +000012795 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012796 if (i > 0) {
12797 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12798 if (j < 0)
12799 goto out;
12800
Matt Carlson535a4902011-07-20 10:20:56 +000012801 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012802 goto out;
12803
12804 i += PCI_VPD_LRDT_TAG_SIZE;
12805 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12806 PCI_VPD_RO_KEYWORD_CHKSUM);
12807 if (j > 0) {
12808 u8 csum8 = 0;
12809
12810 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12811
12812 for (i = 0; i <= j; i++)
12813 csum8 += ((u8 *)buf)[i];
12814
12815 if (csum8)
12816 goto out;
12817 }
12818 }
12819
Michael Chan566f86a2005-05-29 14:56:58 -070012820 err = 0;
12821
12822out:
12823 kfree(buf);
12824 return err;
12825}
12826
Michael Chanca430072005-05-29 14:57:23 -070012827#define TG3_SERDES_TIMEOUT_SEC 2
12828#define TG3_COPPER_TIMEOUT_SEC 6
12829
12830static int tg3_test_link(struct tg3 *tp)
12831{
12832 int i, max;
12833
12834 if (!netif_running(tp->dev))
12835 return -ENODEV;
12836
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012837 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012838 max = TG3_SERDES_TIMEOUT_SEC;
12839 else
12840 max = TG3_COPPER_TIMEOUT_SEC;
12841
12842 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012843 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012844 return 0;
12845
12846 if (msleep_interruptible(1000))
12847 break;
12848 }
12849
12850 return -EIO;
12851}
12852
Michael Chana71116d2005-05-29 14:58:11 -070012853/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012854static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012855{
Michael Chanb16250e2006-09-27 16:10:14 -070012856 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012857 u32 offset, read_mask, write_mask, val, save_val, read_val;
12858 static struct {
12859 u16 offset;
12860 u16 flags;
12861#define TG3_FL_5705 0x1
12862#define TG3_FL_NOT_5705 0x2
12863#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012864#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012865 u32 read_mask;
12866 u32 write_mask;
12867 } reg_tbl[] = {
12868 /* MAC Control Registers */
12869 { MAC_MODE, TG3_FL_NOT_5705,
12870 0x00000000, 0x00ef6f8c },
12871 { MAC_MODE, TG3_FL_5705,
12872 0x00000000, 0x01ef6b8c },
12873 { MAC_STATUS, TG3_FL_NOT_5705,
12874 0x03800107, 0x00000000 },
12875 { MAC_STATUS, TG3_FL_5705,
12876 0x03800100, 0x00000000 },
12877 { MAC_ADDR_0_HIGH, 0x0000,
12878 0x00000000, 0x0000ffff },
12879 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012880 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012881 { MAC_RX_MTU_SIZE, 0x0000,
12882 0x00000000, 0x0000ffff },
12883 { MAC_TX_MODE, 0x0000,
12884 0x00000000, 0x00000070 },
12885 { MAC_TX_LENGTHS, 0x0000,
12886 0x00000000, 0x00003fff },
12887 { MAC_RX_MODE, TG3_FL_NOT_5705,
12888 0x00000000, 0x000007fc },
12889 { MAC_RX_MODE, TG3_FL_5705,
12890 0x00000000, 0x000007dc },
12891 { MAC_HASH_REG_0, 0x0000,
12892 0x00000000, 0xffffffff },
12893 { MAC_HASH_REG_1, 0x0000,
12894 0x00000000, 0xffffffff },
12895 { MAC_HASH_REG_2, 0x0000,
12896 0x00000000, 0xffffffff },
12897 { MAC_HASH_REG_3, 0x0000,
12898 0x00000000, 0xffffffff },
12899
12900 /* Receive Data and Receive BD Initiator Control Registers. */
12901 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12902 0x00000000, 0xffffffff },
12903 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12904 0x00000000, 0xffffffff },
12905 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12906 0x00000000, 0x00000003 },
12907 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12908 0x00000000, 0xffffffff },
12909 { RCVDBDI_STD_BD+0, 0x0000,
12910 0x00000000, 0xffffffff },
12911 { RCVDBDI_STD_BD+4, 0x0000,
12912 0x00000000, 0xffffffff },
12913 { RCVDBDI_STD_BD+8, 0x0000,
12914 0x00000000, 0xffff0002 },
12915 { RCVDBDI_STD_BD+0xc, 0x0000,
12916 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012917
Michael Chana71116d2005-05-29 14:58:11 -070012918 /* Receive BD Initiator Control Registers. */
12919 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12920 0x00000000, 0xffffffff },
12921 { RCVBDI_STD_THRESH, TG3_FL_5705,
12922 0x00000000, 0x000003ff },
12923 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12924 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012925
Michael Chana71116d2005-05-29 14:58:11 -070012926 /* Host Coalescing Control Registers. */
12927 { HOSTCC_MODE, TG3_FL_NOT_5705,
12928 0x00000000, 0x00000004 },
12929 { HOSTCC_MODE, TG3_FL_5705,
12930 0x00000000, 0x000000f6 },
12931 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12932 0x00000000, 0xffffffff },
12933 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12934 0x00000000, 0x000003ff },
12935 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12936 0x00000000, 0xffffffff },
12937 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12938 0x00000000, 0x000003ff },
12939 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12940 0x00000000, 0xffffffff },
12941 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12942 0x00000000, 0x000000ff },
12943 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12944 0x00000000, 0xffffffff },
12945 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12946 0x00000000, 0x000000ff },
12947 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12948 0x00000000, 0xffffffff },
12949 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12950 0x00000000, 0xffffffff },
12951 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12952 0x00000000, 0xffffffff },
12953 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12954 0x00000000, 0x000000ff },
12955 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12956 0x00000000, 0xffffffff },
12957 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12958 0x00000000, 0x000000ff },
12959 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12960 0x00000000, 0xffffffff },
12961 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12962 0x00000000, 0xffffffff },
12963 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12964 0x00000000, 0xffffffff },
12965 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12966 0x00000000, 0xffffffff },
12967 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12968 0x00000000, 0xffffffff },
12969 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12970 0xffffffff, 0x00000000 },
12971 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12972 0xffffffff, 0x00000000 },
12973
12974 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012975 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012976 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012977 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012978 0x00000000, 0x007fffff },
12979 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12980 0x00000000, 0x0000003f },
12981 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12982 0x00000000, 0x000001ff },
12983 { BUFMGR_MB_HIGH_WATER, 0x0000,
12984 0x00000000, 0x000001ff },
12985 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12986 0xffffffff, 0x00000000 },
12987 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12988 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012989
Michael Chana71116d2005-05-29 14:58:11 -070012990 /* Mailbox Registers */
12991 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12992 0x00000000, 0x000001ff },
12993 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12994 0x00000000, 0x000001ff },
12995 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12996 0x00000000, 0x000007ff },
12997 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12998 0x00000000, 0x000001ff },
12999
13000 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13001 };
13002
Michael Chanb16250e2006-09-27 16:10:14 -070013003 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013004 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013005 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013006 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013007 is_5750 = 1;
13008 }
Michael Chana71116d2005-05-29 14:58:11 -070013009
13010 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13011 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13012 continue;
13013
13014 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13015 continue;
13016
Joe Perches63c3a662011-04-26 08:12:10 +000013017 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013018 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13019 continue;
13020
Michael Chanb16250e2006-09-27 16:10:14 -070013021 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13022 continue;
13023
Michael Chana71116d2005-05-29 14:58:11 -070013024 offset = (u32) reg_tbl[i].offset;
13025 read_mask = reg_tbl[i].read_mask;
13026 write_mask = reg_tbl[i].write_mask;
13027
13028 /* Save the original register content */
13029 save_val = tr32(offset);
13030
13031 /* Determine the read-only value. */
13032 read_val = save_val & read_mask;
13033
13034 /* Write zero to the register, then make sure the read-only bits
13035 * are not changed and the read/write bits are all zeros.
13036 */
13037 tw32(offset, 0);
13038
13039 val = tr32(offset);
13040
13041 /* Test the read-only and read/write bits. */
13042 if (((val & read_mask) != read_val) || (val & write_mask))
13043 goto out;
13044
13045 /* Write ones to all the bits defined by RdMask and WrMask, then
13046 * make sure the read-only bits are not changed and the
13047 * read/write bits are all ones.
13048 */
13049 tw32(offset, read_mask | write_mask);
13050
13051 val = tr32(offset);
13052
13053 /* Test the read-only bits. */
13054 if ((val & read_mask) != read_val)
13055 goto out;
13056
13057 /* Test the read/write bits. */
13058 if ((val & write_mask) != write_mask)
13059 goto out;
13060
13061 tw32(offset, save_val);
13062 }
13063
13064 return 0;
13065
13066out:
Michael Chan9f88f292006-12-07 00:22:54 -080013067 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013068 netdev_err(tp->dev,
13069 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013070 tw32(offset, save_val);
13071 return -EIO;
13072}
13073
Michael Chan7942e1d2005-05-29 14:58:36 -070013074static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13075{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013076 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013077 int i;
13078 u32 j;
13079
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013080 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013081 for (j = 0; j < len; j += 4) {
13082 u32 val;
13083
13084 tg3_write_mem(tp, offset + j, test_pattern[i]);
13085 tg3_read_mem(tp, offset + j, &val);
13086 if (val != test_pattern[i])
13087 return -EIO;
13088 }
13089 }
13090 return 0;
13091}
13092
13093static int tg3_test_memory(struct tg3 *tp)
13094{
13095 static struct mem_entry {
13096 u32 offset;
13097 u32 len;
13098 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013099 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013100 { 0x00002000, 0x1c000},
13101 { 0xffffffff, 0x00000}
13102 }, mem_tbl_5705[] = {
13103 { 0x00000100, 0x0000c},
13104 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013105 { 0x00004000, 0x00800},
13106 { 0x00006000, 0x01000},
13107 { 0x00008000, 0x02000},
13108 { 0x00010000, 0x0e000},
13109 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013110 }, mem_tbl_5755[] = {
13111 { 0x00000200, 0x00008},
13112 { 0x00004000, 0x00800},
13113 { 0x00006000, 0x00800},
13114 { 0x00008000, 0x02000},
13115 { 0x00010000, 0x0c000},
13116 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013117 }, mem_tbl_5906[] = {
13118 { 0x00000200, 0x00008},
13119 { 0x00004000, 0x00400},
13120 { 0x00006000, 0x00400},
13121 { 0x00008000, 0x01000},
13122 { 0x00010000, 0x01000},
13123 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013124 }, mem_tbl_5717[] = {
13125 { 0x00000200, 0x00008},
13126 { 0x00010000, 0x0a000},
13127 { 0x00020000, 0x13c00},
13128 { 0xffffffff, 0x00000}
13129 }, mem_tbl_57765[] = {
13130 { 0x00000200, 0x00008},
13131 { 0x00004000, 0x00800},
13132 { 0x00006000, 0x09800},
13133 { 0x00010000, 0x0a000},
13134 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013135 };
13136 struct mem_entry *mem_tbl;
13137 int err = 0;
13138 int i;
13139
Joe Perches63c3a662011-04-26 08:12:10 +000013140 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013141 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013142 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013143 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013144 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013145 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013146 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013147 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013148 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013149 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013150 mem_tbl = mem_tbl_5705;
13151 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013152 mem_tbl = mem_tbl_570x;
13153
13154 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013155 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13156 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013157 break;
13158 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013159
Michael Chan7942e1d2005-05-29 14:58:36 -070013160 return err;
13161}
13162
Matt Carlsonbb158d62011-04-25 12:42:47 +000013163#define TG3_TSO_MSS 500
13164
13165#define TG3_TSO_IP_HDR_LEN 20
13166#define TG3_TSO_TCP_HDR_LEN 20
13167#define TG3_TSO_TCP_OPT_LEN 12
13168
13169static const u8 tg3_tso_header[] = {
131700x08, 0x00,
131710x45, 0x00, 0x00, 0x00,
131720x00, 0x00, 0x40, 0x00,
131730x40, 0x06, 0x00, 0x00,
131740x0a, 0x00, 0x00, 0x01,
131750x0a, 0x00, 0x00, 0x02,
131760x0d, 0x00, 0xe0, 0x00,
131770x00, 0x00, 0x01, 0x00,
131780x00, 0x00, 0x02, 0x00,
131790x80, 0x10, 0x10, 0x00,
131800x14, 0x09, 0x00, 0x00,
131810x01, 0x01, 0x08, 0x0a,
131820x11, 0x11, 0x11, 0x11,
131830x11, 0x11, 0x11, 0x11,
13184};
Michael Chan9f40dea2005-09-05 17:53:06 -070013185
Matt Carlson28a45952011-08-19 13:58:22 +000013186static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013187{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013188 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013189 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013190 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013191 struct sk_buff *skb;
13192 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013193 dma_addr_t map;
13194 int num_pkts, tx_len, rx_len, i, err;
13195 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013196 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013197 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013198
Matt Carlsonc8873402010-02-12 14:47:11 +000013199 tnapi = &tp->napi[0];
13200 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013201 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013202 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013203 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013204 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013205 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013206 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013207 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013208
Michael Chanc76949a2005-05-29 14:58:59 -070013209 err = -EIO;
13210
Matt Carlson4852a862011-04-13 11:05:07 +000013211 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013212 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013213 if (!skb)
13214 return -ENOMEM;
13215
Michael Chanc76949a2005-05-29 14:58:59 -070013216 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013217 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13218 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013219
Matt Carlson4852a862011-04-13 11:05:07 +000013220 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013221
Matt Carlson28a45952011-08-19 13:58:22 +000013222 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013223 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13224
13225 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13226 TG3_TSO_TCP_OPT_LEN;
13227
13228 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13229 sizeof(tg3_tso_header));
13230 mss = TG3_TSO_MSS;
13231
13232 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13233 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13234
13235 /* Set the total length field in the IP header */
13236 iph->tot_len = htons((u16)(mss + hdr_len));
13237
13238 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13239 TXD_FLAG_CPU_POST_DMA);
13240
Joe Perches63c3a662011-04-26 08:12:10 +000013241 if (tg3_flag(tp, HW_TSO_1) ||
13242 tg3_flag(tp, HW_TSO_2) ||
13243 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013244 struct tcphdr *th;
13245 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13246 th = (struct tcphdr *)&tx_data[val];
13247 th->check = 0;
13248 } else
13249 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13250
Joe Perches63c3a662011-04-26 08:12:10 +000013251 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013252 mss |= (hdr_len & 0xc) << 12;
13253 if (hdr_len & 0x10)
13254 base_flags |= 0x00000010;
13255 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013256 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013257 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013258 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013259 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013260 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13261 } else {
13262 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13263 }
13264
13265 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13266 } else {
13267 num_pkts = 1;
13268 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013269
13270 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13271 tx_len > VLAN_ETH_FRAME_LEN)
13272 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013273 }
13274
13275 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013276 tx_data[i] = (u8) (i & 0xff);
13277
Alexander Duyckf4188d82009-12-02 16:48:38 +000013278 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13279 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013280 dev_kfree_skb(skb);
13281 return -EIO;
13282 }
Michael Chanc76949a2005-05-29 14:58:59 -070013283
Matt Carlson0d681b22011-07-27 14:20:49 +000013284 val = tnapi->tx_prod;
13285 tnapi->tx_buffers[val].skb = skb;
13286 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13287
Michael Chanc76949a2005-05-29 14:58:59 -070013288 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013289 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013290
13291 udelay(10);
13292
Matt Carlson898a56f2009-08-28 14:02:40 +000013293 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013294
Matt Carlson84b67b22011-07-27 14:20:52 +000013295 budget = tg3_tx_avail(tnapi);
13296 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013297 base_flags | TXD_FLAG_END, mss, 0)) {
13298 tnapi->tx_buffers[val].skb = NULL;
13299 dev_kfree_skb(skb);
13300 return -EIO;
13301 }
Michael Chanc76949a2005-05-29 14:58:59 -070013302
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013303 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013304
Michael Chan6541b802012-03-04 14:48:14 +000013305 /* Sync BD data before updating mailbox */
13306 wmb();
13307
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013308 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13309 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013310
13311 udelay(10);
13312
Matt Carlson303fc922009-11-02 14:27:34 +000013313 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13314 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013315 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013316 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013317
13318 udelay(10);
13319
Matt Carlson898a56f2009-08-28 14:02:40 +000013320 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13321 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013322 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013323 (rx_idx == (rx_start_idx + num_pkts)))
13324 break;
13325 }
13326
Matt Carlsonba1142e2011-11-04 09:15:00 +000013327 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013328 dev_kfree_skb(skb);
13329
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013330 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013331 goto out;
13332
13333 if (rx_idx != rx_start_idx + num_pkts)
13334 goto out;
13335
Matt Carlsonbb158d62011-04-25 12:42:47 +000013336 val = data_off;
13337 while (rx_idx != rx_start_idx) {
13338 desc = &rnapi->rx_rcb[rx_start_idx++];
13339 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13340 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013341
Matt Carlsonbb158d62011-04-25 12:42:47 +000013342 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13343 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013344 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013345
Matt Carlsonbb158d62011-04-25 12:42:47 +000013346 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13347 - ETH_FCS_LEN;
13348
Matt Carlson28a45952011-08-19 13:58:22 +000013349 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013350 if (rx_len != tx_len)
13351 goto out;
13352
13353 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13354 if (opaque_key != RXD_OPAQUE_RING_STD)
13355 goto out;
13356 } else {
13357 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13358 goto out;
13359 }
13360 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13361 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013362 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013363 goto out;
13364 }
13365
13366 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013367 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013368 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13369 mapping);
13370 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013371 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013372 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13373 mapping);
13374 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013375 goto out;
13376
Matt Carlsonbb158d62011-04-25 12:42:47 +000013377 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13378 PCI_DMA_FROMDEVICE);
13379
Eric Dumazet9205fd92011-11-18 06:47:01 +000013380 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013381 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013382 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013383 goto out;
13384 }
Matt Carlson4852a862011-04-13 11:05:07 +000013385 }
13386
Michael Chanc76949a2005-05-29 14:58:59 -070013387 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013388
Eric Dumazet9205fd92011-11-18 06:47:01 +000013389 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013390out:
13391 return err;
13392}
13393
Matt Carlson00c266b2011-04-25 12:42:46 +000013394#define TG3_STD_LOOPBACK_FAILED 1
13395#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013396#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013397#define TG3_LOOPBACK_FAILED \
13398 (TG3_STD_LOOPBACK_FAILED | \
13399 TG3_JMB_LOOPBACK_FAILED | \
13400 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013401
Matt Carlson941ec902011-08-19 13:58:23 +000013402static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013403{
Matt Carlson28a45952011-08-19 13:58:22 +000013404 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013405 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013406 u32 jmb_pkt_sz = 9000;
13407
13408 if (tp->dma_limit)
13409 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013410
Matt Carlsonab789042011-01-25 15:58:54 +000013411 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13412 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13413
Matt Carlson28a45952011-08-19 13:58:22 +000013414 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013415 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13416 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013417 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013418 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013419 goto done;
13420 }
13421
Joe Perches953c96e2013-04-09 10:18:14 +000013422 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013423 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013424 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13425 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013426 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013427 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013428 goto done;
13429 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013430
Joe Perches63c3a662011-04-26 08:12:10 +000013431 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013432 int i;
13433
13434 /* Reroute all rx packets to the 1st queue */
13435 for (i = MAC_RSS_INDIR_TBL_0;
13436 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13437 tw32(i, 0x0);
13438 }
13439
Matt Carlson6e01b202011-08-19 13:58:20 +000013440 /* HW errata - mac loopback fails in some cases on 5780.
13441 * Normal traffic and PHY loopback are not affected by
13442 * errata. Also, the MAC loopback test is deprecated for
13443 * all newer ASIC revisions.
13444 */
Joe Perches41535772013-02-16 11:20:04 +000013445 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013446 !tg3_flag(tp, CPMU_PRESENT)) {
13447 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013448
Matt Carlson28a45952011-08-19 13:58:22 +000013449 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013450 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013451
13452 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013453 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013454 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013455
13456 tg3_mac_loopback(tp, false);
13457 }
Matt Carlson4852a862011-04-13 11:05:07 +000013458
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013459 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013460 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013461 int i;
13462
Matt Carlson941ec902011-08-19 13:58:23 +000013463 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013464
13465 /* Wait for link */
13466 for (i = 0; i < 100; i++) {
13467 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13468 break;
13469 mdelay(1);
13470 }
13471
Matt Carlson28a45952011-08-19 13:58:22 +000013472 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013473 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013474 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013475 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013476 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013477 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013478 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013479 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013480
Matt Carlson941ec902011-08-19 13:58:23 +000013481 if (do_extlpbk) {
13482 tg3_phy_lpbk_set(tp, 0, true);
13483
13484 /* All link indications report up, but the hardware
13485 * isn't really ready for about 20 msec. Double it
13486 * to be sure.
13487 */
13488 mdelay(40);
13489
13490 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013491 data[TG3_EXT_LOOPB_TEST] |=
13492 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013493 if (tg3_flag(tp, TSO_CAPABLE) &&
13494 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013495 data[TG3_EXT_LOOPB_TEST] |=
13496 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013497 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013498 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013499 data[TG3_EXT_LOOPB_TEST] |=
13500 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013501 }
13502
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013503 /* Re-enable gphy autopowerdown. */
13504 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13505 tg3_phy_toggle_apd(tp, true);
13506 }
Matt Carlson6833c042008-11-21 17:18:59 -080013507
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013508 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13509 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013510
Matt Carlsonab789042011-01-25 15:58:54 +000013511done:
13512 tp->phy_flags |= eee_cap;
13513
Michael Chan9f40dea2005-09-05 17:53:06 -070013514 return err;
13515}
13516
Michael Chan4cafd3f2005-05-29 14:56:34 -070013517static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13518 u64 *data)
13519{
Michael Chan566f86a2005-05-29 14:56:58 -070013520 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013521 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013522
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013523 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13524 if (tg3_power_up(tp)) {
13525 etest->flags |= ETH_TEST_FL_FAILED;
13526 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13527 return;
13528 }
13529 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013530 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013531
Michael Chan566f86a2005-05-29 14:56:58 -070013532 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13533
13534 if (tg3_test_nvram(tp) != 0) {
13535 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013536 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013537 }
Matt Carlson941ec902011-08-19 13:58:23 +000013538 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013539 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013540 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013541 }
Michael Chana71116d2005-05-29 14:58:11 -070013542 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013543 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013544
Michael Chanbbe832c2005-06-24 20:20:04 -070013545 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013546 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013547 tg3_netif_stop(tp);
13548 irq_sync = 1;
13549 }
13550
13551 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013552 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013553 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013554 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013555 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013556 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013557 if (!err)
13558 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013559
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013560 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013561 tg3_phy_reset(tp);
13562
Michael Chana71116d2005-05-29 14:58:11 -070013563 if (tg3_test_registers(tp) != 0) {
13564 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013565 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013566 }
Matt Carlson28a45952011-08-19 13:58:22 +000013567
Michael Chan7942e1d2005-05-29 14:58:36 -070013568 if (tg3_test_memory(tp) != 0) {
13569 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013570 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013571 }
Matt Carlson28a45952011-08-19 13:58:22 +000013572
Matt Carlson941ec902011-08-19 13:58:23 +000013573 if (doextlpbk)
13574 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13575
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013576 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013577 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013578
David S. Millerf47c11e2005-06-24 20:18:35 -070013579 tg3_full_unlock(tp);
13580
Michael Chand4bc3922005-05-29 14:59:20 -070013581 if (tg3_test_interrupt(tp) != 0) {
13582 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013583 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013584 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013585
13586 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013587
Michael Chana71116d2005-05-29 14:58:11 -070013588 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13589 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013590 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013591 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013592 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013593 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013594 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013595
13596 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013597
13598 if (irq_sync && !err2)
13599 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013600 }
Matt Carlson80096062010-08-02 11:26:06 +000013601 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013602 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013603
Michael Chan4cafd3f2005-05-29 14:56:34 -070013604}
13605
Matt Carlson0a633ac2012-12-03 19:36:59 +000013606static int tg3_hwtstamp_ioctl(struct net_device *dev,
13607 struct ifreq *ifr, int cmd)
13608{
13609 struct tg3 *tp = netdev_priv(dev);
13610 struct hwtstamp_config stmpconf;
13611
13612 if (!tg3_flag(tp, PTP_CAPABLE))
13613 return -EINVAL;
13614
13615 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13616 return -EFAULT;
13617
13618 if (stmpconf.flags)
13619 return -EINVAL;
13620
Ben Hutchings58b187c2013-11-14 00:40:56 +000013621 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13622 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013623 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013624
13625 switch (stmpconf.rx_filter) {
13626 case HWTSTAMP_FILTER_NONE:
13627 tp->rxptpctl = 0;
13628 break;
13629 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13630 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13631 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13632 break;
13633 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13634 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13635 TG3_RX_PTP_CTL_SYNC_EVNT;
13636 break;
13637 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13638 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13639 TG3_RX_PTP_CTL_DELAY_REQ;
13640 break;
13641 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13642 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13643 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13644 break;
13645 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13646 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13647 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13648 break;
13649 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13650 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13651 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13652 break;
13653 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13654 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13655 TG3_RX_PTP_CTL_SYNC_EVNT;
13656 break;
13657 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13658 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13659 TG3_RX_PTP_CTL_SYNC_EVNT;
13660 break;
13661 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13662 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13663 TG3_RX_PTP_CTL_SYNC_EVNT;
13664 break;
13665 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13666 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13667 TG3_RX_PTP_CTL_DELAY_REQ;
13668 break;
13669 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13670 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13671 TG3_RX_PTP_CTL_DELAY_REQ;
13672 break;
13673 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13674 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13675 TG3_RX_PTP_CTL_DELAY_REQ;
13676 break;
13677 default:
13678 return -ERANGE;
13679 }
13680
13681 if (netif_running(dev) && tp->rxptpctl)
13682 tw32(TG3_RX_PTP_CTL,
13683 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13684
Ben Hutchings58b187c2013-11-14 00:40:56 +000013685 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13686 tg3_flag_set(tp, TX_TSTAMP_EN);
13687 else
13688 tg3_flag_clear(tp, TX_TSTAMP_EN);
13689
Matt Carlson0a633ac2012-12-03 19:36:59 +000013690 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13691 -EFAULT : 0;
13692}
13693
Linus Torvalds1da177e2005-04-16 15:20:36 -070013694static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13695{
13696 struct mii_ioctl_data *data = if_mii(ifr);
13697 struct tg3 *tp = netdev_priv(dev);
13698 int err;
13699
Joe Perches63c3a662011-04-26 08:12:10 +000013700 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013701 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013702 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013703 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020013704 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Richard Cochran28b04112010-07-17 08:48:55 +000013705 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013706 }
13707
Matt Carlson33f401a2010-04-05 10:19:27 +000013708 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013709 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013710 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013711
13712 /* fallthru */
13713 case SIOCGMIIREG: {
13714 u32 mii_regval;
13715
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013716 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013717 break; /* We have no PHY */
13718
Matt Carlson34eea5a2011-04-20 07:57:38 +000013719 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013720 return -EAGAIN;
13721
David S. Millerf47c11e2005-06-24 20:18:35 -070013722 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013723 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13724 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013725 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013726
13727 data->val_out = mii_regval;
13728
13729 return err;
13730 }
13731
13732 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013733 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013734 break; /* We have no PHY */
13735
Matt Carlson34eea5a2011-04-20 07:57:38 +000013736 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013737 return -EAGAIN;
13738
David S. Millerf47c11e2005-06-24 20:18:35 -070013739 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013740 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13741 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013742 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013743
13744 return err;
13745
Matt Carlson0a633ac2012-12-03 19:36:59 +000013746 case SIOCSHWTSTAMP:
13747 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13748
Linus Torvalds1da177e2005-04-16 15:20:36 -070013749 default:
13750 /* do nothing */
13751 break;
13752 }
13753 return -EOPNOTSUPP;
13754}
13755
David S. Miller15f98502005-05-18 22:49:26 -070013756static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13757{
13758 struct tg3 *tp = netdev_priv(dev);
13759
13760 memcpy(ec, &tp->coal, sizeof(*ec));
13761 return 0;
13762}
13763
Michael Chand244c892005-07-05 14:42:33 -070013764static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13765{
13766 struct tg3 *tp = netdev_priv(dev);
13767 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13768 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13769
Joe Perches63c3a662011-04-26 08:12:10 +000013770 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013771 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13772 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13773 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13774 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13775 }
13776
13777 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13778 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13779 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13780 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13781 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13782 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13783 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13784 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13785 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13786 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13787 return -EINVAL;
13788
13789 /* No rx interrupts will be generated if both are zero */
13790 if ((ec->rx_coalesce_usecs == 0) &&
13791 (ec->rx_max_coalesced_frames == 0))
13792 return -EINVAL;
13793
13794 /* No tx interrupts will be generated if both are zero */
13795 if ((ec->tx_coalesce_usecs == 0) &&
13796 (ec->tx_max_coalesced_frames == 0))
13797 return -EINVAL;
13798
13799 /* Only copy relevant parameters, ignore all others. */
13800 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13801 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13802 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13803 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13804 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13805 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13806 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13807 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13808 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13809
13810 if (netif_running(dev)) {
13811 tg3_full_lock(tp, 0);
13812 __tg3_set_coalesce(tp, &tp->coal);
13813 tg3_full_unlock(tp);
13814 }
13815 return 0;
13816}
13817
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013818static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
13819{
13820 struct tg3 *tp = netdev_priv(dev);
13821
13822 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13823 netdev_warn(tp->dev, "Board does not support EEE!\n");
13824 return -EOPNOTSUPP;
13825 }
13826
13827 if (edata->advertised != tp->eee.advertised) {
13828 netdev_warn(tp->dev,
13829 "Direct manipulation of EEE advertisement is not supported\n");
13830 return -EINVAL;
13831 }
13832
13833 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
13834 netdev_warn(tp->dev,
13835 "Maximal Tx Lpi timer supported is %#x(u)\n",
13836 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
13837 return -EINVAL;
13838 }
13839
13840 tp->eee = *edata;
13841
13842 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
13843 tg3_warn_mgmt_link_flap(tp);
13844
13845 if (netif_running(tp->dev)) {
13846 tg3_full_lock(tp, 0);
13847 tg3_setup_eee(tp);
13848 tg3_phy_reset(tp);
13849 tg3_full_unlock(tp);
13850 }
13851
13852 return 0;
13853}
13854
13855static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
13856{
13857 struct tg3 *tp = netdev_priv(dev);
13858
13859 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13860 netdev_warn(tp->dev,
13861 "Board does not support EEE!\n");
13862 return -EOPNOTSUPP;
13863 }
13864
13865 *edata = tp->eee;
13866 return 0;
13867}
13868
Jeff Garzik7282d492006-09-13 14:30:00 -040013869static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013870 .get_settings = tg3_get_settings,
13871 .set_settings = tg3_set_settings,
13872 .get_drvinfo = tg3_get_drvinfo,
13873 .get_regs_len = tg3_get_regs_len,
13874 .get_regs = tg3_get_regs,
13875 .get_wol = tg3_get_wol,
13876 .set_wol = tg3_set_wol,
13877 .get_msglevel = tg3_get_msglevel,
13878 .set_msglevel = tg3_set_msglevel,
13879 .nway_reset = tg3_nway_reset,
13880 .get_link = ethtool_op_get_link,
13881 .get_eeprom_len = tg3_get_eeprom_len,
13882 .get_eeprom = tg3_get_eeprom,
13883 .set_eeprom = tg3_set_eeprom,
13884 .get_ringparam = tg3_get_ringparam,
13885 .set_ringparam = tg3_set_ringparam,
13886 .get_pauseparam = tg3_get_pauseparam,
13887 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013888 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013889 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013890 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013891 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013892 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013893 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013894 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013895 .get_rxnfc = tg3_get_rxnfc,
13896 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13897 .get_rxfh_indir = tg3_get_rxfh_indir,
13898 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013899 .get_channels = tg3_get_channels,
13900 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013901 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013902 .get_eee = tg3_get_eee,
13903 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013904};
13905
David S. Millerb4017c52012-03-01 17:57:40 -050013906static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13907 struct rtnl_link_stats64 *stats)
13908{
13909 struct tg3 *tp = netdev_priv(dev);
13910
David S. Millerb4017c52012-03-01 17:57:40 -050013911 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013912 if (!tp->hw_stats) {
13913 spin_unlock_bh(&tp->lock);
13914 return &tp->net_stats_prev;
13915 }
13916
David S. Millerb4017c52012-03-01 17:57:40 -050013917 tg3_get_nstats(tp, stats);
13918 spin_unlock_bh(&tp->lock);
13919
13920 return stats;
13921}
13922
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013923static void tg3_set_rx_mode(struct net_device *dev)
13924{
13925 struct tg3 *tp = netdev_priv(dev);
13926
13927 if (!netif_running(dev))
13928 return;
13929
13930 tg3_full_lock(tp, 0);
13931 __tg3_set_rx_mode(dev);
13932 tg3_full_unlock(tp);
13933}
13934
Matt Carlsonfaf16272012-02-13 10:20:07 +000013935static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13936 int new_mtu)
13937{
13938 dev->mtu = new_mtu;
13939
13940 if (new_mtu > ETH_DATA_LEN) {
13941 if (tg3_flag(tp, 5780_CLASS)) {
13942 netdev_update_features(dev);
13943 tg3_flag_clear(tp, TSO_CAPABLE);
13944 } else {
13945 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13946 }
13947 } else {
13948 if (tg3_flag(tp, 5780_CLASS)) {
13949 tg3_flag_set(tp, TSO_CAPABLE);
13950 netdev_update_features(dev);
13951 }
13952 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13953 }
13954}
13955
13956static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13957{
13958 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000013959 int err;
13960 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013961
13962 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13963 return -EINVAL;
13964
13965 if (!netif_running(dev)) {
13966 /* We'll just catch it later when the
13967 * device is up'd.
13968 */
13969 tg3_set_mtu(dev, tp, new_mtu);
13970 return 0;
13971 }
13972
13973 tg3_phy_stop(tp);
13974
13975 tg3_netif_stop(tp);
13976
13977 tg3_full_lock(tp, 1);
13978
13979 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13980
13981 tg3_set_mtu(dev, tp, new_mtu);
13982
Michael Chan2fae5e32012-03-04 14:48:15 +000013983 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13984 * breaks all requests to 256 bytes.
13985 */
Joe Perches41535772013-02-16 11:20:04 +000013986 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000013987 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000013988
13989 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013990
13991 if (!err)
13992 tg3_netif_start(tp);
13993
13994 tg3_full_unlock(tp);
13995
13996 if (!err)
13997 tg3_phy_start(tp);
13998
13999 return err;
14000}
14001
14002static const struct net_device_ops tg3_netdev_ops = {
14003 .ndo_open = tg3_open,
14004 .ndo_stop = tg3_close,
14005 .ndo_start_xmit = tg3_start_xmit,
14006 .ndo_get_stats64 = tg3_get_stats64,
14007 .ndo_validate_addr = eth_validate_addr,
14008 .ndo_set_rx_mode = tg3_set_rx_mode,
14009 .ndo_set_mac_address = tg3_set_mac_addr,
14010 .ndo_do_ioctl = tg3_ioctl,
14011 .ndo_tx_timeout = tg3_tx_timeout,
14012 .ndo_change_mtu = tg3_change_mtu,
14013 .ndo_fix_features = tg3_fix_features,
14014 .ndo_set_features = tg3_set_features,
14015#ifdef CONFIG_NET_POLL_CONTROLLER
14016 .ndo_poll_controller = tg3_poll_controller,
14017#endif
14018};
14019
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014020static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014021{
Michael Chan1b277772006-03-20 22:27:48 -080014022 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023
14024 tp->nvram_size = EEPROM_CHIP_SIZE;
14025
Matt Carlsone4f34112009-02-25 14:25:00 +000014026 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027 return;
14028
Michael Chanb16250e2006-09-27 16:10:14 -070014029 if ((magic != TG3_EEPROM_MAGIC) &&
14030 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14031 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014032 return;
14033
14034 /*
14035 * Size the chip by reading offsets at increasing powers of two.
14036 * When we encounter our validation signature, we know the addressing
14037 * has wrapped around, and thus have our chip size.
14038 */
Michael Chan1b277772006-03-20 22:27:48 -080014039 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014040
14041 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014042 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014043 return;
14044
Michael Chan18201802006-03-20 22:29:15 -080014045 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014046 break;
14047
14048 cursize <<= 1;
14049 }
14050
14051 tp->nvram_size = cursize;
14052}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014053
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014054static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014055{
14056 u32 val;
14057
Joe Perches63c3a662011-04-26 08:12:10 +000014058 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014059 return;
14060
14061 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014062 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014063 tg3_get_eeprom_size(tp);
14064 return;
14065 }
14066
Matt Carlson6d348f22009-02-25 14:25:52 +000014067 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014068 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014069 /* This is confusing. We want to operate on the
14070 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14071 * call will read from NVRAM and byteswap the data
14072 * according to the byteswapping settings for all
14073 * other register accesses. This ensures the data we
14074 * want will always reside in the lower 16-bits.
14075 * However, the data in NVRAM is in LE format, which
14076 * means the data from the NVRAM read will always be
14077 * opposite the endianness of the CPU. The 16-bit
14078 * byteswap then brings the data to CPU endianness.
14079 */
14080 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014081 return;
14082 }
14083 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014084 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014085}
14086
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014087static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014088{
14089 u32 nvcfg1;
14090
14091 nvcfg1 = tr32(NVRAM_CFG1);
14092 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014093 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014094 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014095 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14096 tw32(NVRAM_CFG1, nvcfg1);
14097 }
14098
Joe Perches41535772013-02-16 11:20:04 +000014099 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014100 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014101 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014102 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14103 tp->nvram_jedecnum = JEDEC_ATMEL;
14104 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014105 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014106 break;
14107 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14108 tp->nvram_jedecnum = JEDEC_ATMEL;
14109 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14110 break;
14111 case FLASH_VENDOR_ATMEL_EEPROM:
14112 tp->nvram_jedecnum = JEDEC_ATMEL;
14113 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014114 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014115 break;
14116 case FLASH_VENDOR_ST:
14117 tp->nvram_jedecnum = JEDEC_ST;
14118 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014119 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014120 break;
14121 case FLASH_VENDOR_SAIFUN:
14122 tp->nvram_jedecnum = JEDEC_SAIFUN;
14123 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14124 break;
14125 case FLASH_VENDOR_SST_SMALL:
14126 case FLASH_VENDOR_SST_LARGE:
14127 tp->nvram_jedecnum = JEDEC_SST;
14128 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014130 }
Matt Carlson8590a602009-08-28 12:29:16 +000014131 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132 tp->nvram_jedecnum = JEDEC_ATMEL;
14133 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014134 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014135 }
14136}
14137
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014138static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014139{
14140 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14141 case FLASH_5752PAGE_SIZE_256:
14142 tp->nvram_pagesize = 256;
14143 break;
14144 case FLASH_5752PAGE_SIZE_512:
14145 tp->nvram_pagesize = 512;
14146 break;
14147 case FLASH_5752PAGE_SIZE_1K:
14148 tp->nvram_pagesize = 1024;
14149 break;
14150 case FLASH_5752PAGE_SIZE_2K:
14151 tp->nvram_pagesize = 2048;
14152 break;
14153 case FLASH_5752PAGE_SIZE_4K:
14154 tp->nvram_pagesize = 4096;
14155 break;
14156 case FLASH_5752PAGE_SIZE_264:
14157 tp->nvram_pagesize = 264;
14158 break;
14159 case FLASH_5752PAGE_SIZE_528:
14160 tp->nvram_pagesize = 528;
14161 break;
14162 }
14163}
14164
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014165static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014166{
14167 u32 nvcfg1;
14168
14169 nvcfg1 = tr32(NVRAM_CFG1);
14170
Michael Chane6af3012005-04-21 17:12:05 -070014171 /* NVRAM protection for TPM */
14172 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014173 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014174
Michael Chan361b4ac2005-04-21 17:11:21 -070014175 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014176 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14177 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14178 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014179 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014180 break;
14181 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14182 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014183 tg3_flag_set(tp, NVRAM_BUFFERED);
14184 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014185 break;
14186 case FLASH_5752VENDOR_ST_M45PE10:
14187 case FLASH_5752VENDOR_ST_M45PE20:
14188 case FLASH_5752VENDOR_ST_M45PE40:
14189 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014190 tg3_flag_set(tp, NVRAM_BUFFERED);
14191 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014192 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014193 }
14194
Joe Perches63c3a662011-04-26 08:12:10 +000014195 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014196 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014197 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014198 /* For eeprom, set pagesize to maximum eeprom size */
14199 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14200
14201 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14202 tw32(NVRAM_CFG1, nvcfg1);
14203 }
14204}
14205
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014206static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014207{
Matt Carlson989a9d22007-05-05 11:51:05 -070014208 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014209
14210 nvcfg1 = tr32(NVRAM_CFG1);
14211
14212 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014213 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014214 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014215 protect = 1;
14216 }
Michael Chand3c7b882006-03-23 01:28:25 -080014217
Matt Carlson989a9d22007-05-05 11:51:05 -070014218 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14219 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014220 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14221 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14222 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14223 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14224 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014225 tg3_flag_set(tp, NVRAM_BUFFERED);
14226 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014227 tp->nvram_pagesize = 264;
14228 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14229 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14230 tp->nvram_size = (protect ? 0x3e200 :
14231 TG3_NVRAM_SIZE_512KB);
14232 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14233 tp->nvram_size = (protect ? 0x1f200 :
14234 TG3_NVRAM_SIZE_256KB);
14235 else
14236 tp->nvram_size = (protect ? 0x1f200 :
14237 TG3_NVRAM_SIZE_128KB);
14238 break;
14239 case FLASH_5752VENDOR_ST_M45PE10:
14240 case FLASH_5752VENDOR_ST_M45PE20:
14241 case FLASH_5752VENDOR_ST_M45PE40:
14242 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014243 tg3_flag_set(tp, NVRAM_BUFFERED);
14244 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014245 tp->nvram_pagesize = 256;
14246 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14247 tp->nvram_size = (protect ?
14248 TG3_NVRAM_SIZE_64KB :
14249 TG3_NVRAM_SIZE_128KB);
14250 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14251 tp->nvram_size = (protect ?
14252 TG3_NVRAM_SIZE_64KB :
14253 TG3_NVRAM_SIZE_256KB);
14254 else
14255 tp->nvram_size = (protect ?
14256 TG3_NVRAM_SIZE_128KB :
14257 TG3_NVRAM_SIZE_512KB);
14258 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014259 }
14260}
14261
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014262static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014263{
14264 u32 nvcfg1;
14265
14266 nvcfg1 = tr32(NVRAM_CFG1);
14267
14268 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014269 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14270 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14271 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14272 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14273 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014274 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014275 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014276
Matt Carlson8590a602009-08-28 12:29:16 +000014277 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14278 tw32(NVRAM_CFG1, nvcfg1);
14279 break;
14280 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14281 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14282 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14283 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14284 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014285 tg3_flag_set(tp, NVRAM_BUFFERED);
14286 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014287 tp->nvram_pagesize = 264;
14288 break;
14289 case FLASH_5752VENDOR_ST_M45PE10:
14290 case FLASH_5752VENDOR_ST_M45PE20:
14291 case FLASH_5752VENDOR_ST_M45PE40:
14292 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014293 tg3_flag_set(tp, NVRAM_BUFFERED);
14294 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014295 tp->nvram_pagesize = 256;
14296 break;
Michael Chan1b277772006-03-20 22:27:48 -080014297 }
14298}
14299
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014300static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014301{
14302 u32 nvcfg1, protect = 0;
14303
14304 nvcfg1 = tr32(NVRAM_CFG1);
14305
14306 /* NVRAM protection for TPM */
14307 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014308 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014309 protect = 1;
14310 }
14311
14312 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14313 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014314 case FLASH_5761VENDOR_ATMEL_ADB021D:
14315 case FLASH_5761VENDOR_ATMEL_ADB041D:
14316 case FLASH_5761VENDOR_ATMEL_ADB081D:
14317 case FLASH_5761VENDOR_ATMEL_ADB161D:
14318 case FLASH_5761VENDOR_ATMEL_MDB021D:
14319 case FLASH_5761VENDOR_ATMEL_MDB041D:
14320 case FLASH_5761VENDOR_ATMEL_MDB081D:
14321 case FLASH_5761VENDOR_ATMEL_MDB161D:
14322 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014323 tg3_flag_set(tp, NVRAM_BUFFERED);
14324 tg3_flag_set(tp, FLASH);
14325 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014326 tp->nvram_pagesize = 256;
14327 break;
14328 case FLASH_5761VENDOR_ST_A_M45PE20:
14329 case FLASH_5761VENDOR_ST_A_M45PE40:
14330 case FLASH_5761VENDOR_ST_A_M45PE80:
14331 case FLASH_5761VENDOR_ST_A_M45PE16:
14332 case FLASH_5761VENDOR_ST_M_M45PE20:
14333 case FLASH_5761VENDOR_ST_M_M45PE40:
14334 case FLASH_5761VENDOR_ST_M_M45PE80:
14335 case FLASH_5761VENDOR_ST_M_M45PE16:
14336 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014337 tg3_flag_set(tp, NVRAM_BUFFERED);
14338 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014339 tp->nvram_pagesize = 256;
14340 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014341 }
14342
14343 if (protect) {
14344 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14345 } else {
14346 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014347 case FLASH_5761VENDOR_ATMEL_ADB161D:
14348 case FLASH_5761VENDOR_ATMEL_MDB161D:
14349 case FLASH_5761VENDOR_ST_A_M45PE16:
14350 case FLASH_5761VENDOR_ST_M_M45PE16:
14351 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14352 break;
14353 case FLASH_5761VENDOR_ATMEL_ADB081D:
14354 case FLASH_5761VENDOR_ATMEL_MDB081D:
14355 case FLASH_5761VENDOR_ST_A_M45PE80:
14356 case FLASH_5761VENDOR_ST_M_M45PE80:
14357 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14358 break;
14359 case FLASH_5761VENDOR_ATMEL_ADB041D:
14360 case FLASH_5761VENDOR_ATMEL_MDB041D:
14361 case FLASH_5761VENDOR_ST_A_M45PE40:
14362 case FLASH_5761VENDOR_ST_M_M45PE40:
14363 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14364 break;
14365 case FLASH_5761VENDOR_ATMEL_ADB021D:
14366 case FLASH_5761VENDOR_ATMEL_MDB021D:
14367 case FLASH_5761VENDOR_ST_A_M45PE20:
14368 case FLASH_5761VENDOR_ST_M_M45PE20:
14369 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14370 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014371 }
14372 }
14373}
14374
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014375static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014376{
14377 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014378 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014379 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14380}
14381
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014382static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014383{
14384 u32 nvcfg1;
14385
14386 nvcfg1 = tr32(NVRAM_CFG1);
14387
14388 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14389 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14390 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14391 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014392 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014393 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14394
14395 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14396 tw32(NVRAM_CFG1, nvcfg1);
14397 return;
14398 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14399 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14400 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14401 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14402 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14403 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14404 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14405 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014406 tg3_flag_set(tp, NVRAM_BUFFERED);
14407 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014408
14409 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14410 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14411 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14412 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14413 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14414 break;
14415 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14416 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14417 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14418 break;
14419 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14420 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14421 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14422 break;
14423 }
14424 break;
14425 case FLASH_5752VENDOR_ST_M45PE10:
14426 case FLASH_5752VENDOR_ST_M45PE20:
14427 case FLASH_5752VENDOR_ST_M45PE40:
14428 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014429 tg3_flag_set(tp, NVRAM_BUFFERED);
14430 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014431
14432 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14433 case FLASH_5752VENDOR_ST_M45PE10:
14434 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14435 break;
14436 case FLASH_5752VENDOR_ST_M45PE20:
14437 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14438 break;
14439 case FLASH_5752VENDOR_ST_M45PE40:
14440 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14441 break;
14442 }
14443 break;
14444 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014445 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014446 return;
14447 }
14448
Matt Carlsona1b950d2009-09-01 13:20:17 +000014449 tg3_nvram_get_pagesize(tp, nvcfg1);
14450 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014451 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014452}
14453
14454
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014455static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014456{
14457 u32 nvcfg1;
14458
14459 nvcfg1 = tr32(NVRAM_CFG1);
14460
14461 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14462 case FLASH_5717VENDOR_ATMEL_EEPROM:
14463 case FLASH_5717VENDOR_MICRO_EEPROM:
14464 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014465 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014466 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14467
14468 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14469 tw32(NVRAM_CFG1, nvcfg1);
14470 return;
14471 case FLASH_5717VENDOR_ATMEL_MDB011D:
14472 case FLASH_5717VENDOR_ATMEL_ADB011B:
14473 case FLASH_5717VENDOR_ATMEL_ADB011D:
14474 case FLASH_5717VENDOR_ATMEL_MDB021D:
14475 case FLASH_5717VENDOR_ATMEL_ADB021B:
14476 case FLASH_5717VENDOR_ATMEL_ADB021D:
14477 case FLASH_5717VENDOR_ATMEL_45USPT:
14478 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014479 tg3_flag_set(tp, NVRAM_BUFFERED);
14480 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014481
14482 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14483 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014484 /* Detect size with tg3_nvram_get_size() */
14485 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014486 case FLASH_5717VENDOR_ATMEL_ADB021B:
14487 case FLASH_5717VENDOR_ATMEL_ADB021D:
14488 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14489 break;
14490 default:
14491 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14492 break;
14493 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014494 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014495 case FLASH_5717VENDOR_ST_M_M25PE10:
14496 case FLASH_5717VENDOR_ST_A_M25PE10:
14497 case FLASH_5717VENDOR_ST_M_M45PE10:
14498 case FLASH_5717VENDOR_ST_A_M45PE10:
14499 case FLASH_5717VENDOR_ST_M_M25PE20:
14500 case FLASH_5717VENDOR_ST_A_M25PE20:
14501 case FLASH_5717VENDOR_ST_M_M45PE20:
14502 case FLASH_5717VENDOR_ST_A_M45PE20:
14503 case FLASH_5717VENDOR_ST_25USPT:
14504 case FLASH_5717VENDOR_ST_45USPT:
14505 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014506 tg3_flag_set(tp, NVRAM_BUFFERED);
14507 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014508
14509 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14510 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014511 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014512 /* Detect size with tg3_nvram_get_size() */
14513 break;
14514 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014515 case FLASH_5717VENDOR_ST_A_M45PE20:
14516 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14517 break;
14518 default:
14519 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14520 break;
14521 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014522 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014523 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014524 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014525 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014526 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014527
14528 tg3_nvram_get_pagesize(tp, nvcfg1);
14529 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014530 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014531}
14532
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014533static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014534{
14535 u32 nvcfg1, nvmpinstrp;
14536
14537 nvcfg1 = tr32(NVRAM_CFG1);
14538 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14539
Joe Perches41535772013-02-16 11:20:04 +000014540 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014541 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14542 tg3_flag_set(tp, NO_NVRAM);
14543 return;
14544 }
14545
14546 switch (nvmpinstrp) {
14547 case FLASH_5762_EEPROM_HD:
14548 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014549 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014550 case FLASH_5762_EEPROM_LD:
14551 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014552 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014553 case FLASH_5720VENDOR_M_ST_M45PE20:
14554 /* This pinstrap supports multiple sizes, so force it
14555 * to read the actual size from location 0xf0.
14556 */
14557 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14558 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014559 }
14560 }
14561
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014562 switch (nvmpinstrp) {
14563 case FLASH_5720_EEPROM_HD:
14564 case FLASH_5720_EEPROM_LD:
14565 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014566 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014567
14568 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14569 tw32(NVRAM_CFG1, nvcfg1);
14570 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14571 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14572 else
14573 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14574 return;
14575 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14576 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14577 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14578 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14579 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14580 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14581 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14582 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14583 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14584 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14585 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14586 case FLASH_5720VENDOR_ATMEL_45USPT:
14587 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014588 tg3_flag_set(tp, NVRAM_BUFFERED);
14589 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014590
14591 switch (nvmpinstrp) {
14592 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14593 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14594 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14595 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14596 break;
14597 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14598 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14599 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14600 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14601 break;
14602 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14603 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14604 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14605 break;
14606 default:
Joe Perches41535772013-02-16 11:20:04 +000014607 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014608 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014609 break;
14610 }
14611 break;
14612 case FLASH_5720VENDOR_M_ST_M25PE10:
14613 case FLASH_5720VENDOR_M_ST_M45PE10:
14614 case FLASH_5720VENDOR_A_ST_M25PE10:
14615 case FLASH_5720VENDOR_A_ST_M45PE10:
14616 case FLASH_5720VENDOR_M_ST_M25PE20:
14617 case FLASH_5720VENDOR_M_ST_M45PE20:
14618 case FLASH_5720VENDOR_A_ST_M25PE20:
14619 case FLASH_5720VENDOR_A_ST_M45PE20:
14620 case FLASH_5720VENDOR_M_ST_M25PE40:
14621 case FLASH_5720VENDOR_M_ST_M45PE40:
14622 case FLASH_5720VENDOR_A_ST_M25PE40:
14623 case FLASH_5720VENDOR_A_ST_M45PE40:
14624 case FLASH_5720VENDOR_M_ST_M25PE80:
14625 case FLASH_5720VENDOR_M_ST_M45PE80:
14626 case FLASH_5720VENDOR_A_ST_M25PE80:
14627 case FLASH_5720VENDOR_A_ST_M45PE80:
14628 case FLASH_5720VENDOR_ST_25USPT:
14629 case FLASH_5720VENDOR_ST_45USPT:
14630 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014631 tg3_flag_set(tp, NVRAM_BUFFERED);
14632 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014633
14634 switch (nvmpinstrp) {
14635 case FLASH_5720VENDOR_M_ST_M25PE20:
14636 case FLASH_5720VENDOR_M_ST_M45PE20:
14637 case FLASH_5720VENDOR_A_ST_M25PE20:
14638 case FLASH_5720VENDOR_A_ST_M45PE20:
14639 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14640 break;
14641 case FLASH_5720VENDOR_M_ST_M25PE40:
14642 case FLASH_5720VENDOR_M_ST_M45PE40:
14643 case FLASH_5720VENDOR_A_ST_M25PE40:
14644 case FLASH_5720VENDOR_A_ST_M45PE40:
14645 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14646 break;
14647 case FLASH_5720VENDOR_M_ST_M25PE80:
14648 case FLASH_5720VENDOR_M_ST_M45PE80:
14649 case FLASH_5720VENDOR_A_ST_M25PE80:
14650 case FLASH_5720VENDOR_A_ST_M45PE80:
14651 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14652 break;
14653 default:
Joe Perches41535772013-02-16 11:20:04 +000014654 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014655 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014656 break;
14657 }
14658 break;
14659 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014660 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014661 return;
14662 }
14663
14664 tg3_nvram_get_pagesize(tp, nvcfg1);
14665 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014666 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014667
Joe Perches41535772013-02-16 11:20:04 +000014668 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014669 u32 val;
14670
14671 if (tg3_nvram_read(tp, 0, &val))
14672 return;
14673
14674 if (val != TG3_EEPROM_MAGIC &&
14675 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14676 tg3_flag_set(tp, NO_NVRAM);
14677 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014678}
14679
Linus Torvalds1da177e2005-04-16 15:20:36 -070014680/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014681static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014682{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014683 if (tg3_flag(tp, IS_SSB_CORE)) {
14684 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14685 tg3_flag_clear(tp, NVRAM);
14686 tg3_flag_clear(tp, NVRAM_BUFFERED);
14687 tg3_flag_set(tp, NO_NVRAM);
14688 return;
14689 }
14690
Linus Torvalds1da177e2005-04-16 15:20:36 -070014691 tw32_f(GRC_EEPROM_ADDR,
14692 (EEPROM_ADDR_FSM_RESET |
14693 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14694 EEPROM_ADDR_CLKPERD_SHIFT)));
14695
Michael Chan9d57f012006-12-07 00:23:25 -080014696 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014697
14698 /* Enable seeprom accesses. */
14699 tw32_f(GRC_LOCAL_CTRL,
14700 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14701 udelay(100);
14702
Joe Perches41535772013-02-16 11:20:04 +000014703 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14704 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014705 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014706
Michael Chanec41c7d2006-01-17 02:40:55 -080014707 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014708 netdev_warn(tp->dev,
14709 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014710 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014711 return;
14712 }
Michael Chane6af3012005-04-21 17:12:05 -070014713 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014714
Matt Carlson989a9d22007-05-05 11:51:05 -070014715 tp->nvram_size = 0;
14716
Joe Perches41535772013-02-16 11:20:04 +000014717 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014718 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014719 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014720 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014721 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14722 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14723 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014724 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014725 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014726 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014727 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014728 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014729 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014730 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014731 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014732 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14733 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014734 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014735 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14736 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014737 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014738 else
14739 tg3_get_nvram_info(tp);
14740
Matt Carlson989a9d22007-05-05 11:51:05 -070014741 if (tp->nvram_size == 0)
14742 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014743
Michael Chane6af3012005-04-21 17:12:05 -070014744 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014745 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014746
14747 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014748 tg3_flag_clear(tp, NVRAM);
14749 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014750
14751 tg3_get_eeprom_size(tp);
14752 }
14753}
14754
Linus Torvalds1da177e2005-04-16 15:20:36 -070014755struct subsys_tbl_ent {
14756 u16 subsys_vendor, subsys_devid;
14757 u32 phy_id;
14758};
14759
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014760static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014761 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014762 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014763 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014764 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014765 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014766 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014767 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014768 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14769 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14770 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014771 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014772 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014773 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014774 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14775 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14776 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014777 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014778 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014779 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014780 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014781 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014782 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014783 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784
14785 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014786 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014787 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014788 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014789 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014790 { TG3PCI_SUBVENDOR_ID_3COM,
14791 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14792 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014793 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014794 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014795 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014796
14797 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014798 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014799 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014800 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014801 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014802 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014803 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014804 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014805 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014806
14807 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014808 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014809 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014810 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014811 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014812 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14813 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14814 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014815 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014816 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014817 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014818
14819 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014820 { TG3PCI_SUBVENDOR_ID_IBM,
14821 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014822};
14823
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014824static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014825{
14826 int i;
14827
14828 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14829 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14830 tp->pdev->subsystem_vendor) &&
14831 (subsys_id_to_phy_id[i].subsys_devid ==
14832 tp->pdev->subsystem_device))
14833 return &subsys_id_to_phy_id[i];
14834 }
14835 return NULL;
14836}
14837
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014838static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014839{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014840 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014841
Matt Carlson79eb6902010-02-17 15:17:03 +000014842 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014843 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14844
Gary Zambranoa85feb82007-05-05 11:52:19 -070014845 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014846 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14847 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014848
Joe Perches41535772013-02-16 11:20:04 +000014849 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014850 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014851 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14852 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014853 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014854 val = tr32(VCPU_CFGSHDW);
14855 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014856 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014857 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014858 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014859 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014860 device_set_wakeup_enable(&tp->pdev->dev, true);
14861 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014862 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014863 }
14864
Linus Torvalds1da177e2005-04-16 15:20:36 -070014865 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14866 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14867 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014868 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014869 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014870
14871 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14872 tp->nic_sram_data_cfg = nic_cfg;
14873
14874 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14875 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014876 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14877 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14878 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014879 (ver > 0) && (ver < 0x100))
14880 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14881
Joe Perches41535772013-02-16 11:20:04 +000014882 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014883 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14884
Linus Torvalds1da177e2005-04-16 15:20:36 -070014885 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14886 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14887 eeprom_phy_serdes = 1;
14888
14889 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14890 if (nic_phy_id != 0) {
14891 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14892 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14893
14894 eeprom_phy_id = (id1 >> 16) << 10;
14895 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14896 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14897 } else
14898 eeprom_phy_id = 0;
14899
Michael Chan7d0c41e2005-04-21 17:06:20 -070014900 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014901 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014902 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014903 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014904 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014905 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014906 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014907
Joe Perches63c3a662011-04-26 08:12:10 +000014908 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014909 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14910 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014911 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014912 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14913
14914 switch (led_cfg) {
14915 default:
14916 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14917 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14918 break;
14919
14920 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14921 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14922 break;
14923
14924 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14925 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014926
14927 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14928 * read on some older 5700/5701 bootcode.
14929 */
Joe Perches41535772013-02-16 11:20:04 +000014930 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14931 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014932 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14933
Linus Torvalds1da177e2005-04-16 15:20:36 -070014934 break;
14935
14936 case SHASTA_EXT_LED_SHARED:
14937 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014938 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14939 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014940 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14941 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070014942
14943 if (tg3_flag(tp, 5717_PLUS) ||
14944 tg3_asic_rev(tp) == ASIC_REV_5762)
14945 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
14946 LED_CTRL_BLINK_RATE_MASK;
14947
Linus Torvalds1da177e2005-04-16 15:20:36 -070014948 break;
14949
14950 case SHASTA_EXT_LED_MAC:
14951 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14952 break;
14953
14954 case SHASTA_EXT_LED_COMBO:
14955 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014956 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014957 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14958 LED_CTRL_MODE_PHY_2);
14959 break;
14960
Stephen Hemminger855e1112008-04-16 16:37:28 -070014961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014962
Joe Perches41535772013-02-16 11:20:04 +000014963 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14964 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014965 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14966 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14967
Joe Perches41535772013-02-16 11:20:04 +000014968 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014969 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014970
Michael Chan9d26e212006-12-07 00:21:14 -080014971 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014972 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014973 if ((tp->pdev->subsystem_vendor ==
14974 PCI_VENDOR_ID_ARIMA) &&
14975 (tp->pdev->subsystem_device == 0x205a ||
14976 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014977 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014978 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014979 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14980 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014982
14983 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014984 tg3_flag_set(tp, ENABLE_ASF);
14985 if (tg3_flag(tp, 5750_PLUS))
14986 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014987 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014988
14989 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014990 tg3_flag(tp, 5750_PLUS))
14991 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014992
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014993 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014994 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014995 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014996
Joe Perches63c3a662011-04-26 08:12:10 +000014997 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014998 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014999 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015000 device_set_wakeup_enable(&tp->pdev->dev, true);
15001 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015002
Linus Torvalds1da177e2005-04-16 15:20:36 -070015003 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015004 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015005
15006 /* serdes signal pre-emphasis in register 0x590 set by */
15007 /* bootcode if bit 18 is set */
15008 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015009 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015010
Joe Perches63c3a662011-04-26 08:12:10 +000015011 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015012 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15013 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015014 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015015 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015016
Nithin Sujir942d1af2013-04-09 08:48:07 +000015017 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015018 u32 cfg3;
15019
15020 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015021 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15022 !tg3_flag(tp, 57765_PLUS) &&
15023 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015024 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015025 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15026 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15027 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15028 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015029 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015030
Matt Carlson14417062010-02-17 15:16:59 +000015031 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015032 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015033 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015034 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015035 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015036 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015037 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015038done:
Joe Perches63c3a662011-04-26 08:12:10 +000015039 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015040 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015041 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015042 else
15043 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015044}
15045
Michael Chanc86a8562013-01-06 12:51:08 +000015046static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15047{
15048 int i, err;
15049 u32 val2, off = offset * 8;
15050
15051 err = tg3_nvram_lock(tp);
15052 if (err)
15053 return err;
15054
15055 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15056 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15057 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15058 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15059 udelay(10);
15060
15061 for (i = 0; i < 100; i++) {
15062 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15063 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15064 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15065 break;
15066 }
15067 udelay(10);
15068 }
15069
15070 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15071
15072 tg3_nvram_unlock(tp);
15073 if (val2 & APE_OTP_STATUS_CMD_DONE)
15074 return 0;
15075
15076 return -EBUSY;
15077}
15078
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015079static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015080{
15081 int i;
15082 u32 val;
15083
15084 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15085 tw32(OTP_CTRL, cmd);
15086
15087 /* Wait for up to 1 ms for command to execute. */
15088 for (i = 0; i < 100; i++) {
15089 val = tr32(OTP_STATUS);
15090 if (val & OTP_STATUS_CMD_DONE)
15091 break;
15092 udelay(10);
15093 }
15094
15095 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15096}
15097
15098/* Read the gphy configuration from the OTP region of the chip. The gphy
15099 * configuration is a 32-bit value that straddles the alignment boundary.
15100 * We do two 32-bit reads and then shift and merge the results.
15101 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015102static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015103{
15104 u32 bhalf_otp, thalf_otp;
15105
15106 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15107
15108 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15109 return 0;
15110
15111 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15112
15113 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15114 return 0;
15115
15116 thalf_otp = tr32(OTP_READ_DATA);
15117
15118 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15119
15120 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15121 return 0;
15122
15123 bhalf_otp = tr32(OTP_READ_DATA);
15124
15125 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15126}
15127
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015128static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015129{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015130 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015131
15132 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15133 adv |= ADVERTISED_1000baseT_Half |
15134 ADVERTISED_1000baseT_Full;
15135
15136 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15137 adv |= ADVERTISED_100baseT_Half |
15138 ADVERTISED_100baseT_Full |
15139 ADVERTISED_10baseT_Half |
15140 ADVERTISED_10baseT_Full |
15141 ADVERTISED_TP;
15142 else
15143 adv |= ADVERTISED_FIBRE;
15144
15145 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015146 tp->link_config.speed = SPEED_UNKNOWN;
15147 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015148 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015149 tp->link_config.active_speed = SPEED_UNKNOWN;
15150 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015151
15152 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015153}
15154
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015155static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015156{
15157 u32 hw_phy_id_1, hw_phy_id_2;
15158 u32 hw_phy_id, hw_phy_id_masked;
15159 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015160
Matt Carlsone256f8a2011-03-09 16:58:24 +000015161 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015162 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015163 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15164
Michael Chan8151ad52012-07-29 19:15:41 +000015165 if (tg3_flag(tp, ENABLE_APE)) {
15166 switch (tp->pci_fn) {
15167 case 0:
15168 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15169 break;
15170 case 1:
15171 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15172 break;
15173 case 2:
15174 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15175 break;
15176 case 3:
15177 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15178 break;
15179 }
15180 }
15181
Nithin Sujir942d1af2013-04-09 08:48:07 +000015182 if (!tg3_flag(tp, ENABLE_ASF) &&
15183 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15184 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15185 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15186 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15187
Joe Perches63c3a662011-04-26 08:12:10 +000015188 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015189 return tg3_phy_init(tp);
15190
Linus Torvalds1da177e2005-04-16 15:20:36 -070015191 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015192 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015193 */
15194 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015195 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015196 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015197 } else {
15198 /* Now read the physical PHY_ID from the chip and verify
15199 * that it is sane. If it doesn't look good, we fall back
15200 * to either the hard-coded table based PHY_ID and failing
15201 * that the value found in the eeprom area.
15202 */
15203 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15204 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15205
15206 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15207 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15208 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15209
Matt Carlson79eb6902010-02-17 15:17:03 +000015210 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015211 }
15212
Matt Carlson79eb6902010-02-17 15:17:03 +000015213 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015214 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015215 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015216 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015217 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015218 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015219 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015220 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015221 /* Do nothing, phy ID already set up in
15222 * tg3_get_eeprom_hw_cfg().
15223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015224 } else {
15225 struct subsys_tbl_ent *p;
15226
15227 /* No eeprom signature? Try the hardcoded
15228 * subsys device table.
15229 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015230 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015231 if (p) {
15232 tp->phy_id = p->phy_id;
15233 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15234 /* For now we saw the IDs 0xbc050cd0,
15235 * 0xbc050f80 and 0xbc050c30 on devices
15236 * connected to an BCM4785 and there are
15237 * probably more. Just assume that the phy is
15238 * supported when it is connected to a SSB core
15239 * for now.
15240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015241 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015243
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015245 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015246 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015247 }
15248 }
15249
Matt Carlsona6b68da2010-12-06 08:28:52 +000015250 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015251 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15252 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015253 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015254 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15255 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15256 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15257 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015258 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015259 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15260
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015261 tp->eee.supported = SUPPORTED_100baseT_Full |
15262 SUPPORTED_1000baseT_Full;
15263 tp->eee.advertised = ADVERTISED_100baseT_Full |
15264 ADVERTISED_1000baseT_Full;
15265 tp->eee.eee_enabled = 1;
15266 tp->eee.tx_lpi_enabled = 1;
15267 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15268 }
15269
Matt Carlsone256f8a2011-03-09 16:58:24 +000015270 tg3_phy_init_link_config(tp);
15271
Nithin Sujir942d1af2013-04-09 08:48:07 +000015272 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15273 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015274 !tg3_flag(tp, ENABLE_APE) &&
15275 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015276 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015277
15278 tg3_readphy(tp, MII_BMSR, &bmsr);
15279 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15280 (bmsr & BMSR_LSTATUS))
15281 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015282
Linus Torvalds1da177e2005-04-16 15:20:36 -070015283 err = tg3_phy_reset(tp);
15284 if (err)
15285 return err;
15286
Matt Carlson42b64a42011-05-19 12:12:49 +000015287 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015289 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015290 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15291 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015292
15293 tg3_writephy(tp, MII_BMCR,
15294 BMCR_ANENABLE | BMCR_ANRESTART);
15295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015296 }
15297
15298skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015299 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015300 err = tg3_init_5401phy_dsp(tp);
15301 if (err)
15302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015303
Linus Torvalds1da177e2005-04-16 15:20:36 -070015304 err = tg3_init_5401phy_dsp(tp);
15305 }
15306
Linus Torvalds1da177e2005-04-16 15:20:36 -070015307 return err;
15308}
15309
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015310static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015311{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015312 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015313 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015314 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015315 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015316
Matt Carlson535a4902011-07-20 10:20:56 +000015317 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015318 if (!vpd_data)
15319 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015320
Matt Carlson535a4902011-07-20 10:20:56 +000015321 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015322 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015323 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015324
15325 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15326 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15327 i += PCI_VPD_LRDT_TAG_SIZE;
15328
Matt Carlson535a4902011-07-20 10:20:56 +000015329 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015330 goto out_not_found;
15331
Matt Carlson184b8902010-04-05 10:19:25 +000015332 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15333 PCI_VPD_RO_KEYWORD_MFR_ID);
15334 if (j > 0) {
15335 len = pci_vpd_info_field_size(&vpd_data[j]);
15336
15337 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15338 if (j + len > block_end || len != 4 ||
15339 memcmp(&vpd_data[j], "1028", 4))
15340 goto partno;
15341
15342 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15343 PCI_VPD_RO_KEYWORD_VENDOR0);
15344 if (j < 0)
15345 goto partno;
15346
15347 len = pci_vpd_info_field_size(&vpd_data[j]);
15348
15349 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15350 if (j + len > block_end)
15351 goto partno;
15352
Kees Cook715230a2013-03-27 06:40:50 +000015353 if (len >= sizeof(tp->fw_ver))
15354 len = sizeof(tp->fw_ver) - 1;
15355 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15356 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15357 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015358 }
15359
15360partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015361 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15362 PCI_VPD_RO_KEYWORD_PARTNO);
15363 if (i < 0)
15364 goto out_not_found;
15365
15366 len = pci_vpd_info_field_size(&vpd_data[i]);
15367
15368 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15369 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015370 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015371 goto out_not_found;
15372
15373 memcpy(tp->board_part_number, &vpd_data[i], len);
15374
Linus Torvalds1da177e2005-04-16 15:20:36 -070015375out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015376 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015377 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015378 return;
15379
15380out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015381 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015382 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15383 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015384 strcpy(tp->board_part_number, "BCM5717");
15385 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15386 strcpy(tp->board_part_number, "BCM5718");
15387 else
15388 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015389 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015390 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15391 strcpy(tp->board_part_number, "BCM57780");
15392 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15393 strcpy(tp->board_part_number, "BCM57760");
15394 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15395 strcpy(tp->board_part_number, "BCM57790");
15396 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15397 strcpy(tp->board_part_number, "BCM57788");
15398 else
15399 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015400 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015401 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15402 strcpy(tp->board_part_number, "BCM57761");
15403 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15404 strcpy(tp->board_part_number, "BCM57765");
15405 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15406 strcpy(tp->board_part_number, "BCM57781");
15407 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15408 strcpy(tp->board_part_number, "BCM57785");
15409 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15410 strcpy(tp->board_part_number, "BCM57791");
15411 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15412 strcpy(tp->board_part_number, "BCM57795");
15413 else
15414 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015415 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015416 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15417 strcpy(tp->board_part_number, "BCM57762");
15418 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15419 strcpy(tp->board_part_number, "BCM57766");
15420 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15421 strcpy(tp->board_part_number, "BCM57782");
15422 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15423 strcpy(tp->board_part_number, "BCM57786");
15424 else
15425 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015426 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015427 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015428 } else {
15429nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015430 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015432}
15433
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015434static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015435{
15436 u32 val;
15437
Matt Carlsone4f34112009-02-25 14:25:00 +000015438 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015439 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015440 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015441 val != 0)
15442 return 0;
15443
15444 return 1;
15445}
15446
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015447static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015448{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015449 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015450 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015451 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015452
15453 if (tg3_nvram_read(tp, 0xc, &offset) ||
15454 tg3_nvram_read(tp, 0x4, &start))
15455 return;
15456
15457 offset = tg3_nvram_logical_addr(tp, offset);
15458
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015459 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015460 return;
15461
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015462 if ((val & 0xfc000000) == 0x0c000000) {
15463 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015464 return;
15465
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015466 if (val == 0)
15467 newver = true;
15468 }
15469
Matt Carlson75f99362010-04-05 10:19:24 +000015470 dst_off = strlen(tp->fw_ver);
15471
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015472 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015473 if (TG3_VER_SIZE - dst_off < 16 ||
15474 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015475 return;
15476
15477 offset = offset + ver_offset - start;
15478 for (i = 0; i < 16; i += 4) {
15479 __be32 v;
15480 if (tg3_nvram_read_be32(tp, offset + i, &v))
15481 return;
15482
Matt Carlson75f99362010-04-05 10:19:24 +000015483 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015484 }
15485 } else {
15486 u32 major, minor;
15487
15488 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15489 return;
15490
15491 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15492 TG3_NVM_BCVER_MAJSFT;
15493 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015494 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15495 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015496 }
15497}
15498
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015499static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015500{
15501 u32 val, major, minor;
15502
15503 /* Use native endian representation */
15504 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15505 return;
15506
15507 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15508 TG3_NVM_HWSB_CFG1_MAJSFT;
15509 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15510 TG3_NVM_HWSB_CFG1_MINSFT;
15511
15512 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15513}
15514
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015515static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015516{
15517 u32 offset, major, minor, build;
15518
Matt Carlson75f99362010-04-05 10:19:24 +000015519 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015520
15521 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15522 return;
15523
15524 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15525 case TG3_EEPROM_SB_REVISION_0:
15526 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15527 break;
15528 case TG3_EEPROM_SB_REVISION_2:
15529 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15530 break;
15531 case TG3_EEPROM_SB_REVISION_3:
15532 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15533 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015534 case TG3_EEPROM_SB_REVISION_4:
15535 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15536 break;
15537 case TG3_EEPROM_SB_REVISION_5:
15538 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15539 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015540 case TG3_EEPROM_SB_REVISION_6:
15541 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15542 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015543 default:
15544 return;
15545 }
15546
Matt Carlsone4f34112009-02-25 14:25:00 +000015547 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015548 return;
15549
15550 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15551 TG3_EEPROM_SB_EDH_BLD_SHFT;
15552 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15553 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15554 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15555
15556 if (minor > 99 || build > 26)
15557 return;
15558
Matt Carlson75f99362010-04-05 10:19:24 +000015559 offset = strlen(tp->fw_ver);
15560 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15561 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015562
15563 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015564 offset = strlen(tp->fw_ver);
15565 if (offset < TG3_VER_SIZE - 1)
15566 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015567 }
15568}
15569
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015570static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015571{
15572 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015573 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015574
15575 for (offset = TG3_NVM_DIR_START;
15576 offset < TG3_NVM_DIR_END;
15577 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015578 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015579 return;
15580
15581 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15582 break;
15583 }
15584
15585 if (offset == TG3_NVM_DIR_END)
15586 return;
15587
Joe Perches63c3a662011-04-26 08:12:10 +000015588 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015589 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015590 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015591 return;
15592
Matt Carlsone4f34112009-02-25 14:25:00 +000015593 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015594 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015595 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015596 return;
15597
15598 offset += val - start;
15599
Matt Carlsonacd9c112009-02-25 14:26:33 +000015600 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015601
Matt Carlsonacd9c112009-02-25 14:26:33 +000015602 tp->fw_ver[vlen++] = ',';
15603 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015604
15605 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015606 __be32 v;
15607 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015608 return;
15609
Al Virob9fc7dc2007-12-17 22:59:57 -080015610 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015611
Matt Carlsonacd9c112009-02-25 14:26:33 +000015612 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15613 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015614 break;
15615 }
15616
Matt Carlsonacd9c112009-02-25 14:26:33 +000015617 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15618 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015619 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015620}
15621
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015622static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015623{
Matt Carlson7fd76442009-02-25 14:27:20 +000015624 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015625
15626 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15627 if (apedata != APE_SEG_SIG_MAGIC)
15628 return;
15629
15630 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15631 if (!(apedata & APE_FW_STATUS_READY))
15632 return;
15633
Michael Chan165f4d12012-07-16 16:23:59 +000015634 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15635 tg3_flag_set(tp, APE_HAS_NCSI);
15636}
15637
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015638static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015639{
15640 int vlen;
15641 u32 apedata;
15642 char *fwtype;
15643
Matt Carlson7fd76442009-02-25 14:27:20 +000015644 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15645
Michael Chan165f4d12012-07-16 16:23:59 +000015646 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015647 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015648 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15649 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015650 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015651 fwtype = "DASH";
15652
Matt Carlson7fd76442009-02-25 14:27:20 +000015653 vlen = strlen(tp->fw_ver);
15654
Matt Carlsonecc79642010-08-02 11:26:01 +000015655 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15656 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015657 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15658 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15659 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15660 (apedata & APE_FW_VERSION_BLDMSK));
15661}
15662
Michael Chanc86a8562013-01-06 12:51:08 +000015663static void tg3_read_otp_ver(struct tg3 *tp)
15664{
15665 u32 val, val2;
15666
Joe Perches41535772013-02-16 11:20:04 +000015667 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015668 return;
15669
15670 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15671 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15672 TG3_OTP_MAGIC0_VALID(val)) {
15673 u64 val64 = (u64) val << 32 | val2;
15674 u32 ver = 0;
15675 int i, vlen;
15676
15677 for (i = 0; i < 7; i++) {
15678 if ((val64 & 0xff) == 0)
15679 break;
15680 ver = val64 & 0xff;
15681 val64 >>= 8;
15682 }
15683 vlen = strlen(tp->fw_ver);
15684 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15685 }
15686}
15687
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015688static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015689{
15690 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015691 bool vpd_vers = false;
15692
15693 if (tp->fw_ver[0] != 0)
15694 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015695
Joe Perches63c3a662011-04-26 08:12:10 +000015696 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015697 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015698 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015699 return;
15700 }
15701
Matt Carlsonacd9c112009-02-25 14:26:33 +000015702 if (tg3_nvram_read(tp, 0, &val))
15703 return;
15704
15705 if (val == TG3_EEPROM_MAGIC)
15706 tg3_read_bc_ver(tp);
15707 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15708 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015709 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15710 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015711
Michael Chan165f4d12012-07-16 16:23:59 +000015712 if (tg3_flag(tp, ENABLE_ASF)) {
15713 if (tg3_flag(tp, ENABLE_APE)) {
15714 tg3_probe_ncsi(tp);
15715 if (!vpd_vers)
15716 tg3_read_dash_ver(tp);
15717 } else if (!vpd_vers) {
15718 tg3_read_mgmtfw_ver(tp);
15719 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015720 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015721
15722 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015723}
15724
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015725static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15726{
Joe Perches63c3a662011-04-26 08:12:10 +000015727 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015728 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015729 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015730 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015731 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015732 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015733}
15734
Matt Carlson41434702011-03-09 16:58:22 +000015735static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015736 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15737 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15738 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15739 { },
15740};
15741
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015742static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015743{
15744 struct pci_dev *peer;
15745 unsigned int func, devnr = tp->pdev->devfn & ~7;
15746
15747 for (func = 0; func < 8; func++) {
15748 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15749 if (peer && peer != tp->pdev)
15750 break;
15751 pci_dev_put(peer);
15752 }
15753 /* 5704 can be configured in single-port mode, set peer to
15754 * tp->pdev in that case.
15755 */
15756 if (!peer) {
15757 peer = tp->pdev;
15758 return peer;
15759 }
15760
15761 /*
15762 * We don't need to keep the refcount elevated; there's no way
15763 * to remove one half of this device without removing the other
15764 */
15765 pci_dev_put(peer);
15766
15767 return peer;
15768}
15769
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015770static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015771{
15772 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015773 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015774 u32 reg;
15775
15776 /* All devices that use the alternate
15777 * ASIC REV location have a CPMU.
15778 */
15779 tg3_flag_set(tp, CPMU_PRESENT);
15780
15781 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015782 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015783 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15784 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015785 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070015786 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
15787 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015788 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15789 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070015790 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
15791 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000015792 reg = TG3PCI_GEN2_PRODID_ASICREV;
15793 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15794 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15795 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15796 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15797 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15798 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15799 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15800 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15801 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15802 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15803 reg = TG3PCI_GEN15_PRODID_ASICREV;
15804 else
15805 reg = TG3PCI_PRODID_ASICREV;
15806
15807 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15808 }
15809
15810 /* Wrong chip ID in 5752 A0. This code can be removed later
15811 * as A0 is not in production.
15812 */
Joe Perches41535772013-02-16 11:20:04 +000015813 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015814 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15815
Joe Perches41535772013-02-16 11:20:04 +000015816 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015817 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15818
Joe Perches41535772013-02-16 11:20:04 +000015819 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15820 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15821 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015822 tg3_flag_set(tp, 5717_PLUS);
15823
Joe Perches41535772013-02-16 11:20:04 +000015824 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15825 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015826 tg3_flag_set(tp, 57765_CLASS);
15827
Michael Chanc65a17f2013-01-06 12:51:07 +000015828 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015829 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015830 tg3_flag_set(tp, 57765_PLUS);
15831
15832 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015833 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15834 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15835 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15836 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15837 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15838 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015839 tg3_flag(tp, 57765_PLUS))
15840 tg3_flag_set(tp, 5755_PLUS);
15841
Joe Perches41535772013-02-16 11:20:04 +000015842 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15843 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015844 tg3_flag_set(tp, 5780_CLASS);
15845
Joe Perches41535772013-02-16 11:20:04 +000015846 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15847 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15848 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015849 tg3_flag(tp, 5755_PLUS) ||
15850 tg3_flag(tp, 5780_CLASS))
15851 tg3_flag_set(tp, 5750_PLUS);
15852
Joe Perches41535772013-02-16 11:20:04 +000015853 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015854 tg3_flag(tp, 5750_PLUS))
15855 tg3_flag_set(tp, 5705_PLUS);
15856}
15857
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015858static bool tg3_10_100_only_device(struct tg3 *tp,
15859 const struct pci_device_id *ent)
15860{
15861 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15862
Joe Perches41535772013-02-16 11:20:04 +000015863 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15864 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015865 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15866 return true;
15867
15868 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015869 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015870 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15871 return true;
15872 } else {
15873 return true;
15874 }
15875 }
15876
15877 return false;
15878}
15879
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015880static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015881{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015882 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015883 u32 pci_state_reg, grc_misc_cfg;
15884 u32 val;
15885 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015886 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015887
Linus Torvalds1da177e2005-04-16 15:20:36 -070015888 /* Force memory write invalidate off. If we leave it on,
15889 * then on 5700_BX chips we have to enable a workaround.
15890 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15891 * to match the cacheline size. The Broadcom driver have this
15892 * workaround but turns MWI off all the times so never uses
15893 * it. This seems to suggest that the workaround is insufficient.
15894 */
15895 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15896 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15897 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15898
Matt Carlson16821282011-07-13 09:27:28 +000015899 /* Important! -- Make sure register accesses are byteswapped
15900 * correctly. Also, for those chips that require it, make
15901 * sure that indirect register accesses are enabled before
15902 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015903 */
15904 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15905 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015906 tp->misc_host_ctrl |= (misc_ctrl_reg &
15907 MISC_HOST_CTRL_CHIPREV);
15908 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15909 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015910
Matt Carlson42b123b2012-02-13 15:20:13 +000015911 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015912
Michael Chan68929142005-08-09 20:17:14 -070015913 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15914 * we need to disable memory and use config. cycles
15915 * only to access all registers. The 5702/03 chips
15916 * can mistakenly decode the special cycles from the
15917 * ICH chipsets as memory write cycles, causing corruption
15918 * of register and memory space. Only certain ICH bridges
15919 * will drive special cycles with non-zero data during the
15920 * address phase which can fall within the 5703's address
15921 * range. This is not an ICH bug as the PCI spec allows
15922 * non-zero address during special cycles. However, only
15923 * these ICH bridges are known to drive non-zero addresses
15924 * during special cycles.
15925 *
15926 * Since special cycles do not cross PCI bridges, we only
15927 * enable this workaround if the 5703 is on the secondary
15928 * bus of these ICH bridges.
15929 */
Joe Perches41535772013-02-16 11:20:04 +000015930 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15931 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015932 static struct tg3_dev_id {
15933 u32 vendor;
15934 u32 device;
15935 u32 rev;
15936 } ich_chipsets[] = {
15937 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15938 PCI_ANY_ID },
15939 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15940 PCI_ANY_ID },
15941 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15942 0xa },
15943 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15944 PCI_ANY_ID },
15945 { },
15946 };
15947 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15948 struct pci_dev *bridge = NULL;
15949
15950 while (pci_id->vendor != 0) {
15951 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15952 bridge);
15953 if (!bridge) {
15954 pci_id++;
15955 continue;
15956 }
15957 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015958 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015959 continue;
15960 }
15961 if (bridge->subordinate &&
15962 (bridge->subordinate->number ==
15963 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015964 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015965 pci_dev_put(bridge);
15966 break;
15967 }
15968 }
15969 }
15970
Joe Perches41535772013-02-16 11:20:04 +000015971 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015972 static struct tg3_dev_id {
15973 u32 vendor;
15974 u32 device;
15975 } bridge_chipsets[] = {
15976 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15977 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15978 { },
15979 };
15980 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15981 struct pci_dev *bridge = NULL;
15982
15983 while (pci_id->vendor != 0) {
15984 bridge = pci_get_device(pci_id->vendor,
15985 pci_id->device,
15986 bridge);
15987 if (!bridge) {
15988 pci_id++;
15989 continue;
15990 }
15991 if (bridge->subordinate &&
15992 (bridge->subordinate->number <=
15993 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015994 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015995 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015996 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015997 pci_dev_put(bridge);
15998 break;
15999 }
16000 }
16001 }
16002
Michael Chan4a29cc22006-03-19 13:21:12 -080016003 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16004 * DMA addresses > 40-bit. This bridge may have other additional
16005 * 57xx devices behind it in some 4-port NIC designs for example.
16006 * Any tg3 device found behind the bridge will also need the 40-bit
16007 * DMA workaround.
16008 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016009 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016010 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016011 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016012 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016013 struct pci_dev *bridge = NULL;
16014
16015 do {
16016 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16017 PCI_DEVICE_ID_SERVERWORKS_EPB,
16018 bridge);
16019 if (bridge && bridge->subordinate &&
16020 (bridge->subordinate->number <=
16021 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016022 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016023 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016024 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016025 pci_dev_put(bridge);
16026 break;
16027 }
16028 } while (bridge);
16029 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016030
Joe Perches41535772013-02-16 11:20:04 +000016031 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16032 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016033 tp->pdev_peer = tg3_find_peer(tp);
16034
Matt Carlson507399f2009-11-13 13:03:37 +000016035 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016036 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016037 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016038 else if (tg3_flag(tp, 57765_PLUS))
16039 tg3_flag_set(tp, HW_TSO_3);
16040 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016041 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016042 tg3_flag_set(tp, HW_TSO_2);
16043 else if (tg3_flag(tp, 5750_PLUS)) {
16044 tg3_flag_set(tp, HW_TSO_1);
16045 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016046 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16047 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016048 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016049 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16050 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16051 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016052 tg3_flag_set(tp, FW_TSO);
16053 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016054 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016055 tp->fw_needed = FIRMWARE_TG3TSO5;
16056 else
16057 tp->fw_needed = FIRMWARE_TG3TSO;
16058 }
16059
Matt Carlsondabc5c62011-05-19 12:12:52 +000016060 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016061 if (tg3_flag(tp, HW_TSO_1) ||
16062 tg3_flag(tp, HW_TSO_2) ||
16063 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016064 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016065 /* For firmware TSO, assume ASF is disabled.
16066 * We'll disable TSO later if we discover ASF
16067 * is enabled in tg3_get_eeprom_hw_cfg().
16068 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016069 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016070 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016071 tg3_flag_clear(tp, TSO_CAPABLE);
16072 tg3_flag_clear(tp, TSO_BUG);
16073 tp->fw_needed = NULL;
16074 }
16075
Joe Perches41535772013-02-16 11:20:04 +000016076 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016077 tp->fw_needed = FIRMWARE_TG3;
16078
Nithin Sujirc4dab502013-03-06 17:02:34 +000016079 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16080 tp->fw_needed = FIRMWARE_TG357766;
16081
Matt Carlson507399f2009-11-13 13:03:37 +000016082 tp->irq_max = 1;
16083
Joe Perches63c3a662011-04-26 08:12:10 +000016084 if (tg3_flag(tp, 5750_PLUS)) {
16085 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016086 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16087 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16088 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16089 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016090 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016091 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016092
Joe Perches63c3a662011-04-26 08:12:10 +000016093 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016094 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016095 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016097
Joe Perches63c3a662011-04-26 08:12:10 +000016098 if (tg3_flag(tp, 57765_PLUS)) {
16099 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016100 tp->irq_max = TG3_IRQ_MAX_VECS;
16101 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016102 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016103
Michael Chan91024262012-09-28 07:12:38 +000016104 tp->txq_max = 1;
16105 tp->rxq_max = 1;
16106 if (tp->irq_max > 1) {
16107 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16108 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16109
Joe Perches41535772013-02-16 11:20:04 +000016110 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16111 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016112 tp->txq_max = tp->irq_max - 1;
16113 }
16114
Matt Carlsonb7abee62012-06-07 12:56:54 +000016115 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016116 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016117 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016118
Joe Perches41535772013-02-16 11:20:04 +000016119 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016120 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016121
Joe Perches41535772013-02-16 11:20:04 +000016122 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16123 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16124 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16125 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016126 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016127
Joe Perches63c3a662011-04-26 08:12:10 +000016128 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016129 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016130 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016131
Joe Perches63c3a662011-04-26 08:12:10 +000016132 if (!tg3_flag(tp, 5705_PLUS) ||
16133 tg3_flag(tp, 5780_CLASS) ||
16134 tg3_flag(tp, USE_JUMBO_BDFLAG))
16135 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016136
Matt Carlson52f44902008-11-21 17:17:04 -080016137 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16138 &pci_state_reg);
16139
Jon Mason708ebb3a2011-06-27 12:56:50 +000016140 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016141 u16 lnkctl;
16142
Joe Perches63c3a662011-04-26 08:12:10 +000016143 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016144
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016145 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016146 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016147 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016148 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016149 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016150 }
Joe Perches41535772013-02-16 11:20:04 +000016151 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16152 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16153 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16154 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016155 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016156 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016157 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016158 }
Joe Perches41535772013-02-16 11:20:04 +000016159 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016160 /* BCM5785 devices are effectively PCIe devices, and should
16161 * follow PCIe codepaths, but do not have a PCIe capabilities
16162 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016163 */
Joe Perches63c3a662011-04-26 08:12:10 +000016164 tg3_flag_set(tp, PCI_EXPRESS);
16165 } else if (!tg3_flag(tp, 5705_PLUS) ||
16166 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016167 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16168 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016169 dev_err(&tp->pdev->dev,
16170 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016171 return -EIO;
16172 }
16173
16174 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016175 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016177
Michael Chan399de502005-10-03 14:02:39 -070016178 /* If we have an AMD 762 or VIA K8T800 chipset, write
16179 * reordering to the mailbox registers done by the host
16180 * controller can cause major troubles. We read back from
16181 * every mailbox register write to force the writes to be
16182 * posted to the chip in order.
16183 */
Matt Carlson41434702011-03-09 16:58:22 +000016184 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016185 !tg3_flag(tp, PCI_EXPRESS))
16186 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016187
Matt Carlson69fc4052008-12-21 20:19:57 -080016188 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16189 &tp->pci_cacheline_sz);
16190 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16191 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016192 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016193 tp->pci_lat_timer < 64) {
16194 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016195 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16196 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016197 }
16198
Matt Carlson16821282011-07-13 09:27:28 +000016199 /* Important! -- It is critical that the PCI-X hw workaround
16200 * situation is decided before the first MMIO register access.
16201 */
Joe Perches41535772013-02-16 11:20:04 +000016202 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016203 /* 5700 BX chips need to have their TX producer index
16204 * mailboxes written twice to workaround a bug.
16205 */
Joe Perches63c3a662011-04-26 08:12:10 +000016206 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016207
Matt Carlson52f44902008-11-21 17:17:04 -080016208 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016209 *
16210 * The workaround is to use indirect register accesses
16211 * for all chip writes not to mailbox registers.
16212 */
Joe Perches63c3a662011-04-26 08:12:10 +000016213 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016214 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016215
Joe Perches63c3a662011-04-26 08:12:10 +000016216 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016217
16218 /* The chip can have it's power management PCI config
16219 * space registers clobbered due to this bug.
16220 * So explicitly force the chip into D0 here.
16221 */
Matt Carlson9974a352007-10-07 23:27:28 -070016222 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016223 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016224 &pm_reg);
16225 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16226 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016227 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016228 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016229 pm_reg);
16230
16231 /* Also, force SERR#/PERR# in PCI command. */
16232 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16233 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16234 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16235 }
16236 }
16237
Linus Torvalds1da177e2005-04-16 15:20:36 -070016238 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016239 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016240 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016241 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016242
16243 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016244 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016245 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16246 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16247 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16248 }
16249
Michael Chan1ee582d2005-08-09 20:16:46 -070016250 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016251 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016252 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016253 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016254 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016255 tp->write32_tx_mbox = tg3_write32;
16256 tp->write32_rx_mbox = tg3_write32;
16257
16258 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016259 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016260 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016261 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016262 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016263 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016264 /*
16265 * Back to back register writes can cause problems on these
16266 * chips, the workaround is to read back all reg writes
16267 * except those to mailbox regs.
16268 *
16269 * See tg3_write_indirect_reg32().
16270 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016271 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016272 }
16273
Joe Perches63c3a662011-04-26 08:12:10 +000016274 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016275 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016276 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016277 tp->write32_rx_mbox = tg3_write_flush_reg32;
16278 }
Michael Chan20094932005-08-09 20:16:32 -070016279
Joe Perches63c3a662011-04-26 08:12:10 +000016280 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016281 tp->read32 = tg3_read_indirect_reg32;
16282 tp->write32 = tg3_write_indirect_reg32;
16283 tp->read32_mbox = tg3_read_indirect_mbox;
16284 tp->write32_mbox = tg3_write_indirect_mbox;
16285 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16286 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16287
16288 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016289 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016290
16291 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16292 pci_cmd &= ~PCI_COMMAND_MEMORY;
16293 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16294 }
Joe Perches41535772013-02-16 11:20:04 +000016295 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016296 tp->read32_mbox = tg3_read32_mbox_5906;
16297 tp->write32_mbox = tg3_write32_mbox_5906;
16298 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16299 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16300 }
Michael Chan68929142005-08-09 20:17:14 -070016301
Michael Chanbbadf502006-04-06 21:46:34 -070016302 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016303 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016304 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16305 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016306 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016307
Matt Carlson16821282011-07-13 09:27:28 +000016308 /* The memory arbiter has to be enabled in order for SRAM accesses
16309 * to succeed. Normally on powerup the tg3 chip firmware will make
16310 * sure it is enabled, but other entities such as system netboot
16311 * code might disable it.
16312 */
16313 val = tr32(MEMARB_MODE);
16314 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16315
Matt Carlson9dc5e342011-11-04 09:15:02 +000016316 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016317 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016318 tg3_flag(tp, 5780_CLASS)) {
16319 if (tg3_flag(tp, PCIX_MODE)) {
16320 pci_read_config_dword(tp->pdev,
16321 tp->pcix_cap + PCI_X_STATUS,
16322 &val);
16323 tp->pci_fn = val & 0x7;
16324 }
Joe Perches41535772013-02-16 11:20:04 +000016325 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16326 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16327 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016328 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016329 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16330 val = tr32(TG3_CPMU_STATUS);
16331
Joe Perches41535772013-02-16 11:20:04 +000016332 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016333 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16334 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016335 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16336 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016337 }
16338
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016339 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16340 tp->write32_tx_mbox = tg3_write_flush_reg32;
16341 tp->write32_rx_mbox = tg3_write_flush_reg32;
16342 }
16343
Michael Chan7d0c41e2005-04-21 17:06:20 -070016344 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016345 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016346 * determined before calling tg3_set_power_state() so that
16347 * we know whether or not to switch out of Vaux power.
16348 * When the flag is set, it means that GPIO1 is used for eeprom
16349 * write protect and also implies that it is a LOM where GPIOs
16350 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016351 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016352 tg3_get_eeprom_hw_cfg(tp);
16353
Matt Carlson1caf13e2013-03-06 17:02:29 +000016354 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016355 tg3_flag_clear(tp, TSO_CAPABLE);
16356 tg3_flag_clear(tp, TSO_BUG);
16357 tp->fw_needed = NULL;
16358 }
16359
Joe Perches63c3a662011-04-26 08:12:10 +000016360 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016361 /* Allow reads and writes to the
16362 * APE register and memory space.
16363 */
16364 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016365 PCISTATE_ALLOW_APE_SHMEM_WR |
16366 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016367 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16368 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016369
16370 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016371 }
16372
Matt Carlson16821282011-07-13 09:27:28 +000016373 /* Set up tp->grc_local_ctrl before calling
16374 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16375 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016376 * It is also used as eeprom write protect on LOMs.
16377 */
16378 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016379 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016380 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016381 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16382 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016383 /* Unused GPIO3 must be driven as output on 5752 because there
16384 * are no pull-up resistors on unused GPIO pins.
16385 */
Joe Perches41535772013-02-16 11:20:04 +000016386 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016387 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016388
Joe Perches41535772013-02-16 11:20:04 +000016389 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16390 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016391 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016392 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16393
Matt Carlson8d519ab2009-04-20 06:58:01 +000016394 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16395 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016396 /* Turn off the debug UART. */
16397 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016398 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016399 /* Keep VMain power. */
16400 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16401 GRC_LCLCTRL_GPIO_OUTPUT0;
16402 }
16403
Joe Perches41535772013-02-16 11:20:04 +000016404 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016405 tp->grc_local_ctrl |=
16406 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16407
Matt Carlson16821282011-07-13 09:27:28 +000016408 /* Switch out of Vaux if it is a NIC */
16409 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016410
Linus Torvalds1da177e2005-04-16 15:20:36 -070016411 /* Derive initial jumbo mode from MTU assigned in
16412 * ether_setup() via the alloc_etherdev() call
16413 */
Joe Perches63c3a662011-04-26 08:12:10 +000016414 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16415 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016416
16417 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016418 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16419 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16420 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16421 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016422 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016423 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016424 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016425 }
16426
Joe Perches41535772013-02-16 11:20:04 +000016427 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016428 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016429
Linus Torvalds1da177e2005-04-16 15:20:36 -070016430 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016431 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16432 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16433 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16434 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016435 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16436 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16437 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016438
Joe Perches41535772013-02-16 11:20:04 +000016439 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16440 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016441 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016442 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016443 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016444
Joe Perches63c3a662011-04-26 08:12:10 +000016445 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016446 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016447 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16448 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016449 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016450 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16451 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16452 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16453 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016454 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16455 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016456 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016457 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016458 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016459 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016460 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016462
Joe Perches41535772013-02-16 11:20:04 +000016463 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16464 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016465 tp->phy_otp = tg3_read_otp_phycfg(tp);
16466 if (tp->phy_otp == 0)
16467 tp->phy_otp = TG3_OTP_DEFAULT;
16468 }
16469
Joe Perches63c3a662011-04-26 08:12:10 +000016470 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016471 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16472 else
16473 tp->mi_mode = MAC_MI_MODE_BASE;
16474
Linus Torvalds1da177e2005-04-16 15:20:36 -070016475 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016476 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16477 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016478 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16479
Matt Carlson4d958472011-04-20 07:57:35 +000016480 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016481 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16482 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16483 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016484 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16485 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16486 }
16487
Joe Perches41535772013-02-16 11:20:04 +000016488 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16489 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016490 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016491
Matt Carlson158d7ab2008-05-29 01:37:54 -070016492 err = tg3_mdio_init(tp);
16493 if (err)
16494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016495
16496 /* Initialize data/descriptor byte/word swapping. */
16497 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016498 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16499 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016500 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16501 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16502 GRC_MODE_B2HRX_ENABLE |
16503 GRC_MODE_HTX2B_ENABLE |
16504 GRC_MODE_HOST_STACKUP);
16505 else
16506 val &= GRC_MODE_HOST_STACKUP;
16507
Linus Torvalds1da177e2005-04-16 15:20:36 -070016508 tw32(GRC_MODE, val | tp->grc_mode);
16509
16510 tg3_switch_clocks(tp);
16511
16512 /* Clear this out for sanity. */
16513 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16514
16515 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16516 &pci_state_reg);
16517 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016518 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016519 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16520 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16521 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16522 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016523 void __iomem *sram_base;
16524
16525 /* Write some dummy words into the SRAM status block
16526 * area, see if it reads back correctly. If the return
16527 * value is bad, force enable the PCIX workaround.
16528 */
16529 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16530
16531 writel(0x00000000, sram_base);
16532 writel(0x00000000, sram_base + 4);
16533 writel(0xffffffff, sram_base + 4);
16534 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016535 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016536 }
16537 }
16538
16539 udelay(50);
16540 tg3_nvram_init(tp);
16541
Nithin Sujirc4dab502013-03-06 17:02:34 +000016542 /* If the device has an NVRAM, no need to load patch firmware */
16543 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16544 !tg3_flag(tp, NO_NVRAM))
16545 tp->fw_needed = NULL;
16546
Linus Torvalds1da177e2005-04-16 15:20:36 -070016547 grc_misc_cfg = tr32(GRC_MISC_CFG);
16548 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16549
Joe Perches41535772013-02-16 11:20:04 +000016550 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016551 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16552 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016553 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016554
Joe Perches63c3a662011-04-26 08:12:10 +000016555 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016556 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016557 tg3_flag_set(tp, TAGGED_STATUS);
16558 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016559 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16560 HOSTCC_MODE_CLRTICK_TXBD);
16561
16562 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16563 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16564 tp->misc_host_ctrl);
16565 }
16566
Matt Carlson3bda1252008-08-15 14:08:22 -070016567 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016568 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016569 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016570 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016571 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016572
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016573 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016574 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016575
16576 err = tg3_phy_probe(tp);
16577 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016578 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016579 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016580 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016581 }
16582
Matt Carlson184b8902010-04-05 10:19:25 +000016583 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016584 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016585
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016586 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16587 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016588 } else {
Joe Perches41535772013-02-16 11:20:04 +000016589 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016590 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016591 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016592 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016593 }
16594
16595 /* 5700 {AX,BX} chips have a broken status block link
16596 * change bit implementation, so we must use the
16597 * status register in those cases.
16598 */
Joe Perches41535772013-02-16 11:20:04 +000016599 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016600 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016601 else
Joe Perches63c3a662011-04-26 08:12:10 +000016602 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016603
16604 /* The led_ctrl is set during tg3_phy_probe, here we might
16605 * have to force the link status polling mechanism based
16606 * upon subsystem IDs.
16607 */
16608 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016609 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016610 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16611 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016612 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016613 }
16614
16615 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016616 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016617 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016618 else
Joe Perches63c3a662011-04-26 08:12:10 +000016619 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016620
Eric Dumazet9205fd92011-11-18 06:47:01 +000016621 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016622 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016623 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016624 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016625 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016626#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016627 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016628#endif
16629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016630
Matt Carlson2c49a442010-09-30 10:34:35 +000016631 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16632 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016633 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16634
Matt Carlson2c49a442010-09-30 10:34:35 +000016635 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016636
16637 /* Increment the rx prod index on the rx std ring by at most
16638 * 8 for these chips to workaround hw errata.
16639 */
Joe Perches41535772013-02-16 11:20:04 +000016640 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16641 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16642 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016643 tp->rx_std_max_post = 8;
16644
Joe Perches63c3a662011-04-26 08:12:10 +000016645 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016646 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16647 PCIE_PWR_MGMT_L1_THRESH_MSK;
16648
Linus Torvalds1da177e2005-04-16 15:20:36 -070016649 return err;
16650}
16651
David S. Miller49b6e95f2007-03-29 01:38:42 -070016652#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016653static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016654{
16655 struct net_device *dev = tp->dev;
16656 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016657 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016658 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016659 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016660
David S. Miller49b6e95f2007-03-29 01:38:42 -070016661 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016662 if (addr && len == ETH_ALEN) {
16663 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016665 }
16666 return -ENODEV;
16667}
16668
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016669static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016670{
16671 struct net_device *dev = tp->dev;
16672
Joe Perchesd458cdf2013-10-01 19:04:40 -070016673 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016674 return 0;
16675}
16676#endif
16677
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016678static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016679{
16680 struct net_device *dev = tp->dev;
16681 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016682 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016683 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016684
David S. Miller49b6e95f2007-03-29 01:38:42 -070016685#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016686 if (!tg3_get_macaddr_sparc(tp))
16687 return 0;
16688#endif
16689
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016690 if (tg3_flag(tp, IS_SSB_CORE)) {
16691 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16692 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16693 return 0;
16694 }
16695
Linus Torvalds1da177e2005-04-16 15:20:36 -070016696 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016697 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016698 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016699 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16700 mac_offset = 0xcc;
16701 if (tg3_nvram_lock(tp))
16702 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16703 else
16704 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016705 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016706 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016707 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016708 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016709 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016710 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016711 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016712
16713 /* First try to get it from MAC address mailbox. */
16714 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16715 if ((hi >> 16) == 0x484b) {
16716 dev->dev_addr[0] = (hi >> 8) & 0xff;
16717 dev->dev_addr[1] = (hi >> 0) & 0xff;
16718
16719 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16720 dev->dev_addr[2] = (lo >> 24) & 0xff;
16721 dev->dev_addr[3] = (lo >> 16) & 0xff;
16722 dev->dev_addr[4] = (lo >> 8) & 0xff;
16723 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016724
Michael Chan008652b2006-03-27 23:14:53 -080016725 /* Some old bootcode may report a 0 MAC address in SRAM */
16726 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16727 }
16728 if (!addr_ok) {
16729 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016730 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016731 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016732 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016733 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16734 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016735 }
16736 /* Finally just fetch it out of the MAC control regs. */
16737 else {
16738 hi = tr32(MAC_ADDR_0_HIGH);
16739 lo = tr32(MAC_ADDR_0_LOW);
16740
16741 dev->dev_addr[5] = lo & 0xff;
16742 dev->dev_addr[4] = (lo >> 8) & 0xff;
16743 dev->dev_addr[3] = (lo >> 16) & 0xff;
16744 dev->dev_addr[2] = (lo >> 24) & 0xff;
16745 dev->dev_addr[1] = hi & 0xff;
16746 dev->dev_addr[0] = (hi >> 8) & 0xff;
16747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016748 }
16749
16750 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016751#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016752 if (!tg3_get_default_macaddr_sparc(tp))
16753 return 0;
16754#endif
16755 return -EINVAL;
16756 }
16757 return 0;
16758}
16759
David S. Miller59e6b432005-05-18 22:50:10 -070016760#define BOUNDARY_SINGLE_CACHELINE 1
16761#define BOUNDARY_MULTI_CACHELINE 2
16762
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016763static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016764{
16765 int cacheline_size;
16766 u8 byte;
16767 int goal;
16768
16769 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16770 if (byte == 0)
16771 cacheline_size = 1024;
16772 else
16773 cacheline_size = (int) byte * 4;
16774
16775 /* On 5703 and later chips, the boundary bits have no
16776 * effect.
16777 */
Joe Perches41535772013-02-16 11:20:04 +000016778 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16779 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016780 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016781 goto out;
16782
16783#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16784 goal = BOUNDARY_MULTI_CACHELINE;
16785#else
16786#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16787 goal = BOUNDARY_SINGLE_CACHELINE;
16788#else
16789 goal = 0;
16790#endif
16791#endif
16792
Joe Perches63c3a662011-04-26 08:12:10 +000016793 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016794 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16795 goto out;
16796 }
16797
David S. Miller59e6b432005-05-18 22:50:10 -070016798 if (!goal)
16799 goto out;
16800
16801 /* PCI controllers on most RISC systems tend to disconnect
16802 * when a device tries to burst across a cache-line boundary.
16803 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16804 *
16805 * Unfortunately, for PCI-E there are only limited
16806 * write-side controls for this, and thus for reads
16807 * we will still get the disconnects. We'll also waste
16808 * these PCI cycles for both read and write for chips
16809 * other than 5700 and 5701 which do not implement the
16810 * boundary bits.
16811 */
Joe Perches63c3a662011-04-26 08:12:10 +000016812 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016813 switch (cacheline_size) {
16814 case 16:
16815 case 32:
16816 case 64:
16817 case 128:
16818 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16819 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16820 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16821 } else {
16822 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16823 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16824 }
16825 break;
16826
16827 case 256:
16828 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16829 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16830 break;
16831
16832 default:
16833 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16834 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16835 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016836 }
Joe Perches63c3a662011-04-26 08:12:10 +000016837 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016838 switch (cacheline_size) {
16839 case 16:
16840 case 32:
16841 case 64:
16842 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16843 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16844 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16845 break;
16846 }
16847 /* fallthrough */
16848 case 128:
16849 default:
16850 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16851 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16852 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016853 }
David S. Miller59e6b432005-05-18 22:50:10 -070016854 } else {
16855 switch (cacheline_size) {
16856 case 16:
16857 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16858 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16859 DMA_RWCTRL_WRITE_BNDRY_16);
16860 break;
16861 }
16862 /* fallthrough */
16863 case 32:
16864 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16865 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16866 DMA_RWCTRL_WRITE_BNDRY_32);
16867 break;
16868 }
16869 /* fallthrough */
16870 case 64:
16871 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16872 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16873 DMA_RWCTRL_WRITE_BNDRY_64);
16874 break;
16875 }
16876 /* fallthrough */
16877 case 128:
16878 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16879 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16880 DMA_RWCTRL_WRITE_BNDRY_128);
16881 break;
16882 }
16883 /* fallthrough */
16884 case 256:
16885 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16886 DMA_RWCTRL_WRITE_BNDRY_256);
16887 break;
16888 case 512:
16889 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16890 DMA_RWCTRL_WRITE_BNDRY_512);
16891 break;
16892 case 1024:
16893 default:
16894 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16895 DMA_RWCTRL_WRITE_BNDRY_1024);
16896 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016897 }
David S. Miller59e6b432005-05-18 22:50:10 -070016898 }
16899
16900out:
16901 return val;
16902}
16903
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016904static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000016905 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016906{
16907 struct tg3_internal_buffer_desc test_desc;
16908 u32 sram_dma_descs;
16909 int i, ret;
16910
16911 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16912
16913 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16914 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16915 tw32(RDMAC_STATUS, 0);
16916 tw32(WDMAC_STATUS, 0);
16917
16918 tw32(BUFMGR_MODE, 0);
16919 tw32(FTQ_RESET, 0);
16920
16921 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16922 test_desc.addr_lo = buf_dma & 0xffffffff;
16923 test_desc.nic_mbuf = 0x00002100;
16924 test_desc.len = size;
16925
16926 /*
16927 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16928 * the *second* time the tg3 driver was getting loaded after an
16929 * initial scan.
16930 *
16931 * Broadcom tells me:
16932 * ...the DMA engine is connected to the GRC block and a DMA
16933 * reset may affect the GRC block in some unpredictable way...
16934 * The behavior of resets to individual blocks has not been tested.
16935 *
16936 * Broadcom noted the GRC reset will also reset all sub-components.
16937 */
16938 if (to_device) {
16939 test_desc.cqid_sqid = (13 << 8) | 2;
16940
16941 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16942 udelay(40);
16943 } else {
16944 test_desc.cqid_sqid = (16 << 8) | 7;
16945
16946 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16947 udelay(40);
16948 }
16949 test_desc.flags = 0x00000005;
16950
16951 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16952 u32 val;
16953
16954 val = *(((u32 *)&test_desc) + i);
16955 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16956 sram_dma_descs + (i * sizeof(u32)));
16957 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16958 }
16959 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16960
Matt Carlson859a588792010-04-05 10:19:28 +000016961 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016962 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016963 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016964 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016965
16966 ret = -ENODEV;
16967 for (i = 0; i < 40; i++) {
16968 u32 val;
16969
16970 if (to_device)
16971 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16972 else
16973 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16974 if ((val & 0xffff) == sram_dma_descs) {
16975 ret = 0;
16976 break;
16977 }
16978
16979 udelay(100);
16980 }
16981
16982 return ret;
16983}
16984
David S. Millerded73402005-05-23 13:59:47 -070016985#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016986
Matt Carlson41434702011-03-09 16:58:22 +000016987static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016988 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16989 { },
16990};
16991
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016992static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016993{
16994 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016995 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016996 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016997
Matt Carlson4bae65c2010-11-24 08:31:52 +000016998 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16999 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017000 if (!buf) {
17001 ret = -ENOMEM;
17002 goto out_nofree;
17003 }
17004
17005 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17006 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17007
David S. Miller59e6b432005-05-18 22:50:10 -070017008 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017009
Joe Perches63c3a662011-04-26 08:12:10 +000017010 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017011 goto out;
17012
Joe Perches63c3a662011-04-26 08:12:10 +000017013 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017014 /* DMA read watermark not used on PCIE */
17015 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017016 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017017 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17018 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017019 tp->dma_rwctrl |= 0x003f0000;
17020 else
17021 tp->dma_rwctrl |= 0x003f000f;
17022 } else {
Joe Perches41535772013-02-16 11:20:04 +000017023 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17024 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017025 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017026 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017027
Michael Chan4a29cc22006-03-19 13:21:12 -080017028 /* If the 5704 is behind the EPB bridge, we can
17029 * do the less restrictive ONE_DMA workaround for
17030 * better performance.
17031 */
Joe Perches63c3a662011-04-26 08:12:10 +000017032 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017033 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017034 tp->dma_rwctrl |= 0x8000;
17035 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017036 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17037
Joe Perches41535772013-02-16 11:20:04 +000017038 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017039 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017040 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017041 tp->dma_rwctrl |=
17042 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17043 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17044 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017045 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017046 /* 5780 always in PCIX mode */
17047 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017048 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017049 /* 5714 always in PCIX mode */
17050 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017051 } else {
17052 tp->dma_rwctrl |= 0x001b000f;
17053 }
17054 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017055 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17056 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017057
Joe Perches41535772013-02-16 11:20:04 +000017058 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17059 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017060 tp->dma_rwctrl &= 0xfffffff0;
17061
Joe Perches41535772013-02-16 11:20:04 +000017062 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17063 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017064 /* Remove this if it causes problems for some boards. */
17065 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17066
17067 /* On 5700/5701 chips, we need to set this bit.
17068 * Otherwise the chip will issue cacheline transactions
17069 * to streamable DMA memory with not all the byte
17070 * enables turned on. This is an error on several
17071 * RISC PCI controllers, in particular sparc64.
17072 *
17073 * On 5703/5704 chips, this bit has been reassigned
17074 * a different meaning. In particular, it is used
17075 * on those chips to enable a PCI-X workaround.
17076 */
17077 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17078 }
17079
17080 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17081
Linus Torvalds1da177e2005-04-16 15:20:36 -070017082
Joe Perches41535772013-02-16 11:20:04 +000017083 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17084 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017085 goto out;
17086
David S. Miller59e6b432005-05-18 22:50:10 -070017087 /* It is best to perform DMA test with maximum write burst size
17088 * to expose the 5700/5701 write DMA bug.
17089 */
17090 saved_dma_rwctrl = tp->dma_rwctrl;
17091 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17092 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17093
Linus Torvalds1da177e2005-04-16 15:20:36 -070017094 while (1) {
17095 u32 *p = buf, i;
17096
17097 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17098 p[i] = i;
17099
17100 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017101 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017102 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017103 dev_err(&tp->pdev->dev,
17104 "%s: Buffer write failed. err = %d\n",
17105 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017106 break;
17107 }
17108
Linus Torvalds1da177e2005-04-16 15:20:36 -070017109 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017110 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017111 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017112 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17113 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017114 break;
17115 }
17116
17117 /* Verify it. */
17118 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17119 if (p[i] == i)
17120 continue;
17121
David S. Miller59e6b432005-05-18 22:50:10 -070017122 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17123 DMA_RWCTRL_WRITE_BNDRY_16) {
17124 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017125 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17126 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17127 break;
17128 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017129 dev_err(&tp->pdev->dev,
17130 "%s: Buffer corrupted on read back! "
17131 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017132 ret = -ENODEV;
17133 goto out;
17134 }
17135 }
17136
17137 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17138 /* Success. */
17139 ret = 0;
17140 break;
17141 }
17142 }
David S. Miller59e6b432005-05-18 22:50:10 -070017143 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17144 DMA_RWCTRL_WRITE_BNDRY_16) {
17145 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017146 * now look for chipsets that are known to expose the
17147 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017148 */
Matt Carlson41434702011-03-09 16:58:22 +000017149 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017150 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17151 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017152 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017153 /* Safe to use the calculated DMA boundary. */
17154 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017155 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017156
David S. Miller59e6b432005-05-18 22:50:10 -070017157 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017159
17160out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017161 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017162out_nofree:
17163 return ret;
17164}
17165
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017166static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017167{
Joe Perches63c3a662011-04-26 08:12:10 +000017168 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017169 tp->bufmgr_config.mbuf_read_dma_low_water =
17170 DEFAULT_MB_RDMA_LOW_WATER_5705;
17171 tp->bufmgr_config.mbuf_mac_rx_low_water =
17172 DEFAULT_MB_MACRX_LOW_WATER_57765;
17173 tp->bufmgr_config.mbuf_high_water =
17174 DEFAULT_MB_HIGH_WATER_57765;
17175
17176 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17177 DEFAULT_MB_RDMA_LOW_WATER_5705;
17178 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17179 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17180 tp->bufmgr_config.mbuf_high_water_jumbo =
17181 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017182 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017183 tp->bufmgr_config.mbuf_read_dma_low_water =
17184 DEFAULT_MB_RDMA_LOW_WATER_5705;
17185 tp->bufmgr_config.mbuf_mac_rx_low_water =
17186 DEFAULT_MB_MACRX_LOW_WATER_5705;
17187 tp->bufmgr_config.mbuf_high_water =
17188 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017189 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017190 tp->bufmgr_config.mbuf_mac_rx_low_water =
17191 DEFAULT_MB_MACRX_LOW_WATER_5906;
17192 tp->bufmgr_config.mbuf_high_water =
17193 DEFAULT_MB_HIGH_WATER_5906;
17194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017195
Michael Chanfdfec1722005-07-25 12:31:48 -070017196 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17197 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17198 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17199 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17200 tp->bufmgr_config.mbuf_high_water_jumbo =
17201 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17202 } else {
17203 tp->bufmgr_config.mbuf_read_dma_low_water =
17204 DEFAULT_MB_RDMA_LOW_WATER;
17205 tp->bufmgr_config.mbuf_mac_rx_low_water =
17206 DEFAULT_MB_MACRX_LOW_WATER;
17207 tp->bufmgr_config.mbuf_high_water =
17208 DEFAULT_MB_HIGH_WATER;
17209
17210 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17211 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17212 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17213 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17214 tp->bufmgr_config.mbuf_high_water_jumbo =
17215 DEFAULT_MB_HIGH_WATER_JUMBO;
17216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017217
17218 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17219 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17220}
17221
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017222static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017223{
Matt Carlson79eb6902010-02-17 15:17:03 +000017224 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17225 case TG3_PHY_ID_BCM5400: return "5400";
17226 case TG3_PHY_ID_BCM5401: return "5401";
17227 case TG3_PHY_ID_BCM5411: return "5411";
17228 case TG3_PHY_ID_BCM5701: return "5701";
17229 case TG3_PHY_ID_BCM5703: return "5703";
17230 case TG3_PHY_ID_BCM5704: return "5704";
17231 case TG3_PHY_ID_BCM5705: return "5705";
17232 case TG3_PHY_ID_BCM5750: return "5750";
17233 case TG3_PHY_ID_BCM5752: return "5752";
17234 case TG3_PHY_ID_BCM5714: return "5714";
17235 case TG3_PHY_ID_BCM5780: return "5780";
17236 case TG3_PHY_ID_BCM5755: return "5755";
17237 case TG3_PHY_ID_BCM5787: return "5787";
17238 case TG3_PHY_ID_BCM5784: return "5784";
17239 case TG3_PHY_ID_BCM5756: return "5722/5756";
17240 case TG3_PHY_ID_BCM5906: return "5906";
17241 case TG3_PHY_ID_BCM5761: return "5761";
17242 case TG3_PHY_ID_BCM5718C: return "5718C";
17243 case TG3_PHY_ID_BCM5718S: return "5718S";
17244 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017245 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017246 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017247 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017248 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017249 case 0: return "serdes";
17250 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017252}
17253
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017254static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017255{
Joe Perches63c3a662011-04-26 08:12:10 +000017256 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017257 strcpy(str, "PCI Express");
17258 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017259 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017260 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17261
17262 strcpy(str, "PCIX:");
17263
17264 if ((clock_ctrl == 7) ||
17265 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17266 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17267 strcat(str, "133MHz");
17268 else if (clock_ctrl == 0)
17269 strcat(str, "33MHz");
17270 else if (clock_ctrl == 2)
17271 strcat(str, "50MHz");
17272 else if (clock_ctrl == 4)
17273 strcat(str, "66MHz");
17274 else if (clock_ctrl == 6)
17275 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017276 } else {
17277 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017278 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017279 strcat(str, "66MHz");
17280 else
17281 strcat(str, "33MHz");
17282 }
Joe Perches63c3a662011-04-26 08:12:10 +000017283 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017284 strcat(str, ":32-bit");
17285 else
17286 strcat(str, ":64-bit");
17287 return str;
17288}
17289
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017290static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017291{
17292 struct ethtool_coalesce *ec = &tp->coal;
17293
17294 memset(ec, 0, sizeof(*ec));
17295 ec->cmd = ETHTOOL_GCOALESCE;
17296 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17297 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17298 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17299 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17300 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17301 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17302 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17303 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17304 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17305
17306 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17307 HOSTCC_MODE_CLRTICK_TXBD)) {
17308 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17309 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17310 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17311 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17312 }
Michael Chand244c892005-07-05 14:42:33 -070017313
Joe Perches63c3a662011-04-26 08:12:10 +000017314 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017315 ec->rx_coalesce_usecs_irq = 0;
17316 ec->tx_coalesce_usecs_irq = 0;
17317 ec->stats_block_coalesce_usecs = 0;
17318 }
David S. Miller15f98502005-05-18 22:49:26 -070017319}
17320
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017321static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017322 const struct pci_device_id *ent)
17323{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017324 struct net_device *dev;
17325 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017326 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017327 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017328 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017329 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017330 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017331
Joe Perches05dbe002010-02-17 19:44:19 +000017332 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017333
17334 err = pci_enable_device(pdev);
17335 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017336 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017337 return err;
17338 }
17339
Linus Torvalds1da177e2005-04-16 15:20:36 -070017340 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17341 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017342 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017343 goto err_out_disable_pdev;
17344 }
17345
17346 pci_set_master(pdev);
17347
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017348 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017349 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017350 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017351 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017352 }
17353
Linus Torvalds1da177e2005-04-16 15:20:36 -070017354 SET_NETDEV_DEV(dev, &pdev->dev);
17355
Linus Torvalds1da177e2005-04-16 15:20:36 -070017356 tp = netdev_priv(dev);
17357 tp->pdev = pdev;
17358 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017359 tp->rx_mode = TG3_DEF_RX_MODE;
17360 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017361 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017362
Linus Torvalds1da177e2005-04-16 15:20:36 -070017363 if (tg3_debug > 0)
17364 tp->msg_enable = tg3_debug;
17365 else
17366 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17367
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017368 if (pdev_is_ssb_gige_core(pdev)) {
17369 tg3_flag_set(tp, IS_SSB_CORE);
17370 if (ssb_gige_must_flush_posted_writes(pdev))
17371 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17372 if (ssb_gige_one_dma_at_once(pdev))
17373 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017374 if (ssb_gige_have_roboswitch(pdev)) {
17375 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017376 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017377 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017378 if (ssb_gige_is_rgmii(pdev))
17379 tg3_flag_set(tp, RGMII_MODE);
17380 }
17381
Linus Torvalds1da177e2005-04-16 15:20:36 -070017382 /* The word/byte swap controls here control register access byte
17383 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17384 * setting below.
17385 */
17386 tp->misc_host_ctrl =
17387 MISC_HOST_CTRL_MASK_PCI_INT |
17388 MISC_HOST_CTRL_WORD_SWAP |
17389 MISC_HOST_CTRL_INDIR_ACCESS |
17390 MISC_HOST_CTRL_PCISTATE_RW;
17391
17392 /* The NONFRM (non-frame) byte/word swap controls take effect
17393 * on descriptor entries, anything which isn't packet data.
17394 *
17395 * The StrongARM chips on the board (one for tx, one for rx)
17396 * are running in big-endian mode.
17397 */
17398 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17399 GRC_MODE_WSWAP_NONFRM_DATA);
17400#ifdef __BIG_ENDIAN
17401 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17402#endif
17403 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017404 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017405 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017406
Matt Carlsond5fe4882008-11-21 17:20:32 -080017407 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017408 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017409 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017410 err = -ENOMEM;
17411 goto err_out_free_dev;
17412 }
17413
Matt Carlsonc9cab242011-07-13 09:27:27 +000017414 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17415 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17416 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17417 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17418 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017419 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017420 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17421 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017422 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017423 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17424 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017425 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17426 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017427 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17428 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017429 tg3_flag_set(tp, ENABLE_APE);
17430 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17431 if (!tp->aperegs) {
17432 dev_err(&pdev->dev,
17433 "Cannot map APE registers, aborting\n");
17434 err = -ENOMEM;
17435 goto err_out_iounmap;
17436 }
17437 }
17438
Linus Torvalds1da177e2005-04-16 15:20:36 -070017439 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17440 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017441
Linus Torvalds1da177e2005-04-16 15:20:36 -070017442 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017443 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017444 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017445 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017446
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017447 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017448 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017449 dev_err(&pdev->dev,
17450 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017451 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017452 }
17453
Michael Chan4a29cc22006-03-19 13:21:12 -080017454 /* The EPB bridge inside 5714, 5715, and 5780 and any
17455 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017456 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17457 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17458 * do DMA address check in tg3_start_xmit().
17459 */
Joe Perches63c3a662011-04-26 08:12:10 +000017460 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017461 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017462 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017463 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017464#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017465 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017466#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017467 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017468 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017469
17470 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017471 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017472 err = pci_set_dma_mask(pdev, dma_mask);
17473 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017474 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017475 err = pci_set_consistent_dma_mask(pdev,
17476 persist_dma_mask);
17477 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017478 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17479 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017480 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017481 }
17482 }
17483 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017484 if (err || dma_mask == DMA_BIT_MASK(32)) {
17485 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017486 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017487 dev_err(&pdev->dev,
17488 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017489 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017490 }
17491 }
17492
Michael Chanfdfec1722005-07-25 12:31:48 -070017493 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017494
Patrick McHardyf6469682013-04-19 02:04:27 +000017495 features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlson0da06062011-05-19 12:12:53 +000017496
17497 /* 5700 B0 chips do not support checksumming correctly due
17498 * to hardware bugs.
17499 */
Joe Perches41535772013-02-16 11:20:04 +000017500 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017501 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17502
17503 if (tg3_flag(tp, 5755_PLUS))
17504 features |= NETIF_F_IPV6_CSUM;
17505 }
17506
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017507 /* TSO is on by default on chips that support hardware TSO.
17508 * Firmware TSO on older chips gives lower performance, so it
17509 * is off by default, but can be enabled using ethtool.
17510 */
Joe Perches63c3a662011-04-26 08:12:10 +000017511 if ((tg3_flag(tp, HW_TSO_1) ||
17512 tg3_flag(tp, HW_TSO_2) ||
17513 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017514 (features & NETIF_F_IP_CSUM))
17515 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017516 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017517 if (features & NETIF_F_IPV6_CSUM)
17518 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017519 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017520 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17521 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17522 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17523 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17524 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017525 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017527
Matt Carlsond542fe22011-05-19 16:02:43 +000017528 dev->features |= features;
17529 dev->vlan_features |= features;
17530
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017531 /*
17532 * Add loopback capability only for a subset of devices that support
17533 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17534 * loopback for the remaining devices.
17535 */
Joe Perches41535772013-02-16 11:20:04 +000017536 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017537 !tg3_flag(tp, CPMU_PRESENT))
17538 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017539 features |= NETIF_F_LOOPBACK;
17540
Matt Carlson0da06062011-05-19 12:12:53 +000017541 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017542
Joe Perches41535772013-02-16 11:20:04 +000017543 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017544 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017545 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017546 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017547 tp->rx_pending = 63;
17548 }
17549
Linus Torvalds1da177e2005-04-16 15:20:36 -070017550 err = tg3_get_device_address(tp);
17551 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017552 dev_err(&pdev->dev,
17553 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017554 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017555 }
17556
Matt Carlsonc88864d2007-11-12 21:07:01 -080017557 /*
17558 * Reset chip in case UNDI or EFI driver did not shutdown
17559 * DMA self test will enable WDMAC and we'll see (spurious)
17560 * pending DMA on the PCI bus at that point.
17561 */
17562 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17563 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17564 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17565 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17566 }
17567
17568 err = tg3_test_dma(tp);
17569 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017570 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017571 goto err_out_apeunmap;
17572 }
17573
Matt Carlson78f90dc2009-11-13 13:03:42 +000017574 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17575 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17576 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017577 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017578 struct tg3_napi *tnapi = &tp->napi[i];
17579
17580 tnapi->tp = tp;
17581 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17582
17583 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017584 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017585 intmbx += 0x8;
17586 else
17587 intmbx += 0x4;
17588
17589 tnapi->consmbox = rcvmbx;
17590 tnapi->prodmbox = sndmbx;
17591
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017592 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017593 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017594 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017595 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017596
Joe Perches63c3a662011-04-26 08:12:10 +000017597 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017598 break;
17599
17600 /*
17601 * If we support MSIX, we'll be using RSS. If we're using
17602 * RSS, the first vector only handles link interrupts and the
17603 * remaining vectors handle rx and tx interrupts. Reuse the
17604 * mailbox values for the next iteration. The values we setup
17605 * above are still useful for the single vectored mode.
17606 */
17607 if (!i)
17608 continue;
17609
17610 rcvmbx += 0x8;
17611
17612 if (sndmbx & 0x4)
17613 sndmbx -= 0x4;
17614 else
17615 sndmbx += 0xc;
17616 }
17617
Matt Carlsonc88864d2007-11-12 21:07:01 -080017618 tg3_init_coal(tp);
17619
Michael Chanc49a1562006-12-17 17:07:29 -080017620 pci_set_drvdata(pdev, dev);
17621
Joe Perches41535772013-02-16 11:20:04 +000017622 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17623 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17624 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017625 tg3_flag_set(tp, PTP_CAPABLE);
17626
Matt Carlson21f76382012-02-22 12:35:21 +000017627 tg3_timer_init(tp);
17628
Michael Chan402e1392013-02-14 12:13:41 +000017629 tg3_carrier_off(tp);
17630
Linus Torvalds1da177e2005-04-16 15:20:36 -070017631 err = register_netdev(dev);
17632 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017633 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017634 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017635 }
17636
Joe Perches05dbe002010-02-17 19:44:19 +000017637 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17638 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017639 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017640 tg3_bus_string(tp, str),
17641 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017642
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017643 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017644 struct phy_device *phydev;
Hauke Mehrtensead24022013-09-28 23:15:26 +020017645 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017646 netdev_info(dev,
17647 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017648 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017649 } else {
17650 char *ethtype;
17651
17652 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17653 ethtype = "10/100Base-TX";
17654 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17655 ethtype = "1000Base-SX";
17656 else
17657 ethtype = "10/100/1000Base-T";
17658
Matt Carlson5129c3a2010-04-05 10:19:23 +000017659 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017660 "(WireSpeed[%d], EEE[%d])\n",
17661 tg3_phy_string(tp), ethtype,
17662 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17663 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017664 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017665
Joe Perches05dbe002010-02-17 19:44:19 +000017666 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017667 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017668 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017669 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017670 tg3_flag(tp, ENABLE_ASF) != 0,
17671 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017672 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17673 tp->dma_rwctrl,
17674 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17675 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017676
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017677 pci_save_state(pdev);
17678
Linus Torvalds1da177e2005-04-16 15:20:36 -070017679 return 0;
17680
Matt Carlson0d3031d2007-10-10 18:02:43 -070017681err_out_apeunmap:
17682 if (tp->aperegs) {
17683 iounmap(tp->aperegs);
17684 tp->aperegs = NULL;
17685 }
17686
Linus Torvalds1da177e2005-04-16 15:20:36 -070017687err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017688 if (tp->regs) {
17689 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017690 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017692
17693err_out_free_dev:
17694 free_netdev(dev);
17695
17696err_out_free_res:
17697 pci_release_regions(pdev);
17698
17699err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080017700 if (pci_is_enabled(pdev))
17701 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017702 return err;
17703}
17704
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017705static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017706{
17707 struct net_device *dev = pci_get_drvdata(pdev);
17708
17709 if (dev) {
17710 struct tg3 *tp = netdev_priv(dev);
17711
Jesper Juhle3c55302012-04-09 22:50:15 +020017712 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017713
Matt Carlsondb219972011-11-04 09:15:03 +000017714 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017715
David S. Miller1805b2f2011-10-24 18:18:09 -040017716 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017717 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017718 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017719 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017720
Linus Torvalds1da177e2005-04-16 15:20:36 -070017721 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017722 if (tp->aperegs) {
17723 iounmap(tp->aperegs);
17724 tp->aperegs = NULL;
17725 }
Michael Chan68929142005-08-09 20:17:14 -070017726 if (tp->regs) {
17727 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017728 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017730 free_netdev(dev);
17731 pci_release_regions(pdev);
17732 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017733 }
17734}
17735
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017736#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017737static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017738{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017739 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017740 struct net_device *dev = pci_get_drvdata(pdev);
17741 struct tg3 *tp = netdev_priv(dev);
17742 int err;
17743
17744 if (!netif_running(dev))
17745 return 0;
17746
Matt Carlsondb219972011-11-04 09:15:03 +000017747 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017748 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017749 tg3_netif_stop(tp);
17750
Matt Carlson21f76382012-02-22 12:35:21 +000017751 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017752
David S. Millerf47c11e2005-06-24 20:18:35 -070017753 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017754 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017755 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017756
17757 netif_device_detach(dev);
17758
David S. Millerf47c11e2005-06-24 20:18:35 -070017759 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017760 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017761 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017762 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017763
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017764 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017765 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017766 int err2;
17767
David S. Millerf47c11e2005-06-24 20:18:35 -070017768 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017769
Joe Perches63c3a662011-04-26 08:12:10 +000017770 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017771 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017772 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017774
Matt Carlson21f76382012-02-22 12:35:21 +000017775 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017776
17777 netif_device_attach(dev);
17778 tg3_netif_start(tp);
17779
Michael Chanb9ec6c12006-07-25 16:37:27 -070017780out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017781 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017782
17783 if (!err2)
17784 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017785 }
17786
17787 return err;
17788}
17789
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017790static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017791{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017792 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017793 struct net_device *dev = pci_get_drvdata(pdev);
17794 struct tg3 *tp = netdev_priv(dev);
17795 int err;
17796
17797 if (!netif_running(dev))
17798 return 0;
17799
Linus Torvalds1da177e2005-04-16 15:20:36 -070017800 netif_device_attach(dev);
17801
David S. Millerf47c11e2005-06-24 20:18:35 -070017802 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017803
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017804 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
17805
Joe Perches63c3a662011-04-26 08:12:10 +000017806 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017807 err = tg3_restart_hw(tp,
17808 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017809 if (err)
17810 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017811
Matt Carlson21f76382012-02-22 12:35:21 +000017812 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017813
Linus Torvalds1da177e2005-04-16 15:20:36 -070017814 tg3_netif_start(tp);
17815
Michael Chanb9ec6c12006-07-25 16:37:27 -070017816out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017817 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017818
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017819 if (!err)
17820 tg3_phy_start(tp);
17821
Michael Chanb9ec6c12006-07-25 16:37:27 -070017822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017823}
Fabio Estevam42df36a2013-04-16 09:28:29 +000017824#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017825
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017826static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
17827
Nithin Sujir4c305fa2013-07-29 13:58:37 -070017828static void tg3_shutdown(struct pci_dev *pdev)
17829{
17830 struct net_device *dev = pci_get_drvdata(pdev);
17831 struct tg3 *tp = netdev_priv(dev);
17832
17833 rtnl_lock();
17834 netif_device_detach(dev);
17835
17836 if (netif_running(dev))
17837 dev_close(dev);
17838
17839 if (system_state == SYSTEM_POWER_OFF)
17840 tg3_power_down(tp);
17841
17842 rtnl_unlock();
17843}
17844
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017845/**
17846 * tg3_io_error_detected - called when PCI error is detected
17847 * @pdev: Pointer to PCI device
17848 * @state: The current pci connection state
17849 *
17850 * This function is called after a PCI bus error affecting
17851 * this device has been detected.
17852 */
17853static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17854 pci_channel_state_t state)
17855{
17856 struct net_device *netdev = pci_get_drvdata(pdev);
17857 struct tg3 *tp = netdev_priv(netdev);
17858 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17859
17860 netdev_info(netdev, "PCI I/O error detected\n");
17861
17862 rtnl_lock();
17863
Gavin Shand8af4df2013-07-24 17:25:08 +080017864 /* We probably don't have netdev yet */
17865 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017866 goto done;
17867
17868 tg3_phy_stop(tp);
17869
17870 tg3_netif_stop(tp);
17871
Matt Carlson21f76382012-02-22 12:35:21 +000017872 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017873
17874 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017875 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017876
17877 netif_device_detach(netdev);
17878
17879 /* Clean up software state, even if MMIO is blocked */
17880 tg3_full_lock(tp, 0);
17881 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17882 tg3_full_unlock(tp);
17883
17884done:
Michael Chan72bb72b2013-06-17 13:47:25 -070017885 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070017886 if (netdev) {
17887 tg3_napi_enable(tp);
17888 dev_close(netdev);
17889 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017890 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070017891 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017892 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070017893 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017894
17895 rtnl_unlock();
17896
17897 return err;
17898}
17899
17900/**
17901 * tg3_io_slot_reset - called after the pci bus has been reset.
17902 * @pdev: Pointer to PCI device
17903 *
17904 * Restart the card from scratch, as if from a cold-boot.
17905 * At this point, the card has exprienced a hard reset,
17906 * followed by fixups by BIOS, and has its config space
17907 * set up identically to what it was at cold boot.
17908 */
17909static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17910{
17911 struct net_device *netdev = pci_get_drvdata(pdev);
17912 struct tg3 *tp = netdev_priv(netdev);
17913 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17914 int err;
17915
17916 rtnl_lock();
17917
17918 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070017919 dev_err(&pdev->dev,
17920 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017921 goto done;
17922 }
17923
17924 pci_set_master(pdev);
17925 pci_restore_state(pdev);
17926 pci_save_state(pdev);
17927
Daniel Borkmann68293092013-08-13 11:45:13 -070017928 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017929 rc = PCI_ERS_RESULT_RECOVERED;
17930 goto done;
17931 }
17932
17933 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017934 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017935 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017936
17937 rc = PCI_ERS_RESULT_RECOVERED;
17938
17939done:
Daniel Borkmann68293092013-08-13 11:45:13 -070017940 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070017941 tg3_napi_enable(tp);
17942 dev_close(netdev);
17943 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017944 rtnl_unlock();
17945
17946 return rc;
17947}
17948
17949/**
17950 * tg3_io_resume - called when traffic can start flowing again.
17951 * @pdev: Pointer to PCI device
17952 *
17953 * This callback is called when the error recovery driver tells
17954 * us that its OK to resume normal operation.
17955 */
17956static void tg3_io_resume(struct pci_dev *pdev)
17957{
17958 struct net_device *netdev = pci_get_drvdata(pdev);
17959 struct tg3 *tp = netdev_priv(netdev);
17960 int err;
17961
17962 rtnl_lock();
17963
17964 if (!netif_running(netdev))
17965 goto done;
17966
17967 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017968 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000017969 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017970 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017971 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017972 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017973 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17974 goto done;
17975 }
17976
17977 netif_device_attach(netdev);
17978
Matt Carlson21f76382012-02-22 12:35:21 +000017979 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017980
17981 tg3_netif_start(tp);
17982
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017983 tg3_full_unlock(tp);
17984
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017985 tg3_phy_start(tp);
17986
17987done:
17988 rtnl_unlock();
17989}
17990
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017991static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017992 .error_detected = tg3_io_error_detected,
17993 .slot_reset = tg3_io_slot_reset,
17994 .resume = tg3_io_resume
17995};
17996
Linus Torvalds1da177e2005-04-16 15:20:36 -070017997static struct pci_driver tg3_driver = {
17998 .name = DRV_MODULE_NAME,
17999 .id_table = tg3_pci_tbl,
18000 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018001 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018002 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018003 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018004 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018005};
18006
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018007module_pci_driver(tg3_driver);