blob: dbb41c1923e60cf1a152bf414ef3428c2d0f2167 [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.
Michael Chande750e42014-05-11 20:22:55 -07007 * Copyright (C) 2005-2014 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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000028#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Michael Chane565eec2014-01-03 10:09:12 -080039#include <linux/if.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
Michael Chande750e42014-05-11 20:22:55 -070097#define TG3_MIN_NUM 137
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chande750e42014-05-11 20:22:55 -0700100#define DRV_MODULE_RELDATE "May 11, 2014"
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
Michael Chane565eec2014-01-03 10:09:12 -0800211#define TG3_MAX_UCAST_ADDR(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 3)
212#define TG3_UCAST_ADDR_IDX(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 1)
213
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000214#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000215#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000216
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800217#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000218#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800219#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
220#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
221
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500222static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000223 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
226MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
227MODULE_LICENSE("GPL");
228MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800229MODULE_FIRMWARE(FIRMWARE_TG3);
230MODULE_FIRMWARE(FIRMWARE_TG3TSO);
231MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
234module_param(tg3_debug, int, 0);
235MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
236
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000237#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
238#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
239
Benoit Taine9baa3c32014-08-08 15:56:03 +0200240static const struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
260 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
261 TG3_DRV_DATA_FLAG_5705_10_100},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
263 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
264 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
267 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
268 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
275 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
281 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000289 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
290 PCI_VENDOR_ID_LENOVO,
291 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
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_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
295 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
311 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000312 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
313 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000314 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
315 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
316 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
317 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
318 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
319 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
323 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
333 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
335 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000340 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
341 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
342 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Nithin Sujir68273712013-09-20 16:46:56 -0700343 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
344 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
345 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
346 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
347 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700348 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
349 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
350 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
351 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
352 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
353 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
354 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000355 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700356 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
359MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
360
Andreas Mohr50da8592006-08-14 23:54:30 -0700361static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000363} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 { "rx_octets" },
365 { "rx_fragments" },
366 { "rx_ucast_packets" },
367 { "rx_mcast_packets" },
368 { "rx_bcast_packets" },
369 { "rx_fcs_errors" },
370 { "rx_align_errors" },
371 { "rx_xon_pause_rcvd" },
372 { "rx_xoff_pause_rcvd" },
373 { "rx_mac_ctrl_rcvd" },
374 { "rx_xoff_entered" },
375 { "rx_frame_too_long_errors" },
376 { "rx_jabbers" },
377 { "rx_undersize_packets" },
378 { "rx_in_length_errors" },
379 { "rx_out_length_errors" },
380 { "rx_64_or_less_octet_packets" },
381 { "rx_65_to_127_octet_packets" },
382 { "rx_128_to_255_octet_packets" },
383 { "rx_256_to_511_octet_packets" },
384 { "rx_512_to_1023_octet_packets" },
385 { "rx_1024_to_1522_octet_packets" },
386 { "rx_1523_to_2047_octet_packets" },
387 { "rx_2048_to_4095_octet_packets" },
388 { "rx_4096_to_8191_octet_packets" },
389 { "rx_8192_to_9022_octet_packets" },
390
391 { "tx_octets" },
392 { "tx_collisions" },
393
394 { "tx_xon_sent" },
395 { "tx_xoff_sent" },
396 { "tx_flow_control" },
397 { "tx_mac_errors" },
398 { "tx_single_collisions" },
399 { "tx_mult_collisions" },
400 { "tx_deferred" },
401 { "tx_excessive_collisions" },
402 { "tx_late_collisions" },
403 { "tx_collide_2times" },
404 { "tx_collide_3times" },
405 { "tx_collide_4times" },
406 { "tx_collide_5times" },
407 { "tx_collide_6times" },
408 { "tx_collide_7times" },
409 { "tx_collide_8times" },
410 { "tx_collide_9times" },
411 { "tx_collide_10times" },
412 { "tx_collide_11times" },
413 { "tx_collide_12times" },
414 { "tx_collide_13times" },
415 { "tx_collide_14times" },
416 { "tx_collide_15times" },
417 { "tx_ucast_packets" },
418 { "tx_mcast_packets" },
419 { "tx_bcast_packets" },
420 { "tx_carrier_sense_errors" },
421 { "tx_discards" },
422 { "tx_errors" },
423
424 { "dma_writeq_full" },
425 { "dma_write_prioq_full" },
426 { "rxbds_empty" },
427 { "rx_discards" },
428 { "rx_errors" },
429 { "rx_threshold_hit" },
430
431 { "dma_readq_full" },
432 { "dma_read_prioq_full" },
433 { "tx_comp_queue_full" },
434
435 { "ring_set_send_prod_index" },
436 { "ring_status_update" },
437 { "nic_irqs" },
438 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000439 { "nic_tx_threshold_hit" },
440
441 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442};
443
Matt Carlson48fa55a2011-04-13 11:05:06 +0000444#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000445#define TG3_NVRAM_TEST 0
446#define TG3_LINK_TEST 1
447#define TG3_REGISTER_TEST 2
448#define TG3_MEMORY_TEST 3
449#define TG3_MAC_LOOPB_TEST 4
450#define TG3_PHY_LOOPB_TEST 5
451#define TG3_EXT_LOOPB_TEST 6
452#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000453
454
Andreas Mohr50da8592006-08-14 23:54:30 -0700455static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700456 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000457} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000458 [TG3_NVRAM_TEST] = { "nvram test (online) " },
459 [TG3_LINK_TEST] = { "link test (online) " },
460 [TG3_REGISTER_TEST] = { "register test (offline)" },
461 [TG3_MEMORY_TEST] = { "memory test (offline)" },
462 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
463 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
464 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
465 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700466};
467
Matt Carlson48fa55a2011-04-13 11:05:06 +0000468#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
469
470
Michael Chanb401e9e2005-12-19 16:27:04 -0800471static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
472{
473 writel(val, tp->regs + off);
474}
475
476static u32 tg3_read32(struct tg3 *tp, u32 off)
477{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000478 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800479}
480
Matt Carlson0d3031d2007-10-10 18:02:43 -0700481static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
482{
483 writel(val, tp->aperegs + off);
484}
485
486static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
487{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000488 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
492{
Michael Chan68929142005-08-09 20:17:14 -0700493 unsigned long flags;
494
495 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700496 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
497 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700498 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700499}
500
501static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
502{
503 writel(val, tp->regs + off);
504 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Michael Chan68929142005-08-09 20:17:14 -0700507static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
508{
509 unsigned long flags;
510 u32 val;
511
512 spin_lock_irqsave(&tp->indirect_lock, flags);
513 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
514 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
515 spin_unlock_irqrestore(&tp->indirect_lock, flags);
516 return val;
517}
518
519static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
520{
521 unsigned long flags;
522
523 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
524 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
525 TG3_64BIT_REG_LOW, val);
526 return;
527 }
Matt Carlson66711e62009-11-13 13:03:49 +0000528 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700529 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
530 TG3_64BIT_REG_LOW, val);
531 return;
532 }
533
534 spin_lock_irqsave(&tp->indirect_lock, flags);
535 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
536 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
537 spin_unlock_irqrestore(&tp->indirect_lock, flags);
538
539 /* In indirect mode when disabling interrupts, we also need
540 * to clear the interrupt bit in the GRC local ctrl register.
541 */
542 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
543 (val == 0x1)) {
544 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
545 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
546 }
547}
548
549static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
550{
551 unsigned long flags;
552 u32 val;
553
554 spin_lock_irqsave(&tp->indirect_lock, flags);
555 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
556 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
557 spin_unlock_irqrestore(&tp->indirect_lock, flags);
558 return val;
559}
560
Michael Chanb401e9e2005-12-19 16:27:04 -0800561/* usec_wait specifies the wait time in usec when writing to certain registers
562 * where it is unsafe to read back the register without some delay.
563 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
564 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
565 */
566static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Joe Perches63c3a662011-04-26 08:12:10 +0000568 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800569 /* Non-posted methods */
570 tp->write32(tp, off, val);
571 else {
572 /* Posted method */
573 tg3_write32(tp, off, val);
574 if (usec_wait)
575 udelay(usec_wait);
576 tp->read32(tp, off);
577 }
578 /* Wait again after the read for the posted method to guarantee that
579 * the wait time is met.
580 */
581 if (usec_wait)
582 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Michael Chan09ee9292005-08-09 20:17:00 -0700585static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
586{
587 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000588 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
589 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
590 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700591 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700592}
593
Michael Chan20094932005-08-09 20:16:32 -0700594static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 void __iomem *mbox = tp->regs + off;
597 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000598 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000600 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
601 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 readl(mbox);
603}
604
Michael Chanb5d37722006-09-27 16:06:21 -0700605static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
606{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000607 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700608}
609
610static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
611{
612 writel(val, tp->regs + off + GRCMBOX_BASE);
613}
614
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000615#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700616#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000617#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
618#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
619#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700620
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000621#define tw32(reg, val) tp->write32(tp, reg, val)
622#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
623#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
624#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
627{
Michael Chan68929142005-08-09 20:17:14 -0700628 unsigned long flags;
629
Joe Perches41535772013-02-16 11:20:04 +0000630 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700631 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
632 return;
633
Michael Chan68929142005-08-09 20:17:14 -0700634 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000635 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700636 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
637 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Michael Chanbbadf502006-04-06 21:46:34 -0700639 /* Always leave this as zero. */
640 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
641 } else {
642 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
643 tw32_f(TG3PCI_MEM_WIN_DATA, val);
644
645 /* Always leave this as zero. */
646 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
647 }
Michael Chan68929142005-08-09 20:17:14 -0700648 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
652{
Michael Chan68929142005-08-09 20:17:14 -0700653 unsigned long flags;
654
Joe Perches41535772013-02-16 11:20:04 +0000655 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700656 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
657 *val = 0;
658 return;
659 }
660
Michael Chan68929142005-08-09 20:17:14 -0700661 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000662 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700663 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
664 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Michael Chanbbadf502006-04-06 21:46:34 -0700666 /* Always leave this as zero. */
667 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
668 } else {
669 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
670 *val = tr32(TG3PCI_MEM_WIN_DATA);
671
672 /* Always leave this as zero. */
673 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
674 }
Michael Chan68929142005-08-09 20:17:14 -0700675 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Matt Carlson0d3031d2007-10-10 18:02:43 -0700678static void tg3_ape_lock_init(struct tg3 *tp)
679{
680 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000681 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000682
Joe Perches41535772013-02-16 11:20:04 +0000683 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000684 regbase = TG3_APE_LOCK_GRANT;
685 else
686 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700687
688 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000689 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
690 switch (i) {
691 case TG3_APE_LOCK_PHY0:
692 case TG3_APE_LOCK_PHY1:
693 case TG3_APE_LOCK_PHY2:
694 case TG3_APE_LOCK_PHY3:
695 bit = APE_LOCK_GRANT_DRIVER;
696 break;
697 default:
698 if (!tp->pci_fn)
699 bit = APE_LOCK_GRANT_DRIVER;
700 else
701 bit = 1 << tp->pci_fn;
702 }
703 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000704 }
705
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706}
707
708static int tg3_ape_lock(struct tg3 *tp, int locknum)
709{
710 int i, off;
711 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700713
Joe Perches63c3a662011-04-26 08:12:10 +0000714 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700715 return 0;
716
717 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000718 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000719 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000720 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000721 case TG3_APE_LOCK_GRC:
722 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000723 if (!tp->pci_fn)
724 bit = APE_LOCK_REQ_DRIVER;
725 else
726 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000727 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000728 case TG3_APE_LOCK_PHY0:
729 case TG3_APE_LOCK_PHY1:
730 case TG3_APE_LOCK_PHY2:
731 case TG3_APE_LOCK_PHY3:
732 bit = APE_LOCK_REQ_DRIVER;
733 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000734 default:
735 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700736 }
737
Joe Perches41535772013-02-16 11:20:04 +0000738 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000739 req = TG3_APE_LOCK_REQ;
740 gnt = TG3_APE_LOCK_GRANT;
741 } else {
742 req = TG3_APE_PER_LOCK_REQ;
743 gnt = TG3_APE_PER_LOCK_GRANT;
744 }
745
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746 off = 4 * locknum;
747
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000748 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700749
750 /* Wait for up to 1 millisecond to acquire lock. */
751 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000752 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000753 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700754 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800755 if (pci_channel_offline(tp->pdev))
756 break;
757
Matt Carlson0d3031d2007-10-10 18:02:43 -0700758 udelay(10);
759 }
760
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000761 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700762 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000763 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700764 ret = -EBUSY;
765 }
766
767 return ret;
768}
769
770static void tg3_ape_unlock(struct tg3 *tp, int locknum)
771{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000772 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700773
Joe Perches63c3a662011-04-26 08:12:10 +0000774 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700775 return;
776
777 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000778 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000779 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000780 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000781 case TG3_APE_LOCK_GRC:
782 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000783 if (!tp->pci_fn)
784 bit = APE_LOCK_GRANT_DRIVER;
785 else
786 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000787 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000788 case TG3_APE_LOCK_PHY0:
789 case TG3_APE_LOCK_PHY1:
790 case TG3_APE_LOCK_PHY2:
791 case TG3_APE_LOCK_PHY3:
792 bit = APE_LOCK_GRANT_DRIVER;
793 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000794 default:
795 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700796 }
797
Joe Perches41535772013-02-16 11:20:04 +0000798 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000799 gnt = TG3_APE_LOCK_GRANT;
800 else
801 gnt = TG3_APE_PER_LOCK_GRANT;
802
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000803 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700804}
805
Matt Carlsonb65a3722012-07-16 16:24:00 +0000806static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000807{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000808 u32 apedata;
809
Matt Carlsonb65a3722012-07-16 16:24:00 +0000810 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000811 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000812 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000813
814 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000815 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
816 break;
817
Matt Carlsonb65a3722012-07-16 16:24:00 +0000818 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
819
820 udelay(10);
821 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000822 }
823
Matt Carlsonb65a3722012-07-16 16:24:00 +0000824 return timeout_us ? 0 : -EBUSY;
825}
826
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000827static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
828{
829 u32 i, apedata;
830
831 for (i = 0; i < timeout_us / 10; i++) {
832 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
833
834 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
835 break;
836
837 udelay(10);
838 }
839
840 return i == timeout_us / 10;
841}
842
Michael Chan86449942012-10-02 20:31:14 -0700843static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
844 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000845{
846 int err;
847 u32 i, bufoff, msgoff, maxlen, apedata;
848
849 if (!tg3_flag(tp, APE_HAS_NCSI))
850 return 0;
851
852 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
853 if (apedata != APE_SEG_SIG_MAGIC)
854 return -ENODEV;
855
856 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
857 if (!(apedata & APE_FW_STATUS_READY))
858 return -EAGAIN;
859
860 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
861 TG3_APE_SHMEM_BASE;
862 msgoff = bufoff + 2 * sizeof(u32);
863 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
864
865 while (len) {
866 u32 length;
867
868 /* Cap xfer sizes to scratchpad limits. */
869 length = (len > maxlen) ? maxlen : len;
870 len -= length;
871
872 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
873 if (!(apedata & APE_FW_STATUS_READY))
874 return -EAGAIN;
875
876 /* Wait for up to 1 msec for APE to service previous event. */
877 err = tg3_ape_event_lock(tp, 1000);
878 if (err)
879 return err;
880
881 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
882 APE_EVENT_STATUS_SCRTCHPD_READ |
883 APE_EVENT_STATUS_EVENT_PENDING;
884 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
885
886 tg3_ape_write32(tp, bufoff, base_off);
887 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
888
889 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
890 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
891
892 base_off += length;
893
894 if (tg3_ape_wait_for_event(tp, 30000))
895 return -EAGAIN;
896
897 for (i = 0; length; i += 4, length -= 4) {
898 u32 val = tg3_ape_read32(tp, msgoff + i);
899 memcpy(data, &val, sizeof(u32));
900 data++;
901 }
902 }
903
904 return 0;
905}
906
Matt Carlsonb65a3722012-07-16 16:24:00 +0000907static int tg3_ape_send_event(struct tg3 *tp, u32 event)
908{
909 int err;
910 u32 apedata;
911
912 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
913 if (apedata != APE_SEG_SIG_MAGIC)
914 return -EAGAIN;
915
916 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
917 if (!(apedata & APE_FW_STATUS_READY))
918 return -EAGAIN;
919
920 /* Wait for up to 1 millisecond for APE to service previous event. */
921 err = tg3_ape_event_lock(tp, 1000);
922 if (err)
923 return err;
924
925 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
926 event | APE_EVENT_STATUS_EVENT_PENDING);
927
928 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
929 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
930
931 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000932}
933
934static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
935{
936 u32 event;
937 u32 apedata;
938
939 if (!tg3_flag(tp, ENABLE_APE))
940 return;
941
942 switch (kind) {
943 case RESET_KIND_INIT:
944 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
945 APE_HOST_SEG_SIG_MAGIC);
946 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
947 APE_HOST_SEG_LEN_MAGIC);
948 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
949 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
950 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
951 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
952 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
953 APE_HOST_BEHAV_NO_PHYLOCK);
954 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
955 TG3_APE_HOST_DRVR_STATE_START);
956
957 event = APE_EVENT_STATUS_STATE_START;
958 break;
959 case RESET_KIND_SHUTDOWN:
960 /* With the interface we are currently using,
961 * APE does not track driver state. Wiping
962 * out the HOST SEGMENT SIGNATURE forces
963 * the APE to assume OS absent status.
964 */
965 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
966
967 if (device_may_wakeup(&tp->pdev->dev) &&
968 tg3_flag(tp, WOL_ENABLE)) {
969 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
970 TG3_APE_HOST_WOL_SPEED_AUTO);
971 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
972 } else
973 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
974
975 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
976
977 event = APE_EVENT_STATUS_STATE_UNLOAD;
978 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000979 default:
980 return;
981 }
982
983 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
984
985 tg3_ape_send_event(tp, event);
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static void tg3_disable_ints(struct tg3 *tp)
989{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000990 int i;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 tw32(TG3PCI_MISC_HOST_CTRL,
993 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000994 for (i = 0; i < tp->irq_max; i++)
995 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998static void tg3_enable_ints(struct tg3 *tp)
999{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001000 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001
Michael Chanbbe832c2005-06-24 20:20:04 -07001002 tp->irq_sync = 0;
1003 wmb();
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 tw32(TG3PCI_MISC_HOST_CTRL,
1006 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001007
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001008 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 for (i = 0; i < tp->irq_cnt; i++) {
1010 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001011
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001012 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001013 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001014 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1015
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001016 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001017 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001018
1019 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001020 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001021 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1022 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1023 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001024 tw32(HOSTCC_MODE, tp->coal_now);
1025
1026 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Matt Carlson17375d22009-08-28 14:02:18 +00001029static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001030{
Matt Carlson17375d22009-08-28 14:02:18 +00001031 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001032 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001033 unsigned int work_exists = 0;
1034
1035 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001036 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001037 if (sblk->status & SD_STATUS_LINK_CHG)
1038 work_exists = 1;
1039 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001040
1041 /* check for TX work to do */
1042 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1043 work_exists = 1;
1044
1045 /* check for RX work to do */
1046 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001047 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001048 work_exists = 1;
1049
1050 return work_exists;
1051}
1052
Matt Carlson17375d22009-08-28 14:02:18 +00001053/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001054 * similar to tg3_enable_ints, but it accurately determines whether there
1055 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001056 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Matt Carlson17375d22009-08-28 14:02:18 +00001058static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Matt Carlson17375d22009-08-28 14:02:18 +00001060 struct tg3 *tp = tnapi->tp;
1061
Matt Carlson898a56f2009-08-28 14:02:40 +00001062 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 mmiowb();
1064
David S. Millerfac9b832005-05-18 22:46:34 -07001065 /* When doing tagged status, this work check is unnecessary.
1066 * The last_tag we write above tells the chip which piece of
1067 * work we've completed.
1068 */
Joe Perches63c3a662011-04-26 08:12:10 +00001069 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001070 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001071 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static void tg3_switch_clocks(struct tg3 *tp)
1075{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001076 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 u32 orig_clock_ctrl;
1078
Joe Perches63c3a662011-04-26 08:12:10 +00001079 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001080 return;
1081
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001082 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 orig_clock_ctrl = clock_ctrl;
1085 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1086 CLOCK_CTRL_CLKRUN_OENABLE |
1087 0x1f);
1088 tp->pci_clock_ctrl = clock_ctrl;
1089
Joe Perches63c3a662011-04-26 08:12:10 +00001090 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001092 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1093 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001096 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1097 clock_ctrl |
1098 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1099 40);
1100 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1101 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1102 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001104 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107#define PHY_BUSY_LOOPS 5000
1108
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001109static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1110 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 u32 frame_val;
1113 unsigned int loops;
1114 int ret;
1115
1116 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1117 tw32_f(MAC_MI_MODE,
1118 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1119 udelay(80);
1120 }
1121
Michael Chan8151ad52012-07-29 19:15:41 +00001122 tg3_ape_lock(tp, tp->phy_ape_lock);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *val = 0x0;
1125
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001126 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 MI_COM_PHY_ADDR_MASK);
1128 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1129 MI_COM_REG_ADDR_MASK);
1130 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 tw32_f(MAC_MI_COM, frame_val);
1133
1134 loops = PHY_BUSY_LOOPS;
1135 while (loops != 0) {
1136 udelay(10);
1137 frame_val = tr32(MAC_MI_COM);
1138
1139 if ((frame_val & MI_COM_BUSY) == 0) {
1140 udelay(5);
1141 frame_val = tr32(MAC_MI_COM);
1142 break;
1143 }
1144 loops -= 1;
1145 }
1146
1147 ret = -EBUSY;
1148 if (loops != 0) {
1149 *val = frame_val & MI_COM_DATA_MASK;
1150 ret = 0;
1151 }
1152
1153 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1154 tw32_f(MAC_MI_MODE, tp->mi_mode);
1155 udelay(80);
1156 }
1157
Michael Chan8151ad52012-07-29 19:15:41 +00001158 tg3_ape_unlock(tp, tp->phy_ape_lock);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return ret;
1161}
1162
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001163static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1164{
1165 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1166}
1167
1168static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1169 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 u32 frame_val;
1172 unsigned int loops;
1173 int ret;
1174
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001175 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001176 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001177 return 0;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1180 tw32_f(MAC_MI_MODE,
1181 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1182 udelay(80);
1183 }
1184
Michael Chan8151ad52012-07-29 19:15:41 +00001185 tg3_ape_lock(tp, tp->phy_ape_lock);
1186
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001187 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 MI_COM_PHY_ADDR_MASK);
1189 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1190 MI_COM_REG_ADDR_MASK);
1191 frame_val |= (val & MI_COM_DATA_MASK);
1192 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 tw32_f(MAC_MI_COM, frame_val);
1195
1196 loops = PHY_BUSY_LOOPS;
1197 while (loops != 0) {
1198 udelay(10);
1199 frame_val = tr32(MAC_MI_COM);
1200 if ((frame_val & MI_COM_BUSY) == 0) {
1201 udelay(5);
1202 frame_val = tr32(MAC_MI_COM);
1203 break;
1204 }
1205 loops -= 1;
1206 }
1207
1208 ret = -EBUSY;
1209 if (loops != 0)
1210 ret = 0;
1211
1212 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1213 tw32_f(MAC_MI_MODE, tp->mi_mode);
1214 udelay(80);
1215 }
1216
Michael Chan8151ad52012-07-29 19:15:41 +00001217 tg3_ape_unlock(tp, tp->phy_ape_lock);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return ret;
1220}
1221
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001222static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1223{
1224 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1225}
1226
Matt Carlsonb0988c12011-04-20 07:57:39 +00001227static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1228{
1229 int err;
1230
1231 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1232 if (err)
1233 goto done;
1234
1235 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1236 if (err)
1237 goto done;
1238
1239 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1240 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1241 if (err)
1242 goto done;
1243
1244 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1245
1246done:
1247 return err;
1248}
1249
1250static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1251{
1252 int err;
1253
1254 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1255 if (err)
1256 goto done;
1257
1258 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1259 if (err)
1260 goto done;
1261
1262 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1263 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1264 if (err)
1265 goto done;
1266
1267 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1268
1269done:
1270 return err;
1271}
1272
1273static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1274{
1275 int err;
1276
1277 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1278 if (!err)
1279 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1280
1281 return err;
1282}
1283
1284static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1285{
1286 int err;
1287
1288 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1289 if (!err)
1290 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1291
1292 return err;
1293}
1294
Matt Carlson15ee95c2011-04-20 07:57:40 +00001295static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1296{
1297 int err;
1298
1299 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1300 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1301 MII_TG3_AUXCTL_SHDWSEL_MISC);
1302 if (!err)
1303 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1304
1305 return err;
1306}
1307
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001308static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1309{
1310 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1311 set |= MII_TG3_AUXCTL_MISC_WREN;
1312
1313 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1314}
1315
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001316static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1317{
1318 u32 val;
1319 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001320
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001321 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1322
1323 if (err)
1324 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001325
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001326 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001327 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1328 else
1329 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1330
1331 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1332 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1333
1334 return err;
1335}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001336
Nithin Sujir3ab71072013-09-20 16:46:55 -07001337static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1338{
1339 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1340 reg | val | MII_TG3_MISC_SHDW_WREN);
1341}
1342
Matt Carlson95e28692008-05-25 23:44:14 -07001343static int tg3_bmcr_reset(struct tg3 *tp)
1344{
1345 u32 phy_control;
1346 int limit, err;
1347
1348 /* OK, reset it, and poll the BMCR_RESET bit until it
1349 * clears or we time out.
1350 */
1351 phy_control = BMCR_RESET;
1352 err = tg3_writephy(tp, MII_BMCR, phy_control);
1353 if (err != 0)
1354 return -EBUSY;
1355
1356 limit = 5000;
1357 while (limit--) {
1358 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1359 if (err != 0)
1360 return -EBUSY;
1361
1362 if ((phy_control & BMCR_RESET) == 0) {
1363 udelay(40);
1364 break;
1365 }
1366 udelay(10);
1367 }
Roel Kluind4675b52009-02-12 16:33:27 -08001368 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001369 return -EBUSY;
1370
1371 return 0;
1372}
1373
Matt Carlson158d7ab2008-05-29 01:37:54 -07001374static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1375{
Francois Romieu3d165432009-01-19 16:56:50 -08001376 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001377 u32 val;
1378
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001379 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001380
Hauke Mehrtensead24022013-09-28 23:15:26 +02001381 if (__tg3_readphy(tp, mii_id, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001382 val = -EIO;
1383
1384 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001385
1386 return val;
1387}
1388
1389static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1390{
Francois Romieu3d165432009-01-19 16:56:50 -08001391 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001392 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001393
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001394 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001395
Hauke Mehrtensead24022013-09-28 23:15:26 +02001396 if (__tg3_writephy(tp, mii_id, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001397 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001398
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001399 spin_unlock_bh(&tp->lock);
1400
1401 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001402}
1403
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001404static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001405{
1406 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001407 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001408
Hauke Mehrtensead24022013-09-28 23:15:26 +02001409 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001410 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001411 case PHY_ID_BCM50610:
1412 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001413 val = MAC_PHYCFG2_50610_LED_MODES;
1414 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001415 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001416 val = MAC_PHYCFG2_AC131_LED_MODES;
1417 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001418 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001419 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1420 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001421 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001422 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1423 break;
1424 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001425 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001426 }
1427
1428 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1429 tw32(MAC_PHYCFG2, val);
1430
1431 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001432 val &= ~(MAC_PHYCFG1_RGMII_INT |
1433 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1434 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001435 tw32(MAC_PHYCFG1, val);
1436
1437 return;
1438 }
1439
Joe Perches63c3a662011-04-26 08:12:10 +00001440 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001441 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1442 MAC_PHYCFG2_FMODE_MASK_MASK |
1443 MAC_PHYCFG2_GMODE_MASK_MASK |
1444 MAC_PHYCFG2_ACT_MASK_MASK |
1445 MAC_PHYCFG2_QUAL_MASK_MASK |
1446 MAC_PHYCFG2_INBAND_ENABLE;
1447
1448 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001449
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001450 val = tr32(MAC_PHYCFG1);
1451 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1452 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001453 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1454 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001455 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001456 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001457 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1458 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001459 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1460 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1461 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001462
Matt Carlsona9daf362008-05-25 23:49:44 -07001463 val = tr32(MAC_EXT_RGMII_MODE);
1464 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1465 MAC_RGMII_MODE_RX_QUALITY |
1466 MAC_RGMII_MODE_RX_ACTIVITY |
1467 MAC_RGMII_MODE_RX_ENG_DET |
1468 MAC_RGMII_MODE_TX_ENABLE |
1469 MAC_RGMII_MODE_TX_LOWPWR |
1470 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001471 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1472 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001473 val |= MAC_RGMII_MODE_RX_INT_B |
1474 MAC_RGMII_MODE_RX_QUALITY |
1475 MAC_RGMII_MODE_RX_ACTIVITY |
1476 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001477 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001478 val |= MAC_RGMII_MODE_TX_ENABLE |
1479 MAC_RGMII_MODE_TX_LOWPWR |
1480 MAC_RGMII_MODE_TX_RESET;
1481 }
1482 tw32(MAC_EXT_RGMII_MODE, val);
1483}
1484
Matt Carlson158d7ab2008-05-29 01:37:54 -07001485static void tg3_mdio_start(struct tg3 *tp)
1486{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001487 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1488 tw32_f(MAC_MI_MODE, tp->mi_mode);
1489 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001490
Joe Perches63c3a662011-04-26 08:12:10 +00001491 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001492 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001493 tg3_mdio_config_5785(tp);
1494}
1495
1496static int tg3_mdio_init(struct tg3 *tp)
1497{
1498 int i;
1499 u32 reg;
1500 struct phy_device *phydev;
1501
Joe Perches63c3a662011-04-26 08:12:10 +00001502 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001503 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001504
Matt Carlson69f11c92011-07-13 09:27:30 +00001505 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001506
Joe Perches41535772013-02-16 11:20:04 +00001507 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001508 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1509 else
1510 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1511 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001512 if (is_serdes)
1513 tp->phy_addr += 7;
Hauke Mehrtensee002b62013-09-28 23:15:28 +02001514 } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
1515 int addr;
1516
1517 addr = ssb_gige_get_phyaddr(tp->pdev);
1518 if (addr < 0)
1519 return addr;
1520 tp->phy_addr = addr;
Matt Carlson882e9792009-09-01 13:21:36 +00001521 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001522 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001523
Matt Carlson158d7ab2008-05-29 01:37:54 -07001524 tg3_mdio_start(tp);
1525
Joe Perches63c3a662011-04-26 08:12:10 +00001526 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001527 return 0;
1528
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001529 tp->mdio_bus = mdiobus_alloc();
1530 if (tp->mdio_bus == NULL)
1531 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001532
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001533 tp->mdio_bus->name = "tg3 mdio bus";
1534 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001535 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001536 tp->mdio_bus->priv = tp;
1537 tp->mdio_bus->parent = &tp->pdev->dev;
1538 tp->mdio_bus->read = &tg3_mdio_read;
1539 tp->mdio_bus->write = &tg3_mdio_write;
Hauke Mehrtensead24022013-09-28 23:15:26 +02001540 tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001541 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001542
1543 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001544 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001545
1546 /* The bus registration will look for all the PHYs on the mdio bus.
1547 * Unfortunately, it does not ensure the PHY is powered up before
1548 * accessing the PHY ID registers. A chip reset is the
1549 * quickest way to bring the device back to an operational state..
1550 */
1551 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1552 tg3_bmcr_reset(tp);
1553
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001554 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001555 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001556 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001557 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001558 return i;
1559 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001560
Hauke Mehrtensead24022013-09-28 23:15:26 +02001561 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsona9daf362008-05-25 23:49:44 -07001562
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001563 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001564 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001565 mdiobus_unregister(tp->mdio_bus);
1566 mdiobus_free(tp->mdio_bus);
1567 return -ENODEV;
1568 }
1569
1570 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001571 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001572 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001573 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001574 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001575 case PHY_ID_BCM50610:
1576 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001577 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001578 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001579 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001580 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001581 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001582 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001583 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001584 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001585 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001586 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001587 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001588 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001589 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001590 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001591 case PHY_ID_RTL8201E:
1592 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001593 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001594 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001595 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001596 break;
1597 }
1598
Joe Perches63c3a662011-04-26 08:12:10 +00001599 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001600
Joe Perches41535772013-02-16 11:20:04 +00001601 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001602 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001603
1604 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001605}
1606
1607static void tg3_mdio_fini(struct tg3 *tp)
1608{
Joe Perches63c3a662011-04-26 08:12:10 +00001609 if (tg3_flag(tp, MDIOBUS_INITED)) {
1610 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001611 mdiobus_unregister(tp->mdio_bus);
1612 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001613 }
1614}
1615
Matt Carlson95e28692008-05-25 23:44:14 -07001616/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001617static inline void tg3_generate_fw_event(struct tg3 *tp)
1618{
1619 u32 val;
1620
1621 val = tr32(GRC_RX_CPU_EVENT);
1622 val |= GRC_RX_CPU_DRIVER_EVENT;
1623 tw32_f(GRC_RX_CPU_EVENT, val);
1624
1625 tp->last_event_jiffies = jiffies;
1626}
1627
1628#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1629
1630/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001631static void tg3_wait_for_event_ack(struct tg3 *tp)
1632{
1633 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001634 unsigned int delay_cnt;
1635 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001636
Matt Carlson4ba526c2008-08-15 14:10:04 -07001637 /* If enough time has passed, no wait is necessary. */
1638 time_remain = (long)(tp->last_event_jiffies + 1 +
1639 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1640 (long)jiffies;
1641 if (time_remain < 0)
1642 return;
1643
1644 /* Check if we can shorten the wait time. */
1645 delay_cnt = jiffies_to_usecs(time_remain);
1646 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1647 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1648 delay_cnt = (delay_cnt >> 3) + 1;
1649
1650 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001651 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1652 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001653 if (pci_channel_offline(tp->pdev))
1654 break;
1655
Matt Carlson4ba526c2008-08-15 14:10:04 -07001656 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001657 }
1658}
1659
1660/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001661static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001662{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001663 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001664
1665 val = 0;
1666 if (!tg3_readphy(tp, MII_BMCR, &reg))
1667 val = reg << 16;
1668 if (!tg3_readphy(tp, MII_BMSR, &reg))
1669 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001670 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001671
1672 val = 0;
1673 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1674 val = reg << 16;
1675 if (!tg3_readphy(tp, MII_LPA, &reg))
1676 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001677 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001678
1679 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001680 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001681 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1682 val = reg << 16;
1683 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1684 val |= (reg & 0xffff);
1685 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001686 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001687
1688 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1689 val = reg << 16;
1690 else
1691 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001692 *data++ = val;
1693}
1694
1695/* tp->lock is held. */
1696static void tg3_ump_link_report(struct tg3 *tp)
1697{
1698 u32 data[4];
1699
1700 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1701 return;
1702
1703 tg3_phy_gather_ump_data(tp, data);
1704
1705 tg3_wait_for_event_ack(tp);
1706
1707 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1708 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1709 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1710 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1711 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1712 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001713
Matt Carlson4ba526c2008-08-15 14:10:04 -07001714 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001715}
1716
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001717/* tp->lock is held. */
1718static void tg3_stop_fw(struct tg3 *tp)
1719{
1720 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1721 /* Wait for RX cpu to ACK the previous event. */
1722 tg3_wait_for_event_ack(tp);
1723
1724 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1725
1726 tg3_generate_fw_event(tp);
1727
1728 /* Wait for RX cpu to ACK this event. */
1729 tg3_wait_for_event_ack(tp);
1730 }
1731}
1732
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001733/* tp->lock is held. */
1734static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1735{
1736 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1737 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1738
1739 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1740 switch (kind) {
1741 case RESET_KIND_INIT:
1742 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1743 DRV_STATE_START);
1744 break;
1745
1746 case RESET_KIND_SHUTDOWN:
1747 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1748 DRV_STATE_UNLOAD);
1749 break;
1750
1751 case RESET_KIND_SUSPEND:
1752 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1753 DRV_STATE_SUSPEND);
1754 break;
1755
1756 default:
1757 break;
1758 }
1759 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001760}
1761
1762/* tp->lock is held. */
1763static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1764{
1765 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1766 switch (kind) {
1767 case RESET_KIND_INIT:
1768 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1769 DRV_STATE_START_DONE);
1770 break;
1771
1772 case RESET_KIND_SHUTDOWN:
1773 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1774 DRV_STATE_UNLOAD_DONE);
1775 break;
1776
1777 default:
1778 break;
1779 }
1780 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001781}
1782
1783/* tp->lock is held. */
1784static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1785{
1786 if (tg3_flag(tp, ENABLE_ASF)) {
1787 switch (kind) {
1788 case RESET_KIND_INIT:
1789 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1790 DRV_STATE_START);
1791 break;
1792
1793 case RESET_KIND_SHUTDOWN:
1794 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1795 DRV_STATE_UNLOAD);
1796 break;
1797
1798 case RESET_KIND_SUSPEND:
1799 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1800 DRV_STATE_SUSPEND);
1801 break;
1802
1803 default:
1804 break;
1805 }
1806 }
1807}
1808
1809static int tg3_poll_fw(struct tg3 *tp)
1810{
1811 int i;
1812 u32 val;
1813
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001814 if (tg3_flag(tp, NO_FWARE_REPORTED))
1815 return 0;
1816
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001817 if (tg3_flag(tp, IS_SSB_CORE)) {
1818 /* We don't use firmware. */
1819 return 0;
1820 }
1821
Joe Perches41535772013-02-16 11:20:04 +00001822 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001823 /* Wait up to 20ms for init done. */
1824 for (i = 0; i < 200; i++) {
1825 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1826 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001827 if (pci_channel_offline(tp->pdev))
1828 return -ENODEV;
1829
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001830 udelay(100);
1831 }
1832 return -ENODEV;
1833 }
1834
1835 /* Wait for firmware initialization to complete. */
1836 for (i = 0; i < 100000; i++) {
1837 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1838 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1839 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001840 if (pci_channel_offline(tp->pdev)) {
1841 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1842 tg3_flag_set(tp, NO_FWARE_REPORTED);
1843 netdev_info(tp->dev, "No firmware running\n");
1844 }
1845
1846 break;
1847 }
1848
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001849 udelay(10);
1850 }
1851
1852 /* Chip might not be fitted with firmware. Some Sun onboard
1853 * parts are configured like that. So don't signal the timeout
1854 * of the above loop as an error, but do report the lack of
1855 * running firmware once.
1856 */
1857 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1858 tg3_flag_set(tp, NO_FWARE_REPORTED);
1859
1860 netdev_info(tp->dev, "No firmware running\n");
1861 }
1862
Joe Perches41535772013-02-16 11:20:04 +00001863 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001864 /* The 57765 A0 needs a little more
1865 * time to do some important work.
1866 */
1867 mdelay(10);
1868 }
1869
1870 return 0;
1871}
1872
Matt Carlson95e28692008-05-25 23:44:14 -07001873static void tg3_link_report(struct tg3 *tp)
1874{
1875 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001876 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001877 tg3_ump_link_report(tp);
1878 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001879 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1880 (tp->link_config.active_speed == SPEED_1000 ?
1881 1000 :
1882 (tp->link_config.active_speed == SPEED_100 ?
1883 100 : 10)),
1884 (tp->link_config.active_duplex == DUPLEX_FULL ?
1885 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001886
Joe Perches05dbe002010-02-17 19:44:19 +00001887 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1888 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1889 "on" : "off",
1890 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1891 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001892
1893 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1894 netdev_info(tp->dev, "EEE is %s\n",
1895 tp->setlpicnt ? "enabled" : "disabled");
1896
Matt Carlson95e28692008-05-25 23:44:14 -07001897 tg3_ump_link_report(tp);
1898 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001899
1900 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001901}
1902
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001903static u32 tg3_decode_flowctrl_1000T(u32 adv)
1904{
1905 u32 flowctrl = 0;
1906
1907 if (adv & ADVERTISE_PAUSE_CAP) {
1908 flowctrl |= FLOW_CTRL_RX;
1909 if (!(adv & ADVERTISE_PAUSE_ASYM))
1910 flowctrl |= FLOW_CTRL_TX;
1911 } else if (adv & ADVERTISE_PAUSE_ASYM)
1912 flowctrl |= FLOW_CTRL_TX;
1913
1914 return flowctrl;
1915}
1916
Matt Carlson95e28692008-05-25 23:44:14 -07001917static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1918{
1919 u16 miireg;
1920
Steve Glendinninge18ce342008-12-16 02:00:00 -08001921 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001922 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001923 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001924 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001925 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001926 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1927 else
1928 miireg = 0;
1929
1930 return miireg;
1931}
1932
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001933static u32 tg3_decode_flowctrl_1000X(u32 adv)
1934{
1935 u32 flowctrl = 0;
1936
1937 if (adv & ADVERTISE_1000XPAUSE) {
1938 flowctrl |= FLOW_CTRL_RX;
1939 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1940 flowctrl |= FLOW_CTRL_TX;
1941 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1942 flowctrl |= FLOW_CTRL_TX;
1943
1944 return flowctrl;
1945}
1946
Matt Carlson95e28692008-05-25 23:44:14 -07001947static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1948{
1949 u8 cap = 0;
1950
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001951 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1952 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1953 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1954 if (lcladv & ADVERTISE_1000XPAUSE)
1955 cap = FLOW_CTRL_RX;
1956 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001957 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001958 }
1959
1960 return cap;
1961}
1962
Matt Carlsonf51f3562008-05-25 23:45:08 -07001963static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001964{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001965 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001966 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001967 u32 old_rx_mode = tp->rx_mode;
1968 u32 old_tx_mode = tp->tx_mode;
1969
Joe Perches63c3a662011-04-26 08:12:10 +00001970 if (tg3_flag(tp, USE_PHYLIB))
Hauke Mehrtensead24022013-09-28 23:15:26 +02001971 autoneg = tp->mdio_bus->phy_map[tp->phy_addr]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001972 else
1973 autoneg = tp->link_config.autoneg;
1974
Joe Perches63c3a662011-04-26 08:12:10 +00001975 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001976 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001977 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001978 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001979 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001980 } else
1981 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001982
Matt Carlsonf51f3562008-05-25 23:45:08 -07001983 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001984
Steve Glendinninge18ce342008-12-16 02:00:00 -08001985 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001986 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1987 else
1988 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1989
Matt Carlsonf51f3562008-05-25 23:45:08 -07001990 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001991 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001992
Steve Glendinninge18ce342008-12-16 02:00:00 -08001993 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001994 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1995 else
1996 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1997
Matt Carlsonf51f3562008-05-25 23:45:08 -07001998 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001999 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07002000}
2001
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002002static void tg3_adjust_link(struct net_device *dev)
2003{
2004 u8 oldflowctrl, linkmesg = 0;
2005 u32 mac_mode, lcl_adv, rmt_adv;
2006 struct tg3 *tp = netdev_priv(dev);
Hauke Mehrtensead24022013-09-28 23:15:26 +02002007 struct phy_device *phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002008
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002009 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002010
2011 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2012 MAC_MODE_HALF_DUPLEX);
2013
2014 oldflowctrl = tp->link_config.active_flowctrl;
2015
2016 if (phydev->link) {
2017 lcl_adv = 0;
2018 rmt_adv = 0;
2019
2020 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2021 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002022 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002023 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002024 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002025 else
2026 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002027
2028 if (phydev->duplex == DUPLEX_HALF)
2029 mac_mode |= MAC_MODE_HALF_DUPLEX;
2030 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002031 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002032 tp->link_config.flowctrl);
2033
2034 if (phydev->pause)
2035 rmt_adv = LPA_PAUSE_CAP;
2036 if (phydev->asym_pause)
2037 rmt_adv |= LPA_PAUSE_ASYM;
2038 }
2039
2040 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2041 } else
2042 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2043
2044 if (mac_mode != tp->mac_mode) {
2045 tp->mac_mode = mac_mode;
2046 tw32_f(MAC_MODE, tp->mac_mode);
2047 udelay(40);
2048 }
2049
Joe Perches41535772013-02-16 11:20:04 +00002050 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002051 if (phydev->speed == SPEED_10)
2052 tw32(MAC_MI_STAT,
2053 MAC_MI_STAT_10MBPS_MODE |
2054 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2055 else
2056 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2057 }
2058
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002059 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2060 tw32(MAC_TX_LENGTHS,
2061 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2062 (6 << TX_LENGTHS_IPG_SHIFT) |
2063 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2064 else
2065 tw32(MAC_TX_LENGTHS,
2066 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2067 (6 << TX_LENGTHS_IPG_SHIFT) |
2068 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2069
Matt Carlson34655ad2012-02-22 12:35:18 +00002070 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002071 phydev->speed != tp->link_config.active_speed ||
2072 phydev->duplex != tp->link_config.active_duplex ||
2073 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002074 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002075
Matt Carlson34655ad2012-02-22 12:35:18 +00002076 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002077 tp->link_config.active_speed = phydev->speed;
2078 tp->link_config.active_duplex = phydev->duplex;
2079
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002080 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002081
2082 if (linkmesg)
2083 tg3_link_report(tp);
2084}
2085
2086static int tg3_phy_init(struct tg3 *tp)
2087{
2088 struct phy_device *phydev;
2089
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002090 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002091 return 0;
2092
2093 /* Bring the PHY back to a known state. */
2094 tg3_bmcr_reset(tp);
2095
Hauke Mehrtensead24022013-09-28 23:15:26 +02002096 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002097
2098 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002099 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2100 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002101 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002102 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002103 return PTR_ERR(phydev);
2104 }
2105
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002106 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002107 switch (phydev->interface) {
2108 case PHY_INTERFACE_MODE_GMII:
2109 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002110 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002111 phydev->supported &= (PHY_GBIT_FEATURES |
2112 SUPPORTED_Pause |
2113 SUPPORTED_Asym_Pause);
2114 break;
2115 }
2116 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002117 case PHY_INTERFACE_MODE_MII:
2118 phydev->supported &= (PHY_BASIC_FEATURES |
2119 SUPPORTED_Pause |
2120 SUPPORTED_Asym_Pause);
2121 break;
2122 default:
Hauke Mehrtensead24022013-09-28 23:15:26 +02002123 phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002124 return -EINVAL;
2125 }
2126
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002127 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002128
2129 phydev->advertising = phydev->supported;
2130
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002131 return 0;
2132}
2133
2134static void tg3_phy_start(struct tg3 *tp)
2135{
2136 struct phy_device *phydev;
2137
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002138 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002139 return;
2140
Hauke Mehrtensead24022013-09-28 23:15:26 +02002141 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002142
Matt Carlson80096062010-08-02 11:26:06 +00002143 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2144 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002145 phydev->speed = tp->link_config.speed;
2146 phydev->duplex = tp->link_config.duplex;
2147 phydev->autoneg = tp->link_config.autoneg;
2148 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002149 }
2150
2151 phy_start(phydev);
2152
2153 phy_start_aneg(phydev);
2154}
2155
2156static void tg3_phy_stop(struct tg3 *tp)
2157{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002158 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002159 return;
2160
Hauke Mehrtensead24022013-09-28 23:15:26 +02002161 phy_stop(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002162}
2163
2164static void tg3_phy_fini(struct tg3 *tp)
2165{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002166 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Hauke Mehrtensead24022013-09-28 23:15:26 +02002167 phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002168 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002169 }
2170}
2171
Matt Carlson941ec902011-08-19 13:58:23 +00002172static int tg3_phy_set_extloopbk(struct tg3 *tp)
2173{
2174 int err;
2175 u32 val;
2176
2177 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2178 return 0;
2179
2180 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2181 /* Cannot do read-modify-write on 5401 */
2182 err = tg3_phy_auxctl_write(tp,
2183 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2184 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2185 0x4c20);
2186 goto done;
2187 }
2188
2189 err = tg3_phy_auxctl_read(tp,
2190 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2191 if (err)
2192 return err;
2193
2194 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2195 err = tg3_phy_auxctl_write(tp,
2196 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2197
2198done:
2199 return err;
2200}
2201
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002202static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2203{
2204 u32 phytest;
2205
2206 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2207 u32 phy;
2208
2209 tg3_writephy(tp, MII_TG3_FET_TEST,
2210 phytest | MII_TG3_FET_SHADOW_EN);
2211 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2212 if (enable)
2213 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2214 else
2215 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2216 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2217 }
2218 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2219 }
2220}
2221
Matt Carlson6833c042008-11-21 17:18:59 -08002222static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2223{
2224 u32 reg;
2225
Joe Perches63c3a662011-04-26 08:12:10 +00002226 if (!tg3_flag(tp, 5705_PLUS) ||
2227 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002228 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002229 return;
2230
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002231 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002232 tg3_phy_fet_toggle_apd(tp, enable);
2233 return;
2234 }
2235
Nithin Sujir3ab71072013-09-20 16:46:55 -07002236 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002237 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2238 MII_TG3_MISC_SHDW_SCR5_SDTL |
2239 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002240 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002241 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2242
Nithin Sujir3ab71072013-09-20 16:46:55 -07002243 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002244
2245
Nithin Sujir3ab71072013-09-20 16:46:55 -07002246 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002247 if (enable)
2248 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2249
Nithin Sujir3ab71072013-09-20 16:46:55 -07002250 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002251}
2252
Joe Perches953c96e2013-04-09 10:18:14 +00002253static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002254{
2255 u32 phy;
2256
Joe Perches63c3a662011-04-26 08:12:10 +00002257 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002258 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002259 return;
2260
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002261 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002262 u32 ephy;
2263
Matt Carlson535ef6e2009-08-25 10:09:36 +00002264 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2265 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2266
2267 tg3_writephy(tp, MII_TG3_FET_TEST,
2268 ephy | MII_TG3_FET_SHADOW_EN);
2269 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002270 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002271 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002272 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002273 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2274 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002275 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002276 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002277 }
2278 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002279 int ret;
2280
2281 ret = tg3_phy_auxctl_read(tp,
2282 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2283 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002284 if (enable)
2285 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2286 else
2287 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002288 tg3_phy_auxctl_write(tp,
2289 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002290 }
2291 }
2292}
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294static void tg3_phy_set_wirespeed(struct tg3 *tp)
2295{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002296 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 u32 val;
2298
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002299 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return;
2301
Matt Carlson15ee95c2011-04-20 07:57:40 +00002302 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2303 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002304 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2305 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002308static void tg3_phy_apply_otp(struct tg3 *tp)
2309{
2310 u32 otp, phy;
2311
2312 if (!tp->phy_otp)
2313 return;
2314
2315 otp = tp->phy_otp;
2316
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002317 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002318 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002319
2320 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2321 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2322 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2323
2324 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2325 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2326 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2327
2328 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2329 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2330 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2331
2332 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2333 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2334
2335 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2336 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2337
2338 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2339 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2340 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2341
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002342 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002343}
2344
Nithin Sujir400dfba2013-05-18 06:26:53 +00002345static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2346{
2347 u32 val;
2348 struct ethtool_eee *dest = &tp->eee;
2349
2350 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2351 return;
2352
2353 if (eee)
2354 dest = eee;
2355
2356 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2357 return;
2358
2359 /* Pull eee_active */
2360 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2361 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2362 dest->eee_active = 1;
2363 } else
2364 dest->eee_active = 0;
2365
2366 /* Pull lp advertised settings */
2367 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2368 return;
2369 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2370
2371 /* Pull advertised and eee_enabled settings */
2372 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2373 return;
2374 dest->eee_enabled = !!val;
2375 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2376
2377 /* Pull tx_lpi_enabled */
2378 val = tr32(TG3_CPMU_EEE_MODE);
2379 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2380
2381 /* Pull lpi timer value */
2382 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2383}
2384
Joe Perches953c96e2013-04-09 10:18:14 +00002385static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002386{
2387 u32 val;
2388
2389 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2390 return;
2391
2392 tp->setlpicnt = 0;
2393
2394 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002395 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002396 tp->link_config.active_duplex == DUPLEX_FULL &&
2397 (tp->link_config.active_speed == SPEED_100 ||
2398 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002399 u32 eeectl;
2400
2401 if (tp->link_config.active_speed == SPEED_1000)
2402 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2403 else
2404 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2405
2406 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2407
Nithin Sujir400dfba2013-05-18 06:26:53 +00002408 tg3_eee_pull_config(tp, NULL);
2409 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002410 tp->setlpicnt = 2;
2411 }
2412
2413 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002414 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002415 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002416 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002417 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002418 }
2419
Matt Carlson52b02d02010-10-14 10:37:41 +00002420 val = tr32(TG3_CPMU_EEE_MODE);
2421 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2422 }
2423}
2424
Matt Carlsonb0c59432011-05-19 12:12:48 +00002425static void tg3_phy_eee_enable(struct tg3 *tp)
2426{
2427 u32 val;
2428
2429 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002430 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2431 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002432 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002433 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002434 val = MII_TG3_DSP_TAP26_ALNOKO |
2435 MII_TG3_DSP_TAP26_RMRXSTO;
2436 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002437 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002438 }
2439
2440 val = tr32(TG3_CPMU_EEE_MODE);
2441 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444static int tg3_wait_macro_done(struct tg3 *tp)
2445{
2446 int limit = 100;
2447
2448 while (limit--) {
2449 u32 tmp32;
2450
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002451 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if ((tmp32 & 0x1000) == 0)
2453 break;
2454 }
2455 }
Roel Kluind4675b52009-02-12 16:33:27 -08002456 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return -EBUSY;
2458
2459 return 0;
2460}
2461
2462static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2463{
2464 static const u32 test_pat[4][6] = {
2465 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2466 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2467 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2468 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2469 };
2470 int chan;
2471
2472 for (chan = 0; chan < 4; chan++) {
2473 int i;
2474
2475 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2476 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002477 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 for (i = 0; i < 6; i++)
2480 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2481 test_pat[chan][i]);
2482
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002483 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (tg3_wait_macro_done(tp)) {
2485 *resetp = 1;
2486 return -EBUSY;
2487 }
2488
2489 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2490 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002491 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (tg3_wait_macro_done(tp)) {
2493 *resetp = 1;
2494 return -EBUSY;
2495 }
2496
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002497 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (tg3_wait_macro_done(tp)) {
2499 *resetp = 1;
2500 return -EBUSY;
2501 }
2502
2503 for (i = 0; i < 6; i += 2) {
2504 u32 low, high;
2505
2506 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2507 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2508 tg3_wait_macro_done(tp)) {
2509 *resetp = 1;
2510 return -EBUSY;
2511 }
2512 low &= 0x7fff;
2513 high &= 0x000f;
2514 if (low != test_pat[chan][i] ||
2515 high != test_pat[chan][i+1]) {
2516 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2517 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2518 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2519
2520 return -EBUSY;
2521 }
2522 }
2523 }
2524
2525 return 0;
2526}
2527
2528static int tg3_phy_reset_chanpat(struct tg3 *tp)
2529{
2530 int chan;
2531
2532 for (chan = 0; chan < 4; chan++) {
2533 int i;
2534
2535 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2536 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002537 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 for (i = 0; i < 6; i++)
2539 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002540 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (tg3_wait_macro_done(tp))
2542 return -EBUSY;
2543 }
2544
2545 return 0;
2546}
2547
2548static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2549{
2550 u32 reg32, phy9_orig;
2551 int retries, do_phy_reset, err;
2552
2553 retries = 10;
2554 do_phy_reset = 1;
2555 do {
2556 if (do_phy_reset) {
2557 err = tg3_bmcr_reset(tp);
2558 if (err)
2559 return err;
2560 do_phy_reset = 0;
2561 }
2562
2563 /* Disable transmitter and interrupt. */
2564 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2565 continue;
2566
2567 reg32 |= 0x3000;
2568 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2569
2570 /* Set full-duplex, 1000 mbps. */
2571 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002572 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002575 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 continue;
2577
Matt Carlson221c5632011-06-13 13:39:01 +00002578 tg3_writephy(tp, MII_CTRL1000,
2579 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002581 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002582 if (err)
2583 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002586 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2589 if (!err)
2590 break;
2591 } while (--retries);
2592
2593 err = tg3_phy_reset_chanpat(tp);
2594 if (err)
2595 return err;
2596
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002597 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002600 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002602 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Matt Carlson221c5632011-06-13 13:39:01 +00002604 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002606 err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
2607 if (err)
2608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002610 reg32 &= ~0x3000;
2611 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2612
2613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002616static void tg3_carrier_off(struct tg3 *tp)
2617{
2618 netif_carrier_off(tp->dev);
2619 tp->link_up = false;
2620}
2621
Nithin Sujirce20f162013-04-09 08:48:04 +00002622static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2623{
2624 if (tg3_flag(tp, ENABLE_ASF))
2625 netdev_warn(tp->dev,
2626 "Management side-band traffic will be interrupted during phy settings change\n");
2627}
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629/* This will reset the tigon3 PHY if there is no valid
2630 * link unless the FORCE argument is non-zero.
2631 */
2632static int tg3_phy_reset(struct tg3 *tp)
2633{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002634 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 int err;
2636
Joe Perches41535772013-02-16 11:20:04 +00002637 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002638 val = tr32(GRC_MISC_CFG);
2639 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2640 udelay(40);
2641 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002642 err = tg3_readphy(tp, MII_BMSR, &val);
2643 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (err != 0)
2645 return -EBUSY;
2646
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002647 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002648 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002649 tg3_link_report(tp);
2650 }
2651
Joe Perches41535772013-02-16 11:20:04 +00002652 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2653 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2654 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 err = tg3_phy_reset_5703_4_5(tp);
2656 if (err)
2657 return err;
2658 goto out;
2659 }
2660
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002661 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002662 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2663 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002664 cpmuctrl = tr32(TG3_CPMU_CTRL);
2665 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2666 tw32(TG3_CPMU_CTRL,
2667 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2668 }
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 err = tg3_bmcr_reset(tp);
2671 if (err)
2672 return err;
2673
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002674 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002675 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2676 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002677
2678 tw32(TG3_CPMU_CTRL, cpmuctrl);
2679 }
2680
Joe Perches41535772013-02-16 11:20:04 +00002681 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2682 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002683 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2684 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2685 CPMU_LSPD_1000MB_MACCLK_12_5) {
2686 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2687 udelay(40);
2688 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2689 }
2690 }
2691
Joe Perches63c3a662011-04-26 08:12:10 +00002692 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002693 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002694 return 0;
2695
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002696 tg3_phy_apply_otp(tp);
2697
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002698 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002699 tg3_phy_toggle_apd(tp, true);
2700 else
2701 tg3_phy_toggle_apd(tp, false);
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002704 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002705 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002706 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2707 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002708 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002710
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002711 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002712 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2713 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002715
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002716 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002717 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002718 tg3_phydsp_write(tp, 0x000a, 0x310b);
2719 tg3_phydsp_write(tp, 0x201f, 0x9506);
2720 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002721 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002722 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002723 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002724 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002725 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2726 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2727 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2728 tg3_writephy(tp, MII_TG3_TEST1,
2729 MII_TG3_TEST1_TRIM_EN | 0x4);
2730 } else
2731 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2732
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002733 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002734 }
Michael Chanc424cb22006-04-29 18:56:34 -07002735 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /* Set Extended packet length bit (bit 14) on all chips that */
2738 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002739 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002741 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002742 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002744 err = tg3_phy_auxctl_read(tp,
2745 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2746 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002747 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2748 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
2750
2751 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2752 * jumbo frames transmission.
2753 */
Joe Perches63c3a662011-04-26 08:12:10 +00002754 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002755 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002756 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002757 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
2759
Joe Perches41535772013-02-16 11:20:04 +00002760 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002761 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002762 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002763 }
2764
Joe Perches41535772013-02-16 11:20:04 +00002765 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002766 tg3_phydsp_write(tp, 0xffb, 0x4000);
2767
Joe Perches953c96e2013-04-09 10:18:14 +00002768 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 tg3_phy_set_wirespeed(tp);
2770 return 0;
2771}
2772
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002773#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2774#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2775#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2776 TG3_GPIO_MSG_NEED_VAUX)
2777#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2778 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2779 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2780 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2781 (TG3_GPIO_MSG_DRVR_PRES << 12))
2782
2783#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2784 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2785 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2786 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2787 (TG3_GPIO_MSG_NEED_VAUX << 12))
2788
2789static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2790{
2791 u32 status, shift;
2792
Joe Perches41535772013-02-16 11:20:04 +00002793 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2794 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002795 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2796 else
2797 status = tr32(TG3_CPMU_DRV_STATUS);
2798
2799 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2800 status &= ~(TG3_GPIO_MSG_MASK << shift);
2801 status |= (newstat << shift);
2802
Joe Perches41535772013-02-16 11:20:04 +00002803 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2804 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002805 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2806 else
2807 tw32(TG3_CPMU_DRV_STATUS, status);
2808
2809 return status >> TG3_APE_GPIO_MSG_SHIFT;
2810}
2811
Matt Carlson520b2752011-06-13 13:39:02 +00002812static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2813{
2814 if (!tg3_flag(tp, IS_NIC))
2815 return 0;
2816
Joe Perches41535772013-02-16 11:20:04 +00002817 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2818 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2819 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002820 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2821 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002822
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002823 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2824
2825 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2826 TG3_GRC_LCLCTL_PWRSW_DELAY);
2827
2828 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2829 } else {
2830 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2831 TG3_GRC_LCLCTL_PWRSW_DELAY);
2832 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002833
Matt Carlson520b2752011-06-13 13:39:02 +00002834 return 0;
2835}
2836
2837static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2838{
2839 u32 grc_local_ctrl;
2840
2841 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002842 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2843 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002844 return;
2845
2846 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2847
2848 tw32_wait_f(GRC_LOCAL_CTRL,
2849 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2850 TG3_GRC_LCLCTL_PWRSW_DELAY);
2851
2852 tw32_wait_f(GRC_LOCAL_CTRL,
2853 grc_local_ctrl,
2854 TG3_GRC_LCLCTL_PWRSW_DELAY);
2855
2856 tw32_wait_f(GRC_LOCAL_CTRL,
2857 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2858 TG3_GRC_LCLCTL_PWRSW_DELAY);
2859}
2860
2861static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2862{
2863 if (!tg3_flag(tp, IS_NIC))
2864 return;
2865
Joe Perches41535772013-02-16 11:20:04 +00002866 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2867 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002868 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2869 (GRC_LCLCTRL_GPIO_OE0 |
2870 GRC_LCLCTRL_GPIO_OE1 |
2871 GRC_LCLCTRL_GPIO_OE2 |
2872 GRC_LCLCTRL_GPIO_OUTPUT0 |
2873 GRC_LCLCTRL_GPIO_OUTPUT1),
2874 TG3_GRC_LCLCTL_PWRSW_DELAY);
2875 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2876 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2877 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2878 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2879 GRC_LCLCTRL_GPIO_OE1 |
2880 GRC_LCLCTRL_GPIO_OE2 |
2881 GRC_LCLCTRL_GPIO_OUTPUT0 |
2882 GRC_LCLCTRL_GPIO_OUTPUT1 |
2883 tp->grc_local_ctrl;
2884 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2885 TG3_GRC_LCLCTL_PWRSW_DELAY);
2886
2887 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2888 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2889 TG3_GRC_LCLCTL_PWRSW_DELAY);
2890
2891 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2892 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2893 TG3_GRC_LCLCTL_PWRSW_DELAY);
2894 } else {
2895 u32 no_gpio2;
2896 u32 grc_local_ctrl = 0;
2897
2898 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002899 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002900 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2901 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2902 grc_local_ctrl,
2903 TG3_GRC_LCLCTL_PWRSW_DELAY);
2904 }
2905
2906 /* On 5753 and variants, GPIO2 cannot be used. */
2907 no_gpio2 = tp->nic_sram_data_cfg &
2908 NIC_SRAM_DATA_CFG_NO_GPIO2;
2909
2910 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2911 GRC_LCLCTRL_GPIO_OE1 |
2912 GRC_LCLCTRL_GPIO_OE2 |
2913 GRC_LCLCTRL_GPIO_OUTPUT1 |
2914 GRC_LCLCTRL_GPIO_OUTPUT2;
2915 if (no_gpio2) {
2916 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2917 GRC_LCLCTRL_GPIO_OUTPUT2);
2918 }
2919 tw32_wait_f(GRC_LOCAL_CTRL,
2920 tp->grc_local_ctrl | grc_local_ctrl,
2921 TG3_GRC_LCLCTL_PWRSW_DELAY);
2922
2923 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2924
2925 tw32_wait_f(GRC_LOCAL_CTRL,
2926 tp->grc_local_ctrl | grc_local_ctrl,
2927 TG3_GRC_LCLCTL_PWRSW_DELAY);
2928
2929 if (!no_gpio2) {
2930 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2931 tw32_wait_f(GRC_LOCAL_CTRL,
2932 tp->grc_local_ctrl | grc_local_ctrl,
2933 TG3_GRC_LCLCTL_PWRSW_DELAY);
2934 }
2935 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002936}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002937
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002938static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002939{
2940 u32 msg = 0;
2941
2942 /* Serialize power state transitions */
2943 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2944 return;
2945
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002946 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002947 msg = TG3_GPIO_MSG_NEED_VAUX;
2948
2949 msg = tg3_set_function_status(tp, msg);
2950
2951 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2952 goto done;
2953
2954 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2955 tg3_pwrsrc_switch_to_vaux(tp);
2956 else
2957 tg3_pwrsrc_die_with_vmain(tp);
2958
2959done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002960 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002961}
2962
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002963static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Matt Carlson683644b2011-03-09 16:58:23 +00002965 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Matt Carlson334355a2010-01-20 16:58:10 +00002967 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002968 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return;
2970
Joe Perches41535772013-02-16 11:20:04 +00002971 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2972 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2973 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002974 tg3_frob_aux_power_5717(tp, include_wol ?
2975 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002976 return;
2977 }
2978
2979 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002980 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002982 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002983
Michael Chanbc1c7562006-03-20 17:48:03 -08002984 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002985 if (dev_peer) {
2986 struct tg3 *tp_peer = netdev_priv(dev_peer);
2987
Joe Perches63c3a662011-04-26 08:12:10 +00002988 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002989 return;
2990
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002991 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002992 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002993 need_vaux = true;
2994 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002997 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2998 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002999 need_vaux = true;
3000
Matt Carlson520b2752011-06-13 13:39:02 +00003001 if (need_vaux)
3002 tg3_pwrsrc_switch_to_vaux(tp);
3003 else
3004 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003007static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3008{
3009 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3010 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003011 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003012 if (speed != SPEED_10)
3013 return 1;
3014 } else if (speed == SPEED_10)
3015 return 1;
3016
3017 return 0;
3018}
3019
Nithin Sujir44f3b502013-05-13 11:04:15 +00003020static bool tg3_phy_power_bug(struct tg3 *tp)
3021{
3022 switch (tg3_asic_rev(tp)) {
3023 case ASIC_REV_5700:
3024 case ASIC_REV_5704:
3025 return true;
3026 case ASIC_REV_5780:
3027 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3028 return true;
3029 return false;
3030 case ASIC_REV_5717:
3031 if (!tp->pci_fn)
3032 return true;
3033 return false;
3034 case ASIC_REV_5719:
3035 case ASIC_REV_5720:
3036 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3037 !tp->pci_fn)
3038 return true;
3039 return false;
3040 }
3041
3042 return false;
3043}
3044
Nithin Sujir989038e2013-08-30 17:01:36 -07003045static bool tg3_phy_led_bug(struct tg3 *tp)
3046{
3047 switch (tg3_asic_rev(tp)) {
3048 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003049 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003050 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3051 !tp->pci_fn)
3052 return true;
3053 return false;
3054 }
3055
3056 return false;
3057}
3058
Matt Carlson0a459aa2008-11-03 16:54:15 -08003059static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003060{
Matt Carlsonce057f02007-11-12 21:08:03 -08003061 u32 val;
3062
Nithin Sujir942d1af2013-04-09 08:48:07 +00003063 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3064 return;
3065
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003066 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003067 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003068 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3069 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3070
3071 sg_dig_ctrl |=
3072 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3073 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3074 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3075 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003076 return;
Michael Chan51297242007-02-13 12:17:57 -08003077 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003078
Joe Perches41535772013-02-16 11:20:04 +00003079 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003080 tg3_bmcr_reset(tp);
3081 val = tr32(GRC_MISC_CFG);
3082 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3083 udelay(40);
3084 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003085 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003086 u32 phytest;
3087 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3088 u32 phy;
3089
3090 tg3_writephy(tp, MII_ADVERTISE, 0);
3091 tg3_writephy(tp, MII_BMCR,
3092 BMCR_ANENABLE | BMCR_ANRESTART);
3093
3094 tg3_writephy(tp, MII_TG3_FET_TEST,
3095 phytest | MII_TG3_FET_SHADOW_EN);
3096 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3097 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3098 tg3_writephy(tp,
3099 MII_TG3_FET_SHDW_AUXMODE4,
3100 phy);
3101 }
3102 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3103 }
3104 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003105 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003106 if (!tg3_phy_led_bug(tp))
3107 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3108 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003109
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003110 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3111 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3112 MII_TG3_AUXCTL_PCTL_VREG_11V;
3113 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003114 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003115
Michael Chan15c3b692006-03-22 01:06:52 -08003116 /* The PHY should not be powered down on some chips because
3117 * of bugs.
3118 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003119 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003120 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003121
Joe Perches41535772013-02-16 11:20:04 +00003122 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3123 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003124 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3125 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3126 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3127 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3128 }
3129
Michael Chan15c3b692006-03-22 01:06:52 -08003130 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3131}
3132
Matt Carlson3f007892008-11-03 16:51:36 -08003133/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003134static int tg3_nvram_lock(struct tg3 *tp)
3135{
Joe Perches63c3a662011-04-26 08:12:10 +00003136 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003137 int i;
3138
3139 if (tp->nvram_lock_cnt == 0) {
3140 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3141 for (i = 0; i < 8000; i++) {
3142 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3143 break;
3144 udelay(20);
3145 }
3146 if (i == 8000) {
3147 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3148 return -ENODEV;
3149 }
3150 }
3151 tp->nvram_lock_cnt++;
3152 }
3153 return 0;
3154}
3155
3156/* tp->lock is held. */
3157static void tg3_nvram_unlock(struct tg3 *tp)
3158{
Joe Perches63c3a662011-04-26 08:12:10 +00003159 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003160 if (tp->nvram_lock_cnt > 0)
3161 tp->nvram_lock_cnt--;
3162 if (tp->nvram_lock_cnt == 0)
3163 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3164 }
3165}
3166
3167/* tp->lock is held. */
3168static void tg3_enable_nvram_access(struct tg3 *tp)
3169{
Joe Perches63c3a662011-04-26 08:12:10 +00003170 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003171 u32 nvaccess = tr32(NVRAM_ACCESS);
3172
3173 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3174 }
3175}
3176
3177/* tp->lock is held. */
3178static void tg3_disable_nvram_access(struct tg3 *tp)
3179{
Joe Perches63c3a662011-04-26 08:12:10 +00003180 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003181 u32 nvaccess = tr32(NVRAM_ACCESS);
3182
3183 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3184 }
3185}
3186
3187static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3188 u32 offset, u32 *val)
3189{
3190 u32 tmp;
3191 int i;
3192
3193 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3194 return -EINVAL;
3195
3196 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3197 EEPROM_ADDR_DEVID_MASK |
3198 EEPROM_ADDR_READ);
3199 tw32(GRC_EEPROM_ADDR,
3200 tmp |
3201 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3202 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3203 EEPROM_ADDR_ADDR_MASK) |
3204 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3205
3206 for (i = 0; i < 1000; i++) {
3207 tmp = tr32(GRC_EEPROM_ADDR);
3208
3209 if (tmp & EEPROM_ADDR_COMPLETE)
3210 break;
3211 msleep(1);
3212 }
3213 if (!(tmp & EEPROM_ADDR_COMPLETE))
3214 return -EBUSY;
3215
Matt Carlson62cedd12009-04-20 14:52:29 -07003216 tmp = tr32(GRC_EEPROM_DATA);
3217
3218 /*
3219 * The data will always be opposite the native endian
3220 * format. Perform a blind byteswap to compensate.
3221 */
3222 *val = swab32(tmp);
3223
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003224 return 0;
3225}
3226
Prashant Sreedharan66c965f2014-06-20 23:28:15 -07003227#define NVRAM_CMD_TIMEOUT 5000
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003228
3229static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3230{
3231 int i;
3232
3233 tw32(NVRAM_CMD, nvram_cmd);
3234 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
Prashant Sreedharan66c965f2014-06-20 23:28:15 -07003235 usleep_range(10, 40);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003236 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3237 udelay(10);
3238 break;
3239 }
3240 }
3241
3242 if (i == NVRAM_CMD_TIMEOUT)
3243 return -EBUSY;
3244
3245 return 0;
3246}
3247
3248static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3249{
Joe Perches63c3a662011-04-26 08:12:10 +00003250 if (tg3_flag(tp, NVRAM) &&
3251 tg3_flag(tp, NVRAM_BUFFERED) &&
3252 tg3_flag(tp, FLASH) &&
3253 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003254 (tp->nvram_jedecnum == JEDEC_ATMEL))
3255
3256 addr = ((addr / tp->nvram_pagesize) <<
3257 ATMEL_AT45DB0X1B_PAGE_POS) +
3258 (addr % tp->nvram_pagesize);
3259
3260 return addr;
3261}
3262
3263static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3264{
Joe Perches63c3a662011-04-26 08:12:10 +00003265 if (tg3_flag(tp, NVRAM) &&
3266 tg3_flag(tp, NVRAM_BUFFERED) &&
3267 tg3_flag(tp, FLASH) &&
3268 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003269 (tp->nvram_jedecnum == JEDEC_ATMEL))
3270
3271 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3272 tp->nvram_pagesize) +
3273 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3274
3275 return addr;
3276}
3277
Matt Carlsone4f34112009-02-25 14:25:00 +00003278/* NOTE: Data read in from NVRAM is byteswapped according to
3279 * the byteswapping settings for all other register accesses.
3280 * tg3 devices are BE devices, so on a BE machine, the data
3281 * returned will be exactly as it is seen in NVRAM. On a LE
3282 * machine, the 32-bit value will be byteswapped.
3283 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003284static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3285{
3286 int ret;
3287
Joe Perches63c3a662011-04-26 08:12:10 +00003288 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003289 return tg3_nvram_read_using_eeprom(tp, offset, val);
3290
3291 offset = tg3_nvram_phys_addr(tp, offset);
3292
3293 if (offset > NVRAM_ADDR_MSK)
3294 return -EINVAL;
3295
3296 ret = tg3_nvram_lock(tp);
3297 if (ret)
3298 return ret;
3299
3300 tg3_enable_nvram_access(tp);
3301
3302 tw32(NVRAM_ADDR, offset);
3303 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3304 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3305
3306 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003307 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003308
3309 tg3_disable_nvram_access(tp);
3310
3311 tg3_nvram_unlock(tp);
3312
3313 return ret;
3314}
3315
Matt Carlsona9dc5292009-02-25 14:25:30 +00003316/* Ensures NVRAM data is in bytestream format. */
3317static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003318{
3319 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003320 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003321 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003322 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003323 return res;
3324}
3325
Matt Carlsondbe9b922012-02-13 10:20:09 +00003326static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3327 u32 offset, u32 len, u8 *buf)
3328{
3329 int i, j, rc = 0;
3330 u32 val;
3331
3332 for (i = 0; i < len; i += 4) {
3333 u32 addr;
3334 __be32 data;
3335
3336 addr = offset + i;
3337
3338 memcpy(&data, buf + i, 4);
3339
3340 /*
3341 * The SEEPROM interface expects the data to always be opposite
3342 * the native endian format. We accomplish this by reversing
3343 * all the operations that would have been performed on the
3344 * data from a call to tg3_nvram_read_be32().
3345 */
3346 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3347
3348 val = tr32(GRC_EEPROM_ADDR);
3349 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3350
3351 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3352 EEPROM_ADDR_READ);
3353 tw32(GRC_EEPROM_ADDR, val |
3354 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3355 (addr & EEPROM_ADDR_ADDR_MASK) |
3356 EEPROM_ADDR_START |
3357 EEPROM_ADDR_WRITE);
3358
3359 for (j = 0; j < 1000; j++) {
3360 val = tr32(GRC_EEPROM_ADDR);
3361
3362 if (val & EEPROM_ADDR_COMPLETE)
3363 break;
3364 msleep(1);
3365 }
3366 if (!(val & EEPROM_ADDR_COMPLETE)) {
3367 rc = -EBUSY;
3368 break;
3369 }
3370 }
3371
3372 return rc;
3373}
3374
3375/* offset and length are dword aligned */
3376static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3377 u8 *buf)
3378{
3379 int ret = 0;
3380 u32 pagesize = tp->nvram_pagesize;
3381 u32 pagemask = pagesize - 1;
3382 u32 nvram_cmd;
3383 u8 *tmp;
3384
3385 tmp = kmalloc(pagesize, GFP_KERNEL);
3386 if (tmp == NULL)
3387 return -ENOMEM;
3388
3389 while (len) {
3390 int j;
3391 u32 phy_addr, page_off, size;
3392
3393 phy_addr = offset & ~pagemask;
3394
3395 for (j = 0; j < pagesize; j += 4) {
3396 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3397 (__be32 *) (tmp + j));
3398 if (ret)
3399 break;
3400 }
3401 if (ret)
3402 break;
3403
3404 page_off = offset & pagemask;
3405 size = pagesize;
3406 if (len < size)
3407 size = len;
3408
3409 len -= size;
3410
3411 memcpy(tmp + page_off, buf, size);
3412
3413 offset = offset + (pagesize - page_off);
3414
3415 tg3_enable_nvram_access(tp);
3416
3417 /*
3418 * Before we can erase the flash page, we need
3419 * to issue a special "write enable" command.
3420 */
3421 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3422
3423 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3424 break;
3425
3426 /* Erase the target page */
3427 tw32(NVRAM_ADDR, phy_addr);
3428
3429 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3430 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3431
3432 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3433 break;
3434
3435 /* Issue another write enable to start the write. */
3436 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3437
3438 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3439 break;
3440
3441 for (j = 0; j < pagesize; j += 4) {
3442 __be32 data;
3443
3444 data = *((__be32 *) (tmp + j));
3445
3446 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3447
3448 tw32(NVRAM_ADDR, phy_addr + j);
3449
3450 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3451 NVRAM_CMD_WR;
3452
3453 if (j == 0)
3454 nvram_cmd |= NVRAM_CMD_FIRST;
3455 else if (j == (pagesize - 4))
3456 nvram_cmd |= NVRAM_CMD_LAST;
3457
3458 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3459 if (ret)
3460 break;
3461 }
3462 if (ret)
3463 break;
3464 }
3465
3466 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3467 tg3_nvram_exec_cmd(tp, nvram_cmd);
3468
3469 kfree(tmp);
3470
3471 return ret;
3472}
3473
3474/* offset and length are dword aligned */
3475static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3476 u8 *buf)
3477{
3478 int i, ret = 0;
3479
3480 for (i = 0; i < len; i += 4, offset += 4) {
3481 u32 page_off, phy_addr, nvram_cmd;
3482 __be32 data;
3483
3484 memcpy(&data, buf + i, 4);
3485 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3486
3487 page_off = offset % tp->nvram_pagesize;
3488
3489 phy_addr = tg3_nvram_phys_addr(tp, offset);
3490
Matt Carlsondbe9b922012-02-13 10:20:09 +00003491 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3492
3493 if (page_off == 0 || i == 0)
3494 nvram_cmd |= NVRAM_CMD_FIRST;
3495 if (page_off == (tp->nvram_pagesize - 4))
3496 nvram_cmd |= NVRAM_CMD_LAST;
3497
3498 if (i == (len - 4))
3499 nvram_cmd |= NVRAM_CMD_LAST;
3500
Matt Carlson42278222012-02-13 15:20:11 +00003501 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3502 !tg3_flag(tp, FLASH) ||
3503 !tg3_flag(tp, 57765_PLUS))
3504 tw32(NVRAM_ADDR, phy_addr);
3505
Joe Perches41535772013-02-16 11:20:04 +00003506 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003507 !tg3_flag(tp, 5755_PLUS) &&
3508 (tp->nvram_jedecnum == JEDEC_ST) &&
3509 (nvram_cmd & NVRAM_CMD_FIRST)) {
3510 u32 cmd;
3511
3512 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3513 ret = tg3_nvram_exec_cmd(tp, cmd);
3514 if (ret)
3515 break;
3516 }
3517 if (!tg3_flag(tp, FLASH)) {
3518 /* We always do complete word writes to eeprom. */
3519 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3520 }
3521
3522 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3523 if (ret)
3524 break;
3525 }
3526 return ret;
3527}
3528
3529/* offset and length are dword aligned */
3530static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3531{
3532 int ret;
3533
3534 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3535 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3536 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3537 udelay(40);
3538 }
3539
3540 if (!tg3_flag(tp, NVRAM)) {
3541 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3542 } else {
3543 u32 grc_mode;
3544
3545 ret = tg3_nvram_lock(tp);
3546 if (ret)
3547 return ret;
3548
3549 tg3_enable_nvram_access(tp);
3550 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3551 tw32(NVRAM_WRITE1, 0x406);
3552
3553 grc_mode = tr32(GRC_MODE);
3554 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3555
3556 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3557 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3558 buf);
3559 } else {
3560 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3561 buf);
3562 }
3563
3564 grc_mode = tr32(GRC_MODE);
3565 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3566
3567 tg3_disable_nvram_access(tp);
3568 tg3_nvram_unlock(tp);
3569 }
3570
3571 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3572 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3573 udelay(40);
3574 }
3575
3576 return ret;
3577}
3578
Matt Carlson997b4f12011-08-31 11:44:53 +00003579#define RX_CPU_SCRATCH_BASE 0x30000
3580#define RX_CPU_SCRATCH_SIZE 0x04000
3581#define TX_CPU_SCRATCH_BASE 0x34000
3582#define TX_CPU_SCRATCH_SIZE 0x04000
3583
3584/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003585static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003586{
3587 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003588 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003589
Nithin Sujir837c45b2013-03-06 17:02:30 +00003590 for (i = 0; i < iters; i++) {
3591 tw32(cpu_base + CPU_STATE, 0xffffffff);
3592 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3593 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3594 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003595 if (pci_channel_offline(tp->pdev))
3596 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003597 }
3598
3599 return (i == iters) ? -EBUSY : 0;
3600}
3601
3602/* tp->lock is held. */
3603static int tg3_rxcpu_pause(struct tg3 *tp)
3604{
3605 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3606
3607 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3608 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3609 udelay(10);
3610
3611 return rc;
3612}
3613
3614/* tp->lock is held. */
3615static int tg3_txcpu_pause(struct tg3 *tp)
3616{
3617 return tg3_pause_cpu(tp, TX_CPU_BASE);
3618}
3619
3620/* tp->lock is held. */
3621static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3622{
3623 tw32(cpu_base + CPU_STATE, 0xffffffff);
3624 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3625}
3626
3627/* tp->lock is held. */
3628static void tg3_rxcpu_resume(struct tg3 *tp)
3629{
3630 tg3_resume_cpu(tp, RX_CPU_BASE);
3631}
3632
3633/* tp->lock is held. */
3634static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3635{
3636 int rc;
3637
3638 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003639
Joe Perches41535772013-02-16 11:20:04 +00003640 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003641 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3642
3643 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3644 return 0;
3645 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003646 if (cpu_base == RX_CPU_BASE) {
3647 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003648 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003649 /*
3650 * There is only an Rx CPU for the 5750 derivative in the
3651 * BCM4785.
3652 */
3653 if (tg3_flag(tp, IS_SSB_CORE))
3654 return 0;
3655
Nithin Sujir837c45b2013-03-06 17:02:30 +00003656 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003657 }
3658
Nithin Sujir837c45b2013-03-06 17:02:30 +00003659 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003660 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003661 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003662 return -ENODEV;
3663 }
3664
3665 /* Clear firmware's nvram arbitration. */
3666 if (tg3_flag(tp, NVRAM))
3667 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3668 return 0;
3669}
3670
Nithin Sujir31f11a92013-03-06 17:02:33 +00003671static int tg3_fw_data_len(struct tg3 *tp,
3672 const struct tg3_firmware_hdr *fw_hdr)
3673{
3674 int fw_len;
3675
3676 /* Non fragmented firmware have one firmware header followed by a
3677 * contiguous chunk of data to be written. The length field in that
3678 * header is not the length of data to be written but the complete
3679 * length of the bss. The data length is determined based on
3680 * tp->fw->size minus headers.
3681 *
3682 * Fragmented firmware have a main header followed by multiple
3683 * fragments. Each fragment is identical to non fragmented firmware
3684 * with a firmware header followed by a contiguous chunk of data. In
3685 * the main header, the length field is unused and set to 0xffffffff.
3686 * In each fragment header the length is the entire size of that
3687 * fragment i.e. fragment data + header length. Data length is
3688 * therefore length field in the header minus TG3_FW_HDR_LEN.
3689 */
3690 if (tp->fw_len == 0xffffffff)
3691 fw_len = be32_to_cpu(fw_hdr->len);
3692 else
3693 fw_len = tp->fw->size;
3694
3695 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3696}
3697
Matt Carlson997b4f12011-08-31 11:44:53 +00003698/* tp->lock is held. */
3699static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3700 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003701 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003702{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003703 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003704 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003705 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003706
3707 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3708 netdev_err(tp->dev,
3709 "%s: Trying to load TX cpu firmware which is 5705\n",
3710 __func__);
3711 return -EINVAL;
3712 }
3713
Nithin Sujirc4dab502013-03-06 17:02:34 +00003714 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003715 write_op = tg3_write_mem;
3716 else
3717 write_op = tg3_write_indirect_reg32;
3718
Nithin Sujirc4dab502013-03-06 17:02:34 +00003719 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3720 /* It is possible that bootcode is still loading at this point.
3721 * Get the nvram lock first before halting the cpu.
3722 */
3723 int lock_err = tg3_nvram_lock(tp);
3724 err = tg3_halt_cpu(tp, cpu_base);
3725 if (!lock_err)
3726 tg3_nvram_unlock(tp);
3727 if (err)
3728 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003729
Nithin Sujirc4dab502013-03-06 17:02:34 +00003730 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3731 write_op(tp, cpu_scratch_base + i, 0);
3732 tw32(cpu_base + CPU_STATE, 0xffffffff);
3733 tw32(cpu_base + CPU_MODE,
3734 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3735 } else {
3736 /* Subtract additional main header for fragmented firmware and
3737 * advance to the first fragment
3738 */
3739 total_len -= TG3_FW_HDR_LEN;
3740 fw_hdr++;
3741 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003742
Nithin Sujir31f11a92013-03-06 17:02:33 +00003743 do {
3744 u32 *fw_data = (u32 *)(fw_hdr + 1);
3745 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3746 write_op(tp, cpu_scratch_base +
3747 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3748 (i * sizeof(u32)),
3749 be32_to_cpu(fw_data[i]));
3750
3751 total_len -= be32_to_cpu(fw_hdr->len);
3752
3753 /* Advance to next fragment */
3754 fw_hdr = (struct tg3_firmware_hdr *)
3755 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3756 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003757
3758 err = 0;
3759
3760out:
3761 return err;
3762}
3763
3764/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003765static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3766{
3767 int i;
3768 const int iters = 5;
3769
3770 tw32(cpu_base + CPU_STATE, 0xffffffff);
3771 tw32_f(cpu_base + CPU_PC, pc);
3772
3773 for (i = 0; i < iters; i++) {
3774 if (tr32(cpu_base + CPU_PC) == pc)
3775 break;
3776 tw32(cpu_base + CPU_STATE, 0xffffffff);
3777 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3778 tw32_f(cpu_base + CPU_PC, pc);
3779 udelay(1000);
3780 }
3781
3782 return (i == iters) ? -EBUSY : 0;
3783}
3784
3785/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003786static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3787{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003788 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003789 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003790
Nithin Sujir77997ea2013-03-06 17:02:32 +00003791 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003792
3793 /* Firmware blob starts with version numbers, followed by
3794 start address and length. We are setting complete length.
3795 length = end_address_of_bss - start_address_of_text.
3796 Remainder is the blob to be loaded contiguously
3797 from start address. */
3798
Matt Carlson997b4f12011-08-31 11:44:53 +00003799 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3800 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003801 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003802 if (err)
3803 return err;
3804
3805 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3806 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003807 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003808 if (err)
3809 return err;
3810
3811 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003812 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3813 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003814 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003815 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3816 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003817 tr32(RX_CPU_BASE + CPU_PC),
3818 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003819 return -ENODEV;
3820 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003821
3822 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003823
3824 return 0;
3825}
3826
Nithin Sujirc4dab502013-03-06 17:02:34 +00003827static int tg3_validate_rxcpu_state(struct tg3 *tp)
3828{
3829 const int iters = 1000;
3830 int i;
3831 u32 val;
3832
3833 /* Wait for boot code to complete initialization and enter service
3834 * loop. It is then safe to download service patches
3835 */
3836 for (i = 0; i < iters; i++) {
3837 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3838 break;
3839
3840 udelay(10);
3841 }
3842
3843 if (i == iters) {
3844 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3845 return -EBUSY;
3846 }
3847
3848 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3849 if (val & 0xff) {
3850 netdev_warn(tp->dev,
3851 "Other patches exist. Not downloading EEE patch\n");
3852 return -EEXIST;
3853 }
3854
3855 return 0;
3856}
3857
3858/* tp->lock is held. */
3859static void tg3_load_57766_firmware(struct tg3 *tp)
3860{
3861 struct tg3_firmware_hdr *fw_hdr;
3862
3863 if (!tg3_flag(tp, NO_NVRAM))
3864 return;
3865
3866 if (tg3_validate_rxcpu_state(tp))
3867 return;
3868
3869 if (!tp->fw)
3870 return;
3871
3872 /* This firmware blob has a different format than older firmware
3873 * releases as given below. The main difference is we have fragmented
3874 * data to be written to non-contiguous locations.
3875 *
3876 * In the beginning we have a firmware header identical to other
3877 * firmware which consists of version, base addr and length. The length
3878 * here is unused and set to 0xffffffff.
3879 *
3880 * This is followed by a series of firmware fragments which are
3881 * individually identical to previous firmware. i.e. they have the
3882 * firmware header and followed by data for that fragment. The version
3883 * field of the individual fragment header is unused.
3884 */
3885
3886 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3887 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3888 return;
3889
3890 if (tg3_rxcpu_pause(tp))
3891 return;
3892
3893 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3894 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3895
3896 tg3_rxcpu_resume(tp);
3897}
3898
Matt Carlson997b4f12011-08-31 11:44:53 +00003899/* tp->lock is held. */
3900static int tg3_load_tso_firmware(struct tg3 *tp)
3901{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003902 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003903 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003904 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003905
Matt Carlson1caf13e2013-03-06 17:02:29 +00003906 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003907 return 0;
3908
Nithin Sujir77997ea2013-03-06 17:02:32 +00003909 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003910
3911 /* Firmware blob starts with version numbers, followed by
3912 start address and length. We are setting complete length.
3913 length = end_address_of_bss - start_address_of_text.
3914 Remainder is the blob to be loaded contiguously
3915 from start address. */
3916
Matt Carlson997b4f12011-08-31 11:44:53 +00003917 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003918
Joe Perches41535772013-02-16 11:20:04 +00003919 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003920 cpu_base = RX_CPU_BASE;
3921 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3922 } else {
3923 cpu_base = TX_CPU_BASE;
3924 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3925 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3926 }
3927
3928 err = tg3_load_firmware_cpu(tp, cpu_base,
3929 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003930 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003931 if (err)
3932 return err;
3933
3934 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003935 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3936 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003937 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003938 netdev_err(tp->dev,
3939 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003940 __func__, tr32(cpu_base + CPU_PC),
3941 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003942 return -ENODEV;
3943 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003944
3945 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003946 return 0;
3947}
3948
Michael Chanf022ae62014-01-03 10:09:11 -08003949/* tp->lock is held. */
3950static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
3951{
3952 u32 addr_high, addr_low;
3953
3954 addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
3955 addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
3956 (mac_addr[4] << 8) | mac_addr[5]);
3957
3958 if (index < 4) {
3959 tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
3960 tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
3961 } else {
3962 index -= 4;
3963 tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
3964 tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
3965 }
3966}
Matt Carlson997b4f12011-08-31 11:44:53 +00003967
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003968/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003969static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003970{
Michael Chanf022ae62014-01-03 10:09:11 -08003971 u32 addr_high;
Matt Carlson3f007892008-11-03 16:51:36 -08003972 int i;
3973
Matt Carlson3f007892008-11-03 16:51:36 -08003974 for (i = 0; i < 4; i++) {
3975 if (i == 1 && skip_mac_1)
3976 continue;
Michael Chanf022ae62014-01-03 10:09:11 -08003977 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003978 }
3979
Joe Perches41535772013-02-16 11:20:04 +00003980 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3981 tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chanf022ae62014-01-03 10:09:11 -08003982 for (i = 4; i < 16; i++)
3983 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003984 }
3985
3986 addr_high = (tp->dev->dev_addr[0] +
3987 tp->dev->dev_addr[1] +
3988 tp->dev->dev_addr[2] +
3989 tp->dev->dev_addr[3] +
3990 tp->dev->dev_addr[4] +
3991 tp->dev->dev_addr[5]) &
3992 TX_BACKOFF_SEED_MASK;
3993 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3994}
3995
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003996static void tg3_enable_register_access(struct tg3 *tp)
3997{
3998 /*
3999 * Make sure register accesses (indirect or otherwise) will function
4000 * correctly.
4001 */
4002 pci_write_config_dword(tp->pdev,
4003 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
4004}
4005
4006static int tg3_power_up(struct tg3 *tp)
4007{
Matt Carlsonbed98292011-07-13 09:27:29 +00004008 int err;
4009
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004010 tg3_enable_register_access(tp);
4011
Matt Carlsonbed98292011-07-13 09:27:29 +00004012 err = pci_set_power_state(tp->pdev, PCI_D0);
4013 if (!err) {
4014 /* Switch out of Vaux if it is a NIC */
4015 tg3_pwrsrc_switch_to_vmain(tp);
4016 } else {
4017 netdev_err(tp->dev, "Transition to D0 failed\n");
4018 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004019
Matt Carlsonbed98292011-07-13 09:27:29 +00004020 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004021}
4022
Joe Perches953c96e2013-04-09 10:18:14 +00004023static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004024
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004025static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
4027 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004028 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004030 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004031
4032 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004033 if (tg3_flag(tp, CLKREQ_BUG))
4034 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4035 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4038 tw32(TG3PCI_MISC_HOST_CTRL,
4039 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4040
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004041 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004042 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004043
Joe Perches63c3a662011-04-26 08:12:10 +00004044 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004045 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004046 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00004047 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004048 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004049 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004050
Hauke Mehrtensead24022013-09-28 23:15:26 +02004051 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004052
Matt Carlson80096062010-08-02 11:26:06 +00004053 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004054
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004055 tp->link_config.speed = phydev->speed;
4056 tp->link_config.duplex = phydev->duplex;
4057 tp->link_config.autoneg = phydev->autoneg;
4058 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004059
4060 advertising = ADVERTISED_TP |
4061 ADVERTISED_Pause |
4062 ADVERTISED_Autoneg |
4063 ADVERTISED_10baseT_Half;
4064
Joe Perches63c3a662011-04-26 08:12:10 +00004065 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4066 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004067 advertising |=
4068 ADVERTISED_100baseT_Half |
4069 ADVERTISED_100baseT_Full |
4070 ADVERTISED_10baseT_Full;
4071 else
4072 advertising |= ADVERTISED_10baseT_Full;
4073 }
4074
4075 phydev->advertising = advertising;
4076
4077 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004078
4079 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004080 if (phyid != PHY_ID_BCMAC131) {
4081 phyid &= PHY_BCM_OUI_MASK;
4082 if (phyid == PHY_BCM_OUI_1 ||
4083 phyid == PHY_BCM_OUI_2 ||
4084 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004085 do_low_power = true;
4086 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004087 }
Matt Carlsondd477002008-05-25 23:45:58 -07004088 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004089 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004090
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004091 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004092 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Matt Carlson2855b9f2012-02-13 15:20:14 +00004094 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004095 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 }
4097
Joe Perches41535772013-02-16 11:20:04 +00004098 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004099 u32 val;
4100
4101 val = tr32(GRC_VCPU_EXT_CTRL);
4102 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004103 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004104 int i;
4105 u32 val;
4106
4107 for (i = 0; i < 200; i++) {
4108 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4109 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4110 break;
4111 msleep(1);
4112 }
4113 }
Joe Perches63c3a662011-04-26 08:12:10 +00004114 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004115 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4116 WOL_DRV_STATE_SHUTDOWN |
4117 WOL_DRV_WOL |
4118 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004119
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004120 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 u32 mac_mode;
4122
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004123 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004124 if (do_low_power &&
4125 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4126 tg3_phy_auxctl_write(tp,
4127 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4128 MII_TG3_AUXCTL_PCTL_WOL_EN |
4129 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4130 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004131 udelay(40);
4132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004134 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004135 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004136 else if (tp->phy_flags &
4137 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4138 if (tp->link_config.active_speed == SPEED_1000)
4139 mac_mode = MAC_MODE_PORT_MODE_GMII;
4140 else
4141 mac_mode = MAC_MODE_PORT_MODE_MII;
4142 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004143 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004145 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004146 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004147 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004148 SPEED_100 : SPEED_10;
4149 if (tg3_5700_link_polarity(tp, speed))
4150 mac_mode |= MAC_MODE_LINK_POLARITY;
4151 else
4152 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 } else {
4155 mac_mode = MAC_MODE_PORT_MODE_TBI;
4156 }
4157
Joe Perches63c3a662011-04-26 08:12:10 +00004158 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 tw32(MAC_LED_CTRL, tp->led_ctrl);
4160
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004161 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004162 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4163 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004164 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Joe Perches63c3a662011-04-26 08:12:10 +00004166 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004167 mac_mode |= MAC_MODE_APE_TX_EN |
4168 MAC_MODE_APE_RX_EN |
4169 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 tw32_f(MAC_MODE, mac_mode);
4172 udelay(100);
4173
4174 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4175 udelay(10);
4176 }
4177
Joe Perches63c3a662011-04-26 08:12:10 +00004178 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004179 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4180 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 u32 base_val;
4182
4183 base_val = tp->pci_clock_ctrl;
4184 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4185 CLOCK_CTRL_TXCLK_DISABLE);
4186
Michael Chanb401e9e2005-12-19 16:27:04 -08004187 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4188 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004189 } else if (tg3_flag(tp, 5780_CLASS) ||
4190 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004191 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004192 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004193 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 u32 newbits1, newbits2;
4195
Joe Perches41535772013-02-16 11:20:04 +00004196 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4197 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4199 CLOCK_CTRL_TXCLK_DISABLE |
4200 CLOCK_CTRL_ALTCLK);
4201 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004202 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 newbits1 = CLOCK_CTRL_625_CORE;
4204 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4205 } else {
4206 newbits1 = CLOCK_CTRL_ALTCLK;
4207 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4208 }
4209
Michael Chanb401e9e2005-12-19 16:27:04 -08004210 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4211 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Michael Chanb401e9e2005-12-19 16:27:04 -08004213 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4214 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Joe Perches63c3a662011-04-26 08:12:10 +00004216 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 u32 newbits3;
4218
Joe Perches41535772013-02-16 11:20:04 +00004219 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4220 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4222 CLOCK_CTRL_TXCLK_DISABLE |
4223 CLOCK_CTRL_44MHZ_CORE);
4224 } else {
4225 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4226 }
4227
Michael Chanb401e9e2005-12-19 16:27:04 -08004228 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4229 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 }
4231 }
4232
Joe Perches63c3a662011-04-26 08:12:10 +00004233 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004234 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004235
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004236 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
4238 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004239 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004240 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4241 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 u32 val = tr32(0x7d00);
4243
4244 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4245 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004246 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004247 int err;
4248
4249 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004251 if (!err)
4252 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 }
4255
Michael Chanbbadf502006-04-06 21:46:34 -07004256 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4257
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004258 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 return 0;
4261}
4262
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004263static void tg3_power_down(struct tg3 *tp)
4264{
Joe Perches63c3a662011-04-26 08:12:10 +00004265 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004266 pci_set_power_state(tp->pdev, PCI_D3hot);
4267}
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4270{
4271 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4272 case MII_TG3_AUX_STAT_10HALF:
4273 *speed = SPEED_10;
4274 *duplex = DUPLEX_HALF;
4275 break;
4276
4277 case MII_TG3_AUX_STAT_10FULL:
4278 *speed = SPEED_10;
4279 *duplex = DUPLEX_FULL;
4280 break;
4281
4282 case MII_TG3_AUX_STAT_100HALF:
4283 *speed = SPEED_100;
4284 *duplex = DUPLEX_HALF;
4285 break;
4286
4287 case MII_TG3_AUX_STAT_100FULL:
4288 *speed = SPEED_100;
4289 *duplex = DUPLEX_FULL;
4290 break;
4291
4292 case MII_TG3_AUX_STAT_1000HALF:
4293 *speed = SPEED_1000;
4294 *duplex = DUPLEX_HALF;
4295 break;
4296
4297 case MII_TG3_AUX_STAT_1000FULL:
4298 *speed = SPEED_1000;
4299 *duplex = DUPLEX_FULL;
4300 break;
4301
4302 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004303 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004304 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4305 SPEED_10;
4306 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4307 DUPLEX_HALF;
4308 break;
4309 }
Matt Carlsone7405222012-02-13 15:20:16 +00004310 *speed = SPEED_UNKNOWN;
4311 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
Matt Carlson42b64a42011-05-19 12:12:49 +00004316static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
Matt Carlson42b64a42011-05-19 12:12:49 +00004318 int err = 0;
4319 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
Matt Carlson42b64a42011-05-19 12:12:49 +00004321 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004322 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004323 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Matt Carlson42b64a42011-05-19 12:12:49 +00004325 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4326 if (err)
4327 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Matt Carlson4f272092011-12-14 11:09:57 +00004329 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4330 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004331
Joe Perches41535772013-02-16 11:20:04 +00004332 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4333 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004334 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004335
Matt Carlson4f272092011-12-14 11:09:57 +00004336 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4337 if (err)
4338 goto done;
4339 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004340
Matt Carlson42b64a42011-05-19 12:12:49 +00004341 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4342 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Matt Carlson42b64a42011-05-19 12:12:49 +00004344 tw32(TG3_CPMU_EEE_MODE,
4345 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004346
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004347 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004348 if (!err) {
4349 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004350
Matt Carlsona6b68da2010-12-06 08:28:52 +00004351 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004352 /* Advertise 100-BaseTX EEE ability */
4353 if (advertise & ADVERTISED_100baseT_Full)
4354 val |= MDIO_AN_EEE_ADV_100TX;
4355 /* Advertise 1000-BaseT EEE ability */
4356 if (advertise & ADVERTISED_1000baseT_Full)
4357 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004358
4359 if (!tp->eee.eee_enabled) {
4360 val = 0;
4361 tp->eee.advertised = 0;
4362 } else {
4363 tp->eee.advertised = advertise &
4364 (ADVERTISED_100baseT_Full |
4365 ADVERTISED_1000baseT_Full);
4366 }
4367
Matt Carlson42b64a42011-05-19 12:12:49 +00004368 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004369 if (err)
4370 val = 0;
4371
Joe Perches41535772013-02-16 11:20:04 +00004372 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004373 case ASIC_REV_5717:
4374 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004375 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004376 case ASIC_REV_5719:
4377 /* If we advertised any eee advertisements above... */
4378 if (val)
4379 val = MII_TG3_DSP_TAP26_ALNOKO |
4380 MII_TG3_DSP_TAP26_RMRXSTO |
4381 MII_TG3_DSP_TAP26_OPCSINPT;
4382 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4383 /* Fall through */
4384 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004385 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004386 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4387 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4388 MII_TG3_DSP_CH34TP2_HIBW01);
4389 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004390
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004391 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004392 if (!err)
4393 err = err2;
4394 }
4395
4396done:
4397 return err;
4398}
4399
4400static void tg3_phy_copper_begin(struct tg3 *tp)
4401{
Matt Carlsond13ba512012-02-22 12:35:19 +00004402 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4403 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4404 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004405
Nithin Sujir942d1af2013-04-09 08:48:07 +00004406 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4407 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004408 adv = ADVERTISED_10baseT_Half |
4409 ADVERTISED_10baseT_Full;
4410 if (tg3_flag(tp, WOL_SPEED_100MB))
4411 adv |= ADVERTISED_100baseT_Half |
4412 ADVERTISED_100baseT_Full;
Nithin Sujir7c786062013-12-06 09:53:17 -08004413 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
4414 if (!(tp->phy_flags &
4415 TG3_PHYFLG_DISABLE_1G_HD_ADV))
4416 adv |= ADVERTISED_1000baseT_Half;
4417 adv |= ADVERTISED_1000baseT_Full;
4418 }
Matt Carlson42b64a42011-05-19 12:12:49 +00004419
Matt Carlsond13ba512012-02-22 12:35:19 +00004420 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004421 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004422 adv = tp->link_config.advertising;
4423 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4424 adv &= ~(ADVERTISED_1000baseT_Half |
4425 ADVERTISED_1000baseT_Full);
4426
4427 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004428 }
4429
Matt Carlsond13ba512012-02-22 12:35:19 +00004430 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004431
Nithin Sujir942d1af2013-04-09 08:48:07 +00004432 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4433 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4434 /* Normally during power down we want to autonegotiate
4435 * the lowest possible speed for WOL. However, to avoid
4436 * link flap, we leave it untouched.
4437 */
4438 return;
4439 }
4440
Matt Carlsond13ba512012-02-22 12:35:19 +00004441 tg3_writephy(tp, MII_BMCR,
4442 BMCR_ANENABLE | BMCR_ANRESTART);
4443 } else {
4444 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 u32 bmcr, orig_bmcr;
4446
4447 tp->link_config.active_speed = tp->link_config.speed;
4448 tp->link_config.active_duplex = tp->link_config.duplex;
4449
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004450 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4451 /* With autoneg disabled, 5715 only links up when the
4452 * advertisement register has the configured speed
4453 * enabled.
4454 */
4455 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4456 }
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 bmcr = 0;
4459 switch (tp->link_config.speed) {
4460 default:
4461 case SPEED_10:
4462 break;
4463
4464 case SPEED_100:
4465 bmcr |= BMCR_SPEED100;
4466 break;
4467
4468 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004469 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
4473 if (tp->link_config.duplex == DUPLEX_FULL)
4474 bmcr |= BMCR_FULLDPLX;
4475
4476 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4477 (bmcr != orig_bmcr)) {
4478 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4479 for (i = 0; i < 1500; i++) {
4480 u32 tmp;
4481
4482 udelay(10);
4483 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4484 tg3_readphy(tp, MII_BMSR, &tmp))
4485 continue;
4486 if (!(tmp & BMSR_LSTATUS)) {
4487 udelay(40);
4488 break;
4489 }
4490 }
4491 tg3_writephy(tp, MII_BMCR, bmcr);
4492 udelay(40);
4493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 }
4495}
4496
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004497static int tg3_phy_pull_config(struct tg3 *tp)
4498{
4499 int err;
4500 u32 val;
4501
4502 err = tg3_readphy(tp, MII_BMCR, &val);
4503 if (err)
4504 goto done;
4505
4506 if (!(val & BMCR_ANENABLE)) {
4507 tp->link_config.autoneg = AUTONEG_DISABLE;
4508 tp->link_config.advertising = 0;
4509 tg3_flag_clear(tp, PAUSE_AUTONEG);
4510
4511 err = -EIO;
4512
4513 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4514 case 0:
4515 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4516 goto done;
4517
4518 tp->link_config.speed = SPEED_10;
4519 break;
4520 case BMCR_SPEED100:
4521 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4522 goto done;
4523
4524 tp->link_config.speed = SPEED_100;
4525 break;
4526 case BMCR_SPEED1000:
4527 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4528 tp->link_config.speed = SPEED_1000;
4529 break;
4530 }
4531 /* Fall through */
4532 default:
4533 goto done;
4534 }
4535
4536 if (val & BMCR_FULLDPLX)
4537 tp->link_config.duplex = DUPLEX_FULL;
4538 else
4539 tp->link_config.duplex = DUPLEX_HALF;
4540
4541 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4542
4543 err = 0;
4544 goto done;
4545 }
4546
4547 tp->link_config.autoneg = AUTONEG_ENABLE;
4548 tp->link_config.advertising = ADVERTISED_Autoneg;
4549 tg3_flag_set(tp, PAUSE_AUTONEG);
4550
4551 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4552 u32 adv;
4553
4554 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4555 if (err)
4556 goto done;
4557
4558 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4559 tp->link_config.advertising |= adv | ADVERTISED_TP;
4560
4561 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4562 } else {
4563 tp->link_config.advertising |= ADVERTISED_FIBRE;
4564 }
4565
4566 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4567 u32 adv;
4568
4569 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4570 err = tg3_readphy(tp, MII_CTRL1000, &val);
4571 if (err)
4572 goto done;
4573
4574 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4575 } else {
4576 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4577 if (err)
4578 goto done;
4579
4580 adv = tg3_decode_flowctrl_1000X(val);
4581 tp->link_config.flowctrl = adv;
4582
4583 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4584 adv = mii_adv_to_ethtool_adv_x(val);
4585 }
4586
4587 tp->link_config.advertising |= adv;
4588 }
4589
4590done:
4591 return err;
4592}
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594static int tg3_init_5401phy_dsp(struct tg3 *tp)
4595{
4596 int err;
4597
4598 /* Turn off tap power management. */
4599 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004600 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004602 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4603 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4604 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4605 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4606 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
4608 udelay(40);
4609
4610 return err;
4611}
4612
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004613static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4614{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004615 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004616
4617 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4618 return true;
4619
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004620 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004621
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004622 if (tp->eee.eee_enabled) {
4623 if (tp->eee.advertised != eee.advertised ||
4624 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4625 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4626 return false;
4627 } else {
4628 /* EEE is disabled but we're advertising */
4629 if (eee.advertised)
4630 return false;
4631 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004632
4633 return true;
4634}
4635
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004636static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004638 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004639
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004640 advertising = tp->link_config.advertising;
4641 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004643 advmsk = ADVERTISE_ALL;
4644 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004645 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004646 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004649 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4650 return false;
4651
4652 if ((*lcladv & advmsk) != tgtadv)
4653 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004654
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004655 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 u32 tg3_ctrl;
4657
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004658 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004659
Matt Carlson221c5632011-06-13 13:39:01 +00004660 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004661 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Matt Carlson3198e072012-02-13 15:20:10 +00004663 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004664 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4665 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004666 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4667 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4668 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4669 } else {
4670 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4671 }
4672
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004673 if (tg3_ctrl != tgtadv)
4674 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004676
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004677 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004678}
4679
Matt Carlson859edb22011-12-08 14:40:16 +00004680static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4681{
4682 u32 lpeth = 0;
4683
4684 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4685 u32 val;
4686
4687 if (tg3_readphy(tp, MII_STAT1000, &val))
4688 return false;
4689
4690 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4691 }
4692
4693 if (tg3_readphy(tp, MII_LPA, rmtadv))
4694 return false;
4695
4696 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4697 tp->link_config.rmt_adv = lpeth;
4698
4699 return true;
4700}
4701
Joe Perches953c96e2013-04-09 10:18:14 +00004702static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004703{
4704 if (curr_link_up != tp->link_up) {
4705 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004706 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004707 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004708 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004709 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4710 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4711 }
4712
4713 tg3_link_report(tp);
4714 return true;
4715 }
4716
4717 return false;
4718}
4719
Michael Chan3310e242013-04-09 08:48:05 +00004720static void tg3_clear_mac_status(struct tg3 *tp)
4721{
4722 tw32(MAC_EVENT, 0);
4723
4724 tw32_f(MAC_STATUS,
4725 MAC_STATUS_SYNC_CHANGED |
4726 MAC_STATUS_CFG_CHANGED |
4727 MAC_STATUS_MI_COMPLETION |
4728 MAC_STATUS_LNKSTATE_CHANGED);
4729 udelay(40);
4730}
4731
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004732static void tg3_setup_eee(struct tg3 *tp)
4733{
4734 u32 val;
4735
4736 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4737 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4738 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4739 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4740
4741 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4742
4743 tw32_f(TG3_CPMU_EEE_CTRL,
4744 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4745
4746 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4747 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4748 TG3_CPMU_EEEMD_LPI_IN_RX |
4749 TG3_CPMU_EEEMD_EEE_ENABLE;
4750
4751 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4752 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4753
4754 if (tg3_flag(tp, ENABLE_APE))
4755 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4756
4757 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4758
4759 tw32_f(TG3_CPMU_EEE_DBTMR1,
4760 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4761 (tp->eee.tx_lpi_timer & 0xffff));
4762
4763 tw32_f(TG3_CPMU_EEE_DBTMR2,
4764 TG3_CPMU_DBTMR2_APE_TX_2047US |
4765 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4766}
4767
Joe Perches953c96e2013-04-09 10:18:14 +00004768static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Joe Perches953c96e2013-04-09 10:18:14 +00004770 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004771 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004772 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 u16 current_speed;
4774 u8 current_duplex;
4775 int i, err;
4776
Michael Chan3310e242013-04-09 08:48:05 +00004777 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
Matt Carlson8ef21422008-05-02 16:47:53 -07004779 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4780 tw32_f(MAC_MI_MODE,
4781 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4782 udelay(80);
4783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004785 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
4787 /* Some third-party PHYs need to be reset on link going
4788 * down.
4789 */
Joe Perches41535772013-02-16 11:20:04 +00004790 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4791 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4792 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004793 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 tg3_readphy(tp, MII_BMSR, &bmsr);
4795 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4796 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004797 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 }
4799 if (force_reset)
4800 tg3_phy_reset(tp);
4801
Matt Carlson79eb6902010-02-17 15:17:03 +00004802 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 tg3_readphy(tp, MII_BMSR, &bmsr);
4804 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004805 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 bmsr = 0;
4807
4808 if (!(bmsr & BMSR_LSTATUS)) {
4809 err = tg3_init_5401phy_dsp(tp);
4810 if (err)
4811 return err;
4812
4813 tg3_readphy(tp, MII_BMSR, &bmsr);
4814 for (i = 0; i < 1000; i++) {
4815 udelay(10);
4816 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4817 (bmsr & BMSR_LSTATUS)) {
4818 udelay(40);
4819 break;
4820 }
4821 }
4822
Matt Carlson79eb6902010-02-17 15:17:03 +00004823 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4824 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 !(bmsr & BMSR_LSTATUS) &&
4826 tp->link_config.active_speed == SPEED_1000) {
4827 err = tg3_phy_reset(tp);
4828 if (!err)
4829 err = tg3_init_5401phy_dsp(tp);
4830 if (err)
4831 return err;
4832 }
4833 }
Joe Perches41535772013-02-16 11:20:04 +00004834 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4835 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 /* 5701 {A0,B0} CRC bug workaround */
4837 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004838 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4839 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4840 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 }
4842
4843 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004844 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4845 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004847 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004849 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4851
Joe Perches41535772013-02-16 11:20:04 +00004852 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4853 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4855 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4856 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4857 else
4858 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4859 }
4860
Joe Perches953c96e2013-04-09 10:18:14 +00004861 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004862 current_speed = SPEED_UNKNOWN;
4863 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004864 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004865 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004867 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004868 err = tg3_phy_auxctl_read(tp,
4869 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4870 &val);
4871 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004872 tg3_phy_auxctl_write(tp,
4873 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4874 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 goto relink;
4876 }
4877 }
4878
4879 bmsr = 0;
4880 for (i = 0; i < 100; i++) {
4881 tg3_readphy(tp, MII_BMSR, &bmsr);
4882 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4883 (bmsr & BMSR_LSTATUS))
4884 break;
4885 udelay(40);
4886 }
4887
4888 if (bmsr & BMSR_LSTATUS) {
4889 u32 aux_stat, bmcr;
4890
4891 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4892 for (i = 0; i < 2000; i++) {
4893 udelay(10);
4894 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4895 aux_stat)
4896 break;
4897 }
4898
4899 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4900 &current_speed,
4901 &current_duplex);
4902
4903 bmcr = 0;
4904 for (i = 0; i < 200; i++) {
4905 tg3_readphy(tp, MII_BMCR, &bmcr);
4906 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4907 continue;
4908 if (bmcr && bmcr != 0x7fff)
4909 break;
4910 udelay(10);
4911 }
4912
Matt Carlsonef167e22007-12-20 20:10:01 -08004913 lcl_adv = 0;
4914 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Matt Carlsonef167e22007-12-20 20:10:01 -08004916 tp->link_config.active_speed = current_speed;
4917 tp->link_config.active_duplex = current_duplex;
4918
4919 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004920 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4921
Matt Carlsonef167e22007-12-20 20:10:01 -08004922 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004923 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004924 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004925 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004926 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004927
4928 /* EEE settings changes take effect only after a phy
4929 * reset. If we have skipped a reset due to Link Flap
4930 * Avoidance being enabled, do it now.
4931 */
4932 if (!eee_config_ok &&
4933 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004934 !force_reset) {
4935 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004936 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 } else {
4939 if (!(bmcr & BMCR_ANENABLE) &&
4940 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004941 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004942 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 }
4944 }
4945
Joe Perches953c96e2013-04-09 10:18:14 +00004946 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004947 tp->link_config.active_duplex == DUPLEX_FULL) {
4948 u32 reg, bit;
4949
4950 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4951 reg = MII_TG3_FET_GEN_STAT;
4952 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4953 } else {
4954 reg = MII_TG3_EXT_STAT;
4955 bit = MII_TG3_EXT_STAT_MDIX;
4956 }
4957
4958 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4959 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4960
Matt Carlsonef167e22007-12-20 20:10:01 -08004961 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 }
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004966 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 tg3_phy_copper_begin(tp);
4968
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004969 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004970 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004971 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4972 current_speed = SPEED_1000;
4973 current_duplex = DUPLEX_FULL;
4974 tp->link_config.active_speed = current_speed;
4975 tp->link_config.active_duplex = current_duplex;
4976 }
4977
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004978 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004979 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4980 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004981 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 }
4983
4984 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004985 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 if (tp->link_config.active_speed == SPEED_100 ||
4987 tp->link_config.active_speed == SPEED_10)
4988 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4989 else
4990 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004991 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004992 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4993 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4995
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004996 /* In order for the 5750 core in BCM4785 chip to work properly
4997 * in RGMII mode, the Led Control Register must be set up.
4998 */
4999 if (tg3_flag(tp, RGMII_MODE)) {
5000 u32 led_ctrl = tr32(MAC_LED_CTRL);
5001 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
5002
5003 if (tp->link_config.active_speed == SPEED_10)
5004 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
5005 else if (tp->link_config.active_speed == SPEED_100)
5006 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5007 LED_CTRL_100MBPS_ON);
5008 else if (tp->link_config.active_speed == SPEED_1000)
5009 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5010 LED_CTRL_1000MBPS_ON);
5011
5012 tw32(MAC_LED_CTRL, led_ctrl);
5013 udelay(40);
5014 }
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5017 if (tp->link_config.active_duplex == DUPLEX_HALF)
5018 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5019
Joe Perches41535772013-02-16 11:20:04 +00005020 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005021 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005022 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005024 else
5025 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 }
5027
5028 /* ??? Without this setting Netgear GA302T PHY does not
5029 * ??? send/receive packets...
5030 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005031 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005032 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5034 tw32_f(MAC_MI_MODE, tp->mi_mode);
5035 udelay(80);
5036 }
5037
5038 tw32_f(MAC_MODE, tp->mac_mode);
5039 udelay(40);
5040
Matt Carlson52b02d02010-10-14 10:37:41 +00005041 tg3_phy_eee_adjust(tp, current_link_up);
5042
Joe Perches63c3a662011-04-26 08:12:10 +00005043 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 /* Polled via timer. */
5045 tw32_f(MAC_EVENT, 0);
5046 } else {
5047 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5048 }
5049 udelay(40);
5050
Joe Perches41535772013-02-16 11:20:04 +00005051 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005052 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005054 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 udelay(120);
5056 tw32_f(MAC_STATUS,
5057 (MAC_STATUS_SYNC_CHANGED |
5058 MAC_STATUS_CFG_CHANGED));
5059 udelay(40);
5060 tg3_write_mem(tp,
5061 NIC_SRAM_FIRMWARE_MBOX,
5062 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5063 }
5064
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005065 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005066 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005067 if (tp->link_config.active_speed == SPEED_100 ||
5068 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005069 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5070 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005071 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005072 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5073 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005074 }
5075
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005076 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
5078 return 0;
5079}
5080
5081struct tg3_fiber_aneginfo {
5082 int state;
5083#define ANEG_STATE_UNKNOWN 0
5084#define ANEG_STATE_AN_ENABLE 1
5085#define ANEG_STATE_RESTART_INIT 2
5086#define ANEG_STATE_RESTART 3
5087#define ANEG_STATE_DISABLE_LINK_OK 4
5088#define ANEG_STATE_ABILITY_DETECT_INIT 5
5089#define ANEG_STATE_ABILITY_DETECT 6
5090#define ANEG_STATE_ACK_DETECT_INIT 7
5091#define ANEG_STATE_ACK_DETECT 8
5092#define ANEG_STATE_COMPLETE_ACK_INIT 9
5093#define ANEG_STATE_COMPLETE_ACK 10
5094#define ANEG_STATE_IDLE_DETECT_INIT 11
5095#define ANEG_STATE_IDLE_DETECT 12
5096#define ANEG_STATE_LINK_OK 13
5097#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5098#define ANEG_STATE_NEXT_PAGE_WAIT 15
5099
5100 u32 flags;
5101#define MR_AN_ENABLE 0x00000001
5102#define MR_RESTART_AN 0x00000002
5103#define MR_AN_COMPLETE 0x00000004
5104#define MR_PAGE_RX 0x00000008
5105#define MR_NP_LOADED 0x00000010
5106#define MR_TOGGLE_TX 0x00000020
5107#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5108#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5109#define MR_LP_ADV_SYM_PAUSE 0x00000100
5110#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5111#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5112#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5113#define MR_LP_ADV_NEXT_PAGE 0x00001000
5114#define MR_TOGGLE_RX 0x00002000
5115#define MR_NP_RX 0x00004000
5116
5117#define MR_LINK_OK 0x80000000
5118
5119 unsigned long link_time, cur_time;
5120
5121 u32 ability_match_cfg;
5122 int ability_match_count;
5123
5124 char ability_match, idle_match, ack_match;
5125
5126 u32 txconfig, rxconfig;
5127#define ANEG_CFG_NP 0x00000080
5128#define ANEG_CFG_ACK 0x00000040
5129#define ANEG_CFG_RF2 0x00000020
5130#define ANEG_CFG_RF1 0x00000010
5131#define ANEG_CFG_PS2 0x00000001
5132#define ANEG_CFG_PS1 0x00008000
5133#define ANEG_CFG_HD 0x00004000
5134#define ANEG_CFG_FD 0x00002000
5135#define ANEG_CFG_INVAL 0x00001f06
5136
5137};
5138#define ANEG_OK 0
5139#define ANEG_DONE 1
5140#define ANEG_TIMER_ENAB 2
5141#define ANEG_FAILED -1
5142
5143#define ANEG_STATE_SETTLE_TIME 10000
5144
5145static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5146 struct tg3_fiber_aneginfo *ap)
5147{
Matt Carlson5be73b42007-12-20 20:09:29 -08005148 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 unsigned long delta;
5150 u32 rx_cfg_reg;
5151 int ret;
5152
5153 if (ap->state == ANEG_STATE_UNKNOWN) {
5154 ap->rxconfig = 0;
5155 ap->link_time = 0;
5156 ap->cur_time = 0;
5157 ap->ability_match_cfg = 0;
5158 ap->ability_match_count = 0;
5159 ap->ability_match = 0;
5160 ap->idle_match = 0;
5161 ap->ack_match = 0;
5162 }
5163 ap->cur_time++;
5164
5165 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5166 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5167
5168 if (rx_cfg_reg != ap->ability_match_cfg) {
5169 ap->ability_match_cfg = rx_cfg_reg;
5170 ap->ability_match = 0;
5171 ap->ability_match_count = 0;
5172 } else {
5173 if (++ap->ability_match_count > 1) {
5174 ap->ability_match = 1;
5175 ap->ability_match_cfg = rx_cfg_reg;
5176 }
5177 }
5178 if (rx_cfg_reg & ANEG_CFG_ACK)
5179 ap->ack_match = 1;
5180 else
5181 ap->ack_match = 0;
5182
5183 ap->idle_match = 0;
5184 } else {
5185 ap->idle_match = 1;
5186 ap->ability_match_cfg = 0;
5187 ap->ability_match_count = 0;
5188 ap->ability_match = 0;
5189 ap->ack_match = 0;
5190
5191 rx_cfg_reg = 0;
5192 }
5193
5194 ap->rxconfig = rx_cfg_reg;
5195 ret = ANEG_OK;
5196
Matt Carlson33f401a2010-04-05 10:19:27 +00005197 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 case ANEG_STATE_UNKNOWN:
5199 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5200 ap->state = ANEG_STATE_AN_ENABLE;
5201
5202 /* fallthru */
5203 case ANEG_STATE_AN_ENABLE:
5204 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5205 if (ap->flags & MR_AN_ENABLE) {
5206 ap->link_time = 0;
5207 ap->cur_time = 0;
5208 ap->ability_match_cfg = 0;
5209 ap->ability_match_count = 0;
5210 ap->ability_match = 0;
5211 ap->idle_match = 0;
5212 ap->ack_match = 0;
5213
5214 ap->state = ANEG_STATE_RESTART_INIT;
5215 } else {
5216 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5217 }
5218 break;
5219
5220 case ANEG_STATE_RESTART_INIT:
5221 ap->link_time = ap->cur_time;
5222 ap->flags &= ~(MR_NP_LOADED);
5223 ap->txconfig = 0;
5224 tw32(MAC_TX_AUTO_NEG, 0);
5225 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5226 tw32_f(MAC_MODE, tp->mac_mode);
5227 udelay(40);
5228
5229 ret = ANEG_TIMER_ENAB;
5230 ap->state = ANEG_STATE_RESTART;
5231
5232 /* fallthru */
5233 case ANEG_STATE_RESTART:
5234 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005235 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005237 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 break;
5240
5241 case ANEG_STATE_DISABLE_LINK_OK:
5242 ret = ANEG_DONE;
5243 break;
5244
5245 case ANEG_STATE_ABILITY_DETECT_INIT:
5246 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005247 ap->txconfig = ANEG_CFG_FD;
5248 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5249 if (flowctrl & ADVERTISE_1000XPAUSE)
5250 ap->txconfig |= ANEG_CFG_PS1;
5251 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5252 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5254 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5255 tw32_f(MAC_MODE, tp->mac_mode);
5256 udelay(40);
5257
5258 ap->state = ANEG_STATE_ABILITY_DETECT;
5259 break;
5260
5261 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005262 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 break;
5265
5266 case ANEG_STATE_ACK_DETECT_INIT:
5267 ap->txconfig |= ANEG_CFG_ACK;
5268 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5269 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5270 tw32_f(MAC_MODE, tp->mac_mode);
5271 udelay(40);
5272
5273 ap->state = ANEG_STATE_ACK_DETECT;
5274
5275 /* fallthru */
5276 case ANEG_STATE_ACK_DETECT:
5277 if (ap->ack_match != 0) {
5278 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5279 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5280 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5281 } else {
5282 ap->state = ANEG_STATE_AN_ENABLE;
5283 }
5284 } else if (ap->ability_match != 0 &&
5285 ap->rxconfig == 0) {
5286 ap->state = ANEG_STATE_AN_ENABLE;
5287 }
5288 break;
5289
5290 case ANEG_STATE_COMPLETE_ACK_INIT:
5291 if (ap->rxconfig & ANEG_CFG_INVAL) {
5292 ret = ANEG_FAILED;
5293 break;
5294 }
5295 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5296 MR_LP_ADV_HALF_DUPLEX |
5297 MR_LP_ADV_SYM_PAUSE |
5298 MR_LP_ADV_ASYM_PAUSE |
5299 MR_LP_ADV_REMOTE_FAULT1 |
5300 MR_LP_ADV_REMOTE_FAULT2 |
5301 MR_LP_ADV_NEXT_PAGE |
5302 MR_TOGGLE_RX |
5303 MR_NP_RX);
5304 if (ap->rxconfig & ANEG_CFG_FD)
5305 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5306 if (ap->rxconfig & ANEG_CFG_HD)
5307 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5308 if (ap->rxconfig & ANEG_CFG_PS1)
5309 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5310 if (ap->rxconfig & ANEG_CFG_PS2)
5311 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5312 if (ap->rxconfig & ANEG_CFG_RF1)
5313 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5314 if (ap->rxconfig & ANEG_CFG_RF2)
5315 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5316 if (ap->rxconfig & ANEG_CFG_NP)
5317 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5318
5319 ap->link_time = ap->cur_time;
5320
5321 ap->flags ^= (MR_TOGGLE_TX);
5322 if (ap->rxconfig & 0x0008)
5323 ap->flags |= MR_TOGGLE_RX;
5324 if (ap->rxconfig & ANEG_CFG_NP)
5325 ap->flags |= MR_NP_RX;
5326 ap->flags |= MR_PAGE_RX;
5327
5328 ap->state = ANEG_STATE_COMPLETE_ACK;
5329 ret = ANEG_TIMER_ENAB;
5330 break;
5331
5332 case ANEG_STATE_COMPLETE_ACK:
5333 if (ap->ability_match != 0 &&
5334 ap->rxconfig == 0) {
5335 ap->state = ANEG_STATE_AN_ENABLE;
5336 break;
5337 }
5338 delta = ap->cur_time - ap->link_time;
5339 if (delta > ANEG_STATE_SETTLE_TIME) {
5340 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5341 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5342 } else {
5343 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5344 !(ap->flags & MR_NP_RX)) {
5345 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5346 } else {
5347 ret = ANEG_FAILED;
5348 }
5349 }
5350 }
5351 break;
5352
5353 case ANEG_STATE_IDLE_DETECT_INIT:
5354 ap->link_time = ap->cur_time;
5355 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5356 tw32_f(MAC_MODE, tp->mac_mode);
5357 udelay(40);
5358
5359 ap->state = ANEG_STATE_IDLE_DETECT;
5360 ret = ANEG_TIMER_ENAB;
5361 break;
5362
5363 case ANEG_STATE_IDLE_DETECT:
5364 if (ap->ability_match != 0 &&
5365 ap->rxconfig == 0) {
5366 ap->state = ANEG_STATE_AN_ENABLE;
5367 break;
5368 }
5369 delta = ap->cur_time - ap->link_time;
5370 if (delta > ANEG_STATE_SETTLE_TIME) {
5371 /* XXX another gem from the Broadcom driver :( */
5372 ap->state = ANEG_STATE_LINK_OK;
5373 }
5374 break;
5375
5376 case ANEG_STATE_LINK_OK:
5377 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5378 ret = ANEG_DONE;
5379 break;
5380
5381 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5382 /* ??? unimplemented */
5383 break;
5384
5385 case ANEG_STATE_NEXT_PAGE_WAIT:
5386 /* ??? unimplemented */
5387 break;
5388
5389 default:
5390 ret = ANEG_FAILED;
5391 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
5394 return ret;
5395}
5396
Matt Carlson5be73b42007-12-20 20:09:29 -08005397static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398{
5399 int res = 0;
5400 struct tg3_fiber_aneginfo aninfo;
5401 int status = ANEG_FAILED;
5402 unsigned int tick;
5403 u32 tmp;
5404
5405 tw32_f(MAC_TX_AUTO_NEG, 0);
5406
5407 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5408 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5409 udelay(40);
5410
5411 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5412 udelay(40);
5413
5414 memset(&aninfo, 0, sizeof(aninfo));
5415 aninfo.flags |= MR_AN_ENABLE;
5416 aninfo.state = ANEG_STATE_UNKNOWN;
5417 aninfo.cur_time = 0;
5418 tick = 0;
5419 while (++tick < 195000) {
5420 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5421 if (status == ANEG_DONE || status == ANEG_FAILED)
5422 break;
5423
5424 udelay(1);
5425 }
5426
5427 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5428 tw32_f(MAC_MODE, tp->mac_mode);
5429 udelay(40);
5430
Matt Carlson5be73b42007-12-20 20:09:29 -08005431 *txflags = aninfo.txconfig;
5432 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
5434 if (status == ANEG_DONE &&
5435 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5436 MR_LP_ADV_FULL_DUPLEX)))
5437 res = 1;
5438
5439 return res;
5440}
5441
5442static void tg3_init_bcm8002(struct tg3 *tp)
5443{
5444 u32 mac_status = tr32(MAC_STATUS);
5445 int i;
5446
5447 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005448 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 !(mac_status & MAC_STATUS_PCS_SYNCED))
5450 return;
5451
5452 /* Set PLL lock range. */
5453 tg3_writephy(tp, 0x16, 0x8007);
5454
5455 /* SW reset */
5456 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5457
5458 /* Wait for reset to complete. */
5459 /* XXX schedule_timeout() ... */
5460 for (i = 0; i < 500; i++)
5461 udelay(10);
5462
5463 /* Config mode; select PMA/Ch 1 regs. */
5464 tg3_writephy(tp, 0x10, 0x8411);
5465
5466 /* Enable auto-lock and comdet, select txclk for tx. */
5467 tg3_writephy(tp, 0x11, 0x0a10);
5468
5469 tg3_writephy(tp, 0x18, 0x00a0);
5470 tg3_writephy(tp, 0x16, 0x41ff);
5471
5472 /* Assert and deassert POR. */
5473 tg3_writephy(tp, 0x13, 0x0400);
5474 udelay(40);
5475 tg3_writephy(tp, 0x13, 0x0000);
5476
5477 tg3_writephy(tp, 0x11, 0x0a50);
5478 udelay(40);
5479 tg3_writephy(tp, 0x11, 0x0a10);
5480
5481 /* Wait for signal to stabilize */
5482 /* XXX schedule_timeout() ... */
5483 for (i = 0; i < 15000; i++)
5484 udelay(10);
5485
5486 /* Deselect the channel register so we can read the PHYID
5487 * later.
5488 */
5489 tg3_writephy(tp, 0x10, 0x8011);
5490}
5491
Joe Perches953c96e2013-04-09 10:18:14 +00005492static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005494 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005495 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 u32 sg_dig_ctrl, sg_dig_status;
5497 u32 serdes_cfg, expected_sg_dig_ctrl;
5498 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
5500 serdes_cfg = 0;
5501 expected_sg_dig_ctrl = 0;
5502 workaround = 0;
5503 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005504 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Joe Perches41535772013-02-16 11:20:04 +00005506 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5507 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 workaround = 1;
5509 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5510 port_a = 0;
5511
5512 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5513 /* preserve bits 20-23 for voltage regulator */
5514 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5515 }
5516
5517 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5518
5519 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005520 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 if (workaround) {
5522 u32 val = serdes_cfg;
5523
5524 if (port_a)
5525 val |= 0xc010000;
5526 else
5527 val |= 0x4010000;
5528 tw32_f(MAC_SERDES_CFG, val);
5529 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005530
5531 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 }
5533 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5534 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005535 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 }
5537 goto out;
5538 }
5539
5540 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005541 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Matt Carlson82cd3d12007-12-20 20:09:00 -08005543 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5544 if (flowctrl & ADVERTISE_1000XPAUSE)
5545 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5546 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5547 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
5549 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005550 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005551 tp->serdes_counter &&
5552 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5553 MAC_STATUS_RCVD_CFG)) ==
5554 MAC_STATUS_PCS_SYNCED)) {
5555 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005556 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005557 goto out;
5558 }
5559restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 if (workaround)
5561 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005562 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 udelay(5);
5564 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5565
Michael Chan3d3ebe72006-09-27 15:59:15 -07005566 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005567 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5569 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005570 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 mac_status = tr32(MAC_STATUS);
5572
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005573 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005575 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Matt Carlson82cd3d12007-12-20 20:09:00 -08005577 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5578 local_adv |= ADVERTISE_1000XPAUSE;
5579 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5580 local_adv |= ADVERTISE_1000XPSE_ASYM;
5581
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005582 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005583 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005584 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005585 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
Matt Carlson859edb22011-12-08 14:40:16 +00005587 tp->link_config.rmt_adv =
5588 mii_adv_to_ethtool_adv_x(remote_adv);
5589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005591 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005592 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005593 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005594 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005595 if (tp->serdes_counter)
5596 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 else {
5598 if (workaround) {
5599 u32 val = serdes_cfg;
5600
5601 if (port_a)
5602 val |= 0xc010000;
5603 else
5604 val |= 0x4010000;
5605
5606 tw32_f(MAC_SERDES_CFG, val);
5607 }
5608
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005609 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 udelay(40);
5611
5612 /* Link parallel detection - link is up */
5613 /* only if we have PCS_SYNC and not */
5614 /* receiving config code words */
5615 mac_status = tr32(MAC_STATUS);
5616 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5617 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5618 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005619 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005620 tp->phy_flags |=
5621 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005622 tp->serdes_counter =
5623 SERDES_PARALLEL_DET_TIMEOUT;
5624 } else
5625 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 }
5627 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005628 } else {
5629 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005630 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 }
5632
5633out:
5634 return current_link_up;
5635}
5636
Joe Perches953c96e2013-04-09 10:18:14 +00005637static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
Joe Perches953c96e2013-04-09 10:18:14 +00005639 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005641 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
5644 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005645 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005647
Matt Carlson5be73b42007-12-20 20:09:29 -08005648 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5649 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Matt Carlson5be73b42007-12-20 20:09:29 -08005651 if (txflags & ANEG_CFG_PS1)
5652 local_adv |= ADVERTISE_1000XPAUSE;
5653 if (txflags & ANEG_CFG_PS2)
5654 local_adv |= ADVERTISE_1000XPSE_ASYM;
5655
5656 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5657 remote_adv |= LPA_1000XPAUSE;
5658 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5659 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
Matt Carlson859edb22011-12-08 14:40:16 +00005661 tp->link_config.rmt_adv =
5662 mii_adv_to_ethtool_adv_x(remote_adv);
5663
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 tg3_setup_flow_control(tp, local_adv, remote_adv);
5665
Joe Perches953c96e2013-04-09 10:18:14 +00005666 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 }
5668 for (i = 0; i < 30; i++) {
5669 udelay(20);
5670 tw32_f(MAC_STATUS,
5671 (MAC_STATUS_SYNC_CHANGED |
5672 MAC_STATUS_CFG_CHANGED));
5673 udelay(40);
5674 if ((tr32(MAC_STATUS) &
5675 (MAC_STATUS_SYNC_CHANGED |
5676 MAC_STATUS_CFG_CHANGED)) == 0)
5677 break;
5678 }
5679
5680 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005681 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5683 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005684 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005686 tg3_setup_flow_control(tp, 0, 0);
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005689 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
5691 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5692 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005693
5694 tw32_f(MAC_MODE, tp->mac_mode);
5695 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 }
5697
5698out:
5699 return current_link_up;
5700}
5701
Joe Perches953c96e2013-04-09 10:18:14 +00005702static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
5704 u32 orig_pause_cfg;
5705 u16 orig_active_speed;
5706 u8 orig_active_duplex;
5707 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005708 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 int i;
5710
Matt Carlson8d018622007-12-20 20:05:44 -08005711 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 orig_active_speed = tp->link_config.active_speed;
5713 orig_active_duplex = tp->link_config.active_duplex;
5714
Joe Perches63c3a662011-04-26 08:12:10 +00005715 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005716 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005717 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 mac_status = tr32(MAC_STATUS);
5719 mac_status &= (MAC_STATUS_PCS_SYNCED |
5720 MAC_STATUS_SIGNAL_DET |
5721 MAC_STATUS_CFG_CHANGED |
5722 MAC_STATUS_RCVD_CFG);
5723 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5724 MAC_STATUS_SIGNAL_DET)) {
5725 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5726 MAC_STATUS_CFG_CHANGED));
5727 return 0;
5728 }
5729 }
5730
5731 tw32_f(MAC_TX_AUTO_NEG, 0);
5732
5733 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5734 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5735 tw32_f(MAC_MODE, tp->mac_mode);
5736 udelay(40);
5737
Matt Carlson79eb6902010-02-17 15:17:03 +00005738 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 tg3_init_bcm8002(tp);
5740
5741 /* Enable link change event even when serdes polling. */
5742 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5743 udelay(40);
5744
Joe Perches953c96e2013-04-09 10:18:14 +00005745 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005746 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 mac_status = tr32(MAC_STATUS);
5748
Joe Perches63c3a662011-04-26 08:12:10 +00005749 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5751 else
5752 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5753
Matt Carlson898a56f2009-08-28 14:02:40 +00005754 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005756 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
5758 for (i = 0; i < 100; i++) {
5759 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5760 MAC_STATUS_CFG_CHANGED));
5761 udelay(5);
5762 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005763 MAC_STATUS_CFG_CHANGED |
5764 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 break;
5766 }
5767
5768 mac_status = tr32(MAC_STATUS);
5769 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005770 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005771 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5772 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 tw32_f(MAC_MODE, (tp->mac_mode |
5774 MAC_MODE_SEND_CONFIGS));
5775 udelay(1);
5776 tw32_f(MAC_MODE, tp->mac_mode);
5777 }
5778 }
5779
Joe Perches953c96e2013-04-09 10:18:14 +00005780 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 tp->link_config.active_speed = SPEED_1000;
5782 tp->link_config.active_duplex = DUPLEX_FULL;
5783 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5784 LED_CTRL_LNKLED_OVERRIDE |
5785 LED_CTRL_1000MBPS_ON));
5786 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005787 tp->link_config.active_speed = SPEED_UNKNOWN;
5788 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5790 LED_CTRL_LNKLED_OVERRIDE |
5791 LED_CTRL_TRAFFIC_OVERRIDE));
5792 }
5793
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005794 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005795 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 if (orig_pause_cfg != now_pause_cfg ||
5797 orig_active_speed != tp->link_config.active_speed ||
5798 orig_active_duplex != tp->link_config.active_duplex)
5799 tg3_link_report(tp);
5800 }
5801
5802 return 0;
5803}
5804
Joe Perches953c96e2013-04-09 10:18:14 +00005805static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005806{
Joe Perches953c96e2013-04-09 10:18:14 +00005807 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005808 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005809 u16 current_speed = SPEED_UNKNOWN;
5810 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005811 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005812 u32 local_adv, remote_adv, sgsr;
5813
5814 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5815 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5816 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5817 (sgsr & SERDES_TG3_SGMII_MODE)) {
5818
5819 if (force_reset)
5820 tg3_phy_reset(tp);
5821
5822 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5823
5824 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5825 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5826 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005827 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005828 if (sgsr & SERDES_TG3_SPEED_1000) {
5829 current_speed = SPEED_1000;
5830 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5831 } else if (sgsr & SERDES_TG3_SPEED_100) {
5832 current_speed = SPEED_100;
5833 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5834 } else {
5835 current_speed = SPEED_10;
5836 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5837 }
5838
5839 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5840 current_duplex = DUPLEX_FULL;
5841 else
5842 current_duplex = DUPLEX_HALF;
5843 }
5844
5845 tw32_f(MAC_MODE, tp->mac_mode);
5846 udelay(40);
5847
5848 tg3_clear_mac_status(tp);
5849
5850 goto fiber_setup_done;
5851 }
Michael Chan747e8f82005-07-25 12:33:22 -07005852
5853 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5854 tw32_f(MAC_MODE, tp->mac_mode);
5855 udelay(40);
5856
Michael Chan3310e242013-04-09 08:48:05 +00005857 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005858
5859 if (force_reset)
5860 tg3_phy_reset(tp);
5861
Matt Carlson859edb22011-12-08 14:40:16 +00005862 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005863
5864 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5865 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005866 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005867 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5868 bmsr |= BMSR_LSTATUS;
5869 else
5870 bmsr &= ~BMSR_LSTATUS;
5871 }
Michael Chan747e8f82005-07-25 12:33:22 -07005872
5873 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5874
5875 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005876 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005877 /* do nothing, just check for link up at the end */
5878 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005879 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005880
5881 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005882 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5883 ADVERTISE_1000XPAUSE |
5884 ADVERTISE_1000XPSE_ASYM |
5885 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005886
Matt Carlson28011cf2011-11-16 18:36:59 -05005887 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005888 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005889
Matt Carlson28011cf2011-11-16 18:36:59 -05005890 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5891 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005892 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5893 tg3_writephy(tp, MII_BMCR, bmcr);
5894
5895 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005896 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005897 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005898
5899 return err;
5900 }
5901 } else {
5902 u32 new_bmcr;
5903
5904 bmcr &= ~BMCR_SPEED1000;
5905 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5906
5907 if (tp->link_config.duplex == DUPLEX_FULL)
5908 new_bmcr |= BMCR_FULLDPLX;
5909
5910 if (new_bmcr != bmcr) {
5911 /* BMCR_SPEED1000 is a reserved bit that needs
5912 * to be set on write.
5913 */
5914 new_bmcr |= BMCR_SPEED1000;
5915
5916 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005917 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005918 u32 adv;
5919
5920 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5921 adv &= ~(ADVERTISE_1000XFULL |
5922 ADVERTISE_1000XHALF |
5923 ADVERTISE_SLCT);
5924 tg3_writephy(tp, MII_ADVERTISE, adv);
5925 tg3_writephy(tp, MII_BMCR, bmcr |
5926 BMCR_ANRESTART |
5927 BMCR_ANENABLE);
5928 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005929 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005930 }
5931 tg3_writephy(tp, MII_BMCR, new_bmcr);
5932 bmcr = new_bmcr;
5933 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5934 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005935 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005936 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5937 bmsr |= BMSR_LSTATUS;
5938 else
5939 bmsr &= ~BMSR_LSTATUS;
5940 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005941 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005942 }
5943 }
5944
5945 if (bmsr & BMSR_LSTATUS) {
5946 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005947 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005948 if (bmcr & BMCR_FULLDPLX)
5949 current_duplex = DUPLEX_FULL;
5950 else
5951 current_duplex = DUPLEX_HALF;
5952
Matt Carlsonef167e22007-12-20 20:10:01 -08005953 local_adv = 0;
5954 remote_adv = 0;
5955
Michael Chan747e8f82005-07-25 12:33:22 -07005956 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005957 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005958
5959 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5960 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5961 common = local_adv & remote_adv;
5962 if (common & (ADVERTISE_1000XHALF |
5963 ADVERTISE_1000XFULL)) {
5964 if (common & ADVERTISE_1000XFULL)
5965 current_duplex = DUPLEX_FULL;
5966 else
5967 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005968
5969 tp->link_config.rmt_adv =
5970 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005971 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005972 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005973 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005974 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005975 }
Michael Chan747e8f82005-07-25 12:33:22 -07005976 }
5977 }
5978
Michael Chan85730a62013-04-09 08:48:06 +00005979fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005980 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005981 tg3_setup_flow_control(tp, local_adv, remote_adv);
5982
Michael Chan747e8f82005-07-25 12:33:22 -07005983 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5984 if (tp->link_config.active_duplex == DUPLEX_HALF)
5985 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5986
5987 tw32_f(MAC_MODE, tp->mac_mode);
5988 udelay(40);
5989
5990 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5991
5992 tp->link_config.active_speed = current_speed;
5993 tp->link_config.active_duplex = current_duplex;
5994
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005995 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005996 return err;
5997}
5998
5999static void tg3_serdes_parallel_detect(struct tg3 *tp)
6000{
Michael Chan3d3ebe72006-09-27 15:59:15 -07006001 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07006002 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07006003 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07006004 return;
6005 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006006
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006007 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07006008 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
6009 u32 bmcr;
6010
6011 tg3_readphy(tp, MII_BMCR, &bmcr);
6012 if (bmcr & BMCR_ANENABLE) {
6013 u32 phy1, phy2;
6014
6015 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006016 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6017 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006018
6019 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006020 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6021 MII_TG3_DSP_EXP1_INT_STAT);
6022 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6023 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006024
6025 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6026 /* We have signal detect and not receiving
6027 * config code words, link is up by parallel
6028 * detection.
6029 */
6030
6031 bmcr &= ~BMCR_ANENABLE;
6032 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6033 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006034 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006035 }
6036 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006037 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006038 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006039 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006040 u32 phy2;
6041
6042 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006043 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6044 MII_TG3_DSP_EXP1_INT_STAT);
6045 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006046 if (phy2 & 0x20) {
6047 u32 bmcr;
6048
6049 /* Config code words received, turn on autoneg. */
6050 tg3_readphy(tp, MII_BMCR, &bmcr);
6051 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6052
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006053 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006054
6055 }
6056 }
6057}
6058
Joe Perches953c96e2013-04-09 10:18:14 +00006059static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006061 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 int err;
6063
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006064 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006066 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006067 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006068 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
Joe Perches41535772013-02-16 11:20:04 +00006071 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006072 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006073
6074 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6075 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6076 scale = 65;
6077 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6078 scale = 6;
6079 else
6080 scale = 12;
6081
6082 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6083 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6084 tw32(GRC_MISC_CFG, val);
6085 }
6086
Matt Carlsonf2096f92011-04-05 14:22:48 +00006087 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6088 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006089 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6090 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006091 val |= tr32(MAC_TX_LENGTHS) &
6092 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6093 TX_LENGTHS_CNT_DWN_VAL_MSK);
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 if (tp->link_config.active_speed == SPEED_1000 &&
6096 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006097 tw32(MAC_TX_LENGTHS, val |
6098 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006100 tw32(MAC_TX_LENGTHS, val |
6101 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Joe Perches63c3a662011-04-26 08:12:10 +00006103 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006104 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006106 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 } else {
6108 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6109 }
6110 }
6111
Joe Perches63c3a662011-04-26 08:12:10 +00006112 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006113 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006114 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006115 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6116 tp->pwrmgmt_thresh;
6117 else
6118 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6119 tw32(PCIE_PWR_MGMT_THRESH, val);
6120 }
6121
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 return err;
6123}
6124
Matt Carlsonbe947302012-12-03 19:36:57 +00006125/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006126static u64 tg3_refclk_read(struct tg3 *tp)
6127{
6128 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6129 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6130}
6131
6132/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006133static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6134{
Nithin Sujir92e64572013-07-29 13:58:38 -07006135 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6136
6137 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006138 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6139 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006140 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006141}
6142
Matt Carlson7d41e492012-12-03 19:36:58 +00006143static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6144static inline void tg3_full_unlock(struct tg3 *tp);
6145static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6146{
6147 struct tg3 *tp = netdev_priv(dev);
6148
6149 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6150 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006151 SOF_TIMESTAMPING_SOFTWARE;
6152
6153 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006154 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006155 SOF_TIMESTAMPING_RX_HARDWARE |
6156 SOF_TIMESTAMPING_RAW_HARDWARE;
6157 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006158
6159 if (tp->ptp_clock)
6160 info->phc_index = ptp_clock_index(tp->ptp_clock);
6161 else
6162 info->phc_index = -1;
6163
6164 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6165
6166 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6167 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6168 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6169 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6170 return 0;
6171}
6172
6173static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6174{
6175 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6176 bool neg_adj = false;
6177 u32 correction = 0;
6178
6179 if (ppb < 0) {
6180 neg_adj = true;
6181 ppb = -ppb;
6182 }
6183
6184 /* Frequency adjustment is performed using hardware with a 24 bit
6185 * accumulator and a programmable correction value. On each clk, the
6186 * correction value gets added to the accumulator and when it
6187 * overflows, the time counter is incremented/decremented.
6188 *
6189 * So conversion from ppb to correction value is
6190 * ppb * (1 << 24) / 1000000000
6191 */
6192 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6193 TG3_EAV_REF_CLK_CORRECT_MASK;
6194
6195 tg3_full_lock(tp, 0);
6196
6197 if (correction)
6198 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6199 TG3_EAV_REF_CLK_CORRECT_EN |
6200 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6201 else
6202 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6203
6204 tg3_full_unlock(tp);
6205
6206 return 0;
6207}
6208
6209static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6210{
6211 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6212
6213 tg3_full_lock(tp, 0);
6214 tp->ptp_adjust += delta;
6215 tg3_full_unlock(tp);
6216
6217 return 0;
6218}
6219
6220static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6221{
6222 u64 ns;
6223 u32 remainder;
6224 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6225
6226 tg3_full_lock(tp, 0);
6227 ns = tg3_refclk_read(tp);
6228 ns += tp->ptp_adjust;
6229 tg3_full_unlock(tp);
6230
6231 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6232 ts->tv_nsec = remainder;
6233
6234 return 0;
6235}
6236
6237static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6238 const struct timespec *ts)
6239{
6240 u64 ns;
6241 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6242
6243 ns = timespec_to_ns(ts);
6244
6245 tg3_full_lock(tp, 0);
6246 tg3_refclk_write(tp, ns);
6247 tp->ptp_adjust = 0;
6248 tg3_full_unlock(tp);
6249
6250 return 0;
6251}
6252
6253static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6254 struct ptp_clock_request *rq, int on)
6255{
Nithin Sujir92e64572013-07-29 13:58:38 -07006256 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6257 u32 clock_ctl;
6258 int rval = 0;
6259
6260 switch (rq->type) {
6261 case PTP_CLK_REQ_PEROUT:
6262 if (rq->perout.index != 0)
6263 return -EINVAL;
6264
6265 tg3_full_lock(tp, 0);
6266 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6267 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6268
6269 if (on) {
6270 u64 nsec;
6271
6272 nsec = rq->perout.start.sec * 1000000000ULL +
6273 rq->perout.start.nsec;
6274
6275 if (rq->perout.period.sec || rq->perout.period.nsec) {
6276 netdev_warn(tp->dev,
6277 "Device supports only a one-shot timesync output, period must be 0\n");
6278 rval = -EINVAL;
6279 goto err_out;
6280 }
6281
6282 if (nsec & (1ULL << 63)) {
6283 netdev_warn(tp->dev,
6284 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6285 rval = -EINVAL;
6286 goto err_out;
6287 }
6288
6289 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6290 tw32(TG3_EAV_WATCHDOG0_MSB,
6291 TG3_EAV_WATCHDOG0_EN |
6292 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6293
6294 tw32(TG3_EAV_REF_CLCK_CTL,
6295 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6296 } else {
6297 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6298 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6299 }
6300
6301err_out:
6302 tg3_full_unlock(tp);
6303 return rval;
6304
6305 default:
6306 break;
6307 }
6308
Matt Carlson7d41e492012-12-03 19:36:58 +00006309 return -EOPNOTSUPP;
6310}
6311
6312static const struct ptp_clock_info tg3_ptp_caps = {
6313 .owner = THIS_MODULE,
6314 .name = "tg3 clock",
6315 .max_adj = 250000000,
6316 .n_alarm = 0,
6317 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006318 .n_per_out = 1,
Richard Cochran4986b4f02014-03-20 22:21:55 +01006319 .n_pins = 0,
Matt Carlson7d41e492012-12-03 19:36:58 +00006320 .pps = 0,
6321 .adjfreq = tg3_ptp_adjfreq,
6322 .adjtime = tg3_ptp_adjtime,
6323 .gettime = tg3_ptp_gettime,
6324 .settime = tg3_ptp_settime,
6325 .enable = tg3_ptp_enable,
6326};
6327
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006328static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6329 struct skb_shared_hwtstamps *timestamp)
6330{
6331 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6332 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6333 tp->ptp_adjust);
6334}
6335
Matt Carlsonbe947302012-12-03 19:36:57 +00006336/* tp->lock must be held */
6337static void tg3_ptp_init(struct tg3 *tp)
6338{
6339 if (!tg3_flag(tp, PTP_CAPABLE))
6340 return;
6341
6342 /* Initialize the hardware clock to the system time. */
6343 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6344 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006345 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006346}
6347
6348/* tp->lock must be held */
6349static void tg3_ptp_resume(struct tg3 *tp)
6350{
6351 if (!tg3_flag(tp, PTP_CAPABLE))
6352 return;
6353
6354 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6355 tp->ptp_adjust = 0;
6356}
6357
6358static void tg3_ptp_fini(struct tg3 *tp)
6359{
6360 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6361 return;
6362
Matt Carlson7d41e492012-12-03 19:36:58 +00006363 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006364 tp->ptp_clock = NULL;
6365 tp->ptp_adjust = 0;
6366}
6367
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006368static inline int tg3_irq_sync(struct tg3 *tp)
6369{
6370 return tp->irq_sync;
6371}
6372
Matt Carlson97bd8e42011-04-13 11:05:04 +00006373static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6374{
6375 int i;
6376
6377 dst = (u32 *)((u8 *)dst + off);
6378 for (i = 0; i < len; i += sizeof(u32))
6379 *dst++ = tr32(off + i);
6380}
6381
6382static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6383{
6384 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6385 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6386 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6387 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6388 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6389 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6390 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6391 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6392 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6393 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6394 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6395 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6396 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6397 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6398 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6399 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6400 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6401 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6402 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6403
Joe Perches63c3a662011-04-26 08:12:10 +00006404 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006405 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6406
6407 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6408 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6409 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6410 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6411 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6412 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6413 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6414 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6415
Joe Perches63c3a662011-04-26 08:12:10 +00006416 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006417 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6418 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6419 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6420 }
6421
6422 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6423 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6424 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6425 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6426 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6427
Joe Perches63c3a662011-04-26 08:12:10 +00006428 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006429 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6430}
6431
6432static void tg3_dump_state(struct tg3 *tp)
6433{
6434 int i;
6435 u32 *regs;
6436
6437 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006438 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006439 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006440
Joe Perches63c3a662011-04-26 08:12:10 +00006441 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006442 /* Read up to but not including private PCI registers */
6443 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6444 regs[i / sizeof(u32)] = tr32(i);
6445 } else
6446 tg3_dump_legacy_regs(tp, regs);
6447
6448 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6449 if (!regs[i + 0] && !regs[i + 1] &&
6450 !regs[i + 2] && !regs[i + 3])
6451 continue;
6452
6453 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6454 i * 4,
6455 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6456 }
6457
6458 kfree(regs);
6459
6460 for (i = 0; i < tp->irq_cnt; i++) {
6461 struct tg3_napi *tnapi = &tp->napi[i];
6462
6463 /* SW status block */
6464 netdev_err(tp->dev,
6465 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6466 i,
6467 tnapi->hw_status->status,
6468 tnapi->hw_status->status_tag,
6469 tnapi->hw_status->rx_jumbo_consumer,
6470 tnapi->hw_status->rx_consumer,
6471 tnapi->hw_status->rx_mini_consumer,
6472 tnapi->hw_status->idx[0].rx_producer,
6473 tnapi->hw_status->idx[0].tx_consumer);
6474
6475 netdev_err(tp->dev,
6476 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6477 i,
6478 tnapi->last_tag, tnapi->last_irq_tag,
6479 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6480 tnapi->rx_rcb_ptr,
6481 tnapi->prodring.rx_std_prod_idx,
6482 tnapi->prodring.rx_std_cons_idx,
6483 tnapi->prodring.rx_jmb_prod_idx,
6484 tnapi->prodring.rx_jmb_cons_idx);
6485 }
6486}
6487
Michael Chandf3e6542006-05-26 17:48:07 -07006488/* This is called whenever we suspect that the system chipset is re-
6489 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6490 * is bogus tx completions. We try to recover by setting the
6491 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6492 * in the workqueue.
6493 */
6494static void tg3_tx_recover(struct tg3 *tp)
6495{
Joe Perches63c3a662011-04-26 08:12:10 +00006496 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006497 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6498
Matt Carlson5129c3a2010-04-05 10:19:23 +00006499 netdev_warn(tp->dev,
6500 "The system may be re-ordering memory-mapped I/O "
6501 "cycles to the network device, attempting to recover. "
6502 "Please report the problem to the driver maintainer "
6503 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006504
Joe Perches63c3a662011-04-26 08:12:10 +00006505 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006506}
6507
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006508static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006509{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006510 /* Tell compiler to fetch tx indices from memory. */
6511 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006512 return tnapi->tx_pending -
6513 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006514}
6515
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516/* Tigon3 never reports partial packet sends. So we do not
6517 * need special logic to handle SKBs that have not had all
6518 * of their frags sent yet, like SunGEM does.
6519 */
Matt Carlson17375d22009-08-28 14:02:18 +00006520static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521{
Matt Carlson17375d22009-08-28 14:02:18 +00006522 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006523 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006524 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006525 struct netdev_queue *txq;
6526 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006527 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006528
Joe Perches63c3a662011-04-26 08:12:10 +00006529 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006530 index--;
6531
6532 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533
6534 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006535 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006537 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538
Michael Chandf3e6542006-05-26 17:48:07 -07006539 if (unlikely(skb == NULL)) {
6540 tg3_tx_recover(tp);
6541 return;
6542 }
6543
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006544 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6545 struct skb_shared_hwtstamps timestamp;
6546 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6547 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6548
6549 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6550
6551 skb_tstamp_tx(skb, &timestamp);
6552 }
6553
Alexander Duyckf4188d82009-12-02 16:48:38 +00006554 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006555 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006556 skb_headlen(skb),
6557 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
6559 ri->skb = NULL;
6560
Matt Carlsone01ee142011-07-27 14:20:50 +00006561 while (ri->fragmented) {
6562 ri->fragmented = false;
6563 sw_idx = NEXT_TX(sw_idx);
6564 ri = &tnapi->tx_buffers[sw_idx];
6565 }
6566
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 sw_idx = NEXT_TX(sw_idx);
6568
6569 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006570 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006571 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6572 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006573
6574 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006575 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006576 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006577 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006578
6579 while (ri->fragmented) {
6580 ri->fragmented = false;
6581 sw_idx = NEXT_TX(sw_idx);
6582 ri = &tnapi->tx_buffers[sw_idx];
6583 }
6584
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 sw_idx = NEXT_TX(sw_idx);
6586 }
6587
Tom Herbert298376d2011-11-28 16:33:30 +00006588 pkts_compl++;
6589 bytes_compl += skb->len;
6590
Eric W. Biederman497a27b2014-03-11 14:18:14 -07006591 dev_kfree_skb_any(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006592
6593 if (unlikely(tx_bug)) {
6594 tg3_tx_recover(tp);
6595 return;
6596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 }
6598
Tom Herbert5cb917b2012-03-05 19:53:50 +00006599 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006600
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006601 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
Michael Chan1b2a7202006-08-07 21:46:02 -07006603 /* Need to make the tx_cons update visible to tg3_start_xmit()
6604 * before checking for netif_queue_stopped(). Without the
6605 * memory barrier, there is a small possibility that tg3_start_xmit()
6606 * will miss it and cause the queue to be stopped forever.
6607 */
6608 smp_mb();
6609
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006610 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006611 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006612 __netif_tx_lock(txq, smp_processor_id());
6613 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006614 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006615 netif_tx_wake_queue(txq);
6616 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618}
6619
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006620static void tg3_frag_free(bool is_frag, void *data)
6621{
6622 if (is_frag)
6623 put_page(virt_to_head_page(data));
6624 else
6625 kfree(data);
6626}
6627
Eric Dumazet9205fd92011-11-18 06:47:01 +00006628static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006629{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006630 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6631 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6632
Eric Dumazet9205fd92011-11-18 06:47:01 +00006633 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006634 return;
6635
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006636 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006637 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006638 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006639 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006640}
6641
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006642
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643/* Returns size of skb allocated or < 0 on error.
6644 *
6645 * We only need to fill in the address because the other members
6646 * of the RX descriptor are invariant, see tg3_init_rings.
6647 *
6648 * Note the purposeful assymetry of cpu vs. chip accesses. For
6649 * posting buffers we only dirty the first cache line of the RX
6650 * descriptor (containing the address). Whereas for the RX status
6651 * buffers the cpu only reads the last cacheline of the RX descriptor
6652 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6653 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006654static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006655 u32 opaque_key, u32 dest_idx_unmasked,
6656 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657{
6658 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006659 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006660 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006662 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 switch (opaque_key) {
6665 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006666 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006667 desc = &tpr->rx_std[dest_idx];
6668 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006669 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 break;
6671
6672 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006673 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006674 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006675 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006676 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 break;
6678
6679 default:
6680 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682
6683 /* Do not overwrite any of the map or rp information
6684 * until we are sure we can commit to a new buffer.
6685 *
6686 * Callers depend upon this behavior and assume that
6687 * we leave everything unchanged if we fail.
6688 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006689 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6690 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006691 if (skb_size <= PAGE_SIZE) {
6692 data = netdev_alloc_frag(skb_size);
6693 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006694 } else {
6695 data = kmalloc(skb_size, GFP_ATOMIC);
6696 *frag_size = 0;
6697 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006698 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 return -ENOMEM;
6700
Eric Dumazet9205fd92011-11-18 06:47:01 +00006701 mapping = pci_map_single(tp->pdev,
6702 data + TG3_RX_OFFSET(tp),
6703 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006705 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006706 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006707 return -EIO;
6708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
Eric Dumazet9205fd92011-11-18 06:47:01 +00006710 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006711 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 desc->addr_hi = ((u64)mapping >> 32);
6714 desc->addr_lo = ((u64)mapping & 0xffffffff);
6715
Eric Dumazet9205fd92011-11-18 06:47:01 +00006716 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717}
6718
6719/* We only need to move over in the address because the other
6720 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006721 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 */
Matt Carlsona3896162009-11-13 13:03:44 +00006723static void tg3_recycle_rx(struct tg3_napi *tnapi,
6724 struct tg3_rx_prodring_set *dpr,
6725 u32 opaque_key, int src_idx,
6726 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727{
Matt Carlson17375d22009-08-28 14:02:18 +00006728 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6730 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006731 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006732 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
6734 switch (opaque_key) {
6735 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006736 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006737 dest_desc = &dpr->rx_std[dest_idx];
6738 dest_map = &dpr->rx_std_buffers[dest_idx];
6739 src_desc = &spr->rx_std[src_idx];
6740 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 break;
6742
6743 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006744 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006745 dest_desc = &dpr->rx_jmb[dest_idx].std;
6746 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6747 src_desc = &spr->rx_jmb[src_idx].std;
6748 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 break;
6750
6751 default:
6752 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
Eric Dumazet9205fd92011-11-18 06:47:01 +00006755 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006756 dma_unmap_addr_set(dest_map, mapping,
6757 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 dest_desc->addr_hi = src_desc->addr_hi;
6759 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006760
6761 /* Ensure that the update to the skb happens after the physical
6762 * addresses have been transferred to the new BD location.
6763 */
6764 smp_wmb();
6765
Eric Dumazet9205fd92011-11-18 06:47:01 +00006766 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767}
6768
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769/* The RX ring scheme is composed of multiple rings which post fresh
6770 * buffers to the chip, and one special ring the chip uses to report
6771 * status back to the host.
6772 *
6773 * The special ring reports the status of received packets to the
6774 * host. The chip does not write into the original descriptor the
6775 * RX buffer was obtained from. The chip simply takes the original
6776 * descriptor as provided by the host, updates the status and length
6777 * field, then writes this into the next status ring entry.
6778 *
6779 * Each ring the host uses to post buffers to the chip is described
6780 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6781 * it is first placed into the on-chip ram. When the packet's length
6782 * is known, it walks down the TG3_BDINFO entries to select the ring.
6783 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6784 * which is within the range of the new packet's length is chosen.
6785 *
6786 * The "separate ring for rx status" scheme may sound queer, but it makes
6787 * sense from a cache coherency perspective. If only the host writes
6788 * to the buffer post rings, and only the chip writes to the rx status
6789 * rings, then cache lines never move beyond shared-modified state.
6790 * If both the host and chip were to write into the same ring, cache line
6791 * eviction could occur since both entities want it in an exclusive state.
6792 */
Matt Carlson17375d22009-08-28 14:02:18 +00006793static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794{
Matt Carlson17375d22009-08-28 14:02:18 +00006795 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006796 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006797 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006798 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006799 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006801 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006803 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 /*
6805 * We need to order the read of hw_idx and the read of
6806 * the opaque cookie.
6807 */
6808 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 work_mask = 0;
6810 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006811 std_prod_idx = tpr->rx_std_prod_idx;
6812 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006814 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006815 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 unsigned int len;
6817 struct sk_buff *skb;
6818 dma_addr_t dma_addr;
6819 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006820 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006821 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822
6823 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6824 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6825 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006826 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006827 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006828 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006829 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006830 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006832 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006833 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006834 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006835 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006836 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839 work_mask |= opaque_key;
6840
Michael Chand7b95312014-02-28 15:05:10 -08006841 if (desc->err_vlan & RXD_ERR_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006843 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 desc_idx, *post_ptr);
6845 drop_it_no_recycle:
6846 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006847 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 goto next_pkt;
6849 }
6850
Eric Dumazet9205fd92011-11-18 06:47:01 +00006851 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006852 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6853 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006855 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6856 RXD_FLAG_PTPSTAT_PTPV1 ||
6857 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6858 RXD_FLAG_PTPSTAT_PTPV2) {
6859 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6860 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6861 }
6862
Matt Carlsond2757fc2010-04-12 06:58:27 +00006863 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006865 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Eric Dumazet9205fd92011-11-18 06:47:01 +00006867 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006868 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 if (skb_size < 0)
6870 goto drop_it;
6871
Matt Carlson287be122009-08-28 13:58:46 +00006872 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 PCI_DMA_FROMDEVICE);
6874
Eric Dumazet9205fd92011-11-18 06:47:01 +00006875 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006876 * after the usage of the old DMA mapping.
6877 */
6878 smp_wmb();
6879
Eric Dumazet9205fd92011-11-18 06:47:01 +00006880 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006881
Ivan Vecera85aec732013-11-06 14:02:36 +01006882 skb = build_skb(data, frag_size);
6883 if (!skb) {
6884 tg3_frag_free(frag_size != 0, data);
6885 goto drop_it_no_recycle;
6886 }
6887 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006889 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 desc_idx, *post_ptr);
6891
Eric Dumazet9205fd92011-11-18 06:47:01 +00006892 skb = netdev_alloc_skb(tp->dev,
6893 len + TG3_RAW_IP_ALIGN);
6894 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 goto drop_it_no_recycle;
6896
Eric Dumazet9205fd92011-11-18 06:47:01 +00006897 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006899 memcpy(skb->data,
6900 data + TG3_RX_OFFSET(tp),
6901 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 }
6904
Eric Dumazet9205fd92011-11-18 06:47:01 +00006905 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006906 if (tstamp)
6907 tg3_hwclock_to_timestamp(tp, tstamp,
6908 skb_hwtstamps(skb));
6909
Michał Mirosławdc668912011-04-07 03:35:07 +00006910 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6912 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6913 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6914 skb->ip_summed = CHECKSUM_UNNECESSARY;
6915 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006916 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
6918 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006919
6920 if (len > (tp->dev->mtu + ETH_HLEN) &&
Vlad Yasevich7d3083e2014-09-30 19:39:36 -04006921 skb->protocol != htons(ETH_P_8021Q) &&
6922 skb->protocol != htons(ETH_P_8021AD)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07006923 dev_kfree_skb_any(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006924 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006925 }
6926
Matt Carlson9dc7a112010-04-12 06:58:28 +00006927 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006928 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006929 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006930 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006931
Matt Carlsonbf933c82011-01-25 15:58:49 +00006932 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 received++;
6935 budget--;
6936
6937next_pkt:
6938 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006939
6940 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006941 tpr->rx_std_prod_idx = std_prod_idx &
6942 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006943 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6944 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006945 work_mask &= ~RXD_OPAQUE_RING_STD;
6946 rx_std_posted = 0;
6947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006949 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006950 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006951
6952 /* Refresh hw_idx to see if there is new work */
6953 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006954 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006955 rmb();
6956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 }
6958
6959 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006960 tnapi->rx_rcb_ptr = sw_idx;
6961 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962
6963 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006964 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006965 /* Sync BD data before updating mailbox */
6966 wmb();
6967
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006968 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006969 tpr->rx_std_prod_idx = std_prod_idx &
6970 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006971 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6972 tpr->rx_std_prod_idx);
6973 }
6974 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006975 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6976 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006977 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6978 tpr->rx_jmb_prod_idx);
6979 }
6980 mmiowb();
6981 } else if (work_mask) {
6982 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6983 * updated before the producer indices can be updated.
6984 */
6985 smp_wmb();
6986
Matt Carlson2c49a442010-09-30 10:34:35 +00006987 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6988 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006989
Michael Chan7ae52892012-03-21 15:38:33 +00006990 if (tnapi != &tp->napi[1]) {
6991 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006992 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
6996 return received;
6997}
6998
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006999static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00007002 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007003 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
7004
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 if (sblk->status & SD_STATUS_LINK_CHG) {
7006 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007007 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07007008 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00007009 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007010 tw32_f(MAC_STATUS,
7011 (MAC_STATUS_SYNC_CHANGED |
7012 MAC_STATUS_CFG_CHANGED |
7013 MAC_STATUS_MI_COMPLETION |
7014 MAC_STATUS_LNKSTATE_CHANGED));
7015 udelay(40);
7016 } else
Joe Perches953c96e2013-04-09 10:18:14 +00007017 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007018 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 }
7020 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007021}
7022
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007023static int tg3_rx_prodring_xfer(struct tg3 *tp,
7024 struct tg3_rx_prodring_set *dpr,
7025 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007026{
7027 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007028 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007029
7030 while (1) {
7031 src_prod_idx = spr->rx_std_prod_idx;
7032
7033 /* Make sure updates to the rx_std_buffers[] entries and the
7034 * standard producer index are seen in the correct order.
7035 */
7036 smp_rmb();
7037
7038 if (spr->rx_std_cons_idx == src_prod_idx)
7039 break;
7040
7041 if (spr->rx_std_cons_idx < src_prod_idx)
7042 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7043 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007044 cpycnt = tp->rx_std_ring_mask + 1 -
7045 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007046
Matt Carlson2c49a442010-09-30 10:34:35 +00007047 cpycnt = min(cpycnt,
7048 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007049
7050 si = spr->rx_std_cons_idx;
7051 di = dpr->rx_std_prod_idx;
7052
Matt Carlsone92967b2010-02-12 14:47:06 +00007053 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007054 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007055 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007056 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007057 break;
7058 }
7059 }
7060
7061 if (!cpycnt)
7062 break;
7063
7064 /* Ensure that updates to the rx_std_buffers ring and the
7065 * shadowed hardware producer ring from tg3_recycle_skb() are
7066 * ordered correctly WRT the skb check above.
7067 */
7068 smp_rmb();
7069
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007070 memcpy(&dpr->rx_std_buffers[di],
7071 &spr->rx_std_buffers[si],
7072 cpycnt * sizeof(struct ring_info));
7073
7074 for (i = 0; i < cpycnt; i++, di++, si++) {
7075 struct tg3_rx_buffer_desc *sbd, *dbd;
7076 sbd = &spr->rx_std[si];
7077 dbd = &dpr->rx_std[di];
7078 dbd->addr_hi = sbd->addr_hi;
7079 dbd->addr_lo = sbd->addr_lo;
7080 }
7081
Matt Carlson2c49a442010-09-30 10:34:35 +00007082 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7083 tp->rx_std_ring_mask;
7084 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7085 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007086 }
7087
7088 while (1) {
7089 src_prod_idx = spr->rx_jmb_prod_idx;
7090
7091 /* Make sure updates to the rx_jmb_buffers[] entries and
7092 * the jumbo producer index are seen in the correct order.
7093 */
7094 smp_rmb();
7095
7096 if (spr->rx_jmb_cons_idx == src_prod_idx)
7097 break;
7098
7099 if (spr->rx_jmb_cons_idx < src_prod_idx)
7100 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7101 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007102 cpycnt = tp->rx_jmb_ring_mask + 1 -
7103 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007104
7105 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007106 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007107
7108 si = spr->rx_jmb_cons_idx;
7109 di = dpr->rx_jmb_prod_idx;
7110
Matt Carlsone92967b2010-02-12 14:47:06 +00007111 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007112 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007113 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007114 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007115 break;
7116 }
7117 }
7118
7119 if (!cpycnt)
7120 break;
7121
7122 /* Ensure that updates to the rx_jmb_buffers ring and the
7123 * shadowed hardware producer ring from tg3_recycle_skb() are
7124 * ordered correctly WRT the skb check above.
7125 */
7126 smp_rmb();
7127
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007128 memcpy(&dpr->rx_jmb_buffers[di],
7129 &spr->rx_jmb_buffers[si],
7130 cpycnt * sizeof(struct ring_info));
7131
7132 for (i = 0; i < cpycnt; i++, di++, si++) {
7133 struct tg3_rx_buffer_desc *sbd, *dbd;
7134 sbd = &spr->rx_jmb[si].std;
7135 dbd = &dpr->rx_jmb[di].std;
7136 dbd->addr_hi = sbd->addr_hi;
7137 dbd->addr_lo = sbd->addr_lo;
7138 }
7139
Matt Carlson2c49a442010-09-30 10:34:35 +00007140 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7141 tp->rx_jmb_ring_mask;
7142 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7143 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007144 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007145
7146 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007147}
7148
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007149static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7150{
7151 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
7153 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007154 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007155 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007156 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007157 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 }
7159
Matt Carlsonf891ea12012-04-24 13:37:01 +00007160 if (!tnapi->rx_rcb_prod_idx)
7161 return work_done;
7162
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 /* run RX thread, within the bounds set by NAPI.
7164 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007165 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007167 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007168 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
Joe Perches63c3a662011-04-26 08:12:10 +00007170 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007171 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007172 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007173 u32 std_prod_idx = dpr->rx_std_prod_idx;
7174 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007175
Michael Chan7ae52892012-03-21 15:38:33 +00007176 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007177 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007178 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007179 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007180
7181 wmb();
7182
Matt Carlsone4af1af2010-02-12 14:47:05 +00007183 if (std_prod_idx != dpr->rx_std_prod_idx)
7184 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7185 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007186
Matt Carlsone4af1af2010-02-12 14:47:05 +00007187 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7188 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7189 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007190
7191 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007192
7193 if (err)
7194 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007195 }
7196
David S. Miller6f535762007-10-11 18:08:29 -07007197 return work_done;
7198}
David S. Millerf7383c22005-05-18 22:50:53 -07007199
Matt Carlsondb219972011-11-04 09:15:03 +00007200static inline void tg3_reset_task_schedule(struct tg3 *tp)
7201{
7202 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7203 schedule_work(&tp->reset_task);
7204}
7205
7206static inline void tg3_reset_task_cancel(struct tg3 *tp)
7207{
7208 cancel_work_sync(&tp->reset_task);
7209 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007210 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007211}
7212
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007213static int tg3_poll_msix(struct napi_struct *napi, int budget)
7214{
7215 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7216 struct tg3 *tp = tnapi->tp;
7217 int work_done = 0;
7218 struct tg3_hw_status *sblk = tnapi->hw_status;
7219
7220 while (1) {
7221 work_done = tg3_poll_work(tnapi, work_done, budget);
7222
Joe Perches63c3a662011-04-26 08:12:10 +00007223 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007224 goto tx_recovery;
7225
7226 if (unlikely(work_done >= budget))
7227 break;
7228
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007229 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007230 * to tell the hw how much work has been processed,
7231 * so we must read it before checking for more work.
7232 */
7233 tnapi->last_tag = sblk->status_tag;
7234 tnapi->last_irq_tag = tnapi->last_tag;
7235 rmb();
7236
7237 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007238 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7239 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007240
7241 /* This test here is not race free, but will reduce
7242 * the number of interrupts by looping again.
7243 */
7244 if (tnapi == &tp->napi[1] && tp->rx_refill)
7245 continue;
7246
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007247 napi_complete(napi);
7248 /* Reenable interrupts. */
7249 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007250
7251 /* This test here is synchronized by napi_schedule()
7252 * and napi_complete() to close the race condition.
7253 */
7254 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7255 tw32(HOSTCC_MODE, tp->coalesce_mode |
7256 HOSTCC_MODE_ENABLE |
7257 tnapi->coal_now);
7258 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007259 mmiowb();
7260 break;
7261 }
7262 }
7263
7264 return work_done;
7265
7266tx_recovery:
7267 /* work_done is guaranteed to be less than budget. */
7268 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007269 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007270 return work_done;
7271}
7272
Matt Carlsone64de4e2011-04-13 11:05:05 +00007273static void tg3_process_error(struct tg3 *tp)
7274{
7275 u32 val;
7276 bool real_error = false;
7277
Joe Perches63c3a662011-04-26 08:12:10 +00007278 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007279 return;
7280
7281 /* Check Flow Attention register */
7282 val = tr32(HOSTCC_FLOW_ATTN);
7283 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7284 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7285 real_error = true;
7286 }
7287
7288 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7289 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7290 real_error = true;
7291 }
7292
7293 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7294 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7295 real_error = true;
7296 }
7297
7298 if (!real_error)
7299 return;
7300
7301 tg3_dump_state(tp);
7302
Joe Perches63c3a662011-04-26 08:12:10 +00007303 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007304 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007305}
7306
David S. Miller6f535762007-10-11 18:08:29 -07007307static int tg3_poll(struct napi_struct *napi, int budget)
7308{
Matt Carlson8ef04422009-08-28 14:01:37 +00007309 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7310 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007311 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007312 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007313
7314 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007315 if (sblk->status & SD_STATUS_ERROR)
7316 tg3_process_error(tp);
7317
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007318 tg3_poll_link(tp);
7319
Matt Carlson17375d22009-08-28 14:02:18 +00007320 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007321
Joe Perches63c3a662011-04-26 08:12:10 +00007322 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007323 goto tx_recovery;
7324
7325 if (unlikely(work_done >= budget))
7326 break;
7327
Joe Perches63c3a662011-04-26 08:12:10 +00007328 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007329 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007330 * to tell the hw how much work has been processed,
7331 * so we must read it before checking for more work.
7332 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007333 tnapi->last_tag = sblk->status_tag;
7334 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007335 rmb();
7336 } else
7337 sblk->status &= ~SD_STATUS_UPDATED;
7338
Matt Carlson17375d22009-08-28 14:02:18 +00007339 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007340 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007341 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007342 break;
7343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 }
7345
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007346 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007347
7348tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007349 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007350 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007351 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007352 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353}
7354
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007355static void tg3_napi_disable(struct tg3 *tp)
7356{
7357 int i;
7358
7359 for (i = tp->irq_cnt - 1; i >= 0; i--)
7360 napi_disable(&tp->napi[i].napi);
7361}
7362
7363static void tg3_napi_enable(struct tg3 *tp)
7364{
7365 int i;
7366
7367 for (i = 0; i < tp->irq_cnt; i++)
7368 napi_enable(&tp->napi[i].napi);
7369}
7370
7371static void tg3_napi_init(struct tg3 *tp)
7372{
7373 int i;
7374
7375 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7376 for (i = 1; i < tp->irq_cnt; i++)
7377 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7378}
7379
7380static void tg3_napi_fini(struct tg3 *tp)
7381{
7382 int i;
7383
7384 for (i = 0; i < tp->irq_cnt; i++)
7385 netif_napi_del(&tp->napi[i].napi);
7386}
7387
7388static inline void tg3_netif_stop(struct tg3 *tp)
7389{
7390 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7391 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007392 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007393 netif_tx_disable(tp->dev);
7394}
7395
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007396/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007397static inline void tg3_netif_start(struct tg3 *tp)
7398{
Matt Carlsonbe947302012-12-03 19:36:57 +00007399 tg3_ptp_resume(tp);
7400
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007401 /* NOTE: unconditional netif_tx_wake_all_queues is only
7402 * appropriate so long as all callers are assured to
7403 * have free tx slots (such as after tg3_init_hw)
7404 */
7405 netif_tx_wake_all_queues(tp->dev);
7406
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007407 if (tp->link_up)
7408 netif_carrier_on(tp->dev);
7409
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007410 tg3_napi_enable(tp);
7411 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7412 tg3_enable_ints(tp);
7413}
7414
David S. Millerf47c11e2005-06-24 20:18:35 -07007415static void tg3_irq_quiesce(struct tg3 *tp)
7416{
Matt Carlson4f125f42009-09-01 12:55:02 +00007417 int i;
7418
David S. Millerf47c11e2005-06-24 20:18:35 -07007419 BUG_ON(tp->irq_sync);
7420
7421 tp->irq_sync = 1;
7422 smp_mb();
7423
Matt Carlson4f125f42009-09-01 12:55:02 +00007424 for (i = 0; i < tp->irq_cnt; i++)
7425 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007426}
7427
David S. Millerf47c11e2005-06-24 20:18:35 -07007428/* Fully shutdown all tg3 driver activity elsewhere in the system.
7429 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7430 * with as well. Most of the time, this is not necessary except when
7431 * shutting down the device.
7432 */
7433static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7434{
Michael Chan46966542007-07-11 19:47:19 -07007435 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007436 if (irq_sync)
7437 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007438}
7439
7440static inline void tg3_full_unlock(struct tg3 *tp)
7441{
David S. Millerf47c11e2005-06-24 20:18:35 -07007442 spin_unlock_bh(&tp->lock);
7443}
7444
Michael Chanfcfa0a32006-03-20 22:28:41 -08007445/* One-shot MSI handler - Chip automatically disables interrupt
7446 * after sending MSI so driver doesn't have to do it.
7447 */
David Howells7d12e782006-10-05 14:55:46 +01007448static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007449{
Matt Carlson09943a12009-08-28 14:01:57 +00007450 struct tg3_napi *tnapi = dev_id;
7451 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007452
Matt Carlson898a56f2009-08-28 14:02:40 +00007453 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007454 if (tnapi->rx_rcb)
7455 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007456
7457 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007458 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007459
7460 return IRQ_HANDLED;
7461}
7462
Michael Chan88b06bc22005-04-21 17:13:25 -07007463/* MSI ISR - No need to check for interrupt sharing and no need to
7464 * flush status block and interrupt mailbox. PCI ordering rules
7465 * guarantee that MSI will arrive after the status block.
7466 */
David Howells7d12e782006-10-05 14:55:46 +01007467static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007468{
Matt Carlson09943a12009-08-28 14:01:57 +00007469 struct tg3_napi *tnapi = dev_id;
7470 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007471
Matt Carlson898a56f2009-08-28 14:02:40 +00007472 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007473 if (tnapi->rx_rcb)
7474 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007475 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007476 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007477 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007478 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007479 * NIC to stop sending us irqs, engaging "in-intr-handler"
7480 * event coalescing.
7481 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007482 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007483 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007484 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007485
Michael Chan88b06bc22005-04-21 17:13:25 -07007486 return IRQ_RETVAL(1);
7487}
7488
David Howells7d12e782006-10-05 14:55:46 +01007489static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490{
Matt Carlson09943a12009-08-28 14:01:57 +00007491 struct tg3_napi *tnapi = dev_id;
7492 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007493 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 unsigned int handled = 1;
7495
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 /* In INTx mode, it is possible for the interrupt to arrive at
7497 * the CPU before the status block posted prior to the interrupt.
7498 * Reading the PCI State register will confirm whether the
7499 * interrupt is ours and will flush the status block.
7500 */
Michael Chand18edcb2007-03-24 20:57:11 -07007501 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007502 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007503 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7504 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007505 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007506 }
Michael Chand18edcb2007-03-24 20:57:11 -07007507 }
7508
7509 /*
7510 * Writing any value to intr-mbox-0 clears PCI INTA# and
7511 * chip-internal interrupt pending events.
7512 * Writing non-zero to intr-mbox-0 additional tells the
7513 * NIC to stop sending us irqs, engaging "in-intr-handler"
7514 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007515 *
7516 * Flush the mailbox to de-assert the IRQ immediately to prevent
7517 * spurious interrupts. The flush impacts performance but
7518 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007519 */
Michael Chanc04cb342007-05-07 00:26:15 -07007520 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007521 if (tg3_irq_sync(tp))
7522 goto out;
7523 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007524 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007525 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007526 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007527 } else {
7528 /* No work, shared interrupt perhaps? re-enable
7529 * interrupts, and flush that PCI write
7530 */
7531 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7532 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007533 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007534out:
David S. Millerfac9b832005-05-18 22:46:34 -07007535 return IRQ_RETVAL(handled);
7536}
7537
David Howells7d12e782006-10-05 14:55:46 +01007538static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007539{
Matt Carlson09943a12009-08-28 14:01:57 +00007540 struct tg3_napi *tnapi = dev_id;
7541 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007542 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007543 unsigned int handled = 1;
7544
David S. Millerfac9b832005-05-18 22:46:34 -07007545 /* In INTx mode, it is possible for the interrupt to arrive at
7546 * the CPU before the status block posted prior to the interrupt.
7547 * Reading the PCI State register will confirm whether the
7548 * interrupt is ours and will flush the status block.
7549 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007550 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007551 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007552 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7553 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007554 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 }
Michael Chand18edcb2007-03-24 20:57:11 -07007556 }
7557
7558 /*
7559 * writing any value to intr-mbox-0 clears PCI INTA# and
7560 * chip-internal interrupt pending events.
7561 * writing non-zero to intr-mbox-0 additional tells the
7562 * NIC to stop sending us irqs, engaging "in-intr-handler"
7563 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007564 *
7565 * Flush the mailbox to de-assert the IRQ immediately to prevent
7566 * spurious interrupts. The flush impacts performance but
7567 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007568 */
Michael Chanc04cb342007-05-07 00:26:15 -07007569 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007570
7571 /*
7572 * In a shared interrupt configuration, sometimes other devices'
7573 * interrupts will scream. We record the current status tag here
7574 * so that the above check can report that the screaming interrupts
7575 * are unhandled. Eventually they will be silenced.
7576 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007577 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007578
Michael Chand18edcb2007-03-24 20:57:11 -07007579 if (tg3_irq_sync(tp))
7580 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007581
Matt Carlson72334482009-08-28 14:03:01 +00007582 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007583
Matt Carlson09943a12009-08-28 14:01:57 +00007584 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007585
David S. Millerf47c11e2005-06-24 20:18:35 -07007586out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 return IRQ_RETVAL(handled);
7588}
7589
Michael Chan79381092005-04-21 17:13:59 -07007590/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007591static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007592{
Matt Carlson09943a12009-08-28 14:01:57 +00007593 struct tg3_napi *tnapi = dev_id;
7594 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007595 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007596
Michael Chanf9804dd2005-09-27 12:13:10 -07007597 if ((sblk->status & SD_STATUS_UPDATED) ||
7598 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007599 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007600 return IRQ_RETVAL(1);
7601 }
7602 return IRQ_RETVAL(0);
7603}
7604
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605#ifdef CONFIG_NET_POLL_CONTROLLER
7606static void tg3_poll_controller(struct net_device *dev)
7607{
Matt Carlson4f125f42009-09-01 12:55:02 +00007608 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007609 struct tg3 *tp = netdev_priv(dev);
7610
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007611 if (tg3_irq_sync(tp))
7612 return;
7613
Matt Carlson4f125f42009-09-01 12:55:02 +00007614 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007615 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616}
7617#endif
7618
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619static void tg3_tx_timeout(struct net_device *dev)
7620{
7621 struct tg3 *tp = netdev_priv(dev);
7622
Michael Chanb0408752007-02-13 12:18:30 -08007623 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007624 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007625 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
Matt Carlsondb219972011-11-04 09:15:03 +00007628 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629}
7630
Michael Chanc58ec932005-09-17 00:46:27 -07007631/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7632static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7633{
7634 u32 base = (u32) mapping & 0xffffffff;
7635
Nithin Sujir37567912013-12-19 17:44:11 -08007636 return base + len + 8 < base;
Michael Chanc58ec932005-09-17 00:46:27 -07007637}
7638
Michael Chan0f0d1512013-05-13 11:04:16 +00007639/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7640 * of any 4GB boundaries: 4G, 8G, etc
7641 */
7642static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7643 u32 len, u32 mss)
7644{
7645 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7646 u32 base = (u32) mapping & 0xffffffff;
7647
7648 return ((base + len + (mss & 0x3fff)) < base);
7649 }
7650 return 0;
7651}
7652
Michael Chan72f2afb2006-03-06 19:28:35 -08007653/* Test for DMA addresses > 40-bit */
7654static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7655 int len)
7656{
7657#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007658 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007659 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007660 return 0;
7661#else
7662 return 0;
7663#endif
7664}
7665
Matt Carlsond1a3b732011-07-27 14:20:51 +00007666static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007667 dma_addr_t mapping, u32 len, u32 flags,
7668 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007669{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007670 txbd->addr_hi = ((u64) mapping >> 32);
7671 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7672 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7673 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007674}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675
Matt Carlson84b67b22011-07-27 14:20:52 +00007676static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007677 dma_addr_t map, u32 len, u32 flags,
7678 u32 mss, u32 vlan)
7679{
7680 struct tg3 *tp = tnapi->tp;
7681 bool hwbug = false;
7682
7683 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007684 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007685
7686 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007687 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007688
Michael Chan0f0d1512013-05-13 11:04:16 +00007689 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7690 hwbug = true;
7691
Matt Carlsond1a3b732011-07-27 14:20:51 +00007692 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007693 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007694
Matt Carlsona4cb4282011-12-14 11:09:58 +00007695 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007696 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007697 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007698 while (len > tp->dma_limit && *budget) {
7699 u32 frag_len = tp->dma_limit;
7700 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007701
Matt Carlsonb9e45482011-11-04 09:14:59 +00007702 /* Avoid the 8byte DMA problem */
7703 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007704 len += tp->dma_limit / 2;
7705 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007706 }
7707
Matt Carlsonb9e45482011-11-04 09:14:59 +00007708 tnapi->tx_buffers[*entry].fragmented = true;
7709
7710 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7711 frag_len, tmp_flag, mss, vlan);
7712 *budget -= 1;
7713 prvidx = *entry;
7714 *entry = NEXT_TX(*entry);
7715
Matt Carlsone31aa982011-07-27 14:20:53 +00007716 map += frag_len;
7717 }
7718
7719 if (len) {
7720 if (*budget) {
7721 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7722 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007723 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007724 *entry = NEXT_TX(*entry);
7725 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007726 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007727 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007728 }
7729 }
7730 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007731 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7732 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007733 *entry = NEXT_TX(*entry);
7734 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007735
7736 return hwbug;
7737}
7738
Matt Carlson0d681b22011-07-27 14:20:49 +00007739static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007740{
7741 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007742 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007743 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007744
Matt Carlson0d681b22011-07-27 14:20:49 +00007745 skb = txb->skb;
7746 txb->skb = NULL;
7747
Matt Carlson432aa7e2011-05-19 12:12:45 +00007748 pci_unmap_single(tnapi->tp->pdev,
7749 dma_unmap_addr(txb, mapping),
7750 skb_headlen(skb),
7751 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007752
7753 while (txb->fragmented) {
7754 txb->fragmented = false;
7755 entry = NEXT_TX(entry);
7756 txb = &tnapi->tx_buffers[entry];
7757 }
7758
Matt Carlsonba1142e2011-11-04 09:15:00 +00007759 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007760 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007761
7762 entry = NEXT_TX(entry);
7763 txb = &tnapi->tx_buffers[entry];
7764
7765 pci_unmap_page(tnapi->tp->pdev,
7766 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007767 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007768
7769 while (txb->fragmented) {
7770 txb->fragmented = false;
7771 entry = NEXT_TX(entry);
7772 txb = &tnapi->tx_buffers[entry];
7773 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007774 }
7775}
7776
Michael Chan72f2afb2006-03-06 19:28:35 -08007777/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007778static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007779 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007780 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007781 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007783 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007784 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007785 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007786 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787
Joe Perches41535772013-02-16 11:20:04 +00007788 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007789 new_skb = skb_copy(skb, GFP_ATOMIC);
7790 else {
7791 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7792
7793 new_skb = skb_copy_expand(skb,
7794 skb_headroom(skb) + more_headroom,
7795 skb_tailroom(skb), GFP_ATOMIC);
7796 }
7797
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007799 ret = -1;
7800 } else {
7801 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007802 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7803 PCI_DMA_TODEVICE);
7804 /* Make sure the mapping succeeded */
7805 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007806 dev_kfree_skb_any(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007807 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007808 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007809 u32 save_entry = *entry;
7810
Matt Carlson92cd3a12011-07-27 14:20:47 +00007811 base_flags |= TXD_FLAG_END;
7812
Matt Carlson84b67b22011-07-27 14:20:52 +00007813 tnapi->tx_buffers[*entry].skb = new_skb;
7814 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007815 mapping, new_addr);
7816
Matt Carlson84b67b22011-07-27 14:20:52 +00007817 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007818 new_skb->len, base_flags,
7819 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007820 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007821 dev_kfree_skb_any(new_skb);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007822 ret = -1;
7823 }
Michael Chanc58ec932005-09-17 00:46:27 -07007824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 }
7826
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007827 dev_kfree_skb_any(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007828 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007829 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830}
7831
Matt Carlson2ffcc982011-05-19 12:12:44 +00007832static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007833
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007834/* Use GSO to workaround all TSO packets that meet HW bug conditions
7835 * indicated in tg3_tx_frag_set()
Michael Chan52c0fd82006-06-29 20:15:54 -07007836 */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007837static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
7838 struct netdev_queue *txq, struct sk_buff *skb)
Michael Chan52c0fd82006-06-29 20:15:54 -07007839{
7840 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007841 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007842
7843 /* Estimate the number of fragments in the worst case */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007844 if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
7845 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007846
7847 /* netif_tx_stop_queue() must be done before checking
7848 * checking tx index in tg3_tx_avail() below, because in
7849 * tg3_tx(), we update tx index before checking for
7850 * netif_tx_queue_stopped().
7851 */
7852 smp_mb();
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007853 if (tg3_tx_avail(tnapi) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007854 return NETDEV_TX_BUSY;
7855
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007856 netif_tx_wake_queue(txq);
Michael Chan52c0fd82006-06-29 20:15:54 -07007857 }
7858
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007859 segs = skb_gso_segment(skb, tp->dev->features &
7860 ~(NETIF_F_TSO | NETIF_F_TSO6));
Prashant Sreedharan40c1dea2014-06-18 18:38:13 -07007861 if (IS_ERR(segs) || !segs)
Michael Chan52c0fd82006-06-29 20:15:54 -07007862 goto tg3_tso_bug_end;
7863
7864 do {
7865 nskb = segs;
7866 segs = segs->next;
7867 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007868 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007869 } while (segs);
7870
7871tg3_tso_bug_end:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007872 dev_kfree_skb_any(skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007873
7874 return NETDEV_TX_OK;
7875}
Michael Chan52c0fd82006-06-29 20:15:54 -07007876
Michael Chand71c0dc2014-05-11 20:22:53 -07007877/* hard_start_xmit for all devices */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007878static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007879{
7880 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007881 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007882 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007883 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007884 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007885 struct tg3_napi *tnapi;
7886 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007887 unsigned int last;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007888 struct iphdr *iph = NULL;
7889 struct tcphdr *tcph = NULL;
7890 __sum16 tcp_csum = 0, ip_csum = 0;
7891 __be16 ip_tot_len = 0;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007892
Matt Carlson24f4efd2009-11-13 13:03:35 +00007893 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7894 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007895 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007896 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897
Matt Carlson84b67b22011-07-27 14:20:52 +00007898 budget = tg3_tx_avail(tnapi);
7899
Michael Chan00b70502006-06-17 21:58:45 -07007900 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007901 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007902 * interrupt. Furthermore, IRQ processing runs lockless so we have
7903 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007905 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007906 if (!netif_tx_queue_stopped(txq)) {
7907 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007908
7909 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007910 netdev_err(dev,
7911 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 return NETDEV_TX_BUSY;
7914 }
7915
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007916 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 base_flags = 0;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007918
Matt Carlsonbe98da62010-07-11 09:31:46 +00007919 mss = skb_shinfo(skb)->gso_size;
7920 if (mss) {
Matt Carlson34195c32010-07-11 09:31:42 +00007921 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922
françois romieu105dcb52014-03-29 12:26:29 +01007923 if (skb_cow_head(skb, 0))
Eric Dumazet48855432011-10-24 07:53:03 +00007924 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925
Matt Carlson34195c32010-07-11 09:31:42 +00007926 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007927 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928
Eric Dumazeta5a11952012-01-23 01:22:09 +00007929 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007930
Vlad Yasevich476c1882014-09-18 10:31:17 -04007931 /* HW/FW can not correctly segment packets that have been
7932 * vlan encapsulated.
7933 */
7934 if (skb->protocol == htons(ETH_P_8021Q) ||
7935 skb->protocol == htons(ETH_P_8021AD))
7936 return tg3_tso_bug(tp, tnapi, txq, skb);
7937
Eric Dumazeta5a11952012-01-23 01:22:09 +00007938 if (!skb_is_gso_v6(skb)) {
Michael Chand71c0dc2014-05-11 20:22:53 -07007939 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
7940 tg3_flag(tp, TSO_BUG))
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007941 return tg3_tso_bug(tp, tnapi, txq, skb);
Michael Chand71c0dc2014-05-11 20:22:53 -07007942
Michael Chand3f6f3a2014-05-11 20:22:54 -07007943 ip_csum = iph->check;
7944 ip_tot_len = iph->tot_len;
Matt Carlson34195c32010-07-11 09:31:42 +00007945 iph->check = 0;
7946 iph->tot_len = htons(mss + hdr_len);
7947 }
7948
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7950 TXD_FLAG_CPU_POST_DMA);
7951
Michael Chand3f6f3a2014-05-11 20:22:54 -07007952 tcph = tcp_hdr(skb);
7953 tcp_csum = tcph->check;
7954
Joe Perches63c3a662011-04-26 08:12:10 +00007955 if (tg3_flag(tp, HW_TSO_1) ||
7956 tg3_flag(tp, HW_TSO_2) ||
7957 tg3_flag(tp, HW_TSO_3)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07007958 tcph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007960 } else {
7961 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
7962 0, IPPROTO_TCP, 0);
7963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964
Joe Perches63c3a662011-04-26 08:12:10 +00007965 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007966 mss |= (hdr_len & 0xc) << 12;
7967 if (hdr_len & 0x10)
7968 base_flags |= 0x00000010;
7969 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007970 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007971 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007972 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007973 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007974 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 int tsflags;
7976
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007977 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978 mss |= (tsflags << 11);
7979 }
7980 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007981 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 int tsflags;
7983
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007984 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 base_flags |= tsflags << 12;
7986 }
7987 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04007988 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7989 /* HW/FW can not correctly checksum packets that have been
7990 * vlan encapsulated.
7991 */
7992 if (skb->protocol == htons(ETH_P_8021Q) ||
7993 skb->protocol == htons(ETH_P_8021AD)) {
7994 if (skb_checksum_help(skb))
7995 goto drop;
7996 } else {
7997 base_flags |= TXD_FLAG_TCPUDP_CSUM;
7998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00008000
Matt Carlson93a700a2011-08-31 11:44:54 +00008001 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
8002 !mss && skb->len > VLAN_ETH_FRAME_LEN)
8003 base_flags |= TXD_FLAG_JMB_PKT;
8004
Matt Carlson92cd3a12011-07-27 14:20:47 +00008005 if (vlan_tx_tag_present(skb)) {
8006 base_flags |= TXD_FLAG_VLAN;
8007 vlan = vlan_tx_tag_get(skb);
8008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00008010 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
8011 tg3_flag(tp, TX_TSTAMP_EN)) {
8012 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
8013 base_flags |= TXD_FLAG_HWTSTAMP;
8014 }
8015
Alexander Duyckf4188d82009-12-02 16:48:38 +00008016 len = skb_headlen(skb);
8017
8018 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00008019 if (pci_dma_mapping_error(tp->pdev, mapping))
8020 goto drop;
8021
David S. Miller90079ce2008-09-11 04:52:51 -07008022
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008023 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008024 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025
8026 would_hit_hwbug = 0;
8027
Joe Perches63c3a662011-04-26 08:12:10 +00008028 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07008029 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030
Matt Carlson84b67b22011-07-27 14:20:52 +00008031 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00008032 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008033 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008034 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008035 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008036 u32 tmp_mss = mss;
8037
8038 if (!tg3_flag(tp, HW_TSO_1) &&
8039 !tg3_flag(tp, HW_TSO_2) &&
8040 !tg3_flag(tp, HW_TSO_3))
8041 tmp_mss = 0;
8042
Matt Carlsonc5665a52012-02-13 10:20:12 +00008043 /* Now loop through additional data
8044 * fragments, and queue them.
8045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046 last = skb_shinfo(skb)->nr_frags - 1;
8047 for (i = 0; i <= last; i++) {
8048 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8049
Eric Dumazet9e903e02011-10-18 21:00:24 +00008050 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008051 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008052 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008054 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008055 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008056 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008057 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008058 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
Matt Carlsonb9e45482011-11-04 09:14:59 +00008060 if (!budget ||
8061 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008062 len, base_flags |
8063 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008064 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008065 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008066 break;
8067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 }
8069 }
8070
8071 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008072 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073
Michael Chand3f6f3a2014-05-11 20:22:54 -07008074 if (mss) {
8075 /* If it's a TSO packet, do GSO instead of
8076 * allocating and copying to a large linear SKB
8077 */
8078 if (ip_tot_len) {
8079 iph->check = ip_csum;
8080 iph->tot_len = ip_tot_len;
8081 }
8082 tcph->check = tcp_csum;
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07008083 return tg3_tso_bug(tp, tnapi, txq, skb);
Michael Chand3f6f3a2014-05-11 20:22:54 -07008084 }
8085
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 /* If the workaround fails due to memory/mapping
8087 * failure, silently drop this packet.
8088 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008089 entry = tnapi->tx_prod;
8090 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008091 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008092 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008093 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008094 }
8095
Richard Cochrand515b452011-06-19 03:31:41 +00008096 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008097 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008098
Michael Chan6541b802012-03-04 14:48:14 +00008099 /* Sync BD data before updating mailbox */
8100 wmb();
8101
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008102 tnapi->tx_prod = entry;
8103 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008104 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008105
8106 /* netif_tx_stop_queue() must be done before checking
8107 * checking tx index in tg3_tx_avail() below, because in
8108 * tg3_tx(), we update tx index before checking for
8109 * netif_tx_queue_stopped().
8110 */
8111 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008112 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008113 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115
Prashant Sreedharan2c7c9ea2014-10-13 09:21:42 -07008116 if (!skb->xmit_more || netif_xmit_stopped(txq)) {
8117 /* Packets are ready, update Tx producer idx on card. */
8118 tw32_tx_mbox(tnapi->prodmbox, entry);
8119 mmiowb();
8120 }
8121
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008123
8124dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008125 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008126 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008127drop:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07008128 dev_kfree_skb_any(skb);
Eric Dumazet48855432011-10-24 07:53:03 +00008129drop_nofree:
8130 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008131 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132}
8133
Matt Carlson6e01b202011-08-19 13:58:20 +00008134static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8135{
8136 if (enable) {
8137 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8138 MAC_MODE_PORT_MODE_MASK);
8139
8140 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8141
8142 if (!tg3_flag(tp, 5705_PLUS))
8143 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8144
8145 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8146 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8147 else
8148 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8149 } else {
8150 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8151
8152 if (tg3_flag(tp, 5705_PLUS) ||
8153 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008154 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008155 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8156 }
8157
8158 tw32(MAC_MODE, tp->mac_mode);
8159 udelay(40);
8160}
8161
Matt Carlson941ec902011-08-19 13:58:23 +00008162static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008163{
Matt Carlson941ec902011-08-19 13:58:23 +00008164 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008165
8166 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008167 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008168
Matt Carlson941ec902011-08-19 13:58:23 +00008169 if (extlpbk && tg3_phy_set_extloopbk(tp))
8170 return -EIO;
8171
8172 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008173 switch (speed) {
8174 case SPEED_10:
8175 break;
8176 case SPEED_100:
8177 bmcr |= BMCR_SPEED100;
8178 break;
8179 case SPEED_1000:
8180 default:
8181 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8182 speed = SPEED_100;
8183 bmcr |= BMCR_SPEED100;
8184 } else {
8185 speed = SPEED_1000;
8186 bmcr |= BMCR_SPEED1000;
8187 }
8188 }
8189
Matt Carlson941ec902011-08-19 13:58:23 +00008190 if (extlpbk) {
8191 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8192 tg3_readphy(tp, MII_CTRL1000, &val);
8193 val |= CTL1000_AS_MASTER |
8194 CTL1000_ENABLE_MASTER;
8195 tg3_writephy(tp, MII_CTRL1000, val);
8196 } else {
8197 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8198 MII_TG3_FET_PTEST_TRIM_2;
8199 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8200 }
8201 } else
8202 bmcr |= BMCR_LOOPBACK;
8203
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008204 tg3_writephy(tp, MII_BMCR, bmcr);
8205
8206 /* The write needs to be flushed for the FETs */
8207 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8208 tg3_readphy(tp, MII_BMCR, &bmcr);
8209
8210 udelay(40);
8211
8212 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008213 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008214 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008215 MII_TG3_FET_PTEST_FRC_TX_LINK |
8216 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8217
8218 /* The write needs to be flushed for the AC131 */
8219 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8220 }
8221
8222 /* Reset to prevent losing 1st rx packet intermittently */
8223 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8224 tg3_flag(tp, 5780_CLASS)) {
8225 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8226 udelay(10);
8227 tw32_f(MAC_RX_MODE, tp->rx_mode);
8228 }
8229
8230 mac_mode = tp->mac_mode &
8231 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8232 if (speed == SPEED_1000)
8233 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8234 else
8235 mac_mode |= MAC_MODE_PORT_MODE_MII;
8236
Joe Perches41535772013-02-16 11:20:04 +00008237 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008238 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8239
8240 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8241 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8242 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8243 mac_mode |= MAC_MODE_LINK_POLARITY;
8244
8245 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8246 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8247 }
8248
8249 tw32(MAC_MODE, mac_mode);
8250 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008251
8252 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008253}
8254
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008255static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008256{
8257 struct tg3 *tp = netdev_priv(dev);
8258
8259 if (features & NETIF_F_LOOPBACK) {
8260 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8261 return;
8262
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008263 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008264 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008265 netif_carrier_on(tp->dev);
8266 spin_unlock_bh(&tp->lock);
8267 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8268 } else {
8269 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8270 return;
8271
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008272 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008273 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008274 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008275 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008276 spin_unlock_bh(&tp->lock);
8277 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8278 }
8279}
8280
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008281static netdev_features_t tg3_fix_features(struct net_device *dev,
8282 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008283{
8284 struct tg3 *tp = netdev_priv(dev);
8285
Joe Perches63c3a662011-04-26 08:12:10 +00008286 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008287 features &= ~NETIF_F_ALL_TSO;
8288
8289 return features;
8290}
8291
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008292static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008293{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008294 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008295
8296 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8297 tg3_set_loopback(dev, features);
8298
8299 return 0;
8300}
8301
Matt Carlson21f581a2009-08-28 14:00:25 +00008302static void tg3_rx_prodring_free(struct tg3 *tp,
8303 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 int i;
8306
Matt Carlson8fea32b2010-09-15 08:59:58 +00008307 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008308 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008309 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008310 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008311 tp->rx_pkt_map_sz);
8312
Joe Perches63c3a662011-04-26 08:12:10 +00008313 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008314 for (i = tpr->rx_jmb_cons_idx;
8315 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008316 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008317 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008318 TG3_RX_JMB_MAP_SZ);
8319 }
8320 }
8321
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008322 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324
Matt Carlson2c49a442010-09-30 10:34:35 +00008325 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008326 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008327 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328
Joe Perches63c3a662011-04-26 08:12:10 +00008329 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008330 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008331 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008332 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333 }
8334}
8335
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008336/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337 *
8338 * The chip has been shut down and the driver detached from
8339 * the networking, so no interrupts or new tx packets will
8340 * end up in the driver. tp->{tx,}lock are held and thus
8341 * we may not sleep.
8342 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008343static int tg3_rx_prodring_alloc(struct tg3 *tp,
8344 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345{
Matt Carlson287be122009-08-28 13:58:46 +00008346 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008348 tpr->rx_std_cons_idx = 0;
8349 tpr->rx_std_prod_idx = 0;
8350 tpr->rx_jmb_cons_idx = 0;
8351 tpr->rx_jmb_prod_idx = 0;
8352
Matt Carlson8fea32b2010-09-15 08:59:58 +00008353 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008354 memset(&tpr->rx_std_buffers[0], 0,
8355 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008356 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008357 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008358 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008359 goto done;
8360 }
8361
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008363 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364
Matt Carlson287be122009-08-28 13:58:46 +00008365 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008366 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008367 tp->dev->mtu > ETH_DATA_LEN)
8368 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8369 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008370
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 /* Initialize invariants of the rings, we only set this
8372 * stuff once. This works because the card does not
8373 * write into the rx buffer posting rings.
8374 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008375 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376 struct tg3_rx_buffer_desc *rxd;
8377
Matt Carlson21f581a2009-08-28 14:00:25 +00008378 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008379 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8381 rxd->opaque = (RXD_OPAQUE_RING_STD |
8382 (i << RXD_OPAQUE_INDEX_SHIFT));
8383 }
8384
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008385 /* Now allocate fresh SKBs for each rx ring. */
8386 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008387 unsigned int frag_size;
8388
8389 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8390 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008391 netdev_warn(tp->dev,
8392 "Using a smaller RX standard ring. Only "
8393 "%d out of %d buffers were allocated "
8394 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008395 if (i == 0)
8396 goto initfail;
8397 tp->rx_pending = i;
8398 break;
8399 }
8400 }
8401
Joe Perches63c3a662011-04-26 08:12:10 +00008402 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008403 goto done;
8404
Matt Carlson2c49a442010-09-30 10:34:35 +00008405 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008406
Joe Perches63c3a662011-04-26 08:12:10 +00008407 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008408 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409
Matt Carlson2c49a442010-09-30 10:34:35 +00008410 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008411 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412
Matt Carlson0d86df82010-02-17 15:17:00 +00008413 rxd = &tpr->rx_jmb[i].std;
8414 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8415 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8416 RXD_FLAG_JUMBO;
8417 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8418 (i << RXD_OPAQUE_INDEX_SHIFT));
8419 }
8420
8421 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008422 unsigned int frag_size;
8423
8424 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8425 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008426 netdev_warn(tp->dev,
8427 "Using a smaller RX jumbo ring. Only %d "
8428 "out of %d buffers were allocated "
8429 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008430 if (i == 0)
8431 goto initfail;
8432 tp->rx_jumbo_pending = i;
8433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 }
8435 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008436
8437done:
Michael Chan32d8c572006-07-25 16:38:29 -07008438 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008439
8440initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008441 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008442 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443}
8444
Matt Carlson21f581a2009-08-28 14:00:25 +00008445static void tg3_rx_prodring_fini(struct tg3 *tp,
8446 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447{
Matt Carlson21f581a2009-08-28 14:00:25 +00008448 kfree(tpr->rx_std_buffers);
8449 tpr->rx_std_buffers = NULL;
8450 kfree(tpr->rx_jmb_buffers);
8451 tpr->rx_jmb_buffers = NULL;
8452 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008453 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8454 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008455 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008457 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008458 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8459 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008460 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008462}
8463
Matt Carlson21f581a2009-08-28 14:00:25 +00008464static int tg3_rx_prodring_init(struct tg3 *tp,
8465 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008466{
Matt Carlson2c49a442010-09-30 10:34:35 +00008467 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8468 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008469 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008470 return -ENOMEM;
8471
Matt Carlson4bae65c2010-11-24 08:31:52 +00008472 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8473 TG3_RX_STD_RING_BYTES(tp),
8474 &tpr->rx_std_mapping,
8475 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008476 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008477 goto err_out;
8478
Joe Perches63c3a662011-04-26 08:12:10 +00008479 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008480 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008481 GFP_KERNEL);
8482 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008483 goto err_out;
8484
Matt Carlson4bae65c2010-11-24 08:31:52 +00008485 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8486 TG3_RX_JMB_RING_BYTES(tp),
8487 &tpr->rx_jmb_mapping,
8488 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008489 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008490 goto err_out;
8491 }
8492
8493 return 0;
8494
8495err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008496 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008497 return -ENOMEM;
8498}
8499
8500/* Free up pending packets in all rx/tx rings.
8501 *
8502 * The chip has been shut down and the driver detached from
8503 * the networking, so no interrupts or new tx packets will
8504 * end up in the driver. tp->{tx,}lock is not held and we are not
8505 * in an interrupt context and thus may sleep.
8506 */
8507static void tg3_free_rings(struct tg3 *tp)
8508{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008509 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008510
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008511 for (j = 0; j < tp->irq_cnt; j++) {
8512 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008513
Matt Carlson8fea32b2010-09-15 08:59:58 +00008514 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008515
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008516 if (!tnapi->tx_buffers)
8517 continue;
8518
Matt Carlson0d681b22011-07-27 14:20:49 +00008519 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8520 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008521
Matt Carlson0d681b22011-07-27 14:20:49 +00008522 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008523 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008524
Matt Carlsonba1142e2011-11-04 09:15:00 +00008525 tg3_tx_skb_unmap(tnapi, i,
8526 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008527
8528 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008529 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008530 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008531 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008532}
8533
8534/* Initialize tx/rx rings for packet processing.
8535 *
8536 * The chip has been shut down and the driver detached from
8537 * the networking, so no interrupts or new tx packets will
8538 * end up in the driver. tp->{tx,}lock are held and thus
8539 * we may not sleep.
8540 */
8541static int tg3_init_rings(struct tg3 *tp)
8542{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008543 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008544
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008545 /* Free up all the SKBs. */
8546 tg3_free_rings(tp);
8547
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008548 for (i = 0; i < tp->irq_cnt; i++) {
8549 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008550
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008551 tnapi->last_tag = 0;
8552 tnapi->last_irq_tag = 0;
8553 tnapi->hw_status->status = 0;
8554 tnapi->hw_status->status_tag = 0;
8555 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8556
8557 tnapi->tx_prod = 0;
8558 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008559 if (tnapi->tx_ring)
8560 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008561
8562 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008563 if (tnapi->rx_rcb)
8564 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008565
Matt Carlson8fea32b2010-09-15 08:59:58 +00008566 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008567 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008568 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008569 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008570 }
Matt Carlson72334482009-08-28 14:03:01 +00008571
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008572 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008573}
8574
Michael Chan49a359e2012-09-28 07:12:37 +00008575static void tg3_mem_tx_release(struct tg3 *tp)
8576{
8577 int i;
8578
8579 for (i = 0; i < tp->irq_max; i++) {
8580 struct tg3_napi *tnapi = &tp->napi[i];
8581
8582 if (tnapi->tx_ring) {
8583 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8584 tnapi->tx_ring, tnapi->tx_desc_mapping);
8585 tnapi->tx_ring = NULL;
8586 }
8587
8588 kfree(tnapi->tx_buffers);
8589 tnapi->tx_buffers = NULL;
8590 }
8591}
8592
8593static int tg3_mem_tx_acquire(struct tg3 *tp)
8594{
8595 int i;
8596 struct tg3_napi *tnapi = &tp->napi[0];
8597
8598 /* If multivector TSS is enabled, vector 0 does not handle
8599 * tx interrupts. Don't allocate any resources for it.
8600 */
8601 if (tg3_flag(tp, ENABLE_TSS))
8602 tnapi++;
8603
8604 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8605 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8606 TG3_TX_RING_SIZE, GFP_KERNEL);
8607 if (!tnapi->tx_buffers)
8608 goto err_out;
8609
8610 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8611 TG3_TX_RING_BYTES,
8612 &tnapi->tx_desc_mapping,
8613 GFP_KERNEL);
8614 if (!tnapi->tx_ring)
8615 goto err_out;
8616 }
8617
8618 return 0;
8619
8620err_out:
8621 tg3_mem_tx_release(tp);
8622 return -ENOMEM;
8623}
8624
8625static void tg3_mem_rx_release(struct tg3 *tp)
8626{
8627 int i;
8628
8629 for (i = 0; i < tp->irq_max; i++) {
8630 struct tg3_napi *tnapi = &tp->napi[i];
8631
8632 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8633
8634 if (!tnapi->rx_rcb)
8635 continue;
8636
8637 dma_free_coherent(&tp->pdev->dev,
8638 TG3_RX_RCB_RING_BYTES(tp),
8639 tnapi->rx_rcb,
8640 tnapi->rx_rcb_mapping);
8641 tnapi->rx_rcb = NULL;
8642 }
8643}
8644
8645static int tg3_mem_rx_acquire(struct tg3 *tp)
8646{
8647 unsigned int i, limit;
8648
8649 limit = tp->rxq_cnt;
8650
8651 /* If RSS is enabled, we need a (dummy) producer ring
8652 * set on vector zero. This is the true hw prodring.
8653 */
8654 if (tg3_flag(tp, ENABLE_RSS))
8655 limit++;
8656
8657 for (i = 0; i < limit; i++) {
8658 struct tg3_napi *tnapi = &tp->napi[i];
8659
8660 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8661 goto err_out;
8662
8663 /* If multivector RSS is enabled, vector 0
8664 * does not handle rx or tx interrupts.
8665 * Don't allocate any resources for it.
8666 */
8667 if (!i && tg3_flag(tp, ENABLE_RSS))
8668 continue;
8669
Joe Perchesede23fa82013-08-26 22:45:23 -07008670 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8671 TG3_RX_RCB_RING_BYTES(tp),
8672 &tnapi->rx_rcb_mapping,
8673 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008674 if (!tnapi->rx_rcb)
8675 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008676 }
8677
8678 return 0;
8679
8680err_out:
8681 tg3_mem_rx_release(tp);
8682 return -ENOMEM;
8683}
8684
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008685/*
8686 * Must not be invoked with interrupt sources disabled and
8687 * the hardware shutdown down.
8688 */
8689static void tg3_free_consistent(struct tg3 *tp)
8690{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008691 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008692
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008693 for (i = 0; i < tp->irq_cnt; i++) {
8694 struct tg3_napi *tnapi = &tp->napi[i];
8695
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008696 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008697 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8698 tnapi->hw_status,
8699 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008700 tnapi->hw_status = NULL;
8701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008703
Michael Chan49a359e2012-09-28 07:12:37 +00008704 tg3_mem_rx_release(tp);
8705 tg3_mem_tx_release(tp);
8706
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008708 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8709 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 tp->hw_stats = NULL;
8711 }
8712}
8713
8714/*
8715 * Must not be invoked with interrupt sources disabled and
8716 * the hardware shutdown down. Can sleep.
8717 */
8718static int tg3_alloc_consistent(struct tg3 *tp)
8719{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008720 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008721
Joe Perchesede23fa82013-08-26 22:45:23 -07008722 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8723 sizeof(struct tg3_hw_stats),
8724 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 if (!tp->hw_stats)
8726 goto err_out;
8727
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008728 for (i = 0; i < tp->irq_cnt; i++) {
8729 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008730 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008731
Joe Perchesede23fa82013-08-26 22:45:23 -07008732 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8733 TG3_HW_STATUS_SIZE,
8734 &tnapi->status_mapping,
8735 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008736 if (!tnapi->hw_status)
8737 goto err_out;
8738
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008739 sblk = tnapi->hw_status;
8740
Michael Chan49a359e2012-09-28 07:12:37 +00008741 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008742 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008743
Michael Chan49a359e2012-09-28 07:12:37 +00008744 /*
8745 * When RSS is enabled, the status block format changes
8746 * slightly. The "rx_jumbo_consumer", "reserved",
8747 * and "rx_mini_consumer" members get mapped to the
8748 * other three rx return ring producer indexes.
8749 */
8750 switch (i) {
8751 case 1:
8752 prodptr = &sblk->idx[0].rx_producer;
8753 break;
8754 case 2:
8755 prodptr = &sblk->rx_jumbo_consumer;
8756 break;
8757 case 3:
8758 prodptr = &sblk->reserved;
8759 break;
8760 case 4:
8761 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008762 break;
8763 }
Michael Chan49a359e2012-09-28 07:12:37 +00008764 tnapi->rx_rcb_prod_idx = prodptr;
8765 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008766 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008767 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008768 }
8769
Michael Chan49a359e2012-09-28 07:12:37 +00008770 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8771 goto err_out;
8772
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 return 0;
8774
8775err_out:
8776 tg3_free_consistent(tp);
8777 return -ENOMEM;
8778}
8779
8780#define MAX_WAIT_CNT 1000
8781
8782/* To stop a block, clear the enable bit and poll till it
8783 * clears. tp->lock is held.
8784 */
Joe Perches953c96e2013-04-09 10:18:14 +00008785static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008786{
8787 unsigned int i;
8788 u32 val;
8789
Joe Perches63c3a662011-04-26 08:12:10 +00008790 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008791 switch (ofs) {
8792 case RCVLSC_MODE:
8793 case DMAC_MODE:
8794 case MBFREE_MODE:
8795 case BUFMGR_MODE:
8796 case MEMARB_MODE:
8797 /* We can't enable/disable these bits of the
8798 * 5705/5750, just say success.
8799 */
8800 return 0;
8801
8802 default:
8803 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008805 }
8806
8807 val = tr32(ofs);
8808 val &= ~enable_bit;
8809 tw32_f(ofs, val);
8810
8811 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008812 if (pci_channel_offline(tp->pdev)) {
8813 dev_err(&tp->pdev->dev,
8814 "tg3_stop_block device offline, "
8815 "ofs=%lx enable_bit=%x\n",
8816 ofs, enable_bit);
8817 return -ENODEV;
8818 }
8819
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820 udelay(100);
8821 val = tr32(ofs);
8822 if ((val & enable_bit) == 0)
8823 break;
8824 }
8825
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008826 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008827 dev_err(&tp->pdev->dev,
8828 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8829 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830 return -ENODEV;
8831 }
8832
8833 return 0;
8834}
8835
8836/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008837static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838{
8839 int i, err;
8840
8841 tg3_disable_ints(tp);
8842
Gavin Shan6d446ec2013-06-25 15:24:32 +08008843 if (pci_channel_offline(tp->pdev)) {
8844 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8845 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8846 err = -ENODEV;
8847 goto err_no_dev;
8848 }
8849
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850 tp->rx_mode &= ~RX_MODE_ENABLE;
8851 tw32_f(MAC_RX_MODE, tp->rx_mode);
8852 udelay(10);
8853
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008854 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8855 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8856 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8857 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8858 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8859 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008861 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8862 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8863 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8864 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8865 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8866 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8867 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868
8869 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8870 tw32_f(MAC_MODE, tp->mac_mode);
8871 udelay(40);
8872
8873 tp->tx_mode &= ~TX_MODE_ENABLE;
8874 tw32_f(MAC_TX_MODE, tp->tx_mode);
8875
8876 for (i = 0; i < MAX_WAIT_CNT; i++) {
8877 udelay(100);
8878 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8879 break;
8880 }
8881 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008882 dev_err(&tp->pdev->dev,
8883 "%s timed out, TX_MODE_ENABLE will not clear "
8884 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008885 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008886 }
8887
Michael Chane6de8ad2005-05-05 14:42:41 -07008888 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008889 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8890 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891
8892 tw32(FTQ_RESET, 0xffffffff);
8893 tw32(FTQ_RESET, 0x00000000);
8894
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008895 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8896 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897
Gavin Shan6d446ec2013-06-25 15:24:32 +08008898err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008899 for (i = 0; i < tp->irq_cnt; i++) {
8900 struct tg3_napi *tnapi = &tp->napi[i];
8901 if (tnapi->hw_status)
8902 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904
Linus Torvalds1da177e2005-04-16 15:20:36 -07008905 return err;
8906}
8907
Michael Chanee6a99b2007-07-18 21:49:10 -07008908/* Save PCI command register before chip reset */
8909static void tg3_save_pci_state(struct tg3 *tp)
8910{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008911 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008912}
8913
8914/* Restore PCI state after chip reset */
8915static void tg3_restore_pci_state(struct tg3 *tp)
8916{
8917 u32 val;
8918
8919 /* Re-enable indirect register accesses. */
8920 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8921 tp->misc_host_ctrl);
8922
8923 /* Set MAX PCI retry to zero. */
8924 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008925 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008926 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008927 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008928 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008929 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008930 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008931 PCISTATE_ALLOW_APE_SHMEM_WR |
8932 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008933 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8934
Matt Carlson8a6eac92007-10-21 16:17:55 -07008935 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008936
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008937 if (!tg3_flag(tp, PCI_EXPRESS)) {
8938 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8939 tp->pci_cacheline_sz);
8940 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8941 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008942 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008943
Michael Chanee6a99b2007-07-18 21:49:10 -07008944 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008945 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008946 u16 pcix_cmd;
8947
8948 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8949 &pcix_cmd);
8950 pcix_cmd &= ~PCI_X_CMD_ERO;
8951 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8952 pcix_cmd);
8953 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008954
Joe Perches63c3a662011-04-26 08:12:10 +00008955 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008956
8957 /* Chip reset on 5780 will reset MSI enable bit,
8958 * so need to restore it.
8959 */
Joe Perches63c3a662011-04-26 08:12:10 +00008960 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008961 u16 ctrl;
8962
8963 pci_read_config_word(tp->pdev,
8964 tp->msi_cap + PCI_MSI_FLAGS,
8965 &ctrl);
8966 pci_write_config_word(tp->pdev,
8967 tp->msi_cap + PCI_MSI_FLAGS,
8968 ctrl | PCI_MSI_FLAGS_ENABLE);
8969 val = tr32(MSGINT_MODE);
8970 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8971 }
8972 }
8973}
8974
Nithin Sujirf82995b2014-01-03 10:09:13 -08008975static void tg3_override_clk(struct tg3 *tp)
8976{
8977 u32 val;
8978
8979 switch (tg3_asic_rev(tp)) {
8980 case ASIC_REV_5717:
8981 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
8982 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
8983 TG3_CPMU_MAC_ORIDE_ENABLE);
8984 break;
8985
8986 case ASIC_REV_5719:
8987 case ASIC_REV_5720:
8988 tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8989 break;
8990
8991 default:
8992 return;
8993 }
8994}
8995
8996static void tg3_restore_clk(struct tg3 *tp)
8997{
8998 u32 val;
8999
9000 switch (tg3_asic_rev(tp)) {
9001 case ASIC_REV_5717:
9002 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9003 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
9004 val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
9005 break;
9006
9007 case ASIC_REV_5719:
9008 case ASIC_REV_5720:
9009 val = tr32(TG3_CPMU_CLCK_ORIDE);
9010 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9011 break;
9012
9013 default:
9014 return;
9015 }
9016}
9017
Linus Torvalds1da177e2005-04-16 15:20:36 -07009018/* tp->lock is held. */
9019static int tg3_chip_reset(struct tg3 *tp)
9020{
9021 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07009022 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00009023 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01009025 if (!pci_device_is_present(tp->pdev))
9026 return -ENODEV;
9027
David S. Millerf49639e2006-06-09 11:58:36 -07009028 tg3_nvram_lock(tp);
9029
Matt Carlson77b483f2008-08-15 14:07:24 -07009030 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
9031
David S. Millerf49639e2006-06-09 11:58:36 -07009032 /* No matching tg3_nvram_unlock() after this because
9033 * chip reset below will undo the nvram lock.
9034 */
9035 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036
Michael Chanee6a99b2007-07-18 21:49:10 -07009037 /* GRC_MISC_CFG core clock reset will clear the memory
9038 * enable bit in PCI register 4 and the MSI enable bit
9039 * on some chips, so we save relevant registers here.
9040 */
9041 tg3_save_pci_state(tp);
9042
Joe Perches41535772013-02-16 11:20:04 +00009043 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009044 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08009045 tw32(GRC_FASTBOOT_PC, 0);
9046
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047 /*
9048 * We must avoid the readl() that normally takes place.
9049 * It locks machines, causes machine checks, and other
9050 * fun things. So, temporarily disable the 5701
9051 * hardware workaround, while we do the reset.
9052 */
Michael Chan1ee582d2005-08-09 20:16:46 -07009053 write_op = tp->write32;
9054 if (write_op == tg3_write_flush_reg32)
9055 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056
Michael Chand18edcb2007-03-24 20:57:11 -07009057 /* Prevent the irq handler from reading or writing PCI registers
9058 * during chip reset when the memory enable bit in the PCI command
9059 * register may be cleared. The chip does not generate interrupt
9060 * at this time, but the irq handler may still be called due to irq
9061 * sharing or irqpoll.
9062 */
Joe Perches63c3a662011-04-26 08:12:10 +00009063 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009064 for (i = 0; i < tp->irq_cnt; i++) {
9065 struct tg3_napi *tnapi = &tp->napi[i];
9066 if (tnapi->hw_status) {
9067 tnapi->hw_status->status = 0;
9068 tnapi->hw_status->status_tag = 0;
9069 }
9070 tnapi->last_tag = 0;
9071 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07009072 }
Michael Chand18edcb2007-03-24 20:57:11 -07009073 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00009074
9075 for (i = 0; i < tp->irq_cnt; i++)
9076 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07009077
Joe Perches41535772013-02-16 11:20:04 +00009078 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00009079 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9080 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
9081 }
9082
Linus Torvalds1da177e2005-04-16 15:20:36 -07009083 /* do the reset */
9084 val = GRC_MISC_CFG_CORECLK_RESET;
9085
Joe Perches63c3a662011-04-26 08:12:10 +00009086 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00009087 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00009088 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009089 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00009090 tr32(TG3_PCIE_PHY_TSTCTL) ==
9091 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
9092 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9093
Joe Perches41535772013-02-16 11:20:04 +00009094 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009095 tw32(GRC_MISC_CFG, (1 << 29));
9096 val |= (1 << 29);
9097 }
9098 }
9099
Joe Perches41535772013-02-16 11:20:04 +00009100 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009101 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9102 tw32(GRC_VCPU_EXT_CTRL,
9103 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9104 }
9105
Nithin Sujirf82995b2014-01-03 10:09:13 -08009106 /* Set the clock to the highest frequency to avoid timeouts. With link
9107 * aware mode, the clock speed could be slow and bootcode does not
9108 * complete within the expected time. Override the clock to allow the
9109 * bootcode to finish sooner and then restore it.
9110 */
9111 tg3_override_clk(tp);
9112
Matt Carlsonf37500d2010-08-02 11:25:59 +00009113 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009114 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009116
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117 tw32(GRC_MISC_CFG, val);
9118
Michael Chan1ee582d2005-08-09 20:16:46 -07009119 /* restore 5701 hardware bug workaround write method */
9120 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121
9122 /* Unfortunately, we have to delay before the PCI read back.
9123 * Some 575X chips even will not respond to a PCI cfg access
9124 * when the reset command is given to the chip.
9125 *
9126 * How do these hardware designers expect things to work
9127 * properly if the PCI write is posted for a long period
9128 * of time? It is always necessary to have some method by
9129 * which a register read back can occur to push the write
9130 * out which does the reset.
9131 *
9132 * For most tg3 variants the trick below was working.
9133 * Ho hum...
9134 */
9135 udelay(120);
9136
9137 /* Flush PCI posted writes. The normal MMIO registers
9138 * are inaccessible at this time so this is the only
9139 * way to make this reliably (actually, this is no longer
9140 * the case, see above). I tried to use indirect
9141 * register read/write but this upset some 5701 variants.
9142 */
9143 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9144
9145 udelay(120);
9146
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009147 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009148 u16 val16;
9149
Joe Perches41535772013-02-16 11:20:04 +00009150 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009151 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152 u32 cfg_val;
9153
9154 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009155 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156 udelay(100);
9157
9158 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9159 pci_write_config_dword(tp->pdev, 0xc4,
9160 cfg_val | (1 << 15));
9161 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009162
Matt Carlsone7126992009-08-25 10:08:16 +00009163 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009164 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009165 /*
9166 * Older PCIe devices only support the 128 byte
9167 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009168 */
Joe Perches63c3a662011-04-26 08:12:10 +00009169 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009170 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9171 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009172
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009173 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009174 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009175 PCI_EXP_DEVSTA_CED |
9176 PCI_EXP_DEVSTA_NFED |
9177 PCI_EXP_DEVSTA_FED |
9178 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009179 }
9180
Michael Chanee6a99b2007-07-18 21:49:10 -07009181 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182
Joe Perches63c3a662011-04-26 08:12:10 +00009183 tg3_flag_clear(tp, CHIP_RESETTING);
9184 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009185
Michael Chanee6a99b2007-07-18 21:49:10 -07009186 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009187 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009188 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009189 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190
Joe Perches41535772013-02-16 11:20:04 +00009191 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192 tg3_stop_fw(tp);
9193 tw32(0x5000, 0x400);
9194 }
9195
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009196 if (tg3_flag(tp, IS_SSB_CORE)) {
9197 /*
9198 * BCM4785: In order to avoid repercussions from using
9199 * potentially defective internal ROM, stop the Rx RISC CPU,
9200 * which is not required.
9201 */
9202 tg3_stop_fw(tp);
9203 tg3_halt_cpu(tp, RX_CPU_BASE);
9204 }
9205
Nithin Sujirfb03a432013-05-21 12:57:32 +00009206 err = tg3_poll_fw(tp);
9207 if (err)
9208 return err;
9209
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210 tw32(GRC_MODE, tp->grc_mode);
9211
Joe Perches41535772013-02-16 11:20:04 +00009212 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009213 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009214
9215 tw32(0xc4, val | (1 << 15));
9216 }
9217
9218 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009219 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009220 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009221 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9223 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9224 }
9225
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009226 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009227 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009228 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009229 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009230 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009231 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009233 val = 0;
9234
9235 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236 udelay(40);
9237
Matt Carlson77b483f2008-08-15 14:07:24 -07009238 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9239
Matt Carlson0a9140c2009-08-28 12:27:50 +00009240 tg3_mdio_start(tp);
9241
Joe Perches63c3a662011-04-26 08:12:10 +00009242 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009243 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9244 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009245 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009246 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247
9248 tw32(0x7c00, val | (1 << 25));
9249 }
9250
Nithin Sujirf82995b2014-01-03 10:09:13 -08009251 tg3_restore_clk(tp);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009252
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009254 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009255 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9256 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9257
Joe Perches63c3a662011-04-26 08:12:10 +00009258 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9260 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9261 u32 nic_cfg;
9262
9263 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9264 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009265 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009266 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009267 if (tg3_flag(tp, 5750_PLUS))
9268 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009269
9270 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9271 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9272 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9273 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9274 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275 }
9276 }
9277
9278 return 0;
9279}
9280
Matt Carlson65ec6982012-02-28 23:33:37 +00009281static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9282static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Michael Chane565eec2014-01-03 10:09:12 -08009283static void __tg3_set_rx_mode(struct net_device *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009284
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009286static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287{
9288 int err;
9289
9290 tg3_stop_fw(tp);
9291
Michael Chan944d9802005-05-29 14:57:48 -07009292 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009294 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 err = tg3_chip_reset(tp);
9296
Joe Perches953c96e2013-04-09 10:18:14 +00009297 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009298
Michael Chan944d9802005-05-29 14:57:48 -07009299 tg3_write_sig_legacy(tp, kind);
9300 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301
Matt Carlson92feeab2011-12-08 14:40:14 +00009302 if (tp->hw_stats) {
9303 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009304 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009305 tg3_get_estats(tp, &tp->estats_prev);
9306
9307 /* And make sure the next sample is new data */
9308 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9309 }
9310
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009311 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312}
9313
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314static int tg3_set_mac_addr(struct net_device *dev, void *p)
9315{
9316 struct tg3 *tp = netdev_priv(dev);
9317 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009318 int err = 0;
9319 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320
Michael Chanf9804dd2005-09-27 12:13:10 -07009321 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009322 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009323
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9325
Michael Chane75f7c92006-03-20 21:33:26 -08009326 if (!netif_running(dev))
9327 return 0;
9328
Joe Perches63c3a662011-04-26 08:12:10 +00009329 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009330 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009331
Michael Chan986e0ae2007-05-05 12:10:20 -07009332 addr0_high = tr32(MAC_ADDR_0_HIGH);
9333 addr0_low = tr32(MAC_ADDR_0_LOW);
9334 addr1_high = tr32(MAC_ADDR_1_HIGH);
9335 addr1_low = tr32(MAC_ADDR_1_LOW);
9336
9337 /* Skip MAC addr 1 if ASF is using it. */
9338 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9339 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009340 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009341 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009342 spin_lock_bh(&tp->lock);
9343 __tg3_set_mac_addr(tp, skip_mac_1);
Michael Chane565eec2014-01-03 10:09:12 -08009344 __tg3_set_rx_mode(dev);
Michael Chan986e0ae2007-05-05 12:10:20 -07009345 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346
Michael Chanb9ec6c12006-07-25 16:37:27 -07009347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348}
9349
9350/* tp->lock is held. */
9351static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9352 dma_addr_t mapping, u32 maxlen_flags,
9353 u32 nic_addr)
9354{
9355 tg3_write_mem(tp,
9356 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9357 ((u64) mapping >> 32));
9358 tg3_write_mem(tp,
9359 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9360 ((u64) mapping & 0xffffffff));
9361 tg3_write_mem(tp,
9362 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9363 maxlen_flags);
9364
Joe Perches63c3a662011-04-26 08:12:10 +00009365 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009366 tg3_write_mem(tp,
9367 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9368 nic_addr);
9369}
9370
Michael Chana489b6d2012-09-28 07:12:39 +00009371
9372static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009373{
Michael Chana489b6d2012-09-28 07:12:39 +00009374 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009375
Joe Perches63c3a662011-04-26 08:12:10 +00009376 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009377 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9378 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9379 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009380 } else {
9381 tw32(HOSTCC_TXCOL_TICKS, 0);
9382 tw32(HOSTCC_TXMAX_FRAMES, 0);
9383 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009384
9385 for (; i < tp->txq_cnt; i++) {
9386 u32 reg;
9387
9388 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9389 tw32(reg, ec->tx_coalesce_usecs);
9390 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9391 tw32(reg, ec->tx_max_coalesced_frames);
9392 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9393 tw32(reg, ec->tx_max_coalesced_frames_irq);
9394 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009395 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009396
Michael Chana489b6d2012-09-28 07:12:39 +00009397 for (; i < tp->irq_max - 1; i++) {
9398 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9399 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9400 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9401 }
9402}
9403
9404static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9405{
9406 int i = 0;
9407 u32 limit = tp->rxq_cnt;
9408
Joe Perches63c3a662011-04-26 08:12:10 +00009409 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009410 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9411 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9412 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009413 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009414 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009415 tw32(HOSTCC_RXCOL_TICKS, 0);
9416 tw32(HOSTCC_RXMAX_FRAMES, 0);
9417 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009418 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009419
Michael Chana489b6d2012-09-28 07:12:39 +00009420 for (; i < limit; i++) {
9421 u32 reg;
9422
9423 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9424 tw32(reg, ec->rx_coalesce_usecs);
9425 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9426 tw32(reg, ec->rx_max_coalesced_frames);
9427 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9428 tw32(reg, ec->rx_max_coalesced_frames_irq);
9429 }
9430
9431 for (; i < tp->irq_max - 1; i++) {
9432 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9433 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9434 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9435 }
9436}
9437
9438static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9439{
9440 tg3_coal_tx_init(tp, ec);
9441 tg3_coal_rx_init(tp, ec);
9442
Joe Perches63c3a662011-04-26 08:12:10 +00009443 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009444 u32 val = ec->stats_block_coalesce_usecs;
9445
Matt Carlsonb6080e12009-09-01 13:12:00 +00009446 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9447 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9448
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009449 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009450 val = 0;
9451
9452 tw32(HOSTCC_STAT_COAL_TICKS, val);
9453 }
9454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455
9456/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009457static void tg3_tx_rcbs_disable(struct tg3 *tp)
9458{
9459 u32 txrcb, limit;
9460
9461 /* Disable all transmit rings but the first. */
9462 if (!tg3_flag(tp, 5705_PLUS))
9463 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9464 else if (tg3_flag(tp, 5717_PLUS))
9465 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9466 else if (tg3_flag(tp, 57765_CLASS) ||
9467 tg3_asic_rev(tp) == ASIC_REV_5762)
9468 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9469 else
9470 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9471
9472 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9473 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9474 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9475 BDINFO_FLAGS_DISABLED);
9476}
9477
9478/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009479static void tg3_tx_rcbs_init(struct tg3 *tp)
9480{
9481 int i = 0;
9482 u32 txrcb = NIC_SRAM_SEND_RCB;
9483
9484 if (tg3_flag(tp, ENABLE_TSS))
9485 i++;
9486
9487 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9488 struct tg3_napi *tnapi = &tp->napi[i];
9489
9490 if (!tnapi->tx_ring)
9491 continue;
9492
9493 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9494 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9495 NIC_SRAM_TX_BUFFER_DESC);
9496 }
9497}
9498
9499/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009500static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9501{
9502 u32 rxrcb, limit;
9503
9504 /* Disable all receive return rings but the first. */
9505 if (tg3_flag(tp, 5717_PLUS))
9506 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9507 else if (!tg3_flag(tp, 5705_PLUS))
9508 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9509 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9510 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9511 tg3_flag(tp, 57765_CLASS))
9512 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9513 else
9514 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9515
9516 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9517 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9518 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9519 BDINFO_FLAGS_DISABLED);
9520}
9521
9522/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009523static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9524{
9525 int i = 0;
9526 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9527
9528 if (tg3_flag(tp, ENABLE_RSS))
9529 i++;
9530
9531 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9532 struct tg3_napi *tnapi = &tp->napi[i];
9533
9534 if (!tnapi->rx_rcb)
9535 continue;
9536
9537 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9538 (tp->rx_ret_ring_mask + 1) <<
9539 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9540 }
9541}
9542
9543/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009544static void tg3_rings_reset(struct tg3 *tp)
9545{
9546 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009547 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009548 struct tg3_napi *tnapi = &tp->napi[0];
9549
Nithin Sujir328947f2013-05-23 11:11:24 +00009550 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009551
Nithin Sujir328947f2013-05-23 11:11:24 +00009552 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009553
9554 /* Disable interrupts */
9555 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009556 tp->napi[0].chk_msi_cnt = 0;
9557 tp->napi[0].last_rx_cons = 0;
9558 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009559
9560 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009561 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009562 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009563 tp->napi[i].tx_prod = 0;
9564 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009565 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009566 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009567 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9568 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009569 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009570 tp->napi[i].last_rx_cons = 0;
9571 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009572 }
Joe Perches63c3a662011-04-26 08:12:10 +00009573 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009574 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009575 } else {
9576 tp->napi[0].tx_prod = 0;
9577 tp->napi[0].tx_cons = 0;
9578 tw32_mailbox(tp->napi[0].prodmbox, 0);
9579 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9580 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009581
9582 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009583 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009584 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9585 for (i = 0; i < 16; i++)
9586 tw32_tx_mbox(mbox + i * 8, 0);
9587 }
9588
Matt Carlson2d31eca2009-09-01 12:53:31 +00009589 /* Clear status block in ram. */
9590 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9591
9592 /* Set status block DMA address */
9593 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9594 ((u64) tnapi->status_mapping >> 32));
9595 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9596 ((u64) tnapi->status_mapping & 0xffffffff));
9597
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009598 stblk = HOSTCC_STATBLCK_RING1;
9599
9600 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9601 u64 mapping = (u64)tnapi->status_mapping;
9602 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9603 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009604 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009605
9606 /* Clear status block in ram. */
9607 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009608 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009609
9610 tg3_tx_rcbs_init(tp);
9611 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009612}
9613
Matt Carlsoneb07a942011-04-20 07:57:36 +00009614static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9615{
9616 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9617
Joe Perches63c3a662011-04-26 08:12:10 +00009618 if (!tg3_flag(tp, 5750_PLUS) ||
9619 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009620 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9621 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009622 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009623 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009624 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9625 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009626 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9627 else
9628 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9629
9630 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9631 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9632
9633 val = min(nic_rep_thresh, host_rep_thresh);
9634 tw32(RCVBDI_STD_THRESH, val);
9635
Joe Perches63c3a662011-04-26 08:12:10 +00009636 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009637 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9638
Joe Perches63c3a662011-04-26 08:12:10 +00009639 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009640 return;
9641
Matt Carlson513aa6e2011-11-21 15:01:18 +00009642 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009643
9644 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9645
9646 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9647 tw32(RCVBDI_JUMBO_THRESH, val);
9648
Joe Perches63c3a662011-04-26 08:12:10 +00009649 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009650 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9651}
9652
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009653static inline u32 calc_crc(unsigned char *buf, int len)
9654{
9655 u32 reg;
9656 u32 tmp;
9657 int j, k;
9658
9659 reg = 0xffffffff;
9660
9661 for (j = 0; j < len; j++) {
9662 reg ^= buf[j];
9663
9664 for (k = 0; k < 8; k++) {
9665 tmp = reg & 0x01;
9666
9667 reg >>= 1;
9668
9669 if (tmp)
9670 reg ^= 0xedb88320;
9671 }
9672 }
9673
9674 return ~reg;
9675}
9676
9677static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9678{
9679 /* accept or reject all multicast frames */
9680 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9681 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9682 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9683 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9684}
9685
9686static void __tg3_set_rx_mode(struct net_device *dev)
9687{
9688 struct tg3 *tp = netdev_priv(dev);
9689 u32 rx_mode;
9690
9691 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9692 RX_MODE_KEEP_VLAN_TAG);
9693
9694#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9695 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9696 * flag clear.
9697 */
9698 if (!tg3_flag(tp, ENABLE_ASF))
9699 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9700#endif
9701
9702 if (dev->flags & IFF_PROMISC) {
9703 /* Promiscuous mode. */
9704 rx_mode |= RX_MODE_PROMISC;
9705 } else if (dev->flags & IFF_ALLMULTI) {
9706 /* Accept all multicast. */
9707 tg3_set_multi(tp, 1);
9708 } else if (netdev_mc_empty(dev)) {
9709 /* Reject all multicast. */
9710 tg3_set_multi(tp, 0);
9711 } else {
9712 /* Accept one or more multicast(s). */
9713 struct netdev_hw_addr *ha;
9714 u32 mc_filter[4] = { 0, };
9715 u32 regidx;
9716 u32 bit;
9717 u32 crc;
9718
9719 netdev_for_each_mc_addr(ha, dev) {
9720 crc = calc_crc(ha->addr, ETH_ALEN);
9721 bit = ~crc & 0x7f;
9722 regidx = (bit & 0x60) >> 5;
9723 bit &= 0x1f;
9724 mc_filter[regidx] |= (1 << bit);
9725 }
9726
9727 tw32(MAC_HASH_REG_0, mc_filter[0]);
9728 tw32(MAC_HASH_REG_1, mc_filter[1]);
9729 tw32(MAC_HASH_REG_2, mc_filter[2]);
9730 tw32(MAC_HASH_REG_3, mc_filter[3]);
9731 }
9732
Michael Chane565eec2014-01-03 10:09:12 -08009733 if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
9734 rx_mode |= RX_MODE_PROMISC;
9735 } else if (!(dev->flags & IFF_PROMISC)) {
9736 /* Add all entries into to the mac addr filter list */
9737 int i = 0;
9738 struct netdev_hw_addr *ha;
9739
9740 netdev_for_each_uc_addr(ha, dev) {
9741 __tg3_set_one_mac_addr(tp, ha->addr,
9742 i + TG3_UCAST_ADDR_IDX(tp));
9743 i++;
9744 }
9745 }
9746
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009747 if (rx_mode != tp->rx_mode) {
9748 tp->rx_mode = rx_mode;
9749 tw32_f(MAC_RX_MODE, rx_mode);
9750 udelay(10);
9751 }
9752}
9753
Michael Chan91024262012-09-28 07:12:38 +00009754static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009755{
9756 int i;
9757
9758 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009759 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009760}
9761
9762static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009763{
9764 int i;
9765
9766 if (!tg3_flag(tp, SUPPORT_MSIX))
9767 return;
9768
Michael Chan0b3ba052012-11-14 14:44:29 +00009769 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009770 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009771 return;
9772 }
9773
9774 /* Validate table against current IRQ count */
9775 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009776 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009777 break;
9778 }
9779
9780 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009781 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009782}
9783
Matt Carlson90415472011-12-16 13:33:23 +00009784static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009785{
9786 int i = 0;
9787 u32 reg = MAC_RSS_INDIR_TBL_0;
9788
9789 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9790 u32 val = tp->rss_ind_tbl[i];
9791 i++;
9792 for (; i % 8; i++) {
9793 val <<= 4;
9794 val |= tp->rss_ind_tbl[i];
9795 }
9796 tw32(reg, val);
9797 reg += 4;
9798 }
9799}
9800
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009801static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9802{
9803 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9804 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9805 else
9806 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9807}
9808
Matt Carlson2d31eca2009-09-01 12:53:31 +00009809/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009810static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811{
9812 u32 val, rdmac_mode;
9813 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009814 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009815
9816 tg3_disable_ints(tp);
9817
9818 tg3_stop_fw(tp);
9819
9820 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9821
Joe Perches63c3a662011-04-26 08:12:10 +00009822 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009823 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009825 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9826 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9827 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009828 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009829 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9830 }
9831
Nithin Sujir400dfba2013-05-18 06:26:53 +00009832 /* Enable MAC control of LPI */
9833 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9834 tg3_setup_eee(tp);
9835
Matt Carlson603f1172010-02-12 14:47:10 +00009836 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009837 tg3_phy_reset(tp);
9838
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 err = tg3_chip_reset(tp);
9840 if (err)
9841 return err;
9842
9843 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9844
Joe Perches41535772013-02-16 11:20:04 +00009845 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009846 val = tr32(TG3_CPMU_CTRL);
9847 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9848 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009849
9850 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9851 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9852 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9853 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9854
9855 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9856 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9857 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9858 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9859
9860 val = tr32(TG3_CPMU_HST_ACC);
9861 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9862 val |= CPMU_HST_ACC_MACCLK_6_25;
9863 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009864 }
9865
Joe Perches41535772013-02-16 11:20:04 +00009866 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009867 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9868 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9869 PCIE_PWR_MGMT_L1_THRESH_4MS;
9870 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009871
9872 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9873 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9874
9875 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009876
Matt Carlsonf40386c2009-11-02 14:24:02 +00009877 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9878 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009879 }
9880
Joe Perches63c3a662011-04-26 08:12:10 +00009881 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009882 u32 grc_mode = tr32(GRC_MODE);
9883
9884 /* Access the lower 1K of PL PCIE block registers. */
9885 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9886 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9887
9888 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9889 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9890 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9891
9892 tw32(GRC_MODE, grc_mode);
9893 }
9894
Matt Carlson55086ad2011-12-14 11:09:59 +00009895 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009896 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009897 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009898
Matt Carlson5093eed2010-11-24 08:31:45 +00009899 /* Access the lower 1K of PL PCIE block registers. */
9900 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9901 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009902
Matt Carlson5093eed2010-11-24 08:31:45 +00009903 val = tr32(TG3_PCIE_TLDLPL_PORT +
9904 TG3_PCIE_PL_LO_PHYCTL5);
9905 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9906 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009907
Matt Carlson5093eed2010-11-24 08:31:45 +00009908 tw32(GRC_MODE, grc_mode);
9909 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009910
Joe Perches41535772013-02-16 11:20:04 +00009911 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009912 u32 grc_mode;
9913
9914 /* Fix transmit hangs */
9915 val = tr32(TG3_CPMU_PADRNG_CTL);
9916 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9917 tw32(TG3_CPMU_PADRNG_CTL, val);
9918
9919 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009920
9921 /* Access the lower 1K of DL PCIE block registers. */
9922 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9923 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9924
9925 val = tr32(TG3_PCIE_TLDLPL_PORT +
9926 TG3_PCIE_DL_LO_FTSMAX);
9927 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9928 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9929 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9930
9931 tw32(GRC_MODE, grc_mode);
9932 }
9933
Matt Carlsona977dbe2010-04-12 06:58:26 +00009934 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9935 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9936 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9937 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009938 }
9939
Linus Torvalds1da177e2005-04-16 15:20:36 -07009940 /* This works around an issue with Athlon chipsets on
9941 * B3 tigon3 silicon. This bit has no effect on any
9942 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009943 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 */
Joe Perches63c3a662011-04-26 08:12:10 +00009945 if (!tg3_flag(tp, CPMU_PRESENT)) {
9946 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009947 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9948 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950
Joe Perches41535772013-02-16 11:20:04 +00009951 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009952 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 val = tr32(TG3PCI_PCISTATE);
9954 val |= PCISTATE_RETRY_SAME_DMA;
9955 tw32(TG3PCI_PCISTATE, val);
9956 }
9957
Joe Perches63c3a662011-04-26 08:12:10 +00009958 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009959 /* Allow reads and writes to the
9960 * APE register and memory space.
9961 */
9962 val = tr32(TG3PCI_PCISTATE);
9963 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009964 PCISTATE_ALLOW_APE_SHMEM_WR |
9965 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009966 tw32(TG3PCI_PCISTATE, val);
9967 }
9968
Joe Perches41535772013-02-16 11:20:04 +00009969 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970 /* Enable some hw fixes. */
9971 val = tr32(TG3PCI_MSI_DATA);
9972 val |= (1 << 26) | (1 << 28) | (1 << 29);
9973 tw32(TG3PCI_MSI_DATA, val);
9974 }
9975
9976 /* Descriptor ring init may make accesses to the
9977 * NIC SRAM area to setup the TX descriptors, so we
9978 * can only do this after the hardware has been
9979 * successfully reset.
9980 */
Michael Chan32d8c572006-07-25 16:38:29 -07009981 err = tg3_init_rings(tp);
9982 if (err)
9983 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984
Joe Perches63c3a662011-04-26 08:12:10 +00009985 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009986 val = tr32(TG3PCI_DMA_RW_CTRL) &
9987 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009988 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009989 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009990 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009991 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9992 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009993 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009994 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009995 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9996 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009997 /* This value is determined during the probe time DMA
9998 * engine test, tg3_test_dma.
9999 */
10000 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
10001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002
10003 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
10004 GRC_MODE_4X_NIC_SEND_RINGS |
10005 GRC_MODE_NO_TX_PHDR_CSUM |
10006 GRC_MODE_NO_RX_PHDR_CSUM);
10007 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -070010008
10009 /* Pseudo-header checksum is done by hardware logic and not
10010 * the offload processers, so make the chip do the pseudo-
10011 * header checksums on receive. For transmit it is more
10012 * convenient to do the pseudo-header checksum in software
10013 * as Linux does that on transmit for us in all cases.
10014 */
10015 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010016
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000010017 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
10018 if (tp->rxptpctl)
10019 tw32(TG3_RX_PTP_CTL,
10020 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
10021
10022 if (tg3_flag(tp, PTP_CAPABLE))
10023 val |= GRC_MODE_TIME_SYNC_ENABLE;
10024
10025 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026
10027 /* Setup the timer prescalar register. Clock is always 66Mhz. */
10028 val = tr32(GRC_MISC_CFG);
10029 val &= ~0xff;
10030 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
10031 tw32(GRC_MISC_CFG, val);
10032
10033 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +000010034 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +000010036 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010037 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +000010038 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010039 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
10040 else
10041 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
10042 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
10043 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +000010044 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010045 int fw_len;
10046
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080010047 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010048 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
10049 tw32(BUFMGR_MB_POOL_ADDR,
10050 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
10051 tw32(BUFMGR_MB_POOL_SIZE,
10052 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
10053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054
Michael Chan0f893dc2005-07-25 12:30:38 -070010055 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10057 tp->bufmgr_config.mbuf_read_dma_low_water);
10058 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10059 tp->bufmgr_config.mbuf_mac_rx_low_water);
10060 tw32(BUFMGR_MB_HIGH_WATER,
10061 tp->bufmgr_config.mbuf_high_water);
10062 } else {
10063 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10064 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
10065 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10066 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
10067 tw32(BUFMGR_MB_HIGH_WATER,
10068 tp->bufmgr_config.mbuf_high_water_jumbo);
10069 }
10070 tw32(BUFMGR_DMA_LOW_WATER,
10071 tp->bufmgr_config.dma_low_water);
10072 tw32(BUFMGR_DMA_HIGH_WATER,
10073 tp->bufmgr_config.dma_high_water);
10074
Matt Carlsond309a462010-09-30 10:34:31 +000010075 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +000010076 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +000010077 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +000010078 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080010079 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000010080 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10081 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +000010082 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +000010083 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 for (i = 0; i < 2000; i++) {
10085 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
10086 break;
10087 udelay(10);
10088 }
10089 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +000010090 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091 return -ENODEV;
10092 }
10093
Joe Perches41535772013-02-16 11:20:04 +000010094 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +000010095 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -070010096
Matt Carlsoneb07a942011-04-20 07:57:36 +000010097 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098
10099 /* Initialize TG3_BDINFO's at:
10100 * RCVDBDI_STD_BD: standard eth size rx ring
10101 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
10102 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
10103 *
10104 * like so:
10105 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
10106 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
10107 * ring attribute flags
10108 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
10109 *
10110 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
10111 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
10112 *
10113 * The size of each ring is fixed in the firmware, but the location is
10114 * configurable.
10115 */
10116 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010117 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010119 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010120 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010121 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10122 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010124 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010125 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10127 BDINFO_FLAGS_DISABLED);
10128
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010129 /* Program the jumbo buffer descriptor ring control
10130 * blocks on those devices that have them.
10131 */
Joe Perches41535772013-02-16 11:20:04 +000010132 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010133 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010134
Joe Perches63c3a662011-04-26 08:12:10 +000010135 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010137 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010139 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010140 val = TG3_RX_JMB_RING_SIZE(tp) <<
10141 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010143 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010144 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010145 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010146 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010147 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10148 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149 } else {
10150 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10151 BDINFO_FLAGS_DISABLED);
10152 }
10153
Joe Perches63c3a662011-04-26 08:12:10 +000010154 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010155 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010156 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10157 val |= (TG3_RX_STD_DMA_SZ << 2);
10158 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010159 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010160 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010161 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010162
10163 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164
Matt Carlson411da642009-11-13 13:03:46 +000010165 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +000010166 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167
Joe Perches63c3a662011-04-26 08:12:10 +000010168 tpr->rx_jmb_prod_idx =
10169 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +000010170 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171
Matt Carlson2d31eca2009-09-01 12:53:31 +000010172 tg3_rings_reset(tp);
10173
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010175 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176
10177 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010178 tw32(MAC_RX_MTU_SIZE,
10179 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180
10181 /* The slot time is changed by tg3_setup_phy if we
10182 * run at gigabit with half duplex.
10183 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010184 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10185 (6 << TX_LENGTHS_IPG_SHIFT) |
10186 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10187
Joe Perches41535772013-02-16 11:20:04 +000010188 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10189 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010190 val |= tr32(MAC_TX_LENGTHS) &
10191 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10192 TX_LENGTHS_CNT_DWN_VAL_MSK);
10193
10194 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195
10196 /* Receive rules. */
10197 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10198 tw32(RCVLPC_CONFIG, 0x0181);
10199
10200 /* Calculate RDMAC_MODE setting early, we need it to determine
10201 * the RCVLPC_STATE_ENABLE mask.
10202 */
10203 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10204 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10205 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10206 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10207 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010208
Joe Perches41535772013-02-16 11:20:04 +000010209 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010210 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10211
Joe Perches41535772013-02-16 11:20:04 +000010212 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10213 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10214 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010215 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10216 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10217 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10218
Joe Perches41535772013-02-16 11:20:04 +000010219 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10220 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010221 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010222 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10224 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010225 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10227 }
10228 }
10229
Joe Perches63c3a662011-04-26 08:12:10 +000010230 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010231 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10232
Joe Perches41535772013-02-16 11:20:04 +000010233 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010234 tp->dma_limit = 0;
10235 if (tp->dev->mtu <= ETH_DATA_LEN) {
10236 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10237 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10238 }
10239 }
10240
Joe Perches63c3a662011-04-26 08:12:10 +000010241 if (tg3_flag(tp, HW_TSO_1) ||
10242 tg3_flag(tp, HW_TSO_2) ||
10243 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010244 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10245
Matt Carlson108a6c12011-05-19 12:12:47 +000010246 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010247 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10248 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010249 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010250
Joe Perches41535772013-02-16 11:20:04 +000010251 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10252 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010253 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10254
Joe Perches41535772013-02-16 11:20:04 +000010255 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10256 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10257 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10258 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010259 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010260 u32 tgtreg;
10261
Joe Perches41535772013-02-16 11:20:04 +000010262 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010263 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10264 else
10265 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10266
10267 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010268 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10269 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010270 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10271 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10272 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10273 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10274 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10275 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010276 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010277 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010278 }
10279
Joe Perches41535772013-02-16 11:20:04 +000010280 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10281 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10282 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010283 u32 tgtreg;
10284
Joe Perches41535772013-02-16 11:20:04 +000010285 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010286 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10287 else
10288 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10289
10290 val = tr32(tgtreg);
10291 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010292 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10293 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10294 }
10295
Linus Torvalds1da177e2005-04-16 15:20:36 -070010296 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010297 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010298 val = tr32(RCVLPC_STATS_ENABLE);
10299 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10300 tw32(RCVLPC_STATS_ENABLE, val);
10301 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010302 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303 val = tr32(RCVLPC_STATS_ENABLE);
10304 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10305 tw32(RCVLPC_STATS_ENABLE, val);
10306 } else {
10307 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10308 }
10309 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10310 tw32(SNDDATAI_STATSENAB, 0xffffff);
10311 tw32(SNDDATAI_STATSCTRL,
10312 (SNDDATAI_SCTRL_ENABLE |
10313 SNDDATAI_SCTRL_FASTUPD));
10314
10315 /* Setup host coalescing engine. */
10316 tw32(HOSTCC_MODE, 0);
10317 for (i = 0; i < 2000; i++) {
10318 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10319 break;
10320 udelay(10);
10321 }
10322
Michael Chand244c892005-07-05 14:42:33 -070010323 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324
Joe Perches63c3a662011-04-26 08:12:10 +000010325 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010326 /* Status/statistics block address. See tg3_timer,
10327 * the tg3_periodic_fetch_stats call there, and
10328 * tg3_get_stats to see how this works for 5705/5750 chips.
10329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10331 ((u64) tp->stats_mapping >> 32));
10332 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10333 ((u64) tp->stats_mapping & 0xffffffff));
10334 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010335
Linus Torvalds1da177e2005-04-16 15:20:36 -070010336 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010337
10338 /* Clear statistics and status block memory areas */
10339 for (i = NIC_SRAM_STATS_BLK;
10340 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10341 i += sizeof(u32)) {
10342 tg3_write_mem(tp, i, 0);
10343 udelay(40);
10344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010345 }
10346
10347 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10348
10349 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10350 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010351 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10353
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010354 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10355 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010356 /* reset to prevent losing 1st rx packet intermittently */
10357 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10358 udelay(10);
10359 }
10360
Matt Carlson3bda1252008-08-15 14:08:22 -070010361 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010362 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10363 MAC_MODE_FHDE_ENABLE;
10364 if (tg3_flag(tp, ENABLE_APE))
10365 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010366 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010367 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010368 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010369 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010370 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10371 udelay(40);
10372
Michael Chan314fba32005-04-21 17:07:04 -070010373 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010374 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010375 * register to preserve the GPIO settings for LOMs. The GPIOs,
10376 * whether used as inputs or outputs, are set by boot code after
10377 * reset.
10378 */
Joe Perches63c3a662011-04-26 08:12:10 +000010379 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010380 u32 gpio_mask;
10381
Michael Chan9d26e212006-12-07 00:21:14 -080010382 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10383 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10384 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010385
Joe Perches41535772013-02-16 11:20:04 +000010386 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010387 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10388 GRC_LCLCTRL_GPIO_OUTPUT3;
10389
Joe Perches41535772013-02-16 11:20:04 +000010390 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010391 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10392
Gary Zambranoaaf84462007-05-05 11:51:45 -070010393 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010394 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10395
10396 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010397 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010398 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10399 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10402 udelay(100);
10403
Matt Carlsonc3b50032012-01-17 15:27:23 +000010404 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010405 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010406 val |= MSGINT_MODE_ENABLE;
10407 if (tp->irq_cnt > 1)
10408 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010409 if (!tg3_flag(tp, 1SHOT_MSI))
10410 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010411 tw32(MSGINT_MODE, val);
10412 }
10413
Joe Perches63c3a662011-04-26 08:12:10 +000010414 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10416 udelay(40);
10417 }
10418
10419 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10420 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10421 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10422 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10423 WDMAC_MODE_LNGREAD_ENAB);
10424
Joe Perches41535772013-02-16 11:20:04 +000010425 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10426 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010427 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010428 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10429 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430 /* nothing */
10431 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010432 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433 val |= WDMAC_MODE_RX_ACCEL;
10434 }
10435 }
10436
Michael Chand9ab5ad12006-03-20 22:27:35 -080010437 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010438 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010439 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010440
Joe Perches41535772013-02-16 11:20:04 +000010441 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010442 val |= WDMAC_MODE_BURST_ALL_DATA;
10443
Linus Torvalds1da177e2005-04-16 15:20:36 -070010444 tw32_f(WDMAC_MODE, val);
10445 udelay(40);
10446
Joe Perches63c3a662011-04-26 08:12:10 +000010447 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010448 u16 pcix_cmd;
10449
10450 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10451 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010452 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010453 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10454 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010455 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010456 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10457 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010458 }
Matt Carlson9974a352007-10-07 23:27:28 -070010459 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10460 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461 }
10462
10463 tw32_f(RDMAC_MODE, rdmac_mode);
10464 udelay(40);
10465
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010466 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10467 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010468 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10469 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10470 break;
10471 }
10472 if (i < TG3_NUM_RDMA_CHANNELS) {
10473 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010474 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010475 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010476 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010477 }
10478 }
10479
Linus Torvalds1da177e2005-04-16 15:20:36 -070010480 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010481 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010483
Joe Perches41535772013-02-16 11:20:04 +000010484 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010485 tw32(SNDDATAC_MODE,
10486 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10487 else
10488 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10489
Linus Torvalds1da177e2005-04-16 15:20:36 -070010490 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10491 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010492 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010493 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010494 val |= RCVDBDI_MODE_LRG_RING_SZ;
10495 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010496 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010497 if (tg3_flag(tp, HW_TSO_1) ||
10498 tg3_flag(tp, HW_TSO_2) ||
10499 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010500 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010501 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010502 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010503 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10504 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010505 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10506
Joe Perches41535772013-02-16 11:20:04 +000010507 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010508 err = tg3_load_5701_a0_firmware_fix(tp);
10509 if (err)
10510 return err;
10511 }
10512
Nithin Sujirc4dab502013-03-06 17:02:34 +000010513 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10514 /* Ignore any errors for the firmware download. If download
10515 * fails, the device will operate with EEE disabled
10516 */
10517 tg3_load_57766_firmware(tp);
10518 }
10519
Joe Perches63c3a662011-04-26 08:12:10 +000010520 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010521 err = tg3_load_tso_firmware(tp);
10522 if (err)
10523 return err;
10524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010525
10526 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010527
Joe Perches63c3a662011-04-26 08:12:10 +000010528 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010529 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010530 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010531
Joe Perches41535772013-02-16 11:20:04 +000010532 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10533 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010534 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10535 tp->tx_mode &= ~val;
10536 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10537 }
10538
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 tw32_f(MAC_TX_MODE, tp->tx_mode);
10540 udelay(100);
10541
Joe Perches63c3a662011-04-26 08:12:10 +000010542 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010543 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010544
10545 /* Setup the "secret" hash key. */
10546 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10547 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10548 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10549 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10550 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10551 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10552 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10553 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10554 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10555 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10556 }
10557
Linus Torvalds1da177e2005-04-16 15:20:36 -070010558 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010559 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010560 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10561
Nithin Sujir378b72c2013-07-29 13:58:39 -070010562 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10563 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10564
Joe Perches63c3a662011-04-26 08:12:10 +000010565 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010566 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10567 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10568 RX_MODE_RSS_IPV6_HASH_EN |
10569 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10570 RX_MODE_RSS_IPV4_HASH_EN |
10571 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10572
Linus Torvalds1da177e2005-04-16 15:20:36 -070010573 tw32_f(MAC_RX_MODE, tp->rx_mode);
10574 udelay(10);
10575
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576 tw32(MAC_LED_CTRL, tp->led_ctrl);
10577
10578 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010579 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010580 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10581 udelay(10);
10582 }
10583 tw32_f(MAC_RX_MODE, tp->rx_mode);
10584 udelay(10);
10585
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010586 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010587 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10588 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010589 /* Set drive transmission level to 1.2V */
10590 /* only if the signal pre-emphasis bit is not set */
10591 val = tr32(MAC_SERDES_CFG);
10592 val &= 0xfffff000;
10593 val |= 0x880;
10594 tw32(MAC_SERDES_CFG, val);
10595 }
Joe Perches41535772013-02-16 11:20:04 +000010596 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597 tw32(MAC_SERDES_CFG, 0x616000);
10598 }
10599
10600 /* Prevent chip from dropping frames when flow control
10601 * is enabled.
10602 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010603 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010604 val = 1;
10605 else
10606 val = 2;
10607 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010608
Joe Perches41535772013-02-16 11:20:04 +000010609 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010610 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010611 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010612 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010613 }
10614
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010615 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010616 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010617 u32 tmp;
10618
10619 tmp = tr32(SERDES_RX_CTRL);
10620 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10621 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10622 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10623 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10624 }
10625
Joe Perches63c3a662011-04-26 08:12:10 +000010626 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010627 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010628 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010629
Joe Perches953c96e2013-04-09 10:18:14 +000010630 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010631 if (err)
10632 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010633
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010634 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10635 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010636 u32 tmp;
10637
10638 /* Clear CRC stats. */
10639 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10640 tg3_writephy(tp, MII_TG3_TEST1,
10641 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010642 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010644 }
10645 }
10646
10647 __tg3_set_rx_mode(tp->dev);
10648
10649 /* Initialize receive rules. */
10650 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10651 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10652 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10653 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10654
Joe Perches63c3a662011-04-26 08:12:10 +000010655 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010656 limit = 8;
10657 else
10658 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010659 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010660 limit -= 4;
10661 switch (limit) {
10662 case 16:
10663 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10664 case 15:
10665 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10666 case 14:
10667 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10668 case 13:
10669 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10670 case 12:
10671 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10672 case 11:
10673 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10674 case 10:
10675 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10676 case 9:
10677 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10678 case 8:
10679 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10680 case 7:
10681 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10682 case 6:
10683 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10684 case 5:
10685 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10686 case 4:
10687 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10688 case 3:
10689 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10690 case 2:
10691 case 1:
10692
10693 default:
10694 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010696
Joe Perches63c3a662011-04-26 08:12:10 +000010697 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010698 /* Write our heartbeat update interval to APE. */
10699 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10700 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010701
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10703
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704 return 0;
10705}
10706
10707/* Called at device open time to get the chip ready for
10708 * packet processing. Invoked with tp->lock held.
10709 */
Joe Perches953c96e2013-04-09 10:18:14 +000010710static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010711{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010712 /* Chip may have been just powered on. If so, the boot code may still
10713 * be running initialization. Wait for it to finish to avoid races in
10714 * accessing the hardware.
10715 */
10716 tg3_enable_register_access(tp);
10717 tg3_poll_fw(tp);
10718
Linus Torvalds1da177e2005-04-16 15:20:36 -070010719 tg3_switch_clocks(tp);
10720
10721 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10722
Matt Carlson2f751b62008-08-04 23:17:34 -070010723 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724}
10725
Michael Chanaed93e02012-07-16 16:24:02 +000010726static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10727{
10728 int i;
10729
10730 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10731 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10732
10733 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10734 off += len;
10735
10736 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10737 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10738 memset(ocir, 0, TG3_OCIR_LEN);
10739 }
10740}
10741
10742/* sysfs attributes for hwmon */
10743static ssize_t tg3_show_temp(struct device *dev,
10744 struct device_attribute *devattr, char *buf)
10745{
Michael Chanaed93e02012-07-16 16:24:02 +000010746 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010747 struct tg3 *tp = dev_get_drvdata(dev);
Michael Chanaed93e02012-07-16 16:24:02 +000010748 u32 temperature;
10749
10750 spin_lock_bh(&tp->lock);
10751 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10752 sizeof(temperature));
10753 spin_unlock_bh(&tp->lock);
10754 return sprintf(buf, "%u\n", temperature);
10755}
10756
10757
10758static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10759 TG3_TEMP_SENSOR_OFFSET);
10760static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10761 TG3_TEMP_CAUTION_OFFSET);
10762static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10763 TG3_TEMP_MAX_OFFSET);
10764
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010765static struct attribute *tg3_attrs[] = {
Michael Chanaed93e02012-07-16 16:24:02 +000010766 &sensor_dev_attr_temp1_input.dev_attr.attr,
10767 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10768 &sensor_dev_attr_temp1_max.dev_attr.attr,
10769 NULL
10770};
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010771ATTRIBUTE_GROUPS(tg3);
Michael Chanaed93e02012-07-16 16:24:02 +000010772
Michael Chanaed93e02012-07-16 16:24:02 +000010773static void tg3_hwmon_close(struct tg3 *tp)
10774{
Michael Chanaed93e02012-07-16 16:24:02 +000010775 if (tp->hwmon_dev) {
10776 hwmon_device_unregister(tp->hwmon_dev);
10777 tp->hwmon_dev = NULL;
Michael Chanaed93e02012-07-16 16:24:02 +000010778 }
Michael Chanaed93e02012-07-16 16:24:02 +000010779}
10780
10781static void tg3_hwmon_open(struct tg3 *tp)
10782{
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010783 int i;
Michael Chanaed93e02012-07-16 16:24:02 +000010784 u32 size = 0;
10785 struct pci_dev *pdev = tp->pdev;
10786 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10787
10788 tg3_sd_scan_scratchpad(tp, ocirs);
10789
10790 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10791 if (!ocirs[i].src_data_length)
10792 continue;
10793
10794 size += ocirs[i].src_hdr_length;
10795 size += ocirs[i].src_data_length;
10796 }
10797
10798 if (!size)
10799 return;
10800
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010801 tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
10802 tp, tg3_groups);
Michael Chanaed93e02012-07-16 16:24:02 +000010803 if (IS_ERR(tp->hwmon_dev)) {
10804 tp->hwmon_dev = NULL;
10805 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
Michael Chanaed93e02012-07-16 16:24:02 +000010806 }
Michael Chanaed93e02012-07-16 16:24:02 +000010807}
10808
10809
Linus Torvalds1da177e2005-04-16 15:20:36 -070010810#define TG3_STAT_ADD32(PSTAT, REG) \
10811do { u32 __val = tr32(REG); \
10812 (PSTAT)->low += __val; \
10813 if ((PSTAT)->low < __val) \
10814 (PSTAT)->high += 1; \
10815} while (0)
10816
10817static void tg3_periodic_fetch_stats(struct tg3 *tp)
10818{
10819 struct tg3_hw_stats *sp = tp->hw_stats;
10820
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010821 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010822 return;
10823
10824 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10825 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10826 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10827 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10828 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10829 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10830 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10831 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10832 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10833 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10834 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10835 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10836 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010837 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010838 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10839 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10840 u32 val;
10841
10842 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010843 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010844 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010845 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010847
10848 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10849 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10850 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10851 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10852 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10853 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10854 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10855 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10856 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10857 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10858 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10859 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10860 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10861 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010862
10863 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010864 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
Nithin Sujir94962f72013-12-06 09:53:19 -080010865 tg3_asic_rev(tp) != ASIC_REV_5762 &&
Joe Perches41535772013-02-16 11:20:04 +000010866 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10867 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010868 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10869 } else {
10870 u32 val = tr32(HOSTCC_FLOW_ATTN);
10871 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10872 if (val) {
10873 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10874 sp->rx_discards.low += val;
10875 if (sp->rx_discards.low < val)
10876 sp->rx_discards.high += 1;
10877 }
10878 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10879 }
Michael Chan463d3052006-05-22 16:36:27 -070010880 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010881}
10882
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010883static void tg3_chk_missed_msi(struct tg3 *tp)
10884{
10885 u32 i;
10886
10887 for (i = 0; i < tp->irq_cnt; i++) {
10888 struct tg3_napi *tnapi = &tp->napi[i];
10889
10890 if (tg3_has_work(tnapi)) {
10891 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10892 tnapi->last_tx_cons == tnapi->tx_cons) {
10893 if (tnapi->chk_msi_cnt < 1) {
10894 tnapi->chk_msi_cnt++;
10895 return;
10896 }
Matt Carlson7f230732011-08-31 11:44:48 +000010897 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010898 }
10899 }
10900 tnapi->chk_msi_cnt = 0;
10901 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10902 tnapi->last_tx_cons = tnapi->tx_cons;
10903 }
10904}
10905
Linus Torvalds1da177e2005-04-16 15:20:36 -070010906static void tg3_timer(unsigned long __opaque)
10907{
10908 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010909
Matt Carlson5b190622011-11-04 09:15:04 +000010910 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010911 goto restart_timer;
10912
David S. Millerf47c11e2005-06-24 20:18:35 -070010913 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010914
Joe Perches41535772013-02-16 11:20:04 +000010915 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010916 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010917 tg3_chk_missed_msi(tp);
10918
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010919 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10920 /* BCM4785: Flush posted writes from GbE to host memory. */
10921 tr32(HOSTCC_MODE);
10922 }
10923
Joe Perches63c3a662011-04-26 08:12:10 +000010924 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010925 /* All of this garbage is because when using non-tagged
10926 * IRQ status the mailbox/status_block protocol the chip
10927 * uses with the cpu is race prone.
10928 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010929 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010930 tw32(GRC_LOCAL_CTRL,
10931 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10932 } else {
10933 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010934 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010936
David S. Millerfac9b832005-05-18 22:46:34 -070010937 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010938 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010939 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010940 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010942 }
10943
Linus Torvalds1da177e2005-04-16 15:20:36 -070010944 /* This part only runs once per second. */
10945 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010946 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010947 tg3_periodic_fetch_stats(tp);
10948
Matt Carlsonb0c59432011-05-19 12:12:48 +000010949 if (tp->setlpicnt && !--tp->setlpicnt)
10950 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010951
Joe Perches63c3a662011-04-26 08:12:10 +000010952 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010953 u32 mac_stat;
10954 int phy_event;
10955
10956 mac_stat = tr32(MAC_STATUS);
10957
10958 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010959 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010960 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10961 phy_event = 1;
10962 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10963 phy_event = 1;
10964
10965 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010966 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010967 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010968 u32 mac_stat = tr32(MAC_STATUS);
10969 int need_setup = 0;
10970
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010971 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010972 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10973 need_setup = 1;
10974 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010975 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010976 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10977 MAC_STATUS_SIGNAL_DET))) {
10978 need_setup = 1;
10979 }
10980 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010981 if (!tp->serdes_counter) {
10982 tw32_f(MAC_MODE,
10983 (tp->mac_mode &
10984 ~MAC_MODE_PORT_MODE_MASK));
10985 udelay(40);
10986 tw32_f(MAC_MODE, tp->mac_mode);
10987 udelay(40);
10988 }
Joe Perches953c96e2013-04-09 10:18:14 +000010989 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010990 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010991 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010992 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010993 tg3_serdes_parallel_detect(tp);
Nithin Sujir1743b832014-01-03 10:09:14 -080010994 } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
10995 u32 cpmu = tr32(TG3_CPMU_STATUS);
10996 bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
10997 TG3_CPMU_STATUS_LINK_MASK);
10998
10999 if (link_up != tp->link_up)
11000 tg3_setup_phy(tp, false);
Matt Carlson57d8b882010-06-05 17:24:35 +000011001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011002
11003 tp->timer_counter = tp->timer_multiplier;
11004 }
11005
Michael Chan130b8e42006-09-27 16:00:40 -070011006 /* Heartbeat is only sent once every 2 seconds.
11007 *
11008 * The heartbeat is to tell the ASF firmware that the host
11009 * driver is still alive. In the event that the OS crashes,
11010 * ASF needs to reset the hardware to free up the FIFO space
11011 * that may be filled with rx packets destined for the host.
11012 * If the FIFO is full, ASF will no longer function properly.
11013 *
11014 * Unintended resets have been reported on real time kernels
11015 * where the timer doesn't run on time. Netpoll will also have
11016 * same problem.
11017 *
11018 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
11019 * to check the ring condition when the heartbeat is expiring
11020 * before doing the reset. This will prevent most unintended
11021 * resets.
11022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011023 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000011024 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070011025 tg3_wait_for_event_ack(tp);
11026
Michael Chanbbadf502006-04-06 21:46:34 -070011027 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070011028 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070011029 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011030 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
11031 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070011032
11033 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011034 }
11035 tp->asf_counter = tp->asf_multiplier;
11036 }
11037
David S. Millerf47c11e2005-06-24 20:18:35 -070011038 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011039
Michael Chanf475f162006-03-27 23:20:14 -080011040restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011041 tp->timer.expires = jiffies + tp->timer_offset;
11042 add_timer(&tp->timer);
11043}
11044
Bill Pemberton229b1ad2012-12-03 09:22:59 -050011045static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000011046{
11047 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000011048 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000011049 !tg3_flag(tp, 57765_CLASS))
11050 tp->timer_offset = HZ;
11051 else
11052 tp->timer_offset = HZ / 10;
11053
11054 BUG_ON(tp->timer_offset > HZ);
11055
11056 tp->timer_multiplier = (HZ / tp->timer_offset);
11057 tp->asf_multiplier = (HZ / tp->timer_offset) *
11058 TG3_FW_UPDATE_FREQ_SEC;
11059
11060 init_timer(&tp->timer);
11061 tp->timer.data = (unsigned long) tp;
11062 tp->timer.function = tg3_timer;
11063}
11064
11065static void tg3_timer_start(struct tg3 *tp)
11066{
11067 tp->asf_counter = tp->asf_multiplier;
11068 tp->timer_counter = tp->timer_multiplier;
11069
11070 tp->timer.expires = jiffies + tp->timer_offset;
11071 add_timer(&tp->timer);
11072}
11073
11074static void tg3_timer_stop(struct tg3 *tp)
11075{
11076 del_timer_sync(&tp->timer);
11077}
11078
11079/* Restart hardware after configuration changes, self-test, etc.
11080 * Invoked with tp->lock held.
11081 */
Joe Perches953c96e2013-04-09 10:18:14 +000011082static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000011083 __releases(tp->lock)
11084 __acquires(tp->lock)
11085{
11086 int err;
11087
11088 err = tg3_init_hw(tp, reset_phy);
11089 if (err) {
11090 netdev_err(tp->dev,
11091 "Failed to re-initialize device, aborting\n");
11092 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11093 tg3_full_unlock(tp);
11094 tg3_timer_stop(tp);
11095 tp->irq_sync = 0;
11096 tg3_napi_enable(tp);
11097 dev_close(tp->dev);
11098 tg3_full_lock(tp, 0);
11099 }
11100 return err;
11101}
11102
11103static void tg3_reset_task(struct work_struct *work)
11104{
11105 struct tg3 *tp = container_of(work, struct tg3, reset_task);
11106 int err;
11107
11108 tg3_full_lock(tp, 0);
11109
11110 if (!netif_running(tp->dev)) {
11111 tg3_flag_clear(tp, RESET_TASK_PENDING);
11112 tg3_full_unlock(tp);
11113 return;
11114 }
11115
11116 tg3_full_unlock(tp);
11117
11118 tg3_phy_stop(tp);
11119
11120 tg3_netif_stop(tp);
11121
11122 tg3_full_lock(tp, 1);
11123
11124 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11125 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11126 tp->write32_rx_mbox = tg3_write_flush_reg32;
11127 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11128 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11129 }
11130
11131 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011132 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011133 if (err)
11134 goto out;
11135
11136 tg3_netif_start(tp);
11137
11138out:
11139 tg3_full_unlock(tp);
11140
11141 if (!err)
11142 tg3_phy_start(tp);
11143
11144 tg3_flag_clear(tp, RESET_TASK_PENDING);
11145}
11146
Matt Carlson4f125f42009-09-01 12:55:02 +000011147static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011148{
David Howells7d12e782006-10-05 14:55:46 +010011149 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011150 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011151 char *name;
11152 struct tg3_napi *tnapi = &tp->napi[irq_num];
11153
11154 if (tp->irq_cnt == 1)
11155 name = tp->dev->name;
11156 else {
11157 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011158 if (tnapi->tx_buffers && tnapi->rx_rcb)
11159 snprintf(name, IFNAMSIZ,
11160 "%s-txrx-%d", tp->dev->name, irq_num);
11161 else if (tnapi->tx_buffers)
11162 snprintf(name, IFNAMSIZ,
11163 "%s-tx-%d", tp->dev->name, irq_num);
11164 else if (tnapi->rx_rcb)
11165 snprintf(name, IFNAMSIZ,
11166 "%s-rx-%d", tp->dev->name, irq_num);
11167 else
11168 snprintf(name, IFNAMSIZ,
11169 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011170 name[IFNAMSIZ-1] = 0;
11171 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011172
Joe Perches63c3a662011-04-26 08:12:10 +000011173 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011174 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011175 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011176 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011177 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011178 } else {
11179 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011180 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011181 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011182 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011183 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011184
11185 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011186}
11187
Michael Chan79381092005-04-21 17:13:59 -070011188static int tg3_test_interrupt(struct tg3 *tp)
11189{
Matt Carlson09943a12009-08-28 14:01:57 +000011190 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011191 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011192 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011193 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011194
Michael Chand4bc3922005-05-29 14:59:20 -070011195 if (!netif_running(dev))
11196 return -ENODEV;
11197
Michael Chan79381092005-04-21 17:13:59 -070011198 tg3_disable_ints(tp);
11199
Matt Carlson4f125f42009-09-01 12:55:02 +000011200 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011201
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011202 /*
11203 * Turn off MSI one shot mode. Otherwise this test has no
11204 * observable way to know whether the interrupt was delivered.
11205 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011206 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011207 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11208 tw32(MSGINT_MODE, val);
11209 }
11210
Matt Carlson4f125f42009-09-01 12:55:02 +000011211 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011212 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011213 if (err)
11214 return err;
11215
Matt Carlson898a56f2009-08-28 14:02:40 +000011216 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011217 tg3_enable_ints(tp);
11218
11219 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011220 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011221
11222 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011223 u32 int_mbox, misc_host_ctrl;
11224
Matt Carlson898a56f2009-08-28 14:02:40 +000011225 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011226 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11227
11228 if ((int_mbox != 0) ||
11229 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11230 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011231 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011232 }
11233
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011234 if (tg3_flag(tp, 57765_PLUS) &&
11235 tnapi->hw_status->status_tag != tnapi->last_tag)
11236 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11237
Michael Chan79381092005-04-21 17:13:59 -070011238 msleep(10);
11239 }
11240
11241 tg3_disable_ints(tp);
11242
Matt Carlson4f125f42009-09-01 12:55:02 +000011243 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011244
Matt Carlson4f125f42009-09-01 12:55:02 +000011245 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011246
11247 if (err)
11248 return err;
11249
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011250 if (intr_ok) {
11251 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011252 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011253 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11254 tw32(MSGINT_MODE, val);
11255 }
Michael Chan79381092005-04-21 17:13:59 -070011256 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011257 }
Michael Chan79381092005-04-21 17:13:59 -070011258
11259 return -EIO;
11260}
11261
11262/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11263 * successfully restored
11264 */
11265static int tg3_test_msi(struct tg3 *tp)
11266{
Michael Chan79381092005-04-21 17:13:59 -070011267 int err;
11268 u16 pci_cmd;
11269
Joe Perches63c3a662011-04-26 08:12:10 +000011270 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011271 return 0;
11272
11273 /* Turn off SERR reporting in case MSI terminates with Master
11274 * Abort.
11275 */
11276 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11277 pci_write_config_word(tp->pdev, PCI_COMMAND,
11278 pci_cmd & ~PCI_COMMAND_SERR);
11279
11280 err = tg3_test_interrupt(tp);
11281
11282 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11283
11284 if (!err)
11285 return 0;
11286
11287 /* other failures */
11288 if (err != -EIO)
11289 return err;
11290
11291 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011292 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11293 "to INTx mode. Please report this failure to the PCI "
11294 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011295
Matt Carlson4f125f42009-09-01 12:55:02 +000011296 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011297
Michael Chan79381092005-04-21 17:13:59 -070011298 pci_disable_msi(tp->pdev);
11299
Joe Perches63c3a662011-04-26 08:12:10 +000011300 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011301 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011302
Matt Carlson4f125f42009-09-01 12:55:02 +000011303 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011304 if (err)
11305 return err;
11306
11307 /* Need to reset the chip because the MSI cycle may have terminated
11308 * with Master Abort.
11309 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011310 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011311
Michael Chan944d9802005-05-29 14:57:48 -070011312 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011313 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011314
David S. Millerf47c11e2005-06-24 20:18:35 -070011315 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011316
11317 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011318 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011319
11320 return err;
11321}
11322
Matt Carlson9e9fd122009-01-19 16:57:45 -080011323static int tg3_request_firmware(struct tg3 *tp)
11324{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011325 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011326
11327 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011328 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11329 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011330 return -ENOENT;
11331 }
11332
Nithin Sujir77997ea2013-03-06 17:02:32 +000011333 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011334
11335 /* Firmware blob starts with version numbers, followed by
11336 * start address and _full_ length including BSS sections
11337 * (which must be longer than the actual data, of course
11338 */
11339
Nithin Sujir77997ea2013-03-06 17:02:32 +000011340 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11341 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011342 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11343 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011344 release_firmware(tp->fw);
11345 tp->fw = NULL;
11346 return -EINVAL;
11347 }
11348
11349 /* We no longer need firmware; we have it. */
11350 tp->fw_needed = NULL;
11351 return 0;
11352}
11353
Michael Chan91024262012-09-28 07:12:38 +000011354static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011355{
Michael Chan91024262012-09-28 07:12:38 +000011356 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011357
Michael Chan91024262012-09-28 07:12:38 +000011358 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011359 /* We want as many rx rings enabled as there are cpus.
11360 * In multiqueue MSI-X mode, the first MSI-X vector
11361 * only deals with link interrupts, etc, so we add
11362 * one to the number of vectors we are requesting.
11363 */
Michael Chan91024262012-09-28 07:12:38 +000011364 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011365 }
Matt Carlson679563f2009-09-01 12:55:46 +000011366
Michael Chan91024262012-09-28 07:12:38 +000011367 return irq_cnt;
11368}
11369
11370static bool tg3_enable_msix(struct tg3 *tp)
11371{
11372 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011373 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011374
Michael Chan09681692012-09-28 07:12:42 +000011375 tp->txq_cnt = tp->txq_req;
11376 tp->rxq_cnt = tp->rxq_req;
11377 if (!tp->rxq_cnt)
11378 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011379 if (tp->rxq_cnt > tp->rxq_max)
11380 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011381
11382 /* Disable multiple TX rings by default. Simple round-robin hardware
11383 * scheduling of the TX rings can cause starvation of rings with
11384 * small packets when other rings have TSO or jumbo packets.
11385 */
11386 if (!tp->txq_req)
11387 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011388
11389 tp->irq_cnt = tg3_irq_count(tp);
11390
Matt Carlson679563f2009-09-01 12:55:46 +000011391 for (i = 0; i < tp->irq_max; i++) {
11392 msix_ent[i].entry = i;
11393 msix_ent[i].vector = 0;
11394 }
11395
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011396 rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011397 if (rc < 0) {
11398 return false;
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011399 } else if (rc < tp->irq_cnt) {
Joe Perches05dbe002010-02-17 19:44:19 +000011400 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11401 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011402 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011403 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011404 if (tp->txq_cnt)
11405 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011406 }
11407
11408 for (i = 0; i < tp->irq_max; i++)
11409 tp->napi[i].irq_vec = msix_ent[i].vector;
11410
Michael Chan49a359e2012-09-28 07:12:37 +000011411 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011412 pci_disable_msix(tp->pdev);
11413 return false;
11414 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011415
Michael Chan91024262012-09-28 07:12:38 +000011416 if (tp->irq_cnt == 1)
11417 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011418
Michael Chan91024262012-09-28 07:12:38 +000011419 tg3_flag_set(tp, ENABLE_RSS);
11420
11421 if (tp->txq_cnt > 1)
11422 tg3_flag_set(tp, ENABLE_TSS);
11423
11424 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011425
Matt Carlson679563f2009-09-01 12:55:46 +000011426 return true;
11427}
11428
Matt Carlson07b01732009-08-28 14:01:15 +000011429static void tg3_ints_init(struct tg3 *tp)
11430{
Joe Perches63c3a662011-04-26 08:12:10 +000011431 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11432 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011433 /* All MSI supporting chips should support tagged
11434 * status. Assert that this is the case.
11435 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011436 netdev_warn(tp->dev,
11437 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011438 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011439 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011440
Joe Perches63c3a662011-04-26 08:12:10 +000011441 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11442 tg3_flag_set(tp, USING_MSIX);
11443 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11444 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011445
Joe Perches63c3a662011-04-26 08:12:10 +000011446 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011447 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011448 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011449 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011450 if (!tg3_flag(tp, 1SHOT_MSI))
11451 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011452 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11453 }
11454defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011455 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011456 tp->irq_cnt = 1;
11457 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011458 }
11459
11460 if (tp->irq_cnt == 1) {
11461 tp->txq_cnt = 1;
11462 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011463 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011464 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011465 }
Matt Carlson07b01732009-08-28 14:01:15 +000011466}
11467
11468static void tg3_ints_fini(struct tg3 *tp)
11469{
Joe Perches63c3a662011-04-26 08:12:10 +000011470 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011471 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011472 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011473 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011474 tg3_flag_clear(tp, USING_MSI);
11475 tg3_flag_clear(tp, USING_MSIX);
11476 tg3_flag_clear(tp, ENABLE_RSS);
11477 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011478}
11479
Matt Carlsonbe947302012-12-03 19:36:57 +000011480static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11481 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482{
Michael Chand8f4cd32012-09-28 07:12:40 +000011483 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011484 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011485
Matt Carlson679563f2009-09-01 12:55:46 +000011486 /*
11487 * Setup interrupts first so we know how
11488 * many NAPI resources to allocate
11489 */
11490 tg3_ints_init(tp);
11491
Matt Carlson90415472011-12-16 13:33:23 +000011492 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011493
Linus Torvalds1da177e2005-04-16 15:20:36 -070011494 /* The placement of this call is tied
11495 * to the setup and use of Host TX descriptors.
11496 */
11497 err = tg3_alloc_consistent(tp);
11498 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011499 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011500
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011501 tg3_napi_init(tp);
11502
Matt Carlsonfed97812009-09-01 13:10:19 +000011503 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011504
Matt Carlson4f125f42009-09-01 12:55:02 +000011505 for (i = 0; i < tp->irq_cnt; i++) {
11506 struct tg3_napi *tnapi = &tp->napi[i];
11507 err = tg3_request_irq(tp, i);
11508 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011509 for (i--; i >= 0; i--) {
11510 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011511 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011512 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011513 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011514 }
11515 }
Matt Carlson07b01732009-08-28 14:01:15 +000011516
David S. Millerf47c11e2005-06-24 20:18:35 -070011517 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011518
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011519 if (init)
11520 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11521
Michael Chand8f4cd32012-09-28 07:12:40 +000011522 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011523 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011524 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011525 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011526 }
11527
David S. Millerf47c11e2005-06-24 20:18:35 -070011528 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011529
Matt Carlson07b01732009-08-28 14:01:15 +000011530 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011531 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011532
Michael Chand8f4cd32012-09-28 07:12:40 +000011533 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011534 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011535
Michael Chan79381092005-04-21 17:13:59 -070011536 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011537 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011538 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011539 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011540 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011541
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011542 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011543 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011544
Joe Perches63c3a662011-04-26 08:12:10 +000011545 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011546 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011547
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011548 tw32(PCIE_TRANSACTION_CFG,
11549 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011550 }
Michael Chan79381092005-04-21 17:13:59 -070011551 }
11552
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011553 tg3_phy_start(tp);
11554
Michael Chanaed93e02012-07-16 16:24:02 +000011555 tg3_hwmon_open(tp);
11556
David S. Millerf47c11e2005-06-24 20:18:35 -070011557 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011558
Matt Carlson21f76382012-02-22 12:35:21 +000011559 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011560 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011561 tg3_enable_ints(tp);
11562
Matt Carlsonbe947302012-12-03 19:36:57 +000011563 if (init)
11564 tg3_ptp_init(tp);
11565 else
11566 tg3_ptp_resume(tp);
11567
11568
David S. Millerf47c11e2005-06-24 20:18:35 -070011569 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011570
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011571 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011572
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011573 /*
11574 * Reset loopback feature if it was turned on while the device was down
11575 * make sure that it's installed properly now.
11576 */
11577 if (dev->features & NETIF_F_LOOPBACK)
11578 tg3_set_loopback(dev, dev->features);
11579
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011581
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011582out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011583 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11584 struct tg3_napi *tnapi = &tp->napi[i];
11585 free_irq(tnapi->irq_vec, tnapi);
11586 }
Matt Carlson07b01732009-08-28 14:01:15 +000011587
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011588out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011589 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011590 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011591 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011592
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011593out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011594 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011595
Matt Carlson07b01732009-08-28 14:01:15 +000011596 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011597}
11598
Michael Chan65138592012-09-28 07:12:41 +000011599static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011600{
Matt Carlson4f125f42009-09-01 12:55:02 +000011601 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011602
Matt Carlsondb219972011-11-04 09:15:03 +000011603 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011604 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011605
Matt Carlson21f76382012-02-22 12:35:21 +000011606 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011607
Michael Chanaed93e02012-07-16 16:24:02 +000011608 tg3_hwmon_close(tp);
11609
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011610 tg3_phy_stop(tp);
11611
David S. Millerf47c11e2005-06-24 20:18:35 -070011612 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011613
11614 tg3_disable_ints(tp);
11615
Michael Chan944d9802005-05-29 14:57:48 -070011616 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011617 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011618 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011619
David S. Millerf47c11e2005-06-24 20:18:35 -070011620 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011621
Matt Carlson4f125f42009-09-01 12:55:02 +000011622 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11623 struct tg3_napi *tnapi = &tp->napi[i];
11624 free_irq(tnapi->irq_vec, tnapi);
11625 }
Matt Carlson07b01732009-08-28 14:01:15 +000011626
11627 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011628
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011629 tg3_napi_fini(tp);
11630
Linus Torvalds1da177e2005-04-16 15:20:36 -070011631 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011632}
11633
Michael Chand8f4cd32012-09-28 07:12:40 +000011634static int tg3_open(struct net_device *dev)
11635{
11636 struct tg3 *tp = netdev_priv(dev);
11637 int err;
11638
Ivan Vecera0486a062014-09-01 14:21:57 +020011639 if (tp->pcierr_recovery) {
11640 netdev_err(dev, "Failed to open device. PCI error recovery "
11641 "in progress\n");
11642 return -EAGAIN;
11643 }
11644
Michael Chand8f4cd32012-09-28 07:12:40 +000011645 if (tp->fw_needed) {
11646 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011647 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11648 if (err) {
11649 netdev_warn(tp->dev, "EEE capability disabled\n");
11650 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11651 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11652 netdev_warn(tp->dev, "EEE capability restored\n");
11653 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11654 }
11655 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011656 if (err)
11657 return err;
11658 } else if (err) {
11659 netdev_warn(tp->dev, "TSO capability disabled\n");
11660 tg3_flag_clear(tp, TSO_CAPABLE);
11661 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11662 netdev_notice(tp->dev, "TSO capability restored\n");
11663 tg3_flag_set(tp, TSO_CAPABLE);
11664 }
11665 }
11666
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011667 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011668
11669 err = tg3_power_up(tp);
11670 if (err)
11671 return err;
11672
11673 tg3_full_lock(tp, 0);
11674
11675 tg3_disable_ints(tp);
11676 tg3_flag_clear(tp, INIT_COMPLETE);
11677
11678 tg3_full_unlock(tp);
11679
Nithin Sujir942d1af2013-04-09 08:48:07 +000011680 err = tg3_start(tp,
11681 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11682 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011683 if (err) {
11684 tg3_frob_aux_power(tp, false);
11685 pci_set_power_state(tp->pdev, PCI_D3hot);
11686 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011687
Matt Carlson7d41e492012-12-03 19:36:58 +000011688 if (tg3_flag(tp, PTP_CAPABLE)) {
11689 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11690 &tp->pdev->dev);
11691 if (IS_ERR(tp->ptp_clock))
11692 tp->ptp_clock = NULL;
11693 }
11694
Linus Torvalds1da177e2005-04-16 15:20:36 -070011695 return err;
11696}
11697
11698static int tg3_close(struct net_device *dev)
11699{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011700 struct tg3 *tp = netdev_priv(dev);
11701
Ivan Vecera0486a062014-09-01 14:21:57 +020011702 if (tp->pcierr_recovery) {
11703 netdev_err(dev, "Failed to close device. PCI error recovery "
11704 "in progress\n");
11705 return -EAGAIN;
11706 }
11707
Matt Carlsonbe947302012-12-03 19:36:57 +000011708 tg3_ptp_fini(tp);
11709
Michael Chan65138592012-09-28 07:12:41 +000011710 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011711
11712 /* Clear stats across close / open calls */
11713 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11714 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011715
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011716 if (pci_device_is_present(tp->pdev)) {
11717 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011718
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011719 tg3_carrier_off(tp);
11720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011721 return 0;
11722}
11723
11724static inline u64 get_stat64(tg3_stat64_t *val)
11725{
11726 return ((u64)val->high << 32) | ((u64)val->low);
11727}
11728
11729static u64 tg3_calc_crc_errors(struct tg3 *tp)
11730{
11731 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11732
11733 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011734 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11735 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011736 u32 val;
11737
11738 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11739 tg3_writephy(tp, MII_TG3_TEST1,
11740 val | MII_TG3_TEST1_CRC_EN);
11741 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11742 } else
11743 val = 0;
11744
11745 tp->phy_crc_errors += val;
11746
11747 return tp->phy_crc_errors;
11748 }
11749
11750 return get_stat64(&hw_stats->rx_fcs_errors);
11751}
11752
11753#define ESTAT_ADD(member) \
11754 estats->member = old_estats->member + \
11755 get_stat64(&hw_stats->member)
11756
11757static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11758{
11759 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11760 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11761
11762 ESTAT_ADD(rx_octets);
11763 ESTAT_ADD(rx_fragments);
11764 ESTAT_ADD(rx_ucast_packets);
11765 ESTAT_ADD(rx_mcast_packets);
11766 ESTAT_ADD(rx_bcast_packets);
11767 ESTAT_ADD(rx_fcs_errors);
11768 ESTAT_ADD(rx_align_errors);
11769 ESTAT_ADD(rx_xon_pause_rcvd);
11770 ESTAT_ADD(rx_xoff_pause_rcvd);
11771 ESTAT_ADD(rx_mac_ctrl_rcvd);
11772 ESTAT_ADD(rx_xoff_entered);
11773 ESTAT_ADD(rx_frame_too_long_errors);
11774 ESTAT_ADD(rx_jabbers);
11775 ESTAT_ADD(rx_undersize_packets);
11776 ESTAT_ADD(rx_in_length_errors);
11777 ESTAT_ADD(rx_out_length_errors);
11778 ESTAT_ADD(rx_64_or_less_octet_packets);
11779 ESTAT_ADD(rx_65_to_127_octet_packets);
11780 ESTAT_ADD(rx_128_to_255_octet_packets);
11781 ESTAT_ADD(rx_256_to_511_octet_packets);
11782 ESTAT_ADD(rx_512_to_1023_octet_packets);
11783 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11784 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11785 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11786 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11787 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11788
11789 ESTAT_ADD(tx_octets);
11790 ESTAT_ADD(tx_collisions);
11791 ESTAT_ADD(tx_xon_sent);
11792 ESTAT_ADD(tx_xoff_sent);
11793 ESTAT_ADD(tx_flow_control);
11794 ESTAT_ADD(tx_mac_errors);
11795 ESTAT_ADD(tx_single_collisions);
11796 ESTAT_ADD(tx_mult_collisions);
11797 ESTAT_ADD(tx_deferred);
11798 ESTAT_ADD(tx_excessive_collisions);
11799 ESTAT_ADD(tx_late_collisions);
11800 ESTAT_ADD(tx_collide_2times);
11801 ESTAT_ADD(tx_collide_3times);
11802 ESTAT_ADD(tx_collide_4times);
11803 ESTAT_ADD(tx_collide_5times);
11804 ESTAT_ADD(tx_collide_6times);
11805 ESTAT_ADD(tx_collide_7times);
11806 ESTAT_ADD(tx_collide_8times);
11807 ESTAT_ADD(tx_collide_9times);
11808 ESTAT_ADD(tx_collide_10times);
11809 ESTAT_ADD(tx_collide_11times);
11810 ESTAT_ADD(tx_collide_12times);
11811 ESTAT_ADD(tx_collide_13times);
11812 ESTAT_ADD(tx_collide_14times);
11813 ESTAT_ADD(tx_collide_15times);
11814 ESTAT_ADD(tx_ucast_packets);
11815 ESTAT_ADD(tx_mcast_packets);
11816 ESTAT_ADD(tx_bcast_packets);
11817 ESTAT_ADD(tx_carrier_sense_errors);
11818 ESTAT_ADD(tx_discards);
11819 ESTAT_ADD(tx_errors);
11820
11821 ESTAT_ADD(dma_writeq_full);
11822 ESTAT_ADD(dma_write_prioq_full);
11823 ESTAT_ADD(rxbds_empty);
11824 ESTAT_ADD(rx_discards);
11825 ESTAT_ADD(rx_errors);
11826 ESTAT_ADD(rx_threshold_hit);
11827
11828 ESTAT_ADD(dma_readq_full);
11829 ESTAT_ADD(dma_read_prioq_full);
11830 ESTAT_ADD(tx_comp_queue_full);
11831
11832 ESTAT_ADD(ring_set_send_prod_index);
11833 ESTAT_ADD(ring_status_update);
11834 ESTAT_ADD(nic_irqs);
11835 ESTAT_ADD(nic_avoided_irqs);
11836 ESTAT_ADD(nic_tx_threshold_hit);
11837
Matt Carlson4452d092011-05-19 12:12:51 +000011838 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011839}
11840
Matt Carlson65ec6982012-02-28 23:33:37 +000011841static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842{
Eric Dumazet511d2222010-07-07 20:44:24 +000011843 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011844 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11845
Linus Torvalds1da177e2005-04-16 15:20:36 -070011846 stats->rx_packets = old_stats->rx_packets +
11847 get_stat64(&hw_stats->rx_ucast_packets) +
11848 get_stat64(&hw_stats->rx_mcast_packets) +
11849 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011850
Linus Torvalds1da177e2005-04-16 15:20:36 -070011851 stats->tx_packets = old_stats->tx_packets +
11852 get_stat64(&hw_stats->tx_ucast_packets) +
11853 get_stat64(&hw_stats->tx_mcast_packets) +
11854 get_stat64(&hw_stats->tx_bcast_packets);
11855
11856 stats->rx_bytes = old_stats->rx_bytes +
11857 get_stat64(&hw_stats->rx_octets);
11858 stats->tx_bytes = old_stats->tx_bytes +
11859 get_stat64(&hw_stats->tx_octets);
11860
11861 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011862 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011863 stats->tx_errors = old_stats->tx_errors +
11864 get_stat64(&hw_stats->tx_errors) +
11865 get_stat64(&hw_stats->tx_mac_errors) +
11866 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11867 get_stat64(&hw_stats->tx_discards);
11868
11869 stats->multicast = old_stats->multicast +
11870 get_stat64(&hw_stats->rx_mcast_packets);
11871 stats->collisions = old_stats->collisions +
11872 get_stat64(&hw_stats->tx_collisions);
11873
11874 stats->rx_length_errors = old_stats->rx_length_errors +
11875 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11876 get_stat64(&hw_stats->rx_undersize_packets);
11877
Linus Torvalds1da177e2005-04-16 15:20:36 -070011878 stats->rx_frame_errors = old_stats->rx_frame_errors +
11879 get_stat64(&hw_stats->rx_align_errors);
11880 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11881 get_stat64(&hw_stats->tx_discards);
11882 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11883 get_stat64(&hw_stats->tx_carrier_sense_errors);
11884
11885 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011886 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011887
John W. Linville4f63b872005-09-12 14:43:18 -070011888 stats->rx_missed_errors = old_stats->rx_missed_errors +
11889 get_stat64(&hw_stats->rx_discards);
11890
Eric Dumazetb0057c52010-10-10 19:55:52 +000011891 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011892 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011893}
11894
Linus Torvalds1da177e2005-04-16 15:20:36 -070011895static int tg3_get_regs_len(struct net_device *dev)
11896{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011897 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011898}
11899
11900static void tg3_get_regs(struct net_device *dev,
11901 struct ethtool_regs *regs, void *_p)
11902{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011903 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011904
11905 regs->version = 0;
11906
Matt Carlson97bd8e42011-04-13 11:05:04 +000011907 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011908
Matt Carlson80096062010-08-02 11:26:06 +000011909 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011910 return;
11911
David S. Millerf47c11e2005-06-24 20:18:35 -070011912 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011913
Matt Carlson97bd8e42011-04-13 11:05:04 +000011914 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915
David S. Millerf47c11e2005-06-24 20:18:35 -070011916 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011917}
11918
11919static int tg3_get_eeprom_len(struct net_device *dev)
11920{
11921 struct tg3 *tp = netdev_priv(dev);
11922
11923 return tp->nvram_size;
11924}
11925
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11927{
11928 struct tg3 *tp = netdev_priv(dev);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011929 int ret, cpmu_restore = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011930 u8 *pd;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011931 u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011932 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933
Joe Perches63c3a662011-04-26 08:12:10 +000011934 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011935 return -EINVAL;
11936
Linus Torvalds1da177e2005-04-16 15:20:36 -070011937 offset = eeprom->offset;
11938 len = eeprom->len;
11939 eeprom->len = 0;
11940
11941 eeprom->magic = TG3_EEPROM_MAGIC;
11942
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011943 /* Override clock, link aware and link idle modes */
11944 if (tg3_flag(tp, CPMU_PRESENT)) {
11945 cpmu_val = tr32(TG3_CPMU_CTRL);
11946 if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
11947 CPMU_CTRL_LINK_IDLE_MODE)) {
11948 tw32(TG3_CPMU_CTRL, cpmu_val &
11949 ~(CPMU_CTRL_LINK_AWARE_MODE |
11950 CPMU_CTRL_LINK_IDLE_MODE));
11951 cpmu_restore = 1;
11952 }
11953 }
11954 tg3_override_clk(tp);
11955
Linus Torvalds1da177e2005-04-16 15:20:36 -070011956 if (offset & 3) {
11957 /* adjustments to start on required 4 byte boundary */
11958 b_offset = offset & 3;
11959 b_count = 4 - b_offset;
11960 if (b_count > len) {
11961 /* i.e. offset=1 len=2 */
11962 b_count = len;
11963 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011964 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011966 goto eeprom_done;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011967 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011968 len -= b_count;
11969 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011970 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011971 }
11972
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011973 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011974 pd = &data[eeprom->len];
11975 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011976 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011977 if (ret) {
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011978 if (i)
11979 i -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011980 eeprom->len += i;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011981 goto eeprom_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011983 memcpy(pd + i, &val, 4);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011984 if (need_resched()) {
11985 if (signal_pending(current)) {
11986 eeprom->len += i;
11987 ret = -EINTR;
11988 goto eeprom_done;
11989 }
11990 cond_resched();
11991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011992 }
11993 eeprom->len += i;
11994
11995 if (len & 3) {
11996 /* read last bytes not ending on 4 byte boundary */
11997 pd = &data[eeprom->len];
11998 b_count = len & 3;
11999 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012000 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012001 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012002 goto eeprom_done;
Al Virob9fc7dc2007-12-17 22:59:57 -080012003 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012004 eeprom->len += b_count;
12005 }
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012006 ret = 0;
12007
12008eeprom_done:
12009 /* Restore clock, link aware and link idle modes */
12010 tg3_restore_clk(tp);
12011 if (cpmu_restore)
12012 tw32(TG3_CPMU_CTRL, cpmu_val);
12013
12014 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012015}
12016
Linus Torvalds1da177e2005-04-16 15:20:36 -070012017static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
12018{
12019 struct tg3 *tp = netdev_priv(dev);
12020 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080012021 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012023 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012024
Joe Perches63c3a662011-04-26 08:12:10 +000012025 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000012026 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012027 return -EINVAL;
12028
12029 offset = eeprom->offset;
12030 len = eeprom->len;
12031
12032 if ((b_offset = (offset & 3))) {
12033 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012034 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012035 if (ret)
12036 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012037 len += b_offset;
12038 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070012039 if (len < 4)
12040 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012041 }
12042
12043 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070012044 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012045 /* adjustments to end on required 4 byte boundary */
12046 odd_len = 1;
12047 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012048 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012049 if (ret)
12050 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012051 }
12052
12053 buf = data;
12054 if (b_offset || odd_len) {
12055 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012056 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012057 return -ENOMEM;
12058 if (b_offset)
12059 memcpy(buf, &start, 4);
12060 if (odd_len)
12061 memcpy(buf+len-4, &end, 4);
12062 memcpy(buf + b_offset, data, eeprom->len);
12063 }
12064
12065 ret = tg3_nvram_write_block(tp, offset, len, buf);
12066
12067 if (buf != data)
12068 kfree(buf);
12069
12070 return ret;
12071}
12072
12073static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12074{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012075 struct tg3 *tp = netdev_priv(dev);
12076
Joe Perches63c3a662011-04-26 08:12:10 +000012077 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012078 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012079 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012080 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012081 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012082 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012083 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012084
Linus Torvalds1da177e2005-04-16 15:20:36 -070012085 cmd->supported = (SUPPORTED_Autoneg);
12086
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012087 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012088 cmd->supported |= (SUPPORTED_1000baseT_Half |
12089 SUPPORTED_1000baseT_Full);
12090
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012091 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012092 cmd->supported |= (SUPPORTED_100baseT_Half |
12093 SUPPORTED_100baseT_Full |
12094 SUPPORTED_10baseT_Half |
12095 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080012096 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070012097 cmd->port = PORT_TP;
12098 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012099 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070012100 cmd->port = PORT_FIBRE;
12101 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012102
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000012104 if (tg3_flag(tp, PAUSE_AUTONEG)) {
12105 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
12106 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12107 cmd->advertising |= ADVERTISED_Pause;
12108 } else {
12109 cmd->advertising |= ADVERTISED_Pause |
12110 ADVERTISED_Asym_Pause;
12111 }
12112 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12113 cmd->advertising |= ADVERTISED_Asym_Pause;
12114 }
12115 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012116 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000012117 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012118 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000012119 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012120 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
12121 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
12122 cmd->eth_tp_mdix = ETH_TP_MDI_X;
12123 else
12124 cmd->eth_tp_mdix = ETH_TP_MDI;
12125 }
Matt Carlson64c22182010-10-14 10:37:44 +000012126 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000012127 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
12128 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012129 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012130 }
Matt Carlson882e9792009-09-01 13:21:36 +000012131 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012132 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012133 cmd->autoneg = tp->link_config.autoneg;
12134 cmd->maxtxpkt = 0;
12135 cmd->maxrxpkt = 0;
12136 return 0;
12137}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012138
Linus Torvalds1da177e2005-04-16 15:20:36 -070012139static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12140{
12141 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000012142 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012143
Joe Perches63c3a662011-04-26 08:12:10 +000012144 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012145 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012146 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012147 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012148 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012149 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012150 }
12151
Matt Carlson7e5856b2009-02-25 14:23:01 +000012152 if (cmd->autoneg != AUTONEG_ENABLE &&
12153 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012154 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012155
12156 if (cmd->autoneg == AUTONEG_DISABLE &&
12157 cmd->duplex != DUPLEX_FULL &&
12158 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012159 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012160
Matt Carlson7e5856b2009-02-25 14:23:01 +000012161 if (cmd->autoneg == AUTONEG_ENABLE) {
12162 u32 mask = ADVERTISED_Autoneg |
12163 ADVERTISED_Pause |
12164 ADVERTISED_Asym_Pause;
12165
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012166 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012167 mask |= ADVERTISED_1000baseT_Half |
12168 ADVERTISED_1000baseT_Full;
12169
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012170 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012171 mask |= ADVERTISED_100baseT_Half |
12172 ADVERTISED_100baseT_Full |
12173 ADVERTISED_10baseT_Half |
12174 ADVERTISED_10baseT_Full |
12175 ADVERTISED_TP;
12176 else
12177 mask |= ADVERTISED_FIBRE;
12178
12179 if (cmd->advertising & ~mask)
12180 return -EINVAL;
12181
12182 mask &= (ADVERTISED_1000baseT_Half |
12183 ADVERTISED_1000baseT_Full |
12184 ADVERTISED_100baseT_Half |
12185 ADVERTISED_100baseT_Full |
12186 ADVERTISED_10baseT_Half |
12187 ADVERTISED_10baseT_Full);
12188
12189 cmd->advertising &= mask;
12190 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012191 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012192 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012193 return -EINVAL;
12194
12195 if (cmd->duplex != DUPLEX_FULL)
12196 return -EINVAL;
12197 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012198 if (speed != SPEED_100 &&
12199 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012200 return -EINVAL;
12201 }
12202 }
12203
David S. Millerf47c11e2005-06-24 20:18:35 -070012204 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012205
12206 tp->link_config.autoneg = cmd->autoneg;
12207 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012208 tp->link_config.advertising = (cmd->advertising |
12209 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012210 tp->link_config.speed = SPEED_UNKNOWN;
12211 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012212 } else {
12213 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012214 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012215 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012216 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012217
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012218 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12219
Nithin Sujirce20f162013-04-09 08:48:04 +000012220 tg3_warn_mgmt_link_flap(tp);
12221
Linus Torvalds1da177e2005-04-16 15:20:36 -070012222 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012223 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012224
David S. Millerf47c11e2005-06-24 20:18:35 -070012225 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012226
Linus Torvalds1da177e2005-04-16 15:20:36 -070012227 return 0;
12228}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012229
Linus Torvalds1da177e2005-04-16 15:20:36 -070012230static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12231{
12232 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012233
Rick Jones68aad782011-11-07 13:29:27 +000012234 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12235 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12236 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12237 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012238}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012239
Linus Torvalds1da177e2005-04-16 15:20:36 -070012240static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12241{
12242 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012243
Joe Perches63c3a662011-04-26 08:12:10 +000012244 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012245 wol->supported = WAKE_MAGIC;
12246 else
12247 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012248 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012249 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012250 wol->wolopts = WAKE_MAGIC;
12251 memset(&wol->sopass, 0, sizeof(wol->sopass));
12252}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012253
Linus Torvalds1da177e2005-04-16 15:20:36 -070012254static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12255{
12256 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012257 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012258
Linus Torvalds1da177e2005-04-16 15:20:36 -070012259 if (wol->wolopts & ~WAKE_MAGIC)
12260 return -EINVAL;
12261 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012262 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012263 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012264
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012265 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12266
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012267 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012268 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012269 else
Joe Perches63c3a662011-04-26 08:12:10 +000012270 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012271
Linus Torvalds1da177e2005-04-16 15:20:36 -070012272 return 0;
12273}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012274
Linus Torvalds1da177e2005-04-16 15:20:36 -070012275static u32 tg3_get_msglevel(struct net_device *dev)
12276{
12277 struct tg3 *tp = netdev_priv(dev);
12278 return tp->msg_enable;
12279}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012280
Linus Torvalds1da177e2005-04-16 15:20:36 -070012281static void tg3_set_msglevel(struct net_device *dev, u32 value)
12282{
12283 struct tg3 *tp = netdev_priv(dev);
12284 tp->msg_enable = value;
12285}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012286
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287static int tg3_nway_reset(struct net_device *dev)
12288{
12289 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012291
Linus Torvalds1da177e2005-04-16 15:20:36 -070012292 if (!netif_running(dev))
12293 return -EAGAIN;
12294
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012295 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012296 return -EINVAL;
12297
Nithin Sujirce20f162013-04-09 08:48:04 +000012298 tg3_warn_mgmt_link_flap(tp);
12299
Joe Perches63c3a662011-04-26 08:12:10 +000012300 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012301 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012302 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012303 r = phy_start_aneg(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012304 } else {
12305 u32 bmcr;
12306
12307 spin_lock_bh(&tp->lock);
12308 r = -EINVAL;
12309 tg3_readphy(tp, MII_BMCR, &bmcr);
12310 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12311 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012312 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012313 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12314 BMCR_ANENABLE);
12315 r = 0;
12316 }
12317 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012318 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012319
Linus Torvalds1da177e2005-04-16 15:20:36 -070012320 return r;
12321}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012322
Linus Torvalds1da177e2005-04-16 15:20:36 -070012323static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12324{
12325 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012326
Matt Carlson2c49a442010-09-30 10:34:35 +000012327 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012328 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012329 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012330 else
12331 ering->rx_jumbo_max_pending = 0;
12332
12333 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012334
12335 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012336 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012337 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12338 else
12339 ering->rx_jumbo_pending = 0;
12340
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012341 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012342}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012343
Linus Torvalds1da177e2005-04-16 15:20:36 -070012344static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12345{
12346 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012347 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012348
Matt Carlson2c49a442010-09-30 10:34:35 +000012349 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12350 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012351 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12352 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012353 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012354 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012355 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012356
Michael Chanbbe832c2005-06-24 20:20:04 -070012357 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012358 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012359 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012360 irq_sync = 1;
12361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012362
Michael Chanbbe832c2005-06-24 20:20:04 -070012363 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012364
Linus Torvalds1da177e2005-04-16 15:20:36 -070012365 tp->rx_pending = ering->rx_pending;
12366
Joe Perches63c3a662011-04-26 08:12:10 +000012367 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012368 tp->rx_pending > 63)
12369 tp->rx_pending = 63;
Ivan Veceraba67b512014-04-17 14:51:08 +020012370
12371 if (tg3_flag(tp, JUMBO_RING_ENABLE))
12372 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012373
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012374 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012375 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012376
12377 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012378 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012379 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012380 if (!err)
12381 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012382 }
12383
David S. Millerf47c11e2005-06-24 20:18:35 -070012384 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012385
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012386 if (irq_sync && !err)
12387 tg3_phy_start(tp);
12388
Michael Chanb9ec6c12006-07-25 16:37:27 -070012389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012390}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012391
Linus Torvalds1da177e2005-04-16 15:20:36 -070012392static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12393{
12394 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012395
Joe Perches63c3a662011-04-26 08:12:10 +000012396 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012397
Matt Carlson4a2db502011-12-08 14:40:17 +000012398 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012399 epause->rx_pause = 1;
12400 else
12401 epause->rx_pause = 0;
12402
Matt Carlson4a2db502011-12-08 14:40:17 +000012403 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012404 epause->tx_pause = 1;
12405 else
12406 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012407}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012408
Linus Torvalds1da177e2005-04-16 15:20:36 -070012409static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12410{
12411 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012412 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012413
Nithin Sujirce20f162013-04-09 08:48:04 +000012414 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12415 tg3_warn_mgmt_link_flap(tp);
12416
Joe Perches63c3a662011-04-26 08:12:10 +000012417 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012418 u32 newadv;
12419 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012420
Hauke Mehrtensead24022013-09-28 23:15:26 +020012421 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012422
Matt Carlson27121682010-02-17 15:16:57 +000012423 if (!(phydev->supported & SUPPORTED_Pause) ||
12424 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012425 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012426 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012427
Matt Carlson27121682010-02-17 15:16:57 +000012428 tp->link_config.flowctrl = 0;
12429 if (epause->rx_pause) {
12430 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012431
Matt Carlson27121682010-02-17 15:16:57 +000012432 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012433 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012434 newadv = ADVERTISED_Pause;
12435 } else
12436 newadv = ADVERTISED_Pause |
12437 ADVERTISED_Asym_Pause;
12438 } else if (epause->tx_pause) {
12439 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12440 newadv = ADVERTISED_Asym_Pause;
12441 } else
12442 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012443
Matt Carlson27121682010-02-17 15:16:57 +000012444 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012445 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012446 else
Joe Perches63c3a662011-04-26 08:12:10 +000012447 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012448
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012449 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012450 u32 oldadv = phydev->advertising &
12451 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12452 if (oldadv != newadv) {
12453 phydev->advertising &=
12454 ~(ADVERTISED_Pause |
12455 ADVERTISED_Asym_Pause);
12456 phydev->advertising |= newadv;
12457 if (phydev->autoneg) {
12458 /*
12459 * Always renegotiate the link to
12460 * inform our link partner of our
12461 * flow control settings, even if the
12462 * flow control is forced. Let
12463 * tg3_adjust_link() do the final
12464 * flow control setup.
12465 */
12466 return phy_start_aneg(phydev);
12467 }
12468 }
12469
12470 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012471 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012472 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012473 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012474 ~(ADVERTISED_Pause |
12475 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012476 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012477 }
12478 } else {
12479 int irq_sync = 0;
12480
12481 if (netif_running(dev)) {
12482 tg3_netif_stop(tp);
12483 irq_sync = 1;
12484 }
12485
12486 tg3_full_lock(tp, irq_sync);
12487
12488 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012489 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012490 else
Joe Perches63c3a662011-04-26 08:12:10 +000012491 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012492 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012493 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012494 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012495 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012496 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012497 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012498 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012499 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012500
12501 if (netif_running(dev)) {
12502 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012503 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012504 if (!err)
12505 tg3_netif_start(tp);
12506 }
12507
12508 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012510
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012511 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12512
Michael Chanb9ec6c12006-07-25 16:37:27 -070012513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012514}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012515
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012516static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012517{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012518 switch (sset) {
12519 case ETH_SS_TEST:
12520 return TG3_NUM_TEST;
12521 case ETH_SS_STATS:
12522 return TG3_NUM_STATS;
12523 default:
12524 return -EOPNOTSUPP;
12525 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012526}
12527
Matt Carlson90415472011-12-16 13:33:23 +000012528static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12529 u32 *rules __always_unused)
12530{
12531 struct tg3 *tp = netdev_priv(dev);
12532
12533 if (!tg3_flag(tp, SUPPORT_MSIX))
12534 return -EOPNOTSUPP;
12535
12536 switch (info->cmd) {
12537 case ETHTOOL_GRXRINGS:
12538 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012539 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012540 else {
12541 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012542 if (info->data > TG3_RSS_MAX_NUM_QS)
12543 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012544 }
12545
12546 /* The first interrupt vector only
12547 * handles link interrupts.
12548 */
12549 info->data -= 1;
12550 return 0;
12551
12552 default:
12553 return -EOPNOTSUPP;
12554 }
12555}
12556
12557static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12558{
12559 u32 size = 0;
12560 struct tg3 *tp = netdev_priv(dev);
12561
12562 if (tg3_flag(tp, SUPPORT_MSIX))
12563 size = TG3_RSS_INDIR_TBL_SIZE;
12564
12565 return size;
12566}
12567
Ben Hutchingsfe62d002014-05-15 01:25:27 +010012568static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key)
Matt Carlson90415472011-12-16 13:33:23 +000012569{
12570 struct tg3 *tp = netdev_priv(dev);
12571 int i;
12572
12573 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12574 indir[i] = tp->rss_ind_tbl[i];
12575
12576 return 0;
12577}
12578
Ben Hutchingsfe62d002014-05-15 01:25:27 +010012579static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key)
Matt Carlson90415472011-12-16 13:33:23 +000012580{
12581 struct tg3 *tp = netdev_priv(dev);
12582 size_t i;
12583
12584 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12585 tp->rss_ind_tbl[i] = indir[i];
12586
12587 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12588 return 0;
12589
12590 /* It is legal to write the indirection
12591 * table while the device is running.
12592 */
12593 tg3_full_lock(tp, 0);
12594 tg3_rss_write_indir_tbl(tp);
12595 tg3_full_unlock(tp);
12596
12597 return 0;
12598}
12599
Michael Chan09681692012-09-28 07:12:42 +000012600static void tg3_get_channels(struct net_device *dev,
12601 struct ethtool_channels *channel)
12602{
12603 struct tg3 *tp = netdev_priv(dev);
12604 u32 deflt_qs = netif_get_num_default_rss_queues();
12605
12606 channel->max_rx = tp->rxq_max;
12607 channel->max_tx = tp->txq_max;
12608
12609 if (netif_running(dev)) {
12610 channel->rx_count = tp->rxq_cnt;
12611 channel->tx_count = tp->txq_cnt;
12612 } else {
12613 if (tp->rxq_req)
12614 channel->rx_count = tp->rxq_req;
12615 else
12616 channel->rx_count = min(deflt_qs, tp->rxq_max);
12617
12618 if (tp->txq_req)
12619 channel->tx_count = tp->txq_req;
12620 else
12621 channel->tx_count = min(deflt_qs, tp->txq_max);
12622 }
12623}
12624
12625static int tg3_set_channels(struct net_device *dev,
12626 struct ethtool_channels *channel)
12627{
12628 struct tg3 *tp = netdev_priv(dev);
12629
12630 if (!tg3_flag(tp, SUPPORT_MSIX))
12631 return -EOPNOTSUPP;
12632
12633 if (channel->rx_count > tp->rxq_max ||
12634 channel->tx_count > tp->txq_max)
12635 return -EINVAL;
12636
12637 tp->rxq_req = channel->rx_count;
12638 tp->txq_req = channel->tx_count;
12639
12640 if (!netif_running(dev))
12641 return 0;
12642
12643 tg3_stop(tp);
12644
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012645 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012646
Matt Carlsonbe947302012-12-03 19:36:57 +000012647 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012648
12649 return 0;
12650}
12651
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012652static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012653{
12654 switch (stringset) {
12655 case ETH_SS_STATS:
12656 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12657 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012658 case ETH_SS_TEST:
12659 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012661 default:
12662 WARN_ON(1); /* we need a WARN() */
12663 break;
12664 }
12665}
12666
stephen hemminger81b87092011-04-04 08:43:50 +000012667static int tg3_set_phys_id(struct net_device *dev,
12668 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012669{
12670 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012671
12672 if (!netif_running(tp->dev))
12673 return -EAGAIN;
12674
stephen hemminger81b87092011-04-04 08:43:50 +000012675 switch (state) {
12676 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012677 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012678
stephen hemminger81b87092011-04-04 08:43:50 +000012679 case ETHTOOL_ID_ON:
12680 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12681 LED_CTRL_1000MBPS_ON |
12682 LED_CTRL_100MBPS_ON |
12683 LED_CTRL_10MBPS_ON |
12684 LED_CTRL_TRAFFIC_OVERRIDE |
12685 LED_CTRL_TRAFFIC_BLINK |
12686 LED_CTRL_TRAFFIC_LED);
12687 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012688
stephen hemminger81b87092011-04-04 08:43:50 +000012689 case ETHTOOL_ID_OFF:
12690 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12691 LED_CTRL_TRAFFIC_OVERRIDE);
12692 break;
Michael Chan4009a932005-09-05 17:52:54 -070012693
stephen hemminger81b87092011-04-04 08:43:50 +000012694 case ETHTOOL_ID_INACTIVE:
12695 tw32(MAC_LED_CTRL, tp->led_ctrl);
12696 break;
Michael Chan4009a932005-09-05 17:52:54 -070012697 }
stephen hemminger81b87092011-04-04 08:43:50 +000012698
Michael Chan4009a932005-09-05 17:52:54 -070012699 return 0;
12700}
12701
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012702static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012703 struct ethtool_stats *estats, u64 *tmp_stats)
12704{
12705 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012706
Matt Carlsonb546e462012-02-13 15:20:09 +000012707 if (tp->hw_stats)
12708 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12709 else
12710 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012711}
12712
Matt Carlson535a4902011-07-20 10:20:56 +000012713static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012714{
12715 int i;
12716 __be32 *buf;
12717 u32 offset = 0, len = 0;
12718 u32 magic, val;
12719
Joe Perches63c3a662011-04-26 08:12:10 +000012720 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012721 return NULL;
12722
12723 if (magic == TG3_EEPROM_MAGIC) {
12724 for (offset = TG3_NVM_DIR_START;
12725 offset < TG3_NVM_DIR_END;
12726 offset += TG3_NVM_DIRENT_SIZE) {
12727 if (tg3_nvram_read(tp, offset, &val))
12728 return NULL;
12729
12730 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12731 TG3_NVM_DIRTYPE_EXTVPD)
12732 break;
12733 }
12734
12735 if (offset != TG3_NVM_DIR_END) {
12736 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12737 if (tg3_nvram_read(tp, offset + 4, &offset))
12738 return NULL;
12739
12740 offset = tg3_nvram_logical_addr(tp, offset);
12741 }
12742 }
12743
12744 if (!offset || !len) {
12745 offset = TG3_NVM_VPD_OFF;
12746 len = TG3_NVM_VPD_LEN;
12747 }
12748
12749 buf = kmalloc(len, GFP_KERNEL);
12750 if (buf == NULL)
12751 return NULL;
12752
12753 if (magic == TG3_EEPROM_MAGIC) {
12754 for (i = 0; i < len; i += 4) {
12755 /* The data is in little-endian format in NVRAM.
12756 * Use the big-endian read routines to preserve
12757 * the byte order as it exists in NVRAM.
12758 */
12759 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12760 goto error;
12761 }
12762 } else {
12763 u8 *ptr;
12764 ssize_t cnt;
12765 unsigned int pos = 0;
12766
12767 ptr = (u8 *)&buf[0];
12768 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12769 cnt = pci_read_vpd(tp->pdev, pos,
12770 len - pos, ptr);
12771 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12772 cnt = 0;
12773 else if (cnt < 0)
12774 goto error;
12775 }
12776 if (pos != len)
12777 goto error;
12778 }
12779
Matt Carlson535a4902011-07-20 10:20:56 +000012780 *vpdlen = len;
12781
Matt Carlsonc3e94502011-04-13 11:05:08 +000012782 return buf;
12783
12784error:
12785 kfree(buf);
12786 return NULL;
12787}
12788
Michael Chan566f86a2005-05-29 14:56:58 -070012789#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012790#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12791#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12792#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012793#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12794#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012795#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012796#define NVRAM_SELFBOOT_HW_SIZE 0x20
12797#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012798
12799static int tg3_test_nvram(struct tg3 *tp)
12800{
Matt Carlson535a4902011-07-20 10:20:56 +000012801 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012802 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012803 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012804
Joe Perches63c3a662011-04-26 08:12:10 +000012805 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012806 return 0;
12807
Matt Carlsone4f34112009-02-25 14:25:00 +000012808 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012809 return -EIO;
12810
Michael Chan1b277772006-03-20 22:27:48 -080012811 if (magic == TG3_EEPROM_MAGIC)
12812 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012813 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012814 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12815 TG3_EEPROM_SB_FORMAT_1) {
12816 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12817 case TG3_EEPROM_SB_REVISION_0:
12818 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12819 break;
12820 case TG3_EEPROM_SB_REVISION_2:
12821 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12822 break;
12823 case TG3_EEPROM_SB_REVISION_3:
12824 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12825 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012826 case TG3_EEPROM_SB_REVISION_4:
12827 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12828 break;
12829 case TG3_EEPROM_SB_REVISION_5:
12830 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12831 break;
12832 case TG3_EEPROM_SB_REVISION_6:
12833 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12834 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012835 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012836 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012837 }
12838 } else
Michael Chan1b277772006-03-20 22:27:48 -080012839 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012840 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12841 size = NVRAM_SELFBOOT_HW_SIZE;
12842 else
Michael Chan1b277772006-03-20 22:27:48 -080012843 return -EIO;
12844
12845 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012846 if (buf == NULL)
12847 return -ENOMEM;
12848
Michael Chan1b277772006-03-20 22:27:48 -080012849 err = -EIO;
12850 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012851 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12852 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012853 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012854 }
Michael Chan1b277772006-03-20 22:27:48 -080012855 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012856 goto out;
12857
Michael Chan1b277772006-03-20 22:27:48 -080012858 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012859 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012860 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012861 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012862 u8 *buf8 = (u8 *) buf, csum8 = 0;
12863
Al Virob9fc7dc2007-12-17 22:59:57 -080012864 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012865 TG3_EEPROM_SB_REVISION_2) {
12866 /* For rev 2, the csum doesn't include the MBA. */
12867 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12868 csum8 += buf8[i];
12869 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12870 csum8 += buf8[i];
12871 } else {
12872 for (i = 0; i < size; i++)
12873 csum8 += buf8[i];
12874 }
Michael Chan1b277772006-03-20 22:27:48 -080012875
Adrian Bunkad96b482006-04-05 22:21:04 -070012876 if (csum8 == 0) {
12877 err = 0;
12878 goto out;
12879 }
12880
12881 err = -EIO;
12882 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012883 }
Michael Chan566f86a2005-05-29 14:56:58 -070012884
Al Virob9fc7dc2007-12-17 22:59:57 -080012885 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012886 TG3_EEPROM_MAGIC_HW) {
12887 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012888 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012889 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012890
12891 /* Separate the parity bits and the data bytes. */
12892 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12893 if ((i == 0) || (i == 8)) {
12894 int l;
12895 u8 msk;
12896
12897 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12898 parity[k++] = buf8[i] & msk;
12899 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012900 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012901 int l;
12902 u8 msk;
12903
12904 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12905 parity[k++] = buf8[i] & msk;
12906 i++;
12907
12908 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12909 parity[k++] = buf8[i] & msk;
12910 i++;
12911 }
12912 data[j++] = buf8[i];
12913 }
12914
12915 err = -EIO;
12916 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12917 u8 hw8 = hweight8(data[i]);
12918
12919 if ((hw8 & 0x1) && parity[i])
12920 goto out;
12921 else if (!(hw8 & 0x1) && !parity[i])
12922 goto out;
12923 }
12924 err = 0;
12925 goto out;
12926 }
12927
Matt Carlson01c3a392011-03-09 16:58:20 +000012928 err = -EIO;
12929
Michael Chan566f86a2005-05-29 14:56:58 -070012930 /* Bootstrap checksum at offset 0x10 */
12931 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012932 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012933 goto out;
12934
12935 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12936 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012937 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012938 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012939
Matt Carlsonc3e94502011-04-13 11:05:08 +000012940 kfree(buf);
12941
Matt Carlson535a4902011-07-20 10:20:56 +000012942 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012943 if (!buf)
12944 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012945
Matt Carlson535a4902011-07-20 10:20:56 +000012946 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012947 if (i > 0) {
12948 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12949 if (j < 0)
12950 goto out;
12951
Matt Carlson535a4902011-07-20 10:20:56 +000012952 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012953 goto out;
12954
12955 i += PCI_VPD_LRDT_TAG_SIZE;
12956 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12957 PCI_VPD_RO_KEYWORD_CHKSUM);
12958 if (j > 0) {
12959 u8 csum8 = 0;
12960
12961 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12962
12963 for (i = 0; i <= j; i++)
12964 csum8 += ((u8 *)buf)[i];
12965
12966 if (csum8)
12967 goto out;
12968 }
12969 }
12970
Michael Chan566f86a2005-05-29 14:56:58 -070012971 err = 0;
12972
12973out:
12974 kfree(buf);
12975 return err;
12976}
12977
Michael Chanca430072005-05-29 14:57:23 -070012978#define TG3_SERDES_TIMEOUT_SEC 2
12979#define TG3_COPPER_TIMEOUT_SEC 6
12980
12981static int tg3_test_link(struct tg3 *tp)
12982{
12983 int i, max;
12984
12985 if (!netif_running(tp->dev))
12986 return -ENODEV;
12987
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012988 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012989 max = TG3_SERDES_TIMEOUT_SEC;
12990 else
12991 max = TG3_COPPER_TIMEOUT_SEC;
12992
12993 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012994 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012995 return 0;
12996
12997 if (msleep_interruptible(1000))
12998 break;
12999 }
13000
13001 return -EIO;
13002}
13003
Michael Chana71116d2005-05-29 14:58:11 -070013004/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080013005static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070013006{
Michael Chanb16250e2006-09-27 16:10:14 -070013007 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070013008 u32 offset, read_mask, write_mask, val, save_val, read_val;
13009 static struct {
13010 u16 offset;
13011 u16 flags;
13012#define TG3_FL_5705 0x1
13013#define TG3_FL_NOT_5705 0x2
13014#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070013015#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070013016 u32 read_mask;
13017 u32 write_mask;
13018 } reg_tbl[] = {
13019 /* MAC Control Registers */
13020 { MAC_MODE, TG3_FL_NOT_5705,
13021 0x00000000, 0x00ef6f8c },
13022 { MAC_MODE, TG3_FL_5705,
13023 0x00000000, 0x01ef6b8c },
13024 { MAC_STATUS, TG3_FL_NOT_5705,
13025 0x03800107, 0x00000000 },
13026 { MAC_STATUS, TG3_FL_5705,
13027 0x03800100, 0x00000000 },
13028 { MAC_ADDR_0_HIGH, 0x0000,
13029 0x00000000, 0x0000ffff },
13030 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013031 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070013032 { MAC_RX_MTU_SIZE, 0x0000,
13033 0x00000000, 0x0000ffff },
13034 { MAC_TX_MODE, 0x0000,
13035 0x00000000, 0x00000070 },
13036 { MAC_TX_LENGTHS, 0x0000,
13037 0x00000000, 0x00003fff },
13038 { MAC_RX_MODE, TG3_FL_NOT_5705,
13039 0x00000000, 0x000007fc },
13040 { MAC_RX_MODE, TG3_FL_5705,
13041 0x00000000, 0x000007dc },
13042 { MAC_HASH_REG_0, 0x0000,
13043 0x00000000, 0xffffffff },
13044 { MAC_HASH_REG_1, 0x0000,
13045 0x00000000, 0xffffffff },
13046 { MAC_HASH_REG_2, 0x0000,
13047 0x00000000, 0xffffffff },
13048 { MAC_HASH_REG_3, 0x0000,
13049 0x00000000, 0xffffffff },
13050
13051 /* Receive Data and Receive BD Initiator Control Registers. */
13052 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
13053 0x00000000, 0xffffffff },
13054 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
13055 0x00000000, 0xffffffff },
13056 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
13057 0x00000000, 0x00000003 },
13058 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
13059 0x00000000, 0xffffffff },
13060 { RCVDBDI_STD_BD+0, 0x0000,
13061 0x00000000, 0xffffffff },
13062 { RCVDBDI_STD_BD+4, 0x0000,
13063 0x00000000, 0xffffffff },
13064 { RCVDBDI_STD_BD+8, 0x0000,
13065 0x00000000, 0xffff0002 },
13066 { RCVDBDI_STD_BD+0xc, 0x0000,
13067 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013068
Michael Chana71116d2005-05-29 14:58:11 -070013069 /* Receive BD Initiator Control Registers. */
13070 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
13071 0x00000000, 0xffffffff },
13072 { RCVBDI_STD_THRESH, TG3_FL_5705,
13073 0x00000000, 0x000003ff },
13074 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
13075 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013076
Michael Chana71116d2005-05-29 14:58:11 -070013077 /* Host Coalescing Control Registers. */
13078 { HOSTCC_MODE, TG3_FL_NOT_5705,
13079 0x00000000, 0x00000004 },
13080 { HOSTCC_MODE, TG3_FL_5705,
13081 0x00000000, 0x000000f6 },
13082 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
13083 0x00000000, 0xffffffff },
13084 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
13085 0x00000000, 0x000003ff },
13086 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
13087 0x00000000, 0xffffffff },
13088 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
13089 0x00000000, 0x000003ff },
13090 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
13091 0x00000000, 0xffffffff },
13092 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13093 0x00000000, 0x000000ff },
13094 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
13095 0x00000000, 0xffffffff },
13096 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13097 0x00000000, 0x000000ff },
13098 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
13099 0x00000000, 0xffffffff },
13100 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
13101 0x00000000, 0xffffffff },
13102 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13103 0x00000000, 0xffffffff },
13104 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13105 0x00000000, 0x000000ff },
13106 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13107 0x00000000, 0xffffffff },
13108 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13109 0x00000000, 0x000000ff },
13110 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
13111 0x00000000, 0xffffffff },
13112 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
13113 0x00000000, 0xffffffff },
13114 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
13115 0x00000000, 0xffffffff },
13116 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
13117 0x00000000, 0xffffffff },
13118 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
13119 0x00000000, 0xffffffff },
13120 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
13121 0xffffffff, 0x00000000 },
13122 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
13123 0xffffffff, 0x00000000 },
13124
13125 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070013126 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013127 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070013128 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013129 0x00000000, 0x007fffff },
13130 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
13131 0x00000000, 0x0000003f },
13132 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
13133 0x00000000, 0x000001ff },
13134 { BUFMGR_MB_HIGH_WATER, 0x0000,
13135 0x00000000, 0x000001ff },
13136 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
13137 0xffffffff, 0x00000000 },
13138 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
13139 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013140
Michael Chana71116d2005-05-29 14:58:11 -070013141 /* Mailbox Registers */
13142 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
13143 0x00000000, 0x000001ff },
13144 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
13145 0x00000000, 0x000001ff },
13146 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
13147 0x00000000, 0x000007ff },
13148 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
13149 0x00000000, 0x000001ff },
13150
13151 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13152 };
13153
Michael Chanb16250e2006-09-27 16:10:14 -070013154 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013155 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013156 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013157 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013158 is_5750 = 1;
13159 }
Michael Chana71116d2005-05-29 14:58:11 -070013160
13161 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13162 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13163 continue;
13164
13165 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13166 continue;
13167
Joe Perches63c3a662011-04-26 08:12:10 +000013168 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013169 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13170 continue;
13171
Michael Chanb16250e2006-09-27 16:10:14 -070013172 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13173 continue;
13174
Michael Chana71116d2005-05-29 14:58:11 -070013175 offset = (u32) reg_tbl[i].offset;
13176 read_mask = reg_tbl[i].read_mask;
13177 write_mask = reg_tbl[i].write_mask;
13178
13179 /* Save the original register content */
13180 save_val = tr32(offset);
13181
13182 /* Determine the read-only value. */
13183 read_val = save_val & read_mask;
13184
13185 /* Write zero to the register, then make sure the read-only bits
13186 * are not changed and the read/write bits are all zeros.
13187 */
13188 tw32(offset, 0);
13189
13190 val = tr32(offset);
13191
13192 /* Test the read-only and read/write bits. */
13193 if (((val & read_mask) != read_val) || (val & write_mask))
13194 goto out;
13195
13196 /* Write ones to all the bits defined by RdMask and WrMask, then
13197 * make sure the read-only bits are not changed and the
13198 * read/write bits are all ones.
13199 */
13200 tw32(offset, read_mask | write_mask);
13201
13202 val = tr32(offset);
13203
13204 /* Test the read-only bits. */
13205 if ((val & read_mask) != read_val)
13206 goto out;
13207
13208 /* Test the read/write bits. */
13209 if ((val & write_mask) != write_mask)
13210 goto out;
13211
13212 tw32(offset, save_val);
13213 }
13214
13215 return 0;
13216
13217out:
Michael Chan9f88f292006-12-07 00:22:54 -080013218 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013219 netdev_err(tp->dev,
13220 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013221 tw32(offset, save_val);
13222 return -EIO;
13223}
13224
Michael Chan7942e1d2005-05-29 14:58:36 -070013225static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13226{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013227 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013228 int i;
13229 u32 j;
13230
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013231 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013232 for (j = 0; j < len; j += 4) {
13233 u32 val;
13234
13235 tg3_write_mem(tp, offset + j, test_pattern[i]);
13236 tg3_read_mem(tp, offset + j, &val);
13237 if (val != test_pattern[i])
13238 return -EIO;
13239 }
13240 }
13241 return 0;
13242}
13243
13244static int tg3_test_memory(struct tg3 *tp)
13245{
13246 static struct mem_entry {
13247 u32 offset;
13248 u32 len;
13249 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013250 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013251 { 0x00002000, 0x1c000},
13252 { 0xffffffff, 0x00000}
13253 }, mem_tbl_5705[] = {
13254 { 0x00000100, 0x0000c},
13255 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013256 { 0x00004000, 0x00800},
13257 { 0x00006000, 0x01000},
13258 { 0x00008000, 0x02000},
13259 { 0x00010000, 0x0e000},
13260 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013261 }, mem_tbl_5755[] = {
13262 { 0x00000200, 0x00008},
13263 { 0x00004000, 0x00800},
13264 { 0x00006000, 0x00800},
13265 { 0x00008000, 0x02000},
13266 { 0x00010000, 0x0c000},
13267 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013268 }, mem_tbl_5906[] = {
13269 { 0x00000200, 0x00008},
13270 { 0x00004000, 0x00400},
13271 { 0x00006000, 0x00400},
13272 { 0x00008000, 0x01000},
13273 { 0x00010000, 0x01000},
13274 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013275 }, mem_tbl_5717[] = {
13276 { 0x00000200, 0x00008},
13277 { 0x00010000, 0x0a000},
13278 { 0x00020000, 0x13c00},
13279 { 0xffffffff, 0x00000}
13280 }, mem_tbl_57765[] = {
13281 { 0x00000200, 0x00008},
13282 { 0x00004000, 0x00800},
13283 { 0x00006000, 0x09800},
13284 { 0x00010000, 0x0a000},
13285 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013286 };
13287 struct mem_entry *mem_tbl;
13288 int err = 0;
13289 int i;
13290
Joe Perches63c3a662011-04-26 08:12:10 +000013291 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013292 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013293 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013294 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013295 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013296 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013297 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013298 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013299 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013300 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013301 mem_tbl = mem_tbl_5705;
13302 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013303 mem_tbl = mem_tbl_570x;
13304
13305 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013306 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13307 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013308 break;
13309 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013310
Michael Chan7942e1d2005-05-29 14:58:36 -070013311 return err;
13312}
13313
Matt Carlsonbb158d62011-04-25 12:42:47 +000013314#define TG3_TSO_MSS 500
13315
13316#define TG3_TSO_IP_HDR_LEN 20
13317#define TG3_TSO_TCP_HDR_LEN 20
13318#define TG3_TSO_TCP_OPT_LEN 12
13319
13320static const u8 tg3_tso_header[] = {
133210x08, 0x00,
133220x45, 0x00, 0x00, 0x00,
133230x00, 0x00, 0x40, 0x00,
133240x40, 0x06, 0x00, 0x00,
133250x0a, 0x00, 0x00, 0x01,
133260x0a, 0x00, 0x00, 0x02,
133270x0d, 0x00, 0xe0, 0x00,
133280x00, 0x00, 0x01, 0x00,
133290x00, 0x00, 0x02, 0x00,
133300x80, 0x10, 0x10, 0x00,
133310x14, 0x09, 0x00, 0x00,
133320x01, 0x01, 0x08, 0x0a,
133330x11, 0x11, 0x11, 0x11,
133340x11, 0x11, 0x11, 0x11,
13335};
Michael Chan9f40dea2005-09-05 17:53:06 -070013336
Matt Carlson28a45952011-08-19 13:58:22 +000013337static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013338{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013339 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013340 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013341 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013342 struct sk_buff *skb;
13343 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013344 dma_addr_t map;
13345 int num_pkts, tx_len, rx_len, i, err;
13346 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013347 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013348 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013349
Matt Carlsonc8873402010-02-12 14:47:11 +000013350 tnapi = &tp->napi[0];
13351 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013352 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013353 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013354 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013355 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013356 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013357 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013358 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013359
Michael Chanc76949a2005-05-29 14:58:59 -070013360 err = -EIO;
13361
Matt Carlson4852a862011-04-13 11:05:07 +000013362 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013363 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013364 if (!skb)
13365 return -ENOMEM;
13366
Michael Chanc76949a2005-05-29 14:58:59 -070013367 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013368 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13369 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013370
Matt Carlson4852a862011-04-13 11:05:07 +000013371 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013372
Matt Carlson28a45952011-08-19 13:58:22 +000013373 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013374 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13375
13376 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13377 TG3_TSO_TCP_OPT_LEN;
13378
13379 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13380 sizeof(tg3_tso_header));
13381 mss = TG3_TSO_MSS;
13382
13383 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13384 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13385
13386 /* Set the total length field in the IP header */
13387 iph->tot_len = htons((u16)(mss + hdr_len));
13388
13389 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13390 TXD_FLAG_CPU_POST_DMA);
13391
Joe Perches63c3a662011-04-26 08:12:10 +000013392 if (tg3_flag(tp, HW_TSO_1) ||
13393 tg3_flag(tp, HW_TSO_2) ||
13394 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013395 struct tcphdr *th;
13396 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13397 th = (struct tcphdr *)&tx_data[val];
13398 th->check = 0;
13399 } else
13400 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13401
Joe Perches63c3a662011-04-26 08:12:10 +000013402 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013403 mss |= (hdr_len & 0xc) << 12;
13404 if (hdr_len & 0x10)
13405 base_flags |= 0x00000010;
13406 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013407 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013408 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013409 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013410 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013411 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13412 } else {
13413 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13414 }
13415
13416 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13417 } else {
13418 num_pkts = 1;
13419 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013420
13421 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13422 tx_len > VLAN_ETH_FRAME_LEN)
13423 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013424 }
13425
13426 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013427 tx_data[i] = (u8) (i & 0xff);
13428
Alexander Duyckf4188d82009-12-02 16:48:38 +000013429 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13430 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013431 dev_kfree_skb(skb);
13432 return -EIO;
13433 }
Michael Chanc76949a2005-05-29 14:58:59 -070013434
Matt Carlson0d681b22011-07-27 14:20:49 +000013435 val = tnapi->tx_prod;
13436 tnapi->tx_buffers[val].skb = skb;
13437 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13438
Michael Chanc76949a2005-05-29 14:58:59 -070013439 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013440 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013441
13442 udelay(10);
13443
Matt Carlson898a56f2009-08-28 14:02:40 +000013444 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013445
Matt Carlson84b67b22011-07-27 14:20:52 +000013446 budget = tg3_tx_avail(tnapi);
13447 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013448 base_flags | TXD_FLAG_END, mss, 0)) {
13449 tnapi->tx_buffers[val].skb = NULL;
13450 dev_kfree_skb(skb);
13451 return -EIO;
13452 }
Michael Chanc76949a2005-05-29 14:58:59 -070013453
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013454 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013455
Michael Chan6541b802012-03-04 14:48:14 +000013456 /* Sync BD data before updating mailbox */
13457 wmb();
13458
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013459 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13460 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013461
13462 udelay(10);
13463
Matt Carlson303fc922009-11-02 14:27:34 +000013464 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13465 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013466 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013467 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013468
13469 udelay(10);
13470
Matt Carlson898a56f2009-08-28 14:02:40 +000013471 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13472 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013473 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013474 (rx_idx == (rx_start_idx + num_pkts)))
13475 break;
13476 }
13477
Matt Carlsonba1142e2011-11-04 09:15:00 +000013478 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013479 dev_kfree_skb(skb);
13480
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013481 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013482 goto out;
13483
13484 if (rx_idx != rx_start_idx + num_pkts)
13485 goto out;
13486
Matt Carlsonbb158d62011-04-25 12:42:47 +000013487 val = data_off;
13488 while (rx_idx != rx_start_idx) {
13489 desc = &rnapi->rx_rcb[rx_start_idx++];
13490 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13491 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013492
Matt Carlsonbb158d62011-04-25 12:42:47 +000013493 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13494 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013495 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013496
Matt Carlsonbb158d62011-04-25 12:42:47 +000013497 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13498 - ETH_FCS_LEN;
13499
Matt Carlson28a45952011-08-19 13:58:22 +000013500 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013501 if (rx_len != tx_len)
13502 goto out;
13503
13504 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13505 if (opaque_key != RXD_OPAQUE_RING_STD)
13506 goto out;
13507 } else {
13508 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13509 goto out;
13510 }
13511 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13512 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013513 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013514 goto out;
13515 }
13516
13517 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013518 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013519 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13520 mapping);
13521 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013522 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013523 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13524 mapping);
13525 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013526 goto out;
13527
Matt Carlsonbb158d62011-04-25 12:42:47 +000013528 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13529 PCI_DMA_FROMDEVICE);
13530
Eric Dumazet9205fd92011-11-18 06:47:01 +000013531 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013532 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013533 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013534 goto out;
13535 }
Matt Carlson4852a862011-04-13 11:05:07 +000013536 }
13537
Michael Chanc76949a2005-05-29 14:58:59 -070013538 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013539
Eric Dumazet9205fd92011-11-18 06:47:01 +000013540 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013541out:
13542 return err;
13543}
13544
Matt Carlson00c266b2011-04-25 12:42:46 +000013545#define TG3_STD_LOOPBACK_FAILED 1
13546#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013547#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013548#define TG3_LOOPBACK_FAILED \
13549 (TG3_STD_LOOPBACK_FAILED | \
13550 TG3_JMB_LOOPBACK_FAILED | \
13551 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013552
Matt Carlson941ec902011-08-19 13:58:23 +000013553static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013554{
Matt Carlson28a45952011-08-19 13:58:22 +000013555 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013556 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013557 u32 jmb_pkt_sz = 9000;
13558
13559 if (tp->dma_limit)
13560 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013561
Matt Carlsonab789042011-01-25 15:58:54 +000013562 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13563 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13564
Matt Carlson28a45952011-08-19 13:58:22 +000013565 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013566 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13567 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013568 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013569 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013570 goto done;
13571 }
13572
Joe Perches953c96e2013-04-09 10:18:14 +000013573 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013574 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013575 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13576 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013577 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013578 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013579 goto done;
13580 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013581
Joe Perches63c3a662011-04-26 08:12:10 +000013582 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013583 int i;
13584
13585 /* Reroute all rx packets to the 1st queue */
13586 for (i = MAC_RSS_INDIR_TBL_0;
13587 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13588 tw32(i, 0x0);
13589 }
13590
Matt Carlson6e01b202011-08-19 13:58:20 +000013591 /* HW errata - mac loopback fails in some cases on 5780.
13592 * Normal traffic and PHY loopback are not affected by
13593 * errata. Also, the MAC loopback test is deprecated for
13594 * all newer ASIC revisions.
13595 */
Joe Perches41535772013-02-16 11:20:04 +000013596 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013597 !tg3_flag(tp, CPMU_PRESENT)) {
13598 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013599
Matt Carlson28a45952011-08-19 13:58:22 +000013600 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013601 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013602
13603 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013604 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013605 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013606
13607 tg3_mac_loopback(tp, false);
13608 }
Matt Carlson4852a862011-04-13 11:05:07 +000013609
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013610 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013611 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013612 int i;
13613
Matt Carlson941ec902011-08-19 13:58:23 +000013614 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013615
13616 /* Wait for link */
13617 for (i = 0; i < 100; i++) {
13618 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13619 break;
13620 mdelay(1);
13621 }
13622
Matt Carlson28a45952011-08-19 13:58:22 +000013623 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013624 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013625 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013626 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013627 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013628 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013629 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013630 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013631
Matt Carlson941ec902011-08-19 13:58:23 +000013632 if (do_extlpbk) {
13633 tg3_phy_lpbk_set(tp, 0, true);
13634
13635 /* All link indications report up, but the hardware
13636 * isn't really ready for about 20 msec. Double it
13637 * to be sure.
13638 */
13639 mdelay(40);
13640
13641 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013642 data[TG3_EXT_LOOPB_TEST] |=
13643 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013644 if (tg3_flag(tp, TSO_CAPABLE) &&
13645 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013646 data[TG3_EXT_LOOPB_TEST] |=
13647 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013648 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013649 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013650 data[TG3_EXT_LOOPB_TEST] |=
13651 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013652 }
13653
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013654 /* Re-enable gphy autopowerdown. */
13655 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13656 tg3_phy_toggle_apd(tp, true);
13657 }
Matt Carlson6833c042008-11-21 17:18:59 -080013658
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013659 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13660 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013661
Matt Carlsonab789042011-01-25 15:58:54 +000013662done:
13663 tp->phy_flags |= eee_cap;
13664
Michael Chan9f40dea2005-09-05 17:53:06 -070013665 return err;
13666}
13667
Michael Chan4cafd3f2005-05-29 14:56:34 -070013668static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13669 u64 *data)
13670{
Michael Chan566f86a2005-05-29 14:56:58 -070013671 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013672 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013673
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013674 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13675 if (tg3_power_up(tp)) {
13676 etest->flags |= ETH_TEST_FL_FAILED;
13677 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13678 return;
13679 }
13680 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013681 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013682
Michael Chan566f86a2005-05-29 14:56:58 -070013683 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13684
13685 if (tg3_test_nvram(tp) != 0) {
13686 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013687 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013688 }
Matt Carlson941ec902011-08-19 13:58:23 +000013689 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013690 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013691 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013692 }
Michael Chana71116d2005-05-29 14:58:11 -070013693 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013694 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013695
Michael Chanbbe832c2005-06-24 20:20:04 -070013696 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013697 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013698 tg3_netif_stop(tp);
13699 irq_sync = 1;
13700 }
13701
13702 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013703 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013704 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013705 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013706 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013707 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013708 if (!err)
13709 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013710
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013711 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013712 tg3_phy_reset(tp);
13713
Michael Chana71116d2005-05-29 14:58:11 -070013714 if (tg3_test_registers(tp) != 0) {
13715 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013716 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013717 }
Matt Carlson28a45952011-08-19 13:58:22 +000013718
Michael Chan7942e1d2005-05-29 14:58:36 -070013719 if (tg3_test_memory(tp) != 0) {
13720 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013721 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013722 }
Matt Carlson28a45952011-08-19 13:58:22 +000013723
Matt Carlson941ec902011-08-19 13:58:23 +000013724 if (doextlpbk)
13725 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13726
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013727 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013728 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013729
David S. Millerf47c11e2005-06-24 20:18:35 -070013730 tg3_full_unlock(tp);
13731
Michael Chand4bc3922005-05-29 14:59:20 -070013732 if (tg3_test_interrupt(tp) != 0) {
13733 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013734 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013735 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013736
13737 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013738
Michael Chana71116d2005-05-29 14:58:11 -070013739 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13740 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013741 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013742 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013743 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013744 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013745 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013746
13747 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013748
13749 if (irq_sync && !err2)
13750 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013751 }
Matt Carlson80096062010-08-02 11:26:06 +000013752 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013753 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013754
Michael Chan4cafd3f2005-05-29 14:56:34 -070013755}
13756
Ben Hutchings72608992013-11-18 22:59:43 +000013757static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013758{
13759 struct tg3 *tp = netdev_priv(dev);
13760 struct hwtstamp_config stmpconf;
13761
13762 if (!tg3_flag(tp, PTP_CAPABLE))
Ben Hutchings72608992013-11-18 22:59:43 +000013763 return -EOPNOTSUPP;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013764
13765 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13766 return -EFAULT;
13767
13768 if (stmpconf.flags)
13769 return -EINVAL;
13770
Ben Hutchings58b187c2013-11-14 00:40:56 +000013771 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13772 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013773 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013774
13775 switch (stmpconf.rx_filter) {
13776 case HWTSTAMP_FILTER_NONE:
13777 tp->rxptpctl = 0;
13778 break;
13779 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13780 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13781 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13782 break;
13783 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13784 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13785 TG3_RX_PTP_CTL_SYNC_EVNT;
13786 break;
13787 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13788 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13789 TG3_RX_PTP_CTL_DELAY_REQ;
13790 break;
13791 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13792 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13793 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13794 break;
13795 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13796 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13797 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13798 break;
13799 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13800 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13801 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13802 break;
13803 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13804 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13805 TG3_RX_PTP_CTL_SYNC_EVNT;
13806 break;
13807 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13808 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13809 TG3_RX_PTP_CTL_SYNC_EVNT;
13810 break;
13811 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13812 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13813 TG3_RX_PTP_CTL_SYNC_EVNT;
13814 break;
13815 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13816 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13817 TG3_RX_PTP_CTL_DELAY_REQ;
13818 break;
13819 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13820 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13821 TG3_RX_PTP_CTL_DELAY_REQ;
13822 break;
13823 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13824 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13825 TG3_RX_PTP_CTL_DELAY_REQ;
13826 break;
13827 default:
13828 return -ERANGE;
13829 }
13830
13831 if (netif_running(dev) && tp->rxptpctl)
13832 tw32(TG3_RX_PTP_CTL,
13833 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13834
Ben Hutchings58b187c2013-11-14 00:40:56 +000013835 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13836 tg3_flag_set(tp, TX_TSTAMP_EN);
13837 else
13838 tg3_flag_clear(tp, TX_TSTAMP_EN);
13839
Matt Carlson0a633ac2012-12-03 19:36:59 +000013840 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13841 -EFAULT : 0;
13842}
13843
Ben Hutchings72608992013-11-18 22:59:43 +000013844static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
13845{
13846 struct tg3 *tp = netdev_priv(dev);
13847 struct hwtstamp_config stmpconf;
13848
13849 if (!tg3_flag(tp, PTP_CAPABLE))
13850 return -EOPNOTSUPP;
13851
13852 stmpconf.flags = 0;
13853 stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
13854 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
13855
13856 switch (tp->rxptpctl) {
13857 case 0:
13858 stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
13859 break;
13860 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
13861 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
13862 break;
13863 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13864 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
13865 break;
13866 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13867 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
13868 break;
13869 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13870 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13871 break;
13872 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13873 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
13874 break;
13875 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13876 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
13877 break;
13878 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13879 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
13880 break;
13881 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13882 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
13883 break;
13884 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13885 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
13886 break;
13887 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13888 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
13889 break;
13890 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13891 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
13892 break;
13893 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13894 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
13895 break;
13896 default:
13897 WARN_ON_ONCE(1);
13898 return -ERANGE;
13899 }
13900
13901 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13902 -EFAULT : 0;
13903}
13904
Linus Torvalds1da177e2005-04-16 15:20:36 -070013905static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13906{
13907 struct mii_ioctl_data *data = if_mii(ifr);
13908 struct tg3 *tp = netdev_priv(dev);
13909 int err;
13910
Joe Perches63c3a662011-04-26 08:12:10 +000013911 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013912 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013913 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013914 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020013915 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Richard Cochran28b04112010-07-17 08:48:55 +000013916 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013917 }
13918
Matt Carlson33f401a2010-04-05 10:19:27 +000013919 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013920 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013921 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013922
13923 /* fallthru */
13924 case SIOCGMIIREG: {
13925 u32 mii_regval;
13926
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013927 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013928 break; /* We have no PHY */
13929
Matt Carlson34eea5a2011-04-20 07:57:38 +000013930 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013931 return -EAGAIN;
13932
David S. Millerf47c11e2005-06-24 20:18:35 -070013933 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013934 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13935 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013936 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013937
13938 data->val_out = mii_regval;
13939
13940 return err;
13941 }
13942
13943 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013944 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013945 break; /* We have no PHY */
13946
Matt Carlson34eea5a2011-04-20 07:57:38 +000013947 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013948 return -EAGAIN;
13949
David S. Millerf47c11e2005-06-24 20:18:35 -070013950 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013951 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13952 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013953 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013954
13955 return err;
13956
Matt Carlson0a633ac2012-12-03 19:36:59 +000013957 case SIOCSHWTSTAMP:
Ben Hutchings72608992013-11-18 22:59:43 +000013958 return tg3_hwtstamp_set(dev, ifr);
13959
13960 case SIOCGHWTSTAMP:
13961 return tg3_hwtstamp_get(dev, ifr);
Matt Carlson0a633ac2012-12-03 19:36:59 +000013962
Linus Torvalds1da177e2005-04-16 15:20:36 -070013963 default:
13964 /* do nothing */
13965 break;
13966 }
13967 return -EOPNOTSUPP;
13968}
13969
David S. Miller15f98502005-05-18 22:49:26 -070013970static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13971{
13972 struct tg3 *tp = netdev_priv(dev);
13973
13974 memcpy(ec, &tp->coal, sizeof(*ec));
13975 return 0;
13976}
13977
Michael Chand244c892005-07-05 14:42:33 -070013978static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13979{
13980 struct tg3 *tp = netdev_priv(dev);
13981 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13982 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13983
Joe Perches63c3a662011-04-26 08:12:10 +000013984 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013985 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13986 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13987 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13988 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13989 }
13990
13991 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13992 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13993 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13994 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13995 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13996 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13997 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13998 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13999 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
14000 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
14001 return -EINVAL;
14002
14003 /* No rx interrupts will be generated if both are zero */
14004 if ((ec->rx_coalesce_usecs == 0) &&
14005 (ec->rx_max_coalesced_frames == 0))
14006 return -EINVAL;
14007
14008 /* No tx interrupts will be generated if both are zero */
14009 if ((ec->tx_coalesce_usecs == 0) &&
14010 (ec->tx_max_coalesced_frames == 0))
14011 return -EINVAL;
14012
14013 /* Only copy relevant parameters, ignore all others. */
14014 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
14015 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
14016 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
14017 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
14018 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
14019 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
14020 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
14021 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
14022 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
14023
14024 if (netif_running(dev)) {
14025 tg3_full_lock(tp, 0);
14026 __tg3_set_coalesce(tp, &tp->coal);
14027 tg3_full_unlock(tp);
14028 }
14029 return 0;
14030}
14031
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014032static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
14033{
14034 struct tg3 *tp = netdev_priv(dev);
14035
14036 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14037 netdev_warn(tp->dev, "Board does not support EEE!\n");
14038 return -EOPNOTSUPP;
14039 }
14040
14041 if (edata->advertised != tp->eee.advertised) {
14042 netdev_warn(tp->dev,
14043 "Direct manipulation of EEE advertisement is not supported\n");
14044 return -EINVAL;
14045 }
14046
14047 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
14048 netdev_warn(tp->dev,
14049 "Maximal Tx Lpi timer supported is %#x(u)\n",
14050 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
14051 return -EINVAL;
14052 }
14053
14054 tp->eee = *edata;
14055
14056 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
14057 tg3_warn_mgmt_link_flap(tp);
14058
14059 if (netif_running(tp->dev)) {
14060 tg3_full_lock(tp, 0);
14061 tg3_setup_eee(tp);
14062 tg3_phy_reset(tp);
14063 tg3_full_unlock(tp);
14064 }
14065
14066 return 0;
14067}
14068
14069static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
14070{
14071 struct tg3 *tp = netdev_priv(dev);
14072
14073 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14074 netdev_warn(tp->dev,
14075 "Board does not support EEE!\n");
14076 return -EOPNOTSUPP;
14077 }
14078
14079 *edata = tp->eee;
14080 return 0;
14081}
14082
Jeff Garzik7282d492006-09-13 14:30:00 -040014083static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014084 .get_settings = tg3_get_settings,
14085 .set_settings = tg3_set_settings,
14086 .get_drvinfo = tg3_get_drvinfo,
14087 .get_regs_len = tg3_get_regs_len,
14088 .get_regs = tg3_get_regs,
14089 .get_wol = tg3_get_wol,
14090 .set_wol = tg3_set_wol,
14091 .get_msglevel = tg3_get_msglevel,
14092 .set_msglevel = tg3_set_msglevel,
14093 .nway_reset = tg3_nway_reset,
14094 .get_link = ethtool_op_get_link,
14095 .get_eeprom_len = tg3_get_eeprom_len,
14096 .get_eeprom = tg3_get_eeprom,
14097 .set_eeprom = tg3_set_eeprom,
14098 .get_ringparam = tg3_get_ringparam,
14099 .set_ringparam = tg3_set_ringparam,
14100 .get_pauseparam = tg3_get_pauseparam,
14101 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070014102 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014103 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000014104 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014105 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070014106 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070014107 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070014108 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000014109 .get_rxnfc = tg3_get_rxnfc,
14110 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
Ben Hutchingsfe62d002014-05-15 01:25:27 +010014111 .get_rxfh = tg3_get_rxfh,
14112 .set_rxfh = tg3_set_rxfh,
Michael Chan09681692012-09-28 07:12:42 +000014113 .get_channels = tg3_get_channels,
14114 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000014115 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014116 .get_eee = tg3_get_eee,
14117 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118};
14119
David S. Millerb4017c52012-03-01 17:57:40 -050014120static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
14121 struct rtnl_link_stats64 *stats)
14122{
14123 struct tg3 *tp = netdev_priv(dev);
14124
David S. Millerb4017c52012-03-01 17:57:40 -050014125 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000014126 if (!tp->hw_stats) {
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014127 *stats = tp->net_stats_prev;
Michael Chan0f566b22012-07-29 19:15:44 +000014128 spin_unlock_bh(&tp->lock);
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014129 return stats;
Michael Chan0f566b22012-07-29 19:15:44 +000014130 }
14131
David S. Millerb4017c52012-03-01 17:57:40 -050014132 tg3_get_nstats(tp, stats);
14133 spin_unlock_bh(&tp->lock);
14134
14135 return stats;
14136}
14137
Matt Carlsonccd5ba92012-02-13 10:20:08 +000014138static void tg3_set_rx_mode(struct net_device *dev)
14139{
14140 struct tg3 *tp = netdev_priv(dev);
14141
14142 if (!netif_running(dev))
14143 return;
14144
14145 tg3_full_lock(tp, 0);
14146 __tg3_set_rx_mode(dev);
14147 tg3_full_unlock(tp);
14148}
14149
Matt Carlsonfaf16272012-02-13 10:20:07 +000014150static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
14151 int new_mtu)
14152{
14153 dev->mtu = new_mtu;
14154
14155 if (new_mtu > ETH_DATA_LEN) {
14156 if (tg3_flag(tp, 5780_CLASS)) {
14157 netdev_update_features(dev);
14158 tg3_flag_clear(tp, TSO_CAPABLE);
14159 } else {
14160 tg3_flag_set(tp, JUMBO_RING_ENABLE);
14161 }
14162 } else {
14163 if (tg3_flag(tp, 5780_CLASS)) {
14164 tg3_flag_set(tp, TSO_CAPABLE);
14165 netdev_update_features(dev);
14166 }
14167 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
14168 }
14169}
14170
14171static int tg3_change_mtu(struct net_device *dev, int new_mtu)
14172{
14173 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000014174 int err;
14175 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000014176
14177 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
14178 return -EINVAL;
14179
14180 if (!netif_running(dev)) {
14181 /* We'll just catch it later when the
14182 * device is up'd.
14183 */
14184 tg3_set_mtu(dev, tp, new_mtu);
14185 return 0;
14186 }
14187
14188 tg3_phy_stop(tp);
14189
14190 tg3_netif_stop(tp);
14191
Nithin Sujirc6993df2014-02-06 14:13:05 -080014192 tg3_set_mtu(dev, tp, new_mtu);
14193
Matt Carlsonfaf16272012-02-13 10:20:07 +000014194 tg3_full_lock(tp, 1);
14195
14196 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14197
Michael Chan2fae5e32012-03-04 14:48:15 +000014198 /* Reset PHY, otherwise the read DMA engine will be in a mode that
14199 * breaks all requests to 256 bytes.
14200 */
Joe Perches41535772013-02-16 11:20:04 +000014201 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000014202 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000014203
14204 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000014205
14206 if (!err)
14207 tg3_netif_start(tp);
14208
14209 tg3_full_unlock(tp);
14210
14211 if (!err)
14212 tg3_phy_start(tp);
14213
14214 return err;
14215}
14216
14217static const struct net_device_ops tg3_netdev_ops = {
14218 .ndo_open = tg3_open,
14219 .ndo_stop = tg3_close,
14220 .ndo_start_xmit = tg3_start_xmit,
14221 .ndo_get_stats64 = tg3_get_stats64,
14222 .ndo_validate_addr = eth_validate_addr,
14223 .ndo_set_rx_mode = tg3_set_rx_mode,
14224 .ndo_set_mac_address = tg3_set_mac_addr,
14225 .ndo_do_ioctl = tg3_ioctl,
14226 .ndo_tx_timeout = tg3_tx_timeout,
14227 .ndo_change_mtu = tg3_change_mtu,
14228 .ndo_fix_features = tg3_fix_features,
14229 .ndo_set_features = tg3_set_features,
14230#ifdef CONFIG_NET_POLL_CONTROLLER
14231 .ndo_poll_controller = tg3_poll_controller,
14232#endif
14233};
14234
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014235static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014236{
Michael Chan1b277772006-03-20 22:27:48 -080014237 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014238
14239 tp->nvram_size = EEPROM_CHIP_SIZE;
14240
Matt Carlsone4f34112009-02-25 14:25:00 +000014241 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014242 return;
14243
Michael Chanb16250e2006-09-27 16:10:14 -070014244 if ((magic != TG3_EEPROM_MAGIC) &&
14245 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14246 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014247 return;
14248
14249 /*
14250 * Size the chip by reading offsets at increasing powers of two.
14251 * When we encounter our validation signature, we know the addressing
14252 * has wrapped around, and thus have our chip size.
14253 */
Michael Chan1b277772006-03-20 22:27:48 -080014254 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014255
14256 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014257 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014258 return;
14259
Michael Chan18201802006-03-20 22:29:15 -080014260 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014261 break;
14262
14263 cursize <<= 1;
14264 }
14265
14266 tp->nvram_size = cursize;
14267}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014268
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014269static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270{
14271 u32 val;
14272
Joe Perches63c3a662011-04-26 08:12:10 +000014273 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014274 return;
14275
14276 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014277 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014278 tg3_get_eeprom_size(tp);
14279 return;
14280 }
14281
Matt Carlson6d348f22009-02-25 14:25:52 +000014282 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014283 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014284 /* This is confusing. We want to operate on the
14285 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14286 * call will read from NVRAM and byteswap the data
14287 * according to the byteswapping settings for all
14288 * other register accesses. This ensures the data we
14289 * want will always reside in the lower 16-bits.
14290 * However, the data in NVRAM is in LE format, which
14291 * means the data from the NVRAM read will always be
14292 * opposite the endianness of the CPU. The 16-bit
14293 * byteswap then brings the data to CPU endianness.
14294 */
14295 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014296 return;
14297 }
14298 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014299 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014300}
14301
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014302static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303{
14304 u32 nvcfg1;
14305
14306 nvcfg1 = tr32(NVRAM_CFG1);
14307 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014308 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014309 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014310 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14311 tw32(NVRAM_CFG1, nvcfg1);
14312 }
14313
Joe Perches41535772013-02-16 11:20:04 +000014314 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014315 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014316 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014317 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14318 tp->nvram_jedecnum = JEDEC_ATMEL;
14319 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014320 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014321 break;
14322 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14323 tp->nvram_jedecnum = JEDEC_ATMEL;
14324 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14325 break;
14326 case FLASH_VENDOR_ATMEL_EEPROM:
14327 tp->nvram_jedecnum = JEDEC_ATMEL;
14328 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014329 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014330 break;
14331 case FLASH_VENDOR_ST:
14332 tp->nvram_jedecnum = JEDEC_ST;
14333 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014334 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014335 break;
14336 case FLASH_VENDOR_SAIFUN:
14337 tp->nvram_jedecnum = JEDEC_SAIFUN;
14338 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14339 break;
14340 case FLASH_VENDOR_SST_SMALL:
14341 case FLASH_VENDOR_SST_LARGE:
14342 tp->nvram_jedecnum = JEDEC_SST;
14343 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014345 }
Matt Carlson8590a602009-08-28 12:29:16 +000014346 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014347 tp->nvram_jedecnum = JEDEC_ATMEL;
14348 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014349 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014350 }
14351}
14352
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014353static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014354{
14355 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14356 case FLASH_5752PAGE_SIZE_256:
14357 tp->nvram_pagesize = 256;
14358 break;
14359 case FLASH_5752PAGE_SIZE_512:
14360 tp->nvram_pagesize = 512;
14361 break;
14362 case FLASH_5752PAGE_SIZE_1K:
14363 tp->nvram_pagesize = 1024;
14364 break;
14365 case FLASH_5752PAGE_SIZE_2K:
14366 tp->nvram_pagesize = 2048;
14367 break;
14368 case FLASH_5752PAGE_SIZE_4K:
14369 tp->nvram_pagesize = 4096;
14370 break;
14371 case FLASH_5752PAGE_SIZE_264:
14372 tp->nvram_pagesize = 264;
14373 break;
14374 case FLASH_5752PAGE_SIZE_528:
14375 tp->nvram_pagesize = 528;
14376 break;
14377 }
14378}
14379
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014380static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014381{
14382 u32 nvcfg1;
14383
14384 nvcfg1 = tr32(NVRAM_CFG1);
14385
Michael Chane6af3012005-04-21 17:12:05 -070014386 /* NVRAM protection for TPM */
14387 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014388 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014389
Michael Chan361b4ac2005-04-21 17:11:21 -070014390 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014391 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14392 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14393 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014394 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014395 break;
14396 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14397 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014398 tg3_flag_set(tp, NVRAM_BUFFERED);
14399 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014400 break;
14401 case FLASH_5752VENDOR_ST_M45PE10:
14402 case FLASH_5752VENDOR_ST_M45PE20:
14403 case FLASH_5752VENDOR_ST_M45PE40:
14404 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014405 tg3_flag_set(tp, NVRAM_BUFFERED);
14406 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014407 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014408 }
14409
Joe Perches63c3a662011-04-26 08:12:10 +000014410 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014411 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014412 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014413 /* For eeprom, set pagesize to maximum eeprom size */
14414 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14415
14416 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14417 tw32(NVRAM_CFG1, nvcfg1);
14418 }
14419}
14420
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014421static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014422{
Matt Carlson989a9d22007-05-05 11:51:05 -070014423 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014424
14425 nvcfg1 = tr32(NVRAM_CFG1);
14426
14427 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014428 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014429 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014430 protect = 1;
14431 }
Michael Chand3c7b882006-03-23 01:28:25 -080014432
Matt Carlson989a9d22007-05-05 11:51:05 -070014433 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14434 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014435 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14436 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14437 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14438 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14439 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014440 tg3_flag_set(tp, NVRAM_BUFFERED);
14441 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014442 tp->nvram_pagesize = 264;
14443 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14444 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14445 tp->nvram_size = (protect ? 0x3e200 :
14446 TG3_NVRAM_SIZE_512KB);
14447 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14448 tp->nvram_size = (protect ? 0x1f200 :
14449 TG3_NVRAM_SIZE_256KB);
14450 else
14451 tp->nvram_size = (protect ? 0x1f200 :
14452 TG3_NVRAM_SIZE_128KB);
14453 break;
14454 case FLASH_5752VENDOR_ST_M45PE10:
14455 case FLASH_5752VENDOR_ST_M45PE20:
14456 case FLASH_5752VENDOR_ST_M45PE40:
14457 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014458 tg3_flag_set(tp, NVRAM_BUFFERED);
14459 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014460 tp->nvram_pagesize = 256;
14461 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14462 tp->nvram_size = (protect ?
14463 TG3_NVRAM_SIZE_64KB :
14464 TG3_NVRAM_SIZE_128KB);
14465 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14466 tp->nvram_size = (protect ?
14467 TG3_NVRAM_SIZE_64KB :
14468 TG3_NVRAM_SIZE_256KB);
14469 else
14470 tp->nvram_size = (protect ?
14471 TG3_NVRAM_SIZE_128KB :
14472 TG3_NVRAM_SIZE_512KB);
14473 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014474 }
14475}
14476
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014477static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014478{
14479 u32 nvcfg1;
14480
14481 nvcfg1 = tr32(NVRAM_CFG1);
14482
14483 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014484 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14485 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14486 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14487 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14488 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014489 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014490 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014491
Matt Carlson8590a602009-08-28 12:29:16 +000014492 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14493 tw32(NVRAM_CFG1, nvcfg1);
14494 break;
14495 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14496 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14497 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14498 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14499 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014500 tg3_flag_set(tp, NVRAM_BUFFERED);
14501 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014502 tp->nvram_pagesize = 264;
14503 break;
14504 case FLASH_5752VENDOR_ST_M45PE10:
14505 case FLASH_5752VENDOR_ST_M45PE20:
14506 case FLASH_5752VENDOR_ST_M45PE40:
14507 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014508 tg3_flag_set(tp, NVRAM_BUFFERED);
14509 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014510 tp->nvram_pagesize = 256;
14511 break;
Michael Chan1b277772006-03-20 22:27:48 -080014512 }
14513}
14514
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014515static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014516{
14517 u32 nvcfg1, protect = 0;
14518
14519 nvcfg1 = tr32(NVRAM_CFG1);
14520
14521 /* NVRAM protection for TPM */
14522 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014523 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014524 protect = 1;
14525 }
14526
14527 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14528 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014529 case FLASH_5761VENDOR_ATMEL_ADB021D:
14530 case FLASH_5761VENDOR_ATMEL_ADB041D:
14531 case FLASH_5761VENDOR_ATMEL_ADB081D:
14532 case FLASH_5761VENDOR_ATMEL_ADB161D:
14533 case FLASH_5761VENDOR_ATMEL_MDB021D:
14534 case FLASH_5761VENDOR_ATMEL_MDB041D:
14535 case FLASH_5761VENDOR_ATMEL_MDB081D:
14536 case FLASH_5761VENDOR_ATMEL_MDB161D:
14537 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014538 tg3_flag_set(tp, NVRAM_BUFFERED);
14539 tg3_flag_set(tp, FLASH);
14540 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014541 tp->nvram_pagesize = 256;
14542 break;
14543 case FLASH_5761VENDOR_ST_A_M45PE20:
14544 case FLASH_5761VENDOR_ST_A_M45PE40:
14545 case FLASH_5761VENDOR_ST_A_M45PE80:
14546 case FLASH_5761VENDOR_ST_A_M45PE16:
14547 case FLASH_5761VENDOR_ST_M_M45PE20:
14548 case FLASH_5761VENDOR_ST_M_M45PE40:
14549 case FLASH_5761VENDOR_ST_M_M45PE80:
14550 case FLASH_5761VENDOR_ST_M_M45PE16:
14551 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014552 tg3_flag_set(tp, NVRAM_BUFFERED);
14553 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014554 tp->nvram_pagesize = 256;
14555 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014556 }
14557
14558 if (protect) {
14559 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14560 } else {
14561 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014562 case FLASH_5761VENDOR_ATMEL_ADB161D:
14563 case FLASH_5761VENDOR_ATMEL_MDB161D:
14564 case FLASH_5761VENDOR_ST_A_M45PE16:
14565 case FLASH_5761VENDOR_ST_M_M45PE16:
14566 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14567 break;
14568 case FLASH_5761VENDOR_ATMEL_ADB081D:
14569 case FLASH_5761VENDOR_ATMEL_MDB081D:
14570 case FLASH_5761VENDOR_ST_A_M45PE80:
14571 case FLASH_5761VENDOR_ST_M_M45PE80:
14572 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14573 break;
14574 case FLASH_5761VENDOR_ATMEL_ADB041D:
14575 case FLASH_5761VENDOR_ATMEL_MDB041D:
14576 case FLASH_5761VENDOR_ST_A_M45PE40:
14577 case FLASH_5761VENDOR_ST_M_M45PE40:
14578 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14579 break;
14580 case FLASH_5761VENDOR_ATMEL_ADB021D:
14581 case FLASH_5761VENDOR_ATMEL_MDB021D:
14582 case FLASH_5761VENDOR_ST_A_M45PE20:
14583 case FLASH_5761VENDOR_ST_M_M45PE20:
14584 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14585 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014586 }
14587 }
14588}
14589
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014590static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014591{
14592 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014593 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014594 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14595}
14596
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014597static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014598{
14599 u32 nvcfg1;
14600
14601 nvcfg1 = tr32(NVRAM_CFG1);
14602
14603 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14604 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14605 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14606 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014607 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014608 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14609
14610 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14611 tw32(NVRAM_CFG1, nvcfg1);
14612 return;
14613 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14614 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14615 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14616 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14617 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14618 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14619 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14620 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014621 tg3_flag_set(tp, NVRAM_BUFFERED);
14622 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014623
14624 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14625 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14626 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14627 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14628 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14629 break;
14630 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14631 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14632 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14633 break;
14634 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14635 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14636 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14637 break;
14638 }
14639 break;
14640 case FLASH_5752VENDOR_ST_M45PE10:
14641 case FLASH_5752VENDOR_ST_M45PE20:
14642 case FLASH_5752VENDOR_ST_M45PE40:
14643 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014644 tg3_flag_set(tp, NVRAM_BUFFERED);
14645 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014646
14647 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14648 case FLASH_5752VENDOR_ST_M45PE10:
14649 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14650 break;
14651 case FLASH_5752VENDOR_ST_M45PE20:
14652 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14653 break;
14654 case FLASH_5752VENDOR_ST_M45PE40:
14655 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14656 break;
14657 }
14658 break;
14659 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014660 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014661 return;
14662 }
14663
Matt Carlsona1b950d2009-09-01 13:20:17 +000014664 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);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014667}
14668
14669
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014670static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014671{
14672 u32 nvcfg1;
14673
14674 nvcfg1 = tr32(NVRAM_CFG1);
14675
14676 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14677 case FLASH_5717VENDOR_ATMEL_EEPROM:
14678 case FLASH_5717VENDOR_MICRO_EEPROM:
14679 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014680 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014681 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14682
14683 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14684 tw32(NVRAM_CFG1, nvcfg1);
14685 return;
14686 case FLASH_5717VENDOR_ATMEL_MDB011D:
14687 case FLASH_5717VENDOR_ATMEL_ADB011B:
14688 case FLASH_5717VENDOR_ATMEL_ADB011D:
14689 case FLASH_5717VENDOR_ATMEL_MDB021D:
14690 case FLASH_5717VENDOR_ATMEL_ADB021B:
14691 case FLASH_5717VENDOR_ATMEL_ADB021D:
14692 case FLASH_5717VENDOR_ATMEL_45USPT:
14693 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014694 tg3_flag_set(tp, NVRAM_BUFFERED);
14695 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014696
14697 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14698 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014699 /* Detect size with tg3_nvram_get_size() */
14700 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014701 case FLASH_5717VENDOR_ATMEL_ADB021B:
14702 case FLASH_5717VENDOR_ATMEL_ADB021D:
14703 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14704 break;
14705 default:
14706 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14707 break;
14708 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014709 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014710 case FLASH_5717VENDOR_ST_M_M25PE10:
14711 case FLASH_5717VENDOR_ST_A_M25PE10:
14712 case FLASH_5717VENDOR_ST_M_M45PE10:
14713 case FLASH_5717VENDOR_ST_A_M45PE10:
14714 case FLASH_5717VENDOR_ST_M_M25PE20:
14715 case FLASH_5717VENDOR_ST_A_M25PE20:
14716 case FLASH_5717VENDOR_ST_M_M45PE20:
14717 case FLASH_5717VENDOR_ST_A_M45PE20:
14718 case FLASH_5717VENDOR_ST_25USPT:
14719 case FLASH_5717VENDOR_ST_45USPT:
14720 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014721 tg3_flag_set(tp, NVRAM_BUFFERED);
14722 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014723
14724 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14725 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014726 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014727 /* Detect size with tg3_nvram_get_size() */
14728 break;
14729 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014730 case FLASH_5717VENDOR_ST_A_M45PE20:
14731 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14732 break;
14733 default:
14734 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14735 break;
14736 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014737 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014738 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014739 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014740 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014741 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014742
14743 tg3_nvram_get_pagesize(tp, nvcfg1);
14744 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014745 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014746}
14747
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014748static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014749{
14750 u32 nvcfg1, nvmpinstrp;
14751
14752 nvcfg1 = tr32(NVRAM_CFG1);
14753 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14754
Joe Perches41535772013-02-16 11:20:04 +000014755 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014756 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14757 tg3_flag_set(tp, NO_NVRAM);
14758 return;
14759 }
14760
14761 switch (nvmpinstrp) {
14762 case FLASH_5762_EEPROM_HD:
14763 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014764 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014765 case FLASH_5762_EEPROM_LD:
14766 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014767 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014768 case FLASH_5720VENDOR_M_ST_M45PE20:
14769 /* This pinstrap supports multiple sizes, so force it
14770 * to read the actual size from location 0xf0.
14771 */
14772 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14773 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014774 }
14775 }
14776
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014777 switch (nvmpinstrp) {
14778 case FLASH_5720_EEPROM_HD:
14779 case FLASH_5720_EEPROM_LD:
14780 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014781 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014782
14783 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14784 tw32(NVRAM_CFG1, nvcfg1);
14785 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14786 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14787 else
14788 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14789 return;
14790 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14791 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14792 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14793 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14794 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14795 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14796 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14797 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14798 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14799 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14800 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14801 case FLASH_5720VENDOR_ATMEL_45USPT:
14802 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014803 tg3_flag_set(tp, NVRAM_BUFFERED);
14804 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014805
14806 switch (nvmpinstrp) {
14807 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14808 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14809 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14810 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14811 break;
14812 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14813 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14814 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14815 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14816 break;
14817 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14818 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14819 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14820 break;
14821 default:
Joe Perches41535772013-02-16 11:20:04 +000014822 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014823 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014824 break;
14825 }
14826 break;
14827 case FLASH_5720VENDOR_M_ST_M25PE10:
14828 case FLASH_5720VENDOR_M_ST_M45PE10:
14829 case FLASH_5720VENDOR_A_ST_M25PE10:
14830 case FLASH_5720VENDOR_A_ST_M45PE10:
14831 case FLASH_5720VENDOR_M_ST_M25PE20:
14832 case FLASH_5720VENDOR_M_ST_M45PE20:
14833 case FLASH_5720VENDOR_A_ST_M25PE20:
14834 case FLASH_5720VENDOR_A_ST_M45PE20:
14835 case FLASH_5720VENDOR_M_ST_M25PE40:
14836 case FLASH_5720VENDOR_M_ST_M45PE40:
14837 case FLASH_5720VENDOR_A_ST_M25PE40:
14838 case FLASH_5720VENDOR_A_ST_M45PE40:
14839 case FLASH_5720VENDOR_M_ST_M25PE80:
14840 case FLASH_5720VENDOR_M_ST_M45PE80:
14841 case FLASH_5720VENDOR_A_ST_M25PE80:
14842 case FLASH_5720VENDOR_A_ST_M45PE80:
14843 case FLASH_5720VENDOR_ST_25USPT:
14844 case FLASH_5720VENDOR_ST_45USPT:
14845 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014846 tg3_flag_set(tp, NVRAM_BUFFERED);
14847 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014848
14849 switch (nvmpinstrp) {
14850 case FLASH_5720VENDOR_M_ST_M25PE20:
14851 case FLASH_5720VENDOR_M_ST_M45PE20:
14852 case FLASH_5720VENDOR_A_ST_M25PE20:
14853 case FLASH_5720VENDOR_A_ST_M45PE20:
14854 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14855 break;
14856 case FLASH_5720VENDOR_M_ST_M25PE40:
14857 case FLASH_5720VENDOR_M_ST_M45PE40:
14858 case FLASH_5720VENDOR_A_ST_M25PE40:
14859 case FLASH_5720VENDOR_A_ST_M45PE40:
14860 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14861 break;
14862 case FLASH_5720VENDOR_M_ST_M25PE80:
14863 case FLASH_5720VENDOR_M_ST_M45PE80:
14864 case FLASH_5720VENDOR_A_ST_M25PE80:
14865 case FLASH_5720VENDOR_A_ST_M45PE80:
14866 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14867 break;
14868 default:
Joe Perches41535772013-02-16 11:20:04 +000014869 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014870 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014871 break;
14872 }
14873 break;
14874 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014875 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014876 return;
14877 }
14878
14879 tg3_nvram_get_pagesize(tp, nvcfg1);
14880 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014881 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014882
Joe Perches41535772013-02-16 11:20:04 +000014883 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014884 u32 val;
14885
14886 if (tg3_nvram_read(tp, 0, &val))
14887 return;
14888
14889 if (val != TG3_EEPROM_MAGIC &&
14890 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14891 tg3_flag_set(tp, NO_NVRAM);
14892 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014893}
14894
Linus Torvalds1da177e2005-04-16 15:20:36 -070014895/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014896static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014897{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014898 if (tg3_flag(tp, IS_SSB_CORE)) {
14899 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14900 tg3_flag_clear(tp, NVRAM);
14901 tg3_flag_clear(tp, NVRAM_BUFFERED);
14902 tg3_flag_set(tp, NO_NVRAM);
14903 return;
14904 }
14905
Linus Torvalds1da177e2005-04-16 15:20:36 -070014906 tw32_f(GRC_EEPROM_ADDR,
14907 (EEPROM_ADDR_FSM_RESET |
14908 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14909 EEPROM_ADDR_CLKPERD_SHIFT)));
14910
Michael Chan9d57f012006-12-07 00:23:25 -080014911 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014912
14913 /* Enable seeprom accesses. */
14914 tw32_f(GRC_LOCAL_CTRL,
14915 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14916 udelay(100);
14917
Joe Perches41535772013-02-16 11:20:04 +000014918 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14919 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014920 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014921
Michael Chanec41c7d2006-01-17 02:40:55 -080014922 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014923 netdev_warn(tp->dev,
14924 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014925 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014926 return;
14927 }
Michael Chane6af3012005-04-21 17:12:05 -070014928 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014929
Matt Carlson989a9d22007-05-05 11:51:05 -070014930 tp->nvram_size = 0;
14931
Joe Perches41535772013-02-16 11:20:04 +000014932 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014933 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014934 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014935 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014936 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14937 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14938 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014939 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014940 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014941 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014942 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014943 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014944 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014945 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014946 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014947 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14948 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014949 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014950 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14951 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014952 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014953 else
14954 tg3_get_nvram_info(tp);
14955
Matt Carlson989a9d22007-05-05 11:51:05 -070014956 if (tp->nvram_size == 0)
14957 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014958
Michael Chane6af3012005-04-21 17:12:05 -070014959 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014960 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014961
14962 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014963 tg3_flag_clear(tp, NVRAM);
14964 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014965
14966 tg3_get_eeprom_size(tp);
14967 }
14968}
14969
Linus Torvalds1da177e2005-04-16 15:20:36 -070014970struct subsys_tbl_ent {
14971 u16 subsys_vendor, subsys_devid;
14972 u32 phy_id;
14973};
14974
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014975static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014976 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014977 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014978 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014979 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014980 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014981 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014982 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014983 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14984 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14985 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014986 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014987 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014988 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014989 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14990 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14991 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014992 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014993 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014994 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014995 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014996 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014997 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014998 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999
15000 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015001 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015002 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015003 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015004 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015005 { TG3PCI_SUBVENDOR_ID_3COM,
15006 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
15007 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015008 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015009 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015010 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011
15012 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015013 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015014 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015015 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015016 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015017 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015018 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015019 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015020 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015021
15022 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015023 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015024 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015025 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015026 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015027 { TG3PCI_SUBVENDOR_ID_COMPAQ,
15028 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
15029 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015030 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015031 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015032 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015033
15034 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015035 { TG3PCI_SUBVENDOR_ID_IBM,
15036 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015037};
15038
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015039static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015040{
15041 int i;
15042
15043 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
15044 if ((subsys_id_to_phy_id[i].subsys_vendor ==
15045 tp->pdev->subsystem_vendor) &&
15046 (subsys_id_to_phy_id[i].subsys_devid ==
15047 tp->pdev->subsystem_device))
15048 return &subsys_id_to_phy_id[i];
15049 }
15050 return NULL;
15051}
15052
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015053static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015054{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015055 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070015056
Matt Carlson79eb6902010-02-17 15:17:03 +000015057 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015058 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15059
Gary Zambranoa85feb82007-05-05 11:52:19 -070015060 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000015061 tg3_flag_set(tp, EEPROM_WRITE_PROT);
15062 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080015063
Joe Perches41535772013-02-16 11:20:04 +000015064 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080015065 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015066 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15067 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015068 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015069 val = tr32(VCPU_CFGSHDW);
15070 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000015071 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070015072 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015073 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015074 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015075 device_set_wakeup_enable(&tp->pdev->dev, true);
15076 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015077 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070015078 }
15079
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
15081 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
15082 u32 nic_cfg, led_cfg;
Nithin Sujir7c786062013-12-06 09:53:17 -080015083 u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
15084 u32 nic_phy_id, ver, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015085 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015086
15087 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
15088 tp->nic_sram_data_cfg = nic_cfg;
15089
15090 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
15091 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015092 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15093 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15094 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015095 (ver > 0) && (ver < 0x100))
15096 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
15097
Joe Perches41535772013-02-16 11:20:04 +000015098 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070015099 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
15100
Nithin Sujir7c786062013-12-06 09:53:17 -080015101 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15102 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15103 tg3_asic_rev(tp) == ASIC_REV_5720)
15104 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
15105
Linus Torvalds1da177e2005-04-16 15:20:36 -070015106 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
15107 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
15108 eeprom_phy_serdes = 1;
15109
15110 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
15111 if (nic_phy_id != 0) {
15112 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
15113 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
15114
15115 eeprom_phy_id = (id1 >> 16) << 10;
15116 eeprom_phy_id |= (id2 & 0xfc00) << 16;
15117 eeprom_phy_id |= (id2 & 0x03ff) << 0;
15118 } else
15119 eeprom_phy_id = 0;
15120
Michael Chan7d0c41e2005-04-21 17:06:20 -070015121 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070015122 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000015123 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015124 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000015125 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015126 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070015127 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070015128
Joe Perches63c3a662011-04-26 08:12:10 +000015129 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015130 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
15131 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070015132 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015133 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
15134
15135 switch (led_cfg) {
15136 default:
15137 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
15138 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15139 break;
15140
15141 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
15142 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15143 break;
15144
15145 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
15146 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070015147
15148 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
15149 * read on some older 5700/5701 bootcode.
15150 */
Joe Perches41535772013-02-16 11:20:04 +000015151 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15152 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070015153 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15154
Linus Torvalds1da177e2005-04-16 15:20:36 -070015155 break;
15156
15157 case SHASTA_EXT_LED_SHARED:
15158 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000015159 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
15160 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015161 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15162 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070015163
15164 if (tg3_flag(tp, 5717_PLUS) ||
15165 tg3_asic_rev(tp) == ASIC_REV_5762)
15166 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
15167 LED_CTRL_BLINK_RATE_MASK;
15168
Linus Torvalds1da177e2005-04-16 15:20:36 -070015169 break;
15170
15171 case SHASTA_EXT_LED_MAC:
15172 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
15173 break;
15174
15175 case SHASTA_EXT_LED_COMBO:
15176 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000015177 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015178 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15179 LED_CTRL_MODE_PHY_2);
15180 break;
15181
Stephen Hemminger855e1112008-04-16 16:37:28 -070015182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015183
Joe Perches41535772013-02-16 11:20:04 +000015184 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
15185 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015186 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
15187 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15188
Joe Perches41535772013-02-16 11:20:04 +000015189 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015190 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080015191
Michael Chan9d26e212006-12-07 00:21:14 -080015192 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000015193 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015194 if ((tp->pdev->subsystem_vendor ==
15195 PCI_VENDOR_ID_ARIMA) &&
15196 (tp->pdev->subsystem_device == 0x205a ||
15197 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000015198 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015199 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015200 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15201 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015203
15204 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000015205 tg3_flag_set(tp, ENABLE_ASF);
15206 if (tg3_flag(tp, 5750_PLUS))
15207 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015208 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015209
15210 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015211 tg3_flag(tp, 5750_PLUS))
15212 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015213
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015214 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070015215 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000015216 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015217
Joe Perches63c3a662011-04-26 08:12:10 +000015218 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015219 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015220 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015221 device_set_wakeup_enable(&tp->pdev->dev, true);
15222 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015223
Linus Torvalds1da177e2005-04-16 15:20:36 -070015224 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015225 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015226
15227 /* serdes signal pre-emphasis in register 0x590 set by */
15228 /* bootcode if bit 18 is set */
15229 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015230 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015231
Joe Perches63c3a662011-04-26 08:12:10 +000015232 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015233 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15234 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015235 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015236 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015237
Nithin Sujir942d1af2013-04-09 08:48:07 +000015238 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015239 u32 cfg3;
15240
15241 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015242 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15243 !tg3_flag(tp, 57765_PLUS) &&
15244 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015245 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015246 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15247 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15248 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15249 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015250 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015251
Matt Carlson14417062010-02-17 15:16:59 +000015252 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015253 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015254 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015255 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015256 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015257 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Nithin Sujir7c786062013-12-06 09:53:17 -080015258
15259 if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
15260 tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015261 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015262done:
Joe Perches63c3a662011-04-26 08:12:10 +000015263 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015264 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015265 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015266 else
15267 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015268}
15269
Michael Chanc86a8562013-01-06 12:51:08 +000015270static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15271{
15272 int i, err;
15273 u32 val2, off = offset * 8;
15274
15275 err = tg3_nvram_lock(tp);
15276 if (err)
15277 return err;
15278
15279 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15280 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15281 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15282 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15283 udelay(10);
15284
15285 for (i = 0; i < 100; i++) {
15286 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15287 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15288 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15289 break;
15290 }
15291 udelay(10);
15292 }
15293
15294 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15295
15296 tg3_nvram_unlock(tp);
15297 if (val2 & APE_OTP_STATUS_CMD_DONE)
15298 return 0;
15299
15300 return -EBUSY;
15301}
15302
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015303static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015304{
15305 int i;
15306 u32 val;
15307
15308 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15309 tw32(OTP_CTRL, cmd);
15310
15311 /* Wait for up to 1 ms for command to execute. */
15312 for (i = 0; i < 100; i++) {
15313 val = tr32(OTP_STATUS);
15314 if (val & OTP_STATUS_CMD_DONE)
15315 break;
15316 udelay(10);
15317 }
15318
15319 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15320}
15321
15322/* Read the gphy configuration from the OTP region of the chip. The gphy
15323 * configuration is a 32-bit value that straddles the alignment boundary.
15324 * We do two 32-bit reads and then shift and merge the results.
15325 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015326static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015327{
15328 u32 bhalf_otp, thalf_otp;
15329
15330 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15331
15332 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15333 return 0;
15334
15335 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15336
15337 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15338 return 0;
15339
15340 thalf_otp = tr32(OTP_READ_DATA);
15341
15342 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15343
15344 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15345 return 0;
15346
15347 bhalf_otp = tr32(OTP_READ_DATA);
15348
15349 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15350}
15351
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015352static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015353{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015354 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015355
Nithin Sujir7c786062013-12-06 09:53:17 -080015356 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
15357 if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
15358 adv |= ADVERTISED_1000baseT_Half;
15359 adv |= ADVERTISED_1000baseT_Full;
15360 }
Matt Carlsone256f8a2011-03-09 16:58:24 +000015361
15362 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15363 adv |= ADVERTISED_100baseT_Half |
15364 ADVERTISED_100baseT_Full |
15365 ADVERTISED_10baseT_Half |
15366 ADVERTISED_10baseT_Full |
15367 ADVERTISED_TP;
15368 else
15369 adv |= ADVERTISED_FIBRE;
15370
15371 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015372 tp->link_config.speed = SPEED_UNKNOWN;
15373 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015374 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015375 tp->link_config.active_speed = SPEED_UNKNOWN;
15376 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015377
15378 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015379}
15380
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015381static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015382{
15383 u32 hw_phy_id_1, hw_phy_id_2;
15384 u32 hw_phy_id, hw_phy_id_masked;
15385 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015386
Matt Carlsone256f8a2011-03-09 16:58:24 +000015387 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015388 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015389 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15390
Michael Chan8151ad52012-07-29 19:15:41 +000015391 if (tg3_flag(tp, ENABLE_APE)) {
15392 switch (tp->pci_fn) {
15393 case 0:
15394 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15395 break;
15396 case 1:
15397 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15398 break;
15399 case 2:
15400 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15401 break;
15402 case 3:
15403 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15404 break;
15405 }
15406 }
15407
Nithin Sujir942d1af2013-04-09 08:48:07 +000015408 if (!tg3_flag(tp, ENABLE_ASF) &&
15409 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15410 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15411 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15412 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15413
Joe Perches63c3a662011-04-26 08:12:10 +000015414 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015415 return tg3_phy_init(tp);
15416
Linus Torvalds1da177e2005-04-16 15:20:36 -070015417 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015418 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015419 */
15420 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015421 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015422 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015423 } else {
15424 /* Now read the physical PHY_ID from the chip and verify
15425 * that it is sane. If it doesn't look good, we fall back
15426 * to either the hard-coded table based PHY_ID and failing
15427 * that the value found in the eeprom area.
15428 */
15429 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15430 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15431
15432 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15433 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15434 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15435
Matt Carlson79eb6902010-02-17 15:17:03 +000015436 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015437 }
15438
Matt Carlson79eb6902010-02-17 15:17:03 +000015439 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015440 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015441 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015442 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015443 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015444 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015445 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015446 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015447 /* Do nothing, phy ID already set up in
15448 * tg3_get_eeprom_hw_cfg().
15449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015450 } else {
15451 struct subsys_tbl_ent *p;
15452
15453 /* No eeprom signature? Try the hardcoded
15454 * subsys device table.
15455 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015456 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015457 if (p) {
15458 tp->phy_id = p->phy_id;
15459 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15460 /* For now we saw the IDs 0xbc050cd0,
15461 * 0xbc050f80 and 0xbc050c30 on devices
15462 * connected to an BCM4785 and there are
15463 * probably more. Just assume that the phy is
15464 * supported when it is connected to a SSB core
15465 * for now.
15466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015467 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015469
Linus Torvalds1da177e2005-04-16 15:20:36 -070015470 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015471 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015472 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015473 }
15474 }
15475
Matt Carlsona6b68da2010-12-06 08:28:52 +000015476 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015477 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15478 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015479 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015480 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15481 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15482 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15483 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015484 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015485 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15486
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015487 tp->eee.supported = SUPPORTED_100baseT_Full |
15488 SUPPORTED_1000baseT_Full;
15489 tp->eee.advertised = ADVERTISED_100baseT_Full |
15490 ADVERTISED_1000baseT_Full;
15491 tp->eee.eee_enabled = 1;
15492 tp->eee.tx_lpi_enabled = 1;
15493 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15494 }
15495
Matt Carlsone256f8a2011-03-09 16:58:24 +000015496 tg3_phy_init_link_config(tp);
15497
Nithin Sujir942d1af2013-04-09 08:48:07 +000015498 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15499 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015500 !tg3_flag(tp, ENABLE_APE) &&
15501 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015502 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015503
15504 tg3_readphy(tp, MII_BMSR, &bmsr);
15505 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15506 (bmsr & BMSR_LSTATUS))
15507 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015508
Linus Torvalds1da177e2005-04-16 15:20:36 -070015509 err = tg3_phy_reset(tp);
15510 if (err)
15511 return err;
15512
Matt Carlson42b64a42011-05-19 12:12:49 +000015513 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015514
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015515 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015516 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15517 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015518
15519 tg3_writephy(tp, MII_BMCR,
15520 BMCR_ANENABLE | BMCR_ANRESTART);
15521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015522 }
15523
15524skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015525 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015526 err = tg3_init_5401phy_dsp(tp);
15527 if (err)
15528 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015529
Linus Torvalds1da177e2005-04-16 15:20:36 -070015530 err = tg3_init_5401phy_dsp(tp);
15531 }
15532
Linus Torvalds1da177e2005-04-16 15:20:36 -070015533 return err;
15534}
15535
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015536static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015537{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015538 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015539 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015540 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015541 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015542
Matt Carlson535a4902011-07-20 10:20:56 +000015543 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015544 if (!vpd_data)
15545 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546
Matt Carlson535a4902011-07-20 10:20:56 +000015547 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015548 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015549 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015550
15551 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15552 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15553 i += PCI_VPD_LRDT_TAG_SIZE;
15554
Matt Carlson535a4902011-07-20 10:20:56 +000015555 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015556 goto out_not_found;
15557
Matt Carlson184b8902010-04-05 10:19:25 +000015558 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15559 PCI_VPD_RO_KEYWORD_MFR_ID);
15560 if (j > 0) {
15561 len = pci_vpd_info_field_size(&vpd_data[j]);
15562
15563 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15564 if (j + len > block_end || len != 4 ||
15565 memcmp(&vpd_data[j], "1028", 4))
15566 goto partno;
15567
15568 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15569 PCI_VPD_RO_KEYWORD_VENDOR0);
15570 if (j < 0)
15571 goto partno;
15572
15573 len = pci_vpd_info_field_size(&vpd_data[j]);
15574
15575 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15576 if (j + len > block_end)
15577 goto partno;
15578
Kees Cook715230a2013-03-27 06:40:50 +000015579 if (len >= sizeof(tp->fw_ver))
15580 len = sizeof(tp->fw_ver) - 1;
15581 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15582 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15583 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015584 }
15585
15586partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015587 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15588 PCI_VPD_RO_KEYWORD_PARTNO);
15589 if (i < 0)
15590 goto out_not_found;
15591
15592 len = pci_vpd_info_field_size(&vpd_data[i]);
15593
15594 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15595 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015596 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015597 goto out_not_found;
15598
15599 memcpy(tp->board_part_number, &vpd_data[i], len);
15600
Linus Torvalds1da177e2005-04-16 15:20:36 -070015601out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015602 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015603 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015604 return;
15605
15606out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015607 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015608 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15609 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015610 strcpy(tp->board_part_number, "BCM5717");
15611 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15612 strcpy(tp->board_part_number, "BCM5718");
15613 else
15614 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015615 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015616 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15617 strcpy(tp->board_part_number, "BCM57780");
15618 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15619 strcpy(tp->board_part_number, "BCM57760");
15620 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15621 strcpy(tp->board_part_number, "BCM57790");
15622 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15623 strcpy(tp->board_part_number, "BCM57788");
15624 else
15625 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015626 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015627 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15628 strcpy(tp->board_part_number, "BCM57761");
15629 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15630 strcpy(tp->board_part_number, "BCM57765");
15631 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15632 strcpy(tp->board_part_number, "BCM57781");
15633 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15634 strcpy(tp->board_part_number, "BCM57785");
15635 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15636 strcpy(tp->board_part_number, "BCM57791");
15637 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15638 strcpy(tp->board_part_number, "BCM57795");
15639 else
15640 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015641 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015642 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15643 strcpy(tp->board_part_number, "BCM57762");
15644 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15645 strcpy(tp->board_part_number, "BCM57766");
15646 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15647 strcpy(tp->board_part_number, "BCM57782");
15648 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15649 strcpy(tp->board_part_number, "BCM57786");
15650 else
15651 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015652 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015653 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015654 } else {
15655nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015656 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015658}
15659
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015660static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015661{
15662 u32 val;
15663
Matt Carlsone4f34112009-02-25 14:25:00 +000015664 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015665 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015666 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015667 val != 0)
15668 return 0;
15669
15670 return 1;
15671}
15672
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015673static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015674{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015675 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015676 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015677 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015678
15679 if (tg3_nvram_read(tp, 0xc, &offset) ||
15680 tg3_nvram_read(tp, 0x4, &start))
15681 return;
15682
15683 offset = tg3_nvram_logical_addr(tp, offset);
15684
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015685 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015686 return;
15687
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015688 if ((val & 0xfc000000) == 0x0c000000) {
15689 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015690 return;
15691
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015692 if (val == 0)
15693 newver = true;
15694 }
15695
Matt Carlson75f99362010-04-05 10:19:24 +000015696 dst_off = strlen(tp->fw_ver);
15697
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015698 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015699 if (TG3_VER_SIZE - dst_off < 16 ||
15700 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015701 return;
15702
15703 offset = offset + ver_offset - start;
15704 for (i = 0; i < 16; i += 4) {
15705 __be32 v;
15706 if (tg3_nvram_read_be32(tp, offset + i, &v))
15707 return;
15708
Matt Carlson75f99362010-04-05 10:19:24 +000015709 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015710 }
15711 } else {
15712 u32 major, minor;
15713
15714 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15715 return;
15716
15717 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15718 TG3_NVM_BCVER_MAJSFT;
15719 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015720 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15721 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015722 }
15723}
15724
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015725static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015726{
15727 u32 val, major, minor;
15728
15729 /* Use native endian representation */
15730 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15731 return;
15732
15733 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15734 TG3_NVM_HWSB_CFG1_MAJSFT;
15735 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15736 TG3_NVM_HWSB_CFG1_MINSFT;
15737
15738 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15739}
15740
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015741static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015742{
15743 u32 offset, major, minor, build;
15744
Matt Carlson75f99362010-04-05 10:19:24 +000015745 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015746
15747 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15748 return;
15749
15750 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15751 case TG3_EEPROM_SB_REVISION_0:
15752 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15753 break;
15754 case TG3_EEPROM_SB_REVISION_2:
15755 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15756 break;
15757 case TG3_EEPROM_SB_REVISION_3:
15758 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15759 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015760 case TG3_EEPROM_SB_REVISION_4:
15761 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15762 break;
15763 case TG3_EEPROM_SB_REVISION_5:
15764 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15765 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015766 case TG3_EEPROM_SB_REVISION_6:
15767 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15768 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015769 default:
15770 return;
15771 }
15772
Matt Carlsone4f34112009-02-25 14:25:00 +000015773 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015774 return;
15775
15776 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15777 TG3_EEPROM_SB_EDH_BLD_SHFT;
15778 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15779 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15780 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15781
15782 if (minor > 99 || build > 26)
15783 return;
15784
Matt Carlson75f99362010-04-05 10:19:24 +000015785 offset = strlen(tp->fw_ver);
15786 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15787 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015788
15789 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015790 offset = strlen(tp->fw_ver);
15791 if (offset < TG3_VER_SIZE - 1)
15792 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015793 }
15794}
15795
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015796static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015797{
15798 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015799 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015800
15801 for (offset = TG3_NVM_DIR_START;
15802 offset < TG3_NVM_DIR_END;
15803 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015804 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015805 return;
15806
15807 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15808 break;
15809 }
15810
15811 if (offset == TG3_NVM_DIR_END)
15812 return;
15813
Joe Perches63c3a662011-04-26 08:12:10 +000015814 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015815 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015816 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015817 return;
15818
Matt Carlsone4f34112009-02-25 14:25:00 +000015819 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015820 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015821 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015822 return;
15823
15824 offset += val - start;
15825
Matt Carlsonacd9c112009-02-25 14:26:33 +000015826 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015827
Matt Carlsonacd9c112009-02-25 14:26:33 +000015828 tp->fw_ver[vlen++] = ',';
15829 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015830
15831 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015832 __be32 v;
15833 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015834 return;
15835
Al Virob9fc7dc2007-12-17 22:59:57 -080015836 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015837
Matt Carlsonacd9c112009-02-25 14:26:33 +000015838 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15839 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015840 break;
15841 }
15842
Matt Carlsonacd9c112009-02-25 14:26:33 +000015843 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15844 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015845 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015846}
15847
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015848static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015849{
Matt Carlson7fd76442009-02-25 14:27:20 +000015850 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015851
15852 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15853 if (apedata != APE_SEG_SIG_MAGIC)
15854 return;
15855
15856 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15857 if (!(apedata & APE_FW_STATUS_READY))
15858 return;
15859
Michael Chan165f4d12012-07-16 16:23:59 +000015860 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15861 tg3_flag_set(tp, APE_HAS_NCSI);
15862}
15863
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015864static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015865{
15866 int vlen;
15867 u32 apedata;
15868 char *fwtype;
15869
Matt Carlson7fd76442009-02-25 14:27:20 +000015870 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15871
Michael Chan165f4d12012-07-16 16:23:59 +000015872 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015873 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015874 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15875 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015876 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015877 fwtype = "DASH";
15878
Matt Carlson7fd76442009-02-25 14:27:20 +000015879 vlen = strlen(tp->fw_ver);
15880
Matt Carlsonecc79642010-08-02 11:26:01 +000015881 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15882 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015883 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15884 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15885 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15886 (apedata & APE_FW_VERSION_BLDMSK));
15887}
15888
Michael Chanc86a8562013-01-06 12:51:08 +000015889static void tg3_read_otp_ver(struct tg3 *tp)
15890{
15891 u32 val, val2;
15892
Joe Perches41535772013-02-16 11:20:04 +000015893 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015894 return;
15895
15896 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15897 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15898 TG3_OTP_MAGIC0_VALID(val)) {
15899 u64 val64 = (u64) val << 32 | val2;
15900 u32 ver = 0;
15901 int i, vlen;
15902
15903 for (i = 0; i < 7; i++) {
15904 if ((val64 & 0xff) == 0)
15905 break;
15906 ver = val64 & 0xff;
15907 val64 >>= 8;
15908 }
15909 vlen = strlen(tp->fw_ver);
15910 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15911 }
15912}
15913
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015914static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015915{
15916 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015917 bool vpd_vers = false;
15918
15919 if (tp->fw_ver[0] != 0)
15920 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015921
Joe Perches63c3a662011-04-26 08:12:10 +000015922 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015923 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015924 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015925 return;
15926 }
15927
Matt Carlsonacd9c112009-02-25 14:26:33 +000015928 if (tg3_nvram_read(tp, 0, &val))
15929 return;
15930
15931 if (val == TG3_EEPROM_MAGIC)
15932 tg3_read_bc_ver(tp);
15933 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15934 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015935 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15936 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015937
Michael Chan165f4d12012-07-16 16:23:59 +000015938 if (tg3_flag(tp, ENABLE_ASF)) {
15939 if (tg3_flag(tp, ENABLE_APE)) {
15940 tg3_probe_ncsi(tp);
15941 if (!vpd_vers)
15942 tg3_read_dash_ver(tp);
15943 } else if (!vpd_vers) {
15944 tg3_read_mgmtfw_ver(tp);
15945 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015946 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015947
15948 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015949}
15950
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015951static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15952{
Joe Perches63c3a662011-04-26 08:12:10 +000015953 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015954 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015955 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015956 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015957 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015958 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015959}
15960
Benoit Taine9baa3c32014-08-08 15:56:03 +020015961static const struct pci_device_id tg3_write_reorder_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080015962 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15963 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15964 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15965 { },
15966};
15967
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015968static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015969{
15970 struct pci_dev *peer;
15971 unsigned int func, devnr = tp->pdev->devfn & ~7;
15972
15973 for (func = 0; func < 8; func++) {
15974 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15975 if (peer && peer != tp->pdev)
15976 break;
15977 pci_dev_put(peer);
15978 }
15979 /* 5704 can be configured in single-port mode, set peer to
15980 * tp->pdev in that case.
15981 */
15982 if (!peer) {
15983 peer = tp->pdev;
15984 return peer;
15985 }
15986
15987 /*
15988 * We don't need to keep the refcount elevated; there's no way
15989 * to remove one half of this device without removing the other
15990 */
15991 pci_dev_put(peer);
15992
15993 return peer;
15994}
15995
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015996static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015997{
15998 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015999 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000016000 u32 reg;
16001
16002 /* All devices that use the alternate
16003 * ASIC REV location have a CPMU.
16004 */
16005 tg3_flag_set(tp, CPMU_PRESENT);
16006
16007 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016008 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016009 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16010 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016011 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016012 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
16013 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016014 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16015 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016016 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
16017 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000016018 reg = TG3PCI_GEN2_PRODID_ASICREV;
16019 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
16020 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
16021 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
16022 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
16023 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
16024 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
16025 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
16026 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
16027 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
16028 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
16029 reg = TG3PCI_GEN15_PRODID_ASICREV;
16030 else
16031 reg = TG3PCI_PRODID_ASICREV;
16032
16033 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
16034 }
16035
16036 /* Wrong chip ID in 5752 A0. This code can be removed later
16037 * as A0 is not in production.
16038 */
Joe Perches41535772013-02-16 11:20:04 +000016039 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000016040 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
16041
Joe Perches41535772013-02-16 11:20:04 +000016042 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000016043 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
16044
Joe Perches41535772013-02-16 11:20:04 +000016045 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16046 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16047 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000016048 tg3_flag_set(tp, 5717_PLUS);
16049
Joe Perches41535772013-02-16 11:20:04 +000016050 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
16051 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000016052 tg3_flag_set(tp, 57765_CLASS);
16053
Michael Chanc65a17f2013-01-06 12:51:07 +000016054 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016055 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000016056 tg3_flag_set(tp, 57765_PLUS);
16057
16058 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000016059 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16060 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16061 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16062 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16063 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16064 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016065 tg3_flag(tp, 57765_PLUS))
16066 tg3_flag_set(tp, 5755_PLUS);
16067
Joe Perches41535772013-02-16 11:20:04 +000016068 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
16069 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000016070 tg3_flag_set(tp, 5780_CLASS);
16071
Joe Perches41535772013-02-16 11:20:04 +000016072 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16073 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16074 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016075 tg3_flag(tp, 5755_PLUS) ||
16076 tg3_flag(tp, 5780_CLASS))
16077 tg3_flag_set(tp, 5750_PLUS);
16078
Joe Perches41535772013-02-16 11:20:04 +000016079 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016080 tg3_flag(tp, 5750_PLUS))
16081 tg3_flag_set(tp, 5705_PLUS);
16082}
16083
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016084static bool tg3_10_100_only_device(struct tg3 *tp,
16085 const struct pci_device_id *ent)
16086{
16087 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
16088
Joe Perches41535772013-02-16 11:20:04 +000016089 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
16090 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016091 (tp->phy_flags & TG3_PHYFLG_IS_FET))
16092 return true;
16093
16094 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000016095 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016096 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
16097 return true;
16098 } else {
16099 return true;
16100 }
16101 }
16102
16103 return false;
16104}
16105
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000016106static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016107{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016108 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016109 u32 pci_state_reg, grc_misc_cfg;
16110 u32 val;
16111 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016112 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016113
Linus Torvalds1da177e2005-04-16 15:20:36 -070016114 /* Force memory write invalidate off. If we leave it on,
16115 * then on 5700_BX chips we have to enable a workaround.
16116 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
16117 * to match the cacheline size. The Broadcom driver have this
16118 * workaround but turns MWI off all the times so never uses
16119 * it. This seems to suggest that the workaround is insufficient.
16120 */
16121 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16122 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
16123 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16124
Matt Carlson16821282011-07-13 09:27:28 +000016125 /* Important! -- Make sure register accesses are byteswapped
16126 * correctly. Also, for those chips that require it, make
16127 * sure that indirect register accesses are enabled before
16128 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016129 */
16130 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16131 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000016132 tp->misc_host_ctrl |= (misc_ctrl_reg &
16133 MISC_HOST_CTRL_CHIPREV);
16134 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16135 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016136
Matt Carlson42b123b2012-02-13 15:20:13 +000016137 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070016138
Michael Chan68929142005-08-09 20:17:14 -070016139 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
16140 * we need to disable memory and use config. cycles
16141 * only to access all registers. The 5702/03 chips
16142 * can mistakenly decode the special cycles from the
16143 * ICH chipsets as memory write cycles, causing corruption
16144 * of register and memory space. Only certain ICH bridges
16145 * will drive special cycles with non-zero data during the
16146 * address phase which can fall within the 5703's address
16147 * range. This is not an ICH bug as the PCI spec allows
16148 * non-zero address during special cycles. However, only
16149 * these ICH bridges are known to drive non-zero addresses
16150 * during special cycles.
16151 *
16152 * Since special cycles do not cross PCI bridges, we only
16153 * enable this workaround if the 5703 is on the secondary
16154 * bus of these ICH bridges.
16155 */
Joe Perches41535772013-02-16 11:20:04 +000016156 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
16157 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070016158 static struct tg3_dev_id {
16159 u32 vendor;
16160 u32 device;
16161 u32 rev;
16162 } ich_chipsets[] = {
16163 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
16164 PCI_ANY_ID },
16165 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
16166 PCI_ANY_ID },
16167 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
16168 0xa },
16169 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
16170 PCI_ANY_ID },
16171 { },
16172 };
16173 struct tg3_dev_id *pci_id = &ich_chipsets[0];
16174 struct pci_dev *bridge = NULL;
16175
16176 while (pci_id->vendor != 0) {
16177 bridge = pci_get_device(pci_id->vendor, pci_id->device,
16178 bridge);
16179 if (!bridge) {
16180 pci_id++;
16181 continue;
16182 }
16183 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070016184 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070016185 continue;
16186 }
16187 if (bridge->subordinate &&
16188 (bridge->subordinate->number ==
16189 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016190 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070016191 pci_dev_put(bridge);
16192 break;
16193 }
16194 }
16195 }
16196
Joe Perches41535772013-02-16 11:20:04 +000016197 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070016198 static struct tg3_dev_id {
16199 u32 vendor;
16200 u32 device;
16201 } bridge_chipsets[] = {
16202 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
16203 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
16204 { },
16205 };
16206 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
16207 struct pci_dev *bridge = NULL;
16208
16209 while (pci_id->vendor != 0) {
16210 bridge = pci_get_device(pci_id->vendor,
16211 pci_id->device,
16212 bridge);
16213 if (!bridge) {
16214 pci_id++;
16215 continue;
16216 }
16217 if (bridge->subordinate &&
16218 (bridge->subordinate->number <=
16219 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016220 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070016221 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016222 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070016223 pci_dev_put(bridge);
16224 break;
16225 }
16226 }
16227 }
16228
Michael Chan4a29cc22006-03-19 13:21:12 -080016229 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16230 * DMA addresses > 40-bit. This bridge may have other additional
16231 * 57xx devices behind it in some 4-port NIC designs for example.
16232 * Any tg3 device found behind the bridge will also need the 40-bit
16233 * DMA workaround.
16234 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016235 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016236 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016237 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016238 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016239 struct pci_dev *bridge = NULL;
16240
16241 do {
16242 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16243 PCI_DEVICE_ID_SERVERWORKS_EPB,
16244 bridge);
16245 if (bridge && bridge->subordinate &&
16246 (bridge->subordinate->number <=
16247 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016248 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016249 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016250 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016251 pci_dev_put(bridge);
16252 break;
16253 }
16254 } while (bridge);
16255 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016256
Joe Perches41535772013-02-16 11:20:04 +000016257 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16258 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016259 tp->pdev_peer = tg3_find_peer(tp);
16260
Matt Carlson507399f2009-11-13 13:03:37 +000016261 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016262 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016263 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016264 else if (tg3_flag(tp, 57765_PLUS))
16265 tg3_flag_set(tp, HW_TSO_3);
16266 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016267 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016268 tg3_flag_set(tp, HW_TSO_2);
16269 else if (tg3_flag(tp, 5750_PLUS)) {
16270 tg3_flag_set(tp, HW_TSO_1);
16271 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016272 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16273 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016274 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016275 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16276 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16277 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016278 tg3_flag_set(tp, FW_TSO);
16279 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016280 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016281 tp->fw_needed = FIRMWARE_TG3TSO5;
16282 else
16283 tp->fw_needed = FIRMWARE_TG3TSO;
16284 }
16285
Matt Carlsondabc5c62011-05-19 12:12:52 +000016286 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016287 if (tg3_flag(tp, HW_TSO_1) ||
16288 tg3_flag(tp, HW_TSO_2) ||
16289 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016290 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016291 /* For firmware TSO, assume ASF is disabled.
16292 * We'll disable TSO later if we discover ASF
16293 * is enabled in tg3_get_eeprom_hw_cfg().
16294 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016295 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016296 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016297 tg3_flag_clear(tp, TSO_CAPABLE);
16298 tg3_flag_clear(tp, TSO_BUG);
16299 tp->fw_needed = NULL;
16300 }
16301
Joe Perches41535772013-02-16 11:20:04 +000016302 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016303 tp->fw_needed = FIRMWARE_TG3;
16304
Nithin Sujirc4dab502013-03-06 17:02:34 +000016305 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16306 tp->fw_needed = FIRMWARE_TG357766;
16307
Matt Carlson507399f2009-11-13 13:03:37 +000016308 tp->irq_max = 1;
16309
Joe Perches63c3a662011-04-26 08:12:10 +000016310 if (tg3_flag(tp, 5750_PLUS)) {
16311 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016312 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16313 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16314 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16315 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016316 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016317 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016318
Joe Perches63c3a662011-04-26 08:12:10 +000016319 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016320 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016321 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016323
Joe Perches63c3a662011-04-26 08:12:10 +000016324 if (tg3_flag(tp, 57765_PLUS)) {
16325 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016326 tp->irq_max = TG3_IRQ_MAX_VECS;
16327 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016328 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016329
Michael Chan91024262012-09-28 07:12:38 +000016330 tp->txq_max = 1;
16331 tp->rxq_max = 1;
16332 if (tp->irq_max > 1) {
16333 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16334 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16335
Joe Perches41535772013-02-16 11:20:04 +000016336 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16337 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016338 tp->txq_max = tp->irq_max - 1;
16339 }
16340
Matt Carlsonb7abee62012-06-07 12:56:54 +000016341 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016342 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016343 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016344
Joe Perches41535772013-02-16 11:20:04 +000016345 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016346 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016347
Joe Perches41535772013-02-16 11:20:04 +000016348 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16349 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16350 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16351 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016352 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016353
Joe Perches63c3a662011-04-26 08:12:10 +000016354 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016355 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016356 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016357
Joe Perches63c3a662011-04-26 08:12:10 +000016358 if (!tg3_flag(tp, 5705_PLUS) ||
16359 tg3_flag(tp, 5780_CLASS) ||
16360 tg3_flag(tp, USE_JUMBO_BDFLAG))
16361 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016362
Matt Carlson52f44902008-11-21 17:17:04 -080016363 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16364 &pci_state_reg);
16365
Jon Mason708ebb3a2011-06-27 12:56:50 +000016366 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016367 u16 lnkctl;
16368
Joe Perches63c3a662011-04-26 08:12:10 +000016369 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016370
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016371 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016372 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016373 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016374 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016375 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016376 }
Joe Perches41535772013-02-16 11:20:04 +000016377 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16378 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16379 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16380 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016381 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016382 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016383 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016384 }
Joe Perches41535772013-02-16 11:20:04 +000016385 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016386 /* BCM5785 devices are effectively PCIe devices, and should
16387 * follow PCIe codepaths, but do not have a PCIe capabilities
16388 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016389 */
Joe Perches63c3a662011-04-26 08:12:10 +000016390 tg3_flag_set(tp, PCI_EXPRESS);
16391 } else if (!tg3_flag(tp, 5705_PLUS) ||
16392 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016393 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16394 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016395 dev_err(&tp->pdev->dev,
16396 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016397 return -EIO;
16398 }
16399
16400 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016401 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016403
Michael Chan399de502005-10-03 14:02:39 -070016404 /* If we have an AMD 762 or VIA K8T800 chipset, write
16405 * reordering to the mailbox registers done by the host
16406 * controller can cause major troubles. We read back from
16407 * every mailbox register write to force the writes to be
16408 * posted to the chip in order.
16409 */
Matt Carlson41434702011-03-09 16:58:22 +000016410 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016411 !tg3_flag(tp, PCI_EXPRESS))
16412 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016413
Matt Carlson69fc4052008-12-21 20:19:57 -080016414 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16415 &tp->pci_cacheline_sz);
16416 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16417 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016418 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016419 tp->pci_lat_timer < 64) {
16420 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016421 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16422 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016423 }
16424
Matt Carlson16821282011-07-13 09:27:28 +000016425 /* Important! -- It is critical that the PCI-X hw workaround
16426 * situation is decided before the first MMIO register access.
16427 */
Joe Perches41535772013-02-16 11:20:04 +000016428 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016429 /* 5700 BX chips need to have their TX producer index
16430 * mailboxes written twice to workaround a bug.
16431 */
Joe Perches63c3a662011-04-26 08:12:10 +000016432 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016433
Matt Carlson52f44902008-11-21 17:17:04 -080016434 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016435 *
16436 * The workaround is to use indirect register accesses
16437 * for all chip writes not to mailbox registers.
16438 */
Joe Perches63c3a662011-04-26 08:12:10 +000016439 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016440 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016441
Joe Perches63c3a662011-04-26 08:12:10 +000016442 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016443
16444 /* The chip can have it's power management PCI config
16445 * space registers clobbered due to this bug.
16446 * So explicitly force the chip into D0 here.
16447 */
Matt Carlson9974a352007-10-07 23:27:28 -070016448 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016449 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016450 &pm_reg);
16451 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16452 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016453 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016454 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016455 pm_reg);
16456
16457 /* Also, force SERR#/PERR# in PCI command. */
16458 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16459 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16460 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16461 }
16462 }
16463
Linus Torvalds1da177e2005-04-16 15:20:36 -070016464 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016465 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016466 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016467 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016468
16469 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016470 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016471 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16472 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16473 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16474 }
16475
Michael Chan1ee582d2005-08-09 20:16:46 -070016476 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016477 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016478 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016479 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016480 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016481 tp->write32_tx_mbox = tg3_write32;
16482 tp->write32_rx_mbox = tg3_write32;
16483
16484 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016485 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016486 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016487 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016488 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016489 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016490 /*
16491 * Back to back register writes can cause problems on these
16492 * chips, the workaround is to read back all reg writes
16493 * except those to mailbox regs.
16494 *
16495 * See tg3_write_indirect_reg32().
16496 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016497 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016498 }
16499
Joe Perches63c3a662011-04-26 08:12:10 +000016500 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016501 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016502 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016503 tp->write32_rx_mbox = tg3_write_flush_reg32;
16504 }
Michael Chan20094932005-08-09 20:16:32 -070016505
Joe Perches63c3a662011-04-26 08:12:10 +000016506 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016507 tp->read32 = tg3_read_indirect_reg32;
16508 tp->write32 = tg3_write_indirect_reg32;
16509 tp->read32_mbox = tg3_read_indirect_mbox;
16510 tp->write32_mbox = tg3_write_indirect_mbox;
16511 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16512 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16513
16514 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016515 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016516
16517 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16518 pci_cmd &= ~PCI_COMMAND_MEMORY;
16519 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16520 }
Joe Perches41535772013-02-16 11:20:04 +000016521 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016522 tp->read32_mbox = tg3_read32_mbox_5906;
16523 tp->write32_mbox = tg3_write32_mbox_5906;
16524 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16525 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16526 }
Michael Chan68929142005-08-09 20:17:14 -070016527
Michael Chanbbadf502006-04-06 21:46:34 -070016528 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016529 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016530 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16531 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016532 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016533
Matt Carlson16821282011-07-13 09:27:28 +000016534 /* The memory arbiter has to be enabled in order for SRAM accesses
16535 * to succeed. Normally on powerup the tg3 chip firmware will make
16536 * sure it is enabled, but other entities such as system netboot
16537 * code might disable it.
16538 */
16539 val = tr32(MEMARB_MODE);
16540 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16541
Matt Carlson9dc5e342011-11-04 09:15:02 +000016542 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016543 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016544 tg3_flag(tp, 5780_CLASS)) {
16545 if (tg3_flag(tp, PCIX_MODE)) {
16546 pci_read_config_dword(tp->pdev,
16547 tp->pcix_cap + PCI_X_STATUS,
16548 &val);
16549 tp->pci_fn = val & 0x7;
16550 }
Joe Perches41535772013-02-16 11:20:04 +000016551 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16552 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16553 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016554 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016555 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16556 val = tr32(TG3_CPMU_STATUS);
16557
Joe Perches41535772013-02-16 11:20:04 +000016558 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016559 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16560 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016561 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16562 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016563 }
16564
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016565 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16566 tp->write32_tx_mbox = tg3_write_flush_reg32;
16567 tp->write32_rx_mbox = tg3_write_flush_reg32;
16568 }
16569
Michael Chan7d0c41e2005-04-21 17:06:20 -070016570 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016571 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016572 * determined before calling tg3_set_power_state() so that
16573 * we know whether or not to switch out of Vaux power.
16574 * When the flag is set, it means that GPIO1 is used for eeprom
16575 * write protect and also implies that it is a LOM where GPIOs
16576 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016577 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016578 tg3_get_eeprom_hw_cfg(tp);
16579
Matt Carlson1caf13e2013-03-06 17:02:29 +000016580 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016581 tg3_flag_clear(tp, TSO_CAPABLE);
16582 tg3_flag_clear(tp, TSO_BUG);
16583 tp->fw_needed = NULL;
16584 }
16585
Joe Perches63c3a662011-04-26 08:12:10 +000016586 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016587 /* Allow reads and writes to the
16588 * APE register and memory space.
16589 */
16590 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016591 PCISTATE_ALLOW_APE_SHMEM_WR |
16592 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016593 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16594 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016595
16596 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016597 }
16598
Matt Carlson16821282011-07-13 09:27:28 +000016599 /* Set up tp->grc_local_ctrl before calling
16600 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16601 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016602 * It is also used as eeprom write protect on LOMs.
16603 */
16604 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016605 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016606 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016607 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16608 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016609 /* Unused GPIO3 must be driven as output on 5752 because there
16610 * are no pull-up resistors on unused GPIO pins.
16611 */
Joe Perches41535772013-02-16 11:20:04 +000016612 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016613 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016614
Joe Perches41535772013-02-16 11:20:04 +000016615 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16616 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016617 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016618 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16619
Matt Carlson8d519ab2009-04-20 06:58:01 +000016620 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16621 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016622 /* Turn off the debug UART. */
16623 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016624 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016625 /* Keep VMain power. */
16626 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16627 GRC_LCLCTRL_GPIO_OUTPUT0;
16628 }
16629
Joe Perches41535772013-02-16 11:20:04 +000016630 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016631 tp->grc_local_ctrl |=
16632 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16633
Matt Carlson16821282011-07-13 09:27:28 +000016634 /* Switch out of Vaux if it is a NIC */
16635 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016636
Linus Torvalds1da177e2005-04-16 15:20:36 -070016637 /* Derive initial jumbo mode from MTU assigned in
16638 * ether_setup() via the alloc_etherdev() call
16639 */
Joe Perches63c3a662011-04-26 08:12:10 +000016640 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16641 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016642
16643 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016644 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16645 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16646 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16647 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016648 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016649 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016650 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016651 }
16652
Joe Perches41535772013-02-16 11:20:04 +000016653 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016654 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016655
Linus Torvalds1da177e2005-04-16 15:20:36 -070016656 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016657 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16658 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16659 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16660 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016661 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16662 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16663 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016664
Joe Perches41535772013-02-16 11:20:04 +000016665 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16666 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016667 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016668 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016669 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016670
Joe Perches63c3a662011-04-26 08:12:10 +000016671 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016672 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016673 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16674 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016675 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016676 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16677 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16678 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16679 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016680 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16681 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016682 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016683 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016684 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016685 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016686 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016688
Joe Perches41535772013-02-16 11:20:04 +000016689 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16690 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016691 tp->phy_otp = tg3_read_otp_phycfg(tp);
16692 if (tp->phy_otp == 0)
16693 tp->phy_otp = TG3_OTP_DEFAULT;
16694 }
16695
Joe Perches63c3a662011-04-26 08:12:10 +000016696 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016697 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16698 else
16699 tp->mi_mode = MAC_MI_MODE_BASE;
16700
Linus Torvalds1da177e2005-04-16 15:20:36 -070016701 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016702 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16703 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016704 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16705
Matt Carlson4d958472011-04-20 07:57:35 +000016706 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016707 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080016708 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000016709 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16710 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016711 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16712 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16713 }
16714
Joe Perches41535772013-02-16 11:20:04 +000016715 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16716 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016717 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016718
Matt Carlson158d7ab2008-05-29 01:37:54 -070016719 err = tg3_mdio_init(tp);
16720 if (err)
16721 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016722
16723 /* Initialize data/descriptor byte/word swapping. */
16724 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016725 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16726 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016727 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16728 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16729 GRC_MODE_B2HRX_ENABLE |
16730 GRC_MODE_HTX2B_ENABLE |
16731 GRC_MODE_HOST_STACKUP);
16732 else
16733 val &= GRC_MODE_HOST_STACKUP;
16734
Linus Torvalds1da177e2005-04-16 15:20:36 -070016735 tw32(GRC_MODE, val | tp->grc_mode);
16736
16737 tg3_switch_clocks(tp);
16738
16739 /* Clear this out for sanity. */
16740 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16741
Nat Gurumoorthy388d3332013-12-09 10:43:21 -080016742 /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
16743 tw32(TG3PCI_REG_BASE_ADDR, 0);
16744
Linus Torvalds1da177e2005-04-16 15:20:36 -070016745 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16746 &pci_state_reg);
16747 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016748 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016749 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16750 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16751 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16752 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016753 void __iomem *sram_base;
16754
16755 /* Write some dummy words into the SRAM status block
16756 * area, see if it reads back correctly. If the return
16757 * value is bad, force enable the PCIX workaround.
16758 */
16759 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16760
16761 writel(0x00000000, sram_base);
16762 writel(0x00000000, sram_base + 4);
16763 writel(0xffffffff, sram_base + 4);
16764 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016765 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016766 }
16767 }
16768
16769 udelay(50);
16770 tg3_nvram_init(tp);
16771
Nithin Sujirc4dab502013-03-06 17:02:34 +000016772 /* If the device has an NVRAM, no need to load patch firmware */
16773 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16774 !tg3_flag(tp, NO_NVRAM))
16775 tp->fw_needed = NULL;
16776
Linus Torvalds1da177e2005-04-16 15:20:36 -070016777 grc_misc_cfg = tr32(GRC_MISC_CFG);
16778 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16779
Joe Perches41535772013-02-16 11:20:04 +000016780 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016781 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16782 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016783 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016784
Joe Perches63c3a662011-04-26 08:12:10 +000016785 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016786 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016787 tg3_flag_set(tp, TAGGED_STATUS);
16788 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016789 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16790 HOSTCC_MODE_CLRTICK_TXBD);
16791
16792 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16793 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16794 tp->misc_host_ctrl);
16795 }
16796
Matt Carlson3bda1252008-08-15 14:08:22 -070016797 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016798 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016799 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016800 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016801 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016802
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016803 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016804 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016805
16806 err = tg3_phy_probe(tp);
16807 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016808 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016809 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016810 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016811 }
16812
Matt Carlson184b8902010-04-05 10:19:25 +000016813 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016814 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016815
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016816 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16817 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016818 } else {
Joe Perches41535772013-02-16 11:20:04 +000016819 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016820 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016821 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016822 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016823 }
16824
16825 /* 5700 {AX,BX} chips have a broken status block link
16826 * change bit implementation, so we must use the
16827 * status register in those cases.
16828 */
Joe Perches41535772013-02-16 11:20:04 +000016829 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016830 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016831 else
Joe Perches63c3a662011-04-26 08:12:10 +000016832 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016833
16834 /* The led_ctrl is set during tg3_phy_probe, here we might
16835 * have to force the link status polling mechanism based
16836 * upon subsystem IDs.
16837 */
16838 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016839 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016840 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16841 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016842 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016843 }
16844
16845 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016846 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016847 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016848 else
Joe Perches63c3a662011-04-26 08:12:10 +000016849 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016850
Nithin Sujir1743b832014-01-03 10:09:14 -080016851 if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
16852 tg3_flag_set(tp, POLL_CPMU_LINK);
16853
Eric Dumazet9205fd92011-11-18 06:47:01 +000016854 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016855 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016856 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016857 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016858 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016859#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016860 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016861#endif
16862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016863
Matt Carlson2c49a442010-09-30 10:34:35 +000016864 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16865 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016866 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16867
Matt Carlson2c49a442010-09-30 10:34:35 +000016868 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016869
16870 /* Increment the rx prod index on the rx std ring by at most
16871 * 8 for these chips to workaround hw errata.
16872 */
Joe Perches41535772013-02-16 11:20:04 +000016873 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16874 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16875 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016876 tp->rx_std_max_post = 8;
16877
Joe Perches63c3a662011-04-26 08:12:10 +000016878 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016879 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16880 PCIE_PWR_MGMT_L1_THRESH_MSK;
16881
Linus Torvalds1da177e2005-04-16 15:20:36 -070016882 return err;
16883}
16884
David S. Miller49b6e95f2007-03-29 01:38:42 -070016885#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016886static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016887{
16888 struct net_device *dev = tp->dev;
16889 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016890 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016891 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016892 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016893
David S. Miller49b6e95f2007-03-29 01:38:42 -070016894 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016895 if (addr && len == ETH_ALEN) {
16896 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016898 }
16899 return -ENODEV;
16900}
16901
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016902static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016903{
16904 struct net_device *dev = tp->dev;
16905
Joe Perchesd458cdf2013-10-01 19:04:40 -070016906 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016907 return 0;
16908}
16909#endif
16910
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016911static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016912{
16913 struct net_device *dev = tp->dev;
16914 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016915 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016916 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016917
David S. Miller49b6e95f2007-03-29 01:38:42 -070016918#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016919 if (!tg3_get_macaddr_sparc(tp))
16920 return 0;
16921#endif
16922
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016923 if (tg3_flag(tp, IS_SSB_CORE)) {
16924 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16925 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16926 return 0;
16927 }
16928
Linus Torvalds1da177e2005-04-16 15:20:36 -070016929 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016930 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016931 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016932 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16933 mac_offset = 0xcc;
16934 if (tg3_nvram_lock(tp))
16935 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16936 else
16937 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016938 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016939 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016940 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016941 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016942 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016943 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016944 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016945
16946 /* First try to get it from MAC address mailbox. */
16947 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16948 if ((hi >> 16) == 0x484b) {
16949 dev->dev_addr[0] = (hi >> 8) & 0xff;
16950 dev->dev_addr[1] = (hi >> 0) & 0xff;
16951
16952 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16953 dev->dev_addr[2] = (lo >> 24) & 0xff;
16954 dev->dev_addr[3] = (lo >> 16) & 0xff;
16955 dev->dev_addr[4] = (lo >> 8) & 0xff;
16956 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016957
Michael Chan008652b2006-03-27 23:14:53 -080016958 /* Some old bootcode may report a 0 MAC address in SRAM */
16959 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16960 }
16961 if (!addr_ok) {
16962 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016963 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016964 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016965 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016966 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16967 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016968 }
16969 /* Finally just fetch it out of the MAC control regs. */
16970 else {
16971 hi = tr32(MAC_ADDR_0_HIGH);
16972 lo = tr32(MAC_ADDR_0_LOW);
16973
16974 dev->dev_addr[5] = lo & 0xff;
16975 dev->dev_addr[4] = (lo >> 8) & 0xff;
16976 dev->dev_addr[3] = (lo >> 16) & 0xff;
16977 dev->dev_addr[2] = (lo >> 24) & 0xff;
16978 dev->dev_addr[1] = hi & 0xff;
16979 dev->dev_addr[0] = (hi >> 8) & 0xff;
16980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016981 }
16982
16983 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016984#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016985 if (!tg3_get_default_macaddr_sparc(tp))
16986 return 0;
16987#endif
16988 return -EINVAL;
16989 }
16990 return 0;
16991}
16992
David S. Miller59e6b432005-05-18 22:50:10 -070016993#define BOUNDARY_SINGLE_CACHELINE 1
16994#define BOUNDARY_MULTI_CACHELINE 2
16995
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016996static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016997{
16998 int cacheline_size;
16999 u8 byte;
17000 int goal;
17001
17002 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
17003 if (byte == 0)
17004 cacheline_size = 1024;
17005 else
17006 cacheline_size = (int) byte * 4;
17007
17008 /* On 5703 and later chips, the boundary bits have no
17009 * effect.
17010 */
Joe Perches41535772013-02-16 11:20:04 +000017011 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17012 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017013 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070017014 goto out;
17015
17016#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
17017 goal = BOUNDARY_MULTI_CACHELINE;
17018#else
17019#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
17020 goal = BOUNDARY_SINGLE_CACHELINE;
17021#else
17022 goal = 0;
17023#endif
17024#endif
17025
Joe Perches63c3a662011-04-26 08:12:10 +000017026 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017027 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
17028 goto out;
17029 }
17030
David S. Miller59e6b432005-05-18 22:50:10 -070017031 if (!goal)
17032 goto out;
17033
17034 /* PCI controllers on most RISC systems tend to disconnect
17035 * when a device tries to burst across a cache-line boundary.
17036 * Therefore, letting tg3 do so just wastes PCI bandwidth.
17037 *
17038 * Unfortunately, for PCI-E there are only limited
17039 * write-side controls for this, and thus for reads
17040 * we will still get the disconnects. We'll also waste
17041 * these PCI cycles for both read and write for chips
17042 * other than 5700 and 5701 which do not implement the
17043 * boundary bits.
17044 */
Joe Perches63c3a662011-04-26 08:12:10 +000017045 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017046 switch (cacheline_size) {
17047 case 16:
17048 case 32:
17049 case 64:
17050 case 128:
17051 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17052 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
17053 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
17054 } else {
17055 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17056 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17057 }
17058 break;
17059
17060 case 256:
17061 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
17062 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
17063 break;
17064
17065 default:
17066 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17067 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17068 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017069 }
Joe Perches63c3a662011-04-26 08:12:10 +000017070 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017071 switch (cacheline_size) {
17072 case 16:
17073 case 32:
17074 case 64:
17075 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17076 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17077 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
17078 break;
17079 }
17080 /* fallthrough */
17081 case 128:
17082 default:
17083 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17084 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
17085 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017086 }
David S. Miller59e6b432005-05-18 22:50:10 -070017087 } else {
17088 switch (cacheline_size) {
17089 case 16:
17090 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17091 val |= (DMA_RWCTRL_READ_BNDRY_16 |
17092 DMA_RWCTRL_WRITE_BNDRY_16);
17093 break;
17094 }
17095 /* fallthrough */
17096 case 32:
17097 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17098 val |= (DMA_RWCTRL_READ_BNDRY_32 |
17099 DMA_RWCTRL_WRITE_BNDRY_32);
17100 break;
17101 }
17102 /* fallthrough */
17103 case 64:
17104 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17105 val |= (DMA_RWCTRL_READ_BNDRY_64 |
17106 DMA_RWCTRL_WRITE_BNDRY_64);
17107 break;
17108 }
17109 /* fallthrough */
17110 case 128:
17111 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17112 val |= (DMA_RWCTRL_READ_BNDRY_128 |
17113 DMA_RWCTRL_WRITE_BNDRY_128);
17114 break;
17115 }
17116 /* fallthrough */
17117 case 256:
17118 val |= (DMA_RWCTRL_READ_BNDRY_256 |
17119 DMA_RWCTRL_WRITE_BNDRY_256);
17120 break;
17121 case 512:
17122 val |= (DMA_RWCTRL_READ_BNDRY_512 |
17123 DMA_RWCTRL_WRITE_BNDRY_512);
17124 break;
17125 case 1024:
17126 default:
17127 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
17128 DMA_RWCTRL_WRITE_BNDRY_1024);
17129 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017130 }
David S. Miller59e6b432005-05-18 22:50:10 -070017131 }
17132
17133out:
17134 return val;
17135}
17136
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017137static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000017138 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017139{
17140 struct tg3_internal_buffer_desc test_desc;
17141 u32 sram_dma_descs;
17142 int i, ret;
17143
17144 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
17145
17146 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
17147 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
17148 tw32(RDMAC_STATUS, 0);
17149 tw32(WDMAC_STATUS, 0);
17150
17151 tw32(BUFMGR_MODE, 0);
17152 tw32(FTQ_RESET, 0);
17153
17154 test_desc.addr_hi = ((u64) buf_dma) >> 32;
17155 test_desc.addr_lo = buf_dma & 0xffffffff;
17156 test_desc.nic_mbuf = 0x00002100;
17157 test_desc.len = size;
17158
17159 /*
17160 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
17161 * the *second* time the tg3 driver was getting loaded after an
17162 * initial scan.
17163 *
17164 * Broadcom tells me:
17165 * ...the DMA engine is connected to the GRC block and a DMA
17166 * reset may affect the GRC block in some unpredictable way...
17167 * The behavior of resets to individual blocks has not been tested.
17168 *
17169 * Broadcom noted the GRC reset will also reset all sub-components.
17170 */
17171 if (to_device) {
17172 test_desc.cqid_sqid = (13 << 8) | 2;
17173
17174 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
17175 udelay(40);
17176 } else {
17177 test_desc.cqid_sqid = (16 << 8) | 7;
17178
17179 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
17180 udelay(40);
17181 }
17182 test_desc.flags = 0x00000005;
17183
17184 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
17185 u32 val;
17186
17187 val = *(((u32 *)&test_desc) + i);
17188 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
17189 sram_dma_descs + (i * sizeof(u32)));
17190 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
17191 }
17192 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
17193
Matt Carlson859a588792010-04-05 10:19:28 +000017194 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017195 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000017196 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070017197 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017198
17199 ret = -ENODEV;
17200 for (i = 0; i < 40; i++) {
17201 u32 val;
17202
17203 if (to_device)
17204 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
17205 else
17206 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
17207 if ((val & 0xffff) == sram_dma_descs) {
17208 ret = 0;
17209 break;
17210 }
17211
17212 udelay(100);
17213 }
17214
17215 return ret;
17216}
17217
David S. Millerded73402005-05-23 13:59:47 -070017218#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017219
Benoit Taine9baa3c32014-08-08 15:56:03 +020017220static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080017221 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
17222 { },
17223};
17224
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017225static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017226{
17227 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070017228 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017229 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017230
Matt Carlson4bae65c2010-11-24 08:31:52 +000017231 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
17232 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017233 if (!buf) {
17234 ret = -ENOMEM;
17235 goto out_nofree;
17236 }
17237
17238 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17239 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17240
David S. Miller59e6b432005-05-18 22:50:10 -070017241 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017242
Joe Perches63c3a662011-04-26 08:12:10 +000017243 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017244 goto out;
17245
Joe Perches63c3a662011-04-26 08:12:10 +000017246 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017247 /* DMA read watermark not used on PCIE */
17248 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017249 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017250 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17251 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017252 tp->dma_rwctrl |= 0x003f0000;
17253 else
17254 tp->dma_rwctrl |= 0x003f000f;
17255 } else {
Joe Perches41535772013-02-16 11:20:04 +000017256 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17257 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017258 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017259 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017260
Michael Chan4a29cc22006-03-19 13:21:12 -080017261 /* If the 5704 is behind the EPB bridge, we can
17262 * do the less restrictive ONE_DMA workaround for
17263 * better performance.
17264 */
Joe Perches63c3a662011-04-26 08:12:10 +000017265 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017266 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017267 tp->dma_rwctrl |= 0x8000;
17268 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017269 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17270
Joe Perches41535772013-02-16 11:20:04 +000017271 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017272 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017273 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017274 tp->dma_rwctrl |=
17275 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17276 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17277 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017278 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017279 /* 5780 always in PCIX mode */
17280 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017281 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017282 /* 5714 always in PCIX mode */
17283 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017284 } else {
17285 tp->dma_rwctrl |= 0x001b000f;
17286 }
17287 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017288 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17289 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017290
Joe Perches41535772013-02-16 11:20:04 +000017291 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17292 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017293 tp->dma_rwctrl &= 0xfffffff0;
17294
Joe Perches41535772013-02-16 11:20:04 +000017295 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17296 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017297 /* Remove this if it causes problems for some boards. */
17298 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17299
17300 /* On 5700/5701 chips, we need to set this bit.
17301 * Otherwise the chip will issue cacheline transactions
17302 * to streamable DMA memory with not all the byte
17303 * enables turned on. This is an error on several
17304 * RISC PCI controllers, in particular sparc64.
17305 *
17306 * On 5703/5704 chips, this bit has been reassigned
17307 * a different meaning. In particular, it is used
17308 * on those chips to enable a PCI-X workaround.
17309 */
17310 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17311 }
17312
17313 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17314
Linus Torvalds1da177e2005-04-16 15:20:36 -070017315
Joe Perches41535772013-02-16 11:20:04 +000017316 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17317 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017318 goto out;
17319
David S. Miller59e6b432005-05-18 22:50:10 -070017320 /* It is best to perform DMA test with maximum write burst size
17321 * to expose the 5700/5701 write DMA bug.
17322 */
17323 saved_dma_rwctrl = tp->dma_rwctrl;
17324 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17325 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17326
Linus Torvalds1da177e2005-04-16 15:20:36 -070017327 while (1) {
17328 u32 *p = buf, i;
17329
17330 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17331 p[i] = i;
17332
17333 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017334 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017335 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017336 dev_err(&tp->pdev->dev,
17337 "%s: Buffer write failed. err = %d\n",
17338 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017339 break;
17340 }
17341
Linus Torvalds1da177e2005-04-16 15:20:36 -070017342 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017343 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017344 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017345 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17346 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017347 break;
17348 }
17349
17350 /* Verify it. */
17351 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17352 if (p[i] == i)
17353 continue;
17354
David S. Miller59e6b432005-05-18 22:50:10 -070017355 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17356 DMA_RWCTRL_WRITE_BNDRY_16) {
17357 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017358 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17359 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17360 break;
17361 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017362 dev_err(&tp->pdev->dev,
17363 "%s: Buffer corrupted on read back! "
17364 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017365 ret = -ENODEV;
17366 goto out;
17367 }
17368 }
17369
17370 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17371 /* Success. */
17372 ret = 0;
17373 break;
17374 }
17375 }
David S. Miller59e6b432005-05-18 22:50:10 -070017376 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17377 DMA_RWCTRL_WRITE_BNDRY_16) {
17378 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017379 * now look for chipsets that are known to expose the
17380 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017381 */
Matt Carlson41434702011-03-09 16:58:22 +000017382 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017383 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17384 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017385 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017386 /* Safe to use the calculated DMA boundary. */
17387 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017388 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017389
David S. Miller59e6b432005-05-18 22:50:10 -070017390 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017392
17393out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017394 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017395out_nofree:
17396 return ret;
17397}
17398
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017399static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017400{
Joe Perches63c3a662011-04-26 08:12:10 +000017401 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017402 tp->bufmgr_config.mbuf_read_dma_low_water =
17403 DEFAULT_MB_RDMA_LOW_WATER_5705;
17404 tp->bufmgr_config.mbuf_mac_rx_low_water =
17405 DEFAULT_MB_MACRX_LOW_WATER_57765;
17406 tp->bufmgr_config.mbuf_high_water =
17407 DEFAULT_MB_HIGH_WATER_57765;
17408
17409 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17410 DEFAULT_MB_RDMA_LOW_WATER_5705;
17411 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17412 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17413 tp->bufmgr_config.mbuf_high_water_jumbo =
17414 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017415 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017416 tp->bufmgr_config.mbuf_read_dma_low_water =
17417 DEFAULT_MB_RDMA_LOW_WATER_5705;
17418 tp->bufmgr_config.mbuf_mac_rx_low_water =
17419 DEFAULT_MB_MACRX_LOW_WATER_5705;
17420 tp->bufmgr_config.mbuf_high_water =
17421 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017422 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017423 tp->bufmgr_config.mbuf_mac_rx_low_water =
17424 DEFAULT_MB_MACRX_LOW_WATER_5906;
17425 tp->bufmgr_config.mbuf_high_water =
17426 DEFAULT_MB_HIGH_WATER_5906;
17427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017428
Michael Chanfdfec1722005-07-25 12:31:48 -070017429 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17430 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17431 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17432 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17433 tp->bufmgr_config.mbuf_high_water_jumbo =
17434 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17435 } else {
17436 tp->bufmgr_config.mbuf_read_dma_low_water =
17437 DEFAULT_MB_RDMA_LOW_WATER;
17438 tp->bufmgr_config.mbuf_mac_rx_low_water =
17439 DEFAULT_MB_MACRX_LOW_WATER;
17440 tp->bufmgr_config.mbuf_high_water =
17441 DEFAULT_MB_HIGH_WATER;
17442
17443 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17444 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17445 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17446 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17447 tp->bufmgr_config.mbuf_high_water_jumbo =
17448 DEFAULT_MB_HIGH_WATER_JUMBO;
17449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017450
17451 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17452 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17453}
17454
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017455static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017456{
Matt Carlson79eb6902010-02-17 15:17:03 +000017457 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17458 case TG3_PHY_ID_BCM5400: return "5400";
17459 case TG3_PHY_ID_BCM5401: return "5401";
17460 case TG3_PHY_ID_BCM5411: return "5411";
17461 case TG3_PHY_ID_BCM5701: return "5701";
17462 case TG3_PHY_ID_BCM5703: return "5703";
17463 case TG3_PHY_ID_BCM5704: return "5704";
17464 case TG3_PHY_ID_BCM5705: return "5705";
17465 case TG3_PHY_ID_BCM5750: return "5750";
17466 case TG3_PHY_ID_BCM5752: return "5752";
17467 case TG3_PHY_ID_BCM5714: return "5714";
17468 case TG3_PHY_ID_BCM5780: return "5780";
17469 case TG3_PHY_ID_BCM5755: return "5755";
17470 case TG3_PHY_ID_BCM5787: return "5787";
17471 case TG3_PHY_ID_BCM5784: return "5784";
17472 case TG3_PHY_ID_BCM5756: return "5722/5756";
17473 case TG3_PHY_ID_BCM5906: return "5906";
17474 case TG3_PHY_ID_BCM5761: return "5761";
17475 case TG3_PHY_ID_BCM5718C: return "5718C";
17476 case TG3_PHY_ID_BCM5718S: return "5718S";
17477 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017478 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017479 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017480 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017481 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017482 case 0: return "serdes";
17483 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017485}
17486
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017487static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017488{
Joe Perches63c3a662011-04-26 08:12:10 +000017489 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017490 strcpy(str, "PCI Express");
17491 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017492 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017493 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17494
17495 strcpy(str, "PCIX:");
17496
17497 if ((clock_ctrl == 7) ||
17498 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17499 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17500 strcat(str, "133MHz");
17501 else if (clock_ctrl == 0)
17502 strcat(str, "33MHz");
17503 else if (clock_ctrl == 2)
17504 strcat(str, "50MHz");
17505 else if (clock_ctrl == 4)
17506 strcat(str, "66MHz");
17507 else if (clock_ctrl == 6)
17508 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017509 } else {
17510 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017511 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017512 strcat(str, "66MHz");
17513 else
17514 strcat(str, "33MHz");
17515 }
Joe Perches63c3a662011-04-26 08:12:10 +000017516 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017517 strcat(str, ":32-bit");
17518 else
17519 strcat(str, ":64-bit");
17520 return str;
17521}
17522
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017523static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017524{
17525 struct ethtool_coalesce *ec = &tp->coal;
17526
17527 memset(ec, 0, sizeof(*ec));
17528 ec->cmd = ETHTOOL_GCOALESCE;
17529 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17530 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17531 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17532 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17533 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17534 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17535 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17536 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17537 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17538
17539 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17540 HOSTCC_MODE_CLRTICK_TXBD)) {
17541 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17542 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17543 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17544 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17545 }
Michael Chand244c892005-07-05 14:42:33 -070017546
Joe Perches63c3a662011-04-26 08:12:10 +000017547 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017548 ec->rx_coalesce_usecs_irq = 0;
17549 ec->tx_coalesce_usecs_irq = 0;
17550 ec->stats_block_coalesce_usecs = 0;
17551 }
David S. Miller15f98502005-05-18 22:49:26 -070017552}
17553
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017554static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017555 const struct pci_device_id *ent)
17556{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017557 struct net_device *dev;
17558 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017559 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017560 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017561 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017562 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017563 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017564
Joe Perches05dbe002010-02-17 19:44:19 +000017565 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017566
17567 err = pci_enable_device(pdev);
17568 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017569 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017570 return err;
17571 }
17572
Linus Torvalds1da177e2005-04-16 15:20:36 -070017573 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17574 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017575 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017576 goto err_out_disable_pdev;
17577 }
17578
17579 pci_set_master(pdev);
17580
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017581 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017582 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017583 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017584 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017585 }
17586
Linus Torvalds1da177e2005-04-16 15:20:36 -070017587 SET_NETDEV_DEV(dev, &pdev->dev);
17588
Linus Torvalds1da177e2005-04-16 15:20:36 -070017589 tp = netdev_priv(dev);
17590 tp->pdev = pdev;
17591 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017592 tp->rx_mode = TG3_DEF_RX_MODE;
17593 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017594 tp->irq_sync = 1;
Ivan Vecera0486a062014-09-01 14:21:57 +020017595 tp->pcierr_recovery = false;
Matt Carlson8ef21422008-05-02 16:47:53 -070017596
Linus Torvalds1da177e2005-04-16 15:20:36 -070017597 if (tg3_debug > 0)
17598 tp->msg_enable = tg3_debug;
17599 else
17600 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17601
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017602 if (pdev_is_ssb_gige_core(pdev)) {
17603 tg3_flag_set(tp, IS_SSB_CORE);
17604 if (ssb_gige_must_flush_posted_writes(pdev))
17605 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17606 if (ssb_gige_one_dma_at_once(pdev))
17607 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017608 if (ssb_gige_have_roboswitch(pdev)) {
17609 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017610 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017611 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017612 if (ssb_gige_is_rgmii(pdev))
17613 tg3_flag_set(tp, RGMII_MODE);
17614 }
17615
Linus Torvalds1da177e2005-04-16 15:20:36 -070017616 /* The word/byte swap controls here control register access byte
17617 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17618 * setting below.
17619 */
17620 tp->misc_host_ctrl =
17621 MISC_HOST_CTRL_MASK_PCI_INT |
17622 MISC_HOST_CTRL_WORD_SWAP |
17623 MISC_HOST_CTRL_INDIR_ACCESS |
17624 MISC_HOST_CTRL_PCISTATE_RW;
17625
17626 /* The NONFRM (non-frame) byte/word swap controls take effect
17627 * on descriptor entries, anything which isn't packet data.
17628 *
17629 * The StrongARM chips on the board (one for tx, one for rx)
17630 * are running in big-endian mode.
17631 */
17632 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17633 GRC_MODE_WSWAP_NONFRM_DATA);
17634#ifdef __BIG_ENDIAN
17635 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17636#endif
17637 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017638 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017639 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017640
Matt Carlsond5fe4882008-11-21 17:20:32 -080017641 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017642 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017643 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017644 err = -ENOMEM;
17645 goto err_out_free_dev;
17646 }
17647
Matt Carlsonc9cab242011-07-13 09:27:27 +000017648 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17649 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17650 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17651 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17652 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017653 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017654 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17655 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017656 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017657 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17658 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017659 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17660 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017661 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17662 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017663 tg3_flag_set(tp, ENABLE_APE);
17664 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17665 if (!tp->aperegs) {
17666 dev_err(&pdev->dev,
17667 "Cannot map APE registers, aborting\n");
17668 err = -ENOMEM;
17669 goto err_out_iounmap;
17670 }
17671 }
17672
Linus Torvalds1da177e2005-04-16 15:20:36 -070017673 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17674 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017675
Linus Torvalds1da177e2005-04-16 15:20:36 -070017676 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017677 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017678 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017679 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017680
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017681 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017682 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017683 dev_err(&pdev->dev,
17684 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017685 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017686 }
17687
Michael Chan4a29cc22006-03-19 13:21:12 -080017688 /* The EPB bridge inside 5714, 5715, and 5780 and any
17689 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017690 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17691 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17692 * do DMA address check in tg3_start_xmit().
17693 */
Joe Perches63c3a662011-04-26 08:12:10 +000017694 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017695 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017696 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017697 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017698#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017699 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017700#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017701 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017702 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017703
17704 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017705 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017706 err = pci_set_dma_mask(pdev, dma_mask);
17707 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017708 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017709 err = pci_set_consistent_dma_mask(pdev,
17710 persist_dma_mask);
17711 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017712 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17713 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017714 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017715 }
17716 }
17717 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017718 if (err || dma_mask == DMA_BIT_MASK(32)) {
17719 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017720 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017721 dev_err(&pdev->dev,
17722 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017723 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017724 }
17725 }
17726
Michael Chanfdfec1722005-07-25 12:31:48 -070017727 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017728
Matt Carlson0da06062011-05-19 12:12:53 +000017729 /* 5700 B0 chips do not support checksumming correctly due
17730 * to hardware bugs.
17731 */
Joe Perches41535772013-02-16 11:20:04 +000017732 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017733 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17734
17735 if (tg3_flag(tp, 5755_PLUS))
17736 features |= NETIF_F_IPV6_CSUM;
17737 }
17738
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017739 /* TSO is on by default on chips that support hardware TSO.
17740 * Firmware TSO on older chips gives lower performance, so it
17741 * is off by default, but can be enabled using ethtool.
17742 */
Joe Perches63c3a662011-04-26 08:12:10 +000017743 if ((tg3_flag(tp, HW_TSO_1) ||
17744 tg3_flag(tp, HW_TSO_2) ||
17745 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017746 (features & NETIF_F_IP_CSUM))
17747 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017748 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017749 if (features & NETIF_F_IPV6_CSUM)
17750 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017751 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017752 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17753 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17754 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17755 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17756 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017757 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017759
Vlad Yasevich51dfe7b2014-03-24 17:52:12 -040017760 dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
17761 NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlsond542fe22011-05-19 16:02:43 +000017762 dev->vlan_features |= features;
17763
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017764 /*
17765 * Add loopback capability only for a subset of devices that support
17766 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17767 * loopback for the remaining devices.
17768 */
Joe Perches41535772013-02-16 11:20:04 +000017769 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017770 !tg3_flag(tp, CPMU_PRESENT))
17771 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017772 features |= NETIF_F_LOOPBACK;
17773
Matt Carlson0da06062011-05-19 12:12:53 +000017774 dev->hw_features |= features;
Michael Chane565eec2014-01-03 10:09:12 -080017775 dev->priv_flags |= IFF_UNICAST_FLT;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017776
Joe Perches41535772013-02-16 11:20:04 +000017777 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017778 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017779 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017780 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017781 tp->rx_pending = 63;
17782 }
17783
Linus Torvalds1da177e2005-04-16 15:20:36 -070017784 err = tg3_get_device_address(tp);
17785 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017786 dev_err(&pdev->dev,
17787 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017788 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017789 }
17790
Matt Carlsonc88864d2007-11-12 21:07:01 -080017791 /*
17792 * Reset chip in case UNDI or EFI driver did not shutdown
17793 * DMA self test will enable WDMAC and we'll see (spurious)
17794 * pending DMA on the PCI bus at that point.
17795 */
17796 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17797 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17798 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17799 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17800 }
17801
17802 err = tg3_test_dma(tp);
17803 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017804 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017805 goto err_out_apeunmap;
17806 }
17807
Matt Carlson78f90dc2009-11-13 13:03:42 +000017808 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17809 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17810 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017811 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017812 struct tg3_napi *tnapi = &tp->napi[i];
17813
17814 tnapi->tp = tp;
17815 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17816
17817 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017818 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017819 intmbx += 0x8;
17820 else
17821 intmbx += 0x4;
17822
17823 tnapi->consmbox = rcvmbx;
17824 tnapi->prodmbox = sndmbx;
17825
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017826 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017827 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017828 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017829 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017830
Joe Perches63c3a662011-04-26 08:12:10 +000017831 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017832 break;
17833
17834 /*
17835 * If we support MSIX, we'll be using RSS. If we're using
17836 * RSS, the first vector only handles link interrupts and the
17837 * remaining vectors handle rx and tx interrupts. Reuse the
17838 * mailbox values for the next iteration. The values we setup
17839 * above are still useful for the single vectored mode.
17840 */
17841 if (!i)
17842 continue;
17843
17844 rcvmbx += 0x8;
17845
17846 if (sndmbx & 0x4)
17847 sndmbx -= 0x4;
17848 else
17849 sndmbx += 0xc;
17850 }
17851
Matt Carlsonc88864d2007-11-12 21:07:01 -080017852 tg3_init_coal(tp);
17853
Michael Chanc49a1562006-12-17 17:07:29 -080017854 pci_set_drvdata(pdev, dev);
17855
Joe Perches41535772013-02-16 11:20:04 +000017856 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17857 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17858 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017859 tg3_flag_set(tp, PTP_CAPABLE);
17860
Matt Carlson21f76382012-02-22 12:35:21 +000017861 tg3_timer_init(tp);
17862
Michael Chan402e1392013-02-14 12:13:41 +000017863 tg3_carrier_off(tp);
17864
Linus Torvalds1da177e2005-04-16 15:20:36 -070017865 err = register_netdev(dev);
17866 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017867 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017868 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017869 }
17870
Joe Perches05dbe002010-02-17 19:44:19 +000017871 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17872 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017873 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017874 tg3_bus_string(tp, str),
17875 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017876
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017877 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017878 struct phy_device *phydev;
Hauke Mehrtensead24022013-09-28 23:15:26 +020017879 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017880 netdev_info(dev,
17881 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017882 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017883 } else {
17884 char *ethtype;
17885
17886 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17887 ethtype = "10/100Base-TX";
17888 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17889 ethtype = "1000Base-SX";
17890 else
17891 ethtype = "10/100/1000Base-T";
17892
Matt Carlson5129c3a2010-04-05 10:19:23 +000017893 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017894 "(WireSpeed[%d], EEE[%d])\n",
17895 tg3_phy_string(tp), ethtype,
17896 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17897 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017898 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017899
Joe Perches05dbe002010-02-17 19:44:19 +000017900 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017901 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017902 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017903 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017904 tg3_flag(tp, ENABLE_ASF) != 0,
17905 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017906 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17907 tp->dma_rwctrl,
17908 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17909 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017910
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017911 pci_save_state(pdev);
17912
Linus Torvalds1da177e2005-04-16 15:20:36 -070017913 return 0;
17914
Matt Carlson0d3031d2007-10-10 18:02:43 -070017915err_out_apeunmap:
17916 if (tp->aperegs) {
17917 iounmap(tp->aperegs);
17918 tp->aperegs = NULL;
17919 }
17920
Linus Torvalds1da177e2005-04-16 15:20:36 -070017921err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017922 if (tp->regs) {
17923 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017924 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017926
17927err_out_free_dev:
17928 free_netdev(dev);
17929
17930err_out_free_res:
17931 pci_release_regions(pdev);
17932
17933err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080017934 if (pci_is_enabled(pdev))
17935 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017936 return err;
17937}
17938
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017939static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017940{
17941 struct net_device *dev = pci_get_drvdata(pdev);
17942
17943 if (dev) {
17944 struct tg3 *tp = netdev_priv(dev);
17945
Jesper Juhle3c55302012-04-09 22:50:15 +020017946 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017947
Matt Carlsondb219972011-11-04 09:15:03 +000017948 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017949
David S. Miller1805b2f2011-10-24 18:18:09 -040017950 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017951 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017952 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017953 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017954
Linus Torvalds1da177e2005-04-16 15:20:36 -070017955 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017956 if (tp->aperegs) {
17957 iounmap(tp->aperegs);
17958 tp->aperegs = NULL;
17959 }
Michael Chan68929142005-08-09 20:17:14 -070017960 if (tp->regs) {
17961 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017962 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017964 free_netdev(dev);
17965 pci_release_regions(pdev);
17966 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017967 }
17968}
17969
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017970#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017971static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017972{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017973 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017974 struct net_device *dev = pci_get_drvdata(pdev);
17975 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017976 int err = 0;
17977
17978 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070017979
17980 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017981 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017982
Matt Carlsondb219972011-11-04 09:15:03 +000017983 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017984 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017985 tg3_netif_stop(tp);
17986
Matt Carlson21f76382012-02-22 12:35:21 +000017987 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017988
David S. Millerf47c11e2005-06-24 20:18:35 -070017989 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017990 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017991 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017992
17993 netif_device_detach(dev);
17994
David S. Millerf47c11e2005-06-24 20:18:35 -070017995 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017996 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017997 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017998 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017999
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018000 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018001 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018002 int err2;
18003
David S. Millerf47c11e2005-06-24 20:18:35 -070018004 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018005
Joe Perches63c3a662011-04-26 08:12:10 +000018006 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018007 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018008 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070018009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018010
Matt Carlson21f76382012-02-22 12:35:21 +000018011 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018012
18013 netif_device_attach(dev);
18014 tg3_netif_start(tp);
18015
Michael Chanb9ec6c12006-07-25 16:37:27 -070018016out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018017 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018018
18019 if (!err2)
18020 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018021 }
18022
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018023unlock:
18024 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018025 return err;
18026}
18027
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018028static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018029{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018030 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018031 struct net_device *dev = pci_get_drvdata(pdev);
18032 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018033 int err = 0;
18034
18035 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018036
18037 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018038 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018039
Linus Torvalds1da177e2005-04-16 15:20:36 -070018040 netif_device_attach(dev);
18041
David S. Millerf47c11e2005-06-24 20:18:35 -070018042 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018043
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018044 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
18045
Joe Perches63c3a662011-04-26 08:12:10 +000018046 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000018047 err = tg3_restart_hw(tp,
18048 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070018049 if (err)
18050 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018051
Matt Carlson21f76382012-02-22 12:35:21 +000018052 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018053
Linus Torvalds1da177e2005-04-16 15:20:36 -070018054 tg3_netif_start(tp);
18055
Michael Chanb9ec6c12006-07-25 16:37:27 -070018056out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018057 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018058
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018059 if (!err)
18060 tg3_phy_start(tp);
18061
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018062unlock:
18063 rtnl_unlock();
Michael Chanb9ec6c12006-07-25 16:37:27 -070018064 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018065}
Fabio Estevam42df36a2013-04-16 09:28:29 +000018066#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018067
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018068static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
18069
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018070static void tg3_shutdown(struct pci_dev *pdev)
18071{
18072 struct net_device *dev = pci_get_drvdata(pdev);
18073 struct tg3 *tp = netdev_priv(dev);
18074
18075 rtnl_lock();
18076 netif_device_detach(dev);
18077
18078 if (netif_running(dev))
18079 dev_close(dev);
18080
18081 if (system_state == SYSTEM_POWER_OFF)
18082 tg3_power_down(tp);
18083
18084 rtnl_unlock();
18085}
18086
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018087/**
18088 * tg3_io_error_detected - called when PCI error is detected
18089 * @pdev: Pointer to PCI device
18090 * @state: The current pci connection state
18091 *
18092 * This function is called after a PCI bus error affecting
18093 * this device has been detected.
18094 */
18095static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
18096 pci_channel_state_t state)
18097{
18098 struct net_device *netdev = pci_get_drvdata(pdev);
18099 struct tg3 *tp = netdev_priv(netdev);
18100 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
18101
18102 netdev_info(netdev, "PCI I/O error detected\n");
18103
18104 rtnl_lock();
18105
Ivan Vecera0486a062014-09-01 14:21:57 +020018106 tp->pcierr_recovery = true;
18107
Gavin Shand8af4df2013-07-24 17:25:08 +080018108 /* We probably don't have netdev yet */
18109 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018110 goto done;
18111
18112 tg3_phy_stop(tp);
18113
18114 tg3_netif_stop(tp);
18115
Matt Carlson21f76382012-02-22 12:35:21 +000018116 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018117
18118 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000018119 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018120
18121 netif_device_detach(netdev);
18122
18123 /* Clean up software state, even if MMIO is blocked */
18124 tg3_full_lock(tp, 0);
18125 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
18126 tg3_full_unlock(tp);
18127
18128done:
Michael Chan72bb72b2013-06-17 13:47:25 -070018129 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018130 if (netdev) {
18131 tg3_napi_enable(tp);
18132 dev_close(netdev);
18133 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018134 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070018135 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018136 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070018137 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018138
18139 rtnl_unlock();
18140
18141 return err;
18142}
18143
18144/**
18145 * tg3_io_slot_reset - called after the pci bus has been reset.
18146 * @pdev: Pointer to PCI device
18147 *
18148 * Restart the card from scratch, as if from a cold-boot.
18149 * At this point, the card has exprienced a hard reset,
18150 * followed by fixups by BIOS, and has its config space
18151 * set up identically to what it was at cold boot.
18152 */
18153static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
18154{
18155 struct net_device *netdev = pci_get_drvdata(pdev);
18156 struct tg3 *tp = netdev_priv(netdev);
18157 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
18158 int err;
18159
18160 rtnl_lock();
18161
18162 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018163 dev_err(&pdev->dev,
18164 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018165 goto done;
18166 }
18167
18168 pci_set_master(pdev);
18169 pci_restore_state(pdev);
18170 pci_save_state(pdev);
18171
Daniel Borkmann68293092013-08-13 11:45:13 -070018172 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018173 rc = PCI_ERS_RESULT_RECOVERED;
18174 goto done;
18175 }
18176
18177 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000018178 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018179 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018180
18181 rc = PCI_ERS_RESULT_RECOVERED;
18182
18183done:
Daniel Borkmann68293092013-08-13 11:45:13 -070018184 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070018185 tg3_napi_enable(tp);
18186 dev_close(netdev);
18187 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018188 rtnl_unlock();
18189
18190 return rc;
18191}
18192
18193/**
18194 * tg3_io_resume - called when traffic can start flowing again.
18195 * @pdev: Pointer to PCI device
18196 *
18197 * This callback is called when the error recovery driver tells
18198 * us that its OK to resume normal operation.
18199 */
18200static void tg3_io_resume(struct pci_dev *pdev)
18201{
18202 struct net_device *netdev = pci_get_drvdata(pdev);
18203 struct tg3 *tp = netdev_priv(netdev);
18204 int err;
18205
18206 rtnl_lock();
18207
18208 if (!netif_running(netdev))
18209 goto done;
18210
18211 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018212 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000018213 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018214 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018215 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018216 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018217 netdev_err(netdev, "Cannot restart hardware after reset.\n");
18218 goto done;
18219 }
18220
18221 netif_device_attach(netdev);
18222
Matt Carlson21f76382012-02-22 12:35:21 +000018223 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018224
18225 tg3_netif_start(tp);
18226
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018227 tg3_full_unlock(tp);
18228
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018229 tg3_phy_start(tp);
18230
18231done:
Ivan Vecera0486a062014-09-01 14:21:57 +020018232 tp->pcierr_recovery = false;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018233 rtnl_unlock();
18234}
18235
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070018236static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018237 .error_detected = tg3_io_error_detected,
18238 .slot_reset = tg3_io_slot_reset,
18239 .resume = tg3_io_resume
18240};
18241
Linus Torvalds1da177e2005-04-16 15:20:36 -070018242static struct pci_driver tg3_driver = {
18243 .name = DRV_MODULE_NAME,
18244 .id_table = tg3_pci_tbl,
18245 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018246 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018247 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018248 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018249 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018250};
18251
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018252module_pci_driver(tg3_driver);