blob: ff300f7cf5295fc0d4f1e1b0692d74e6a6091912 [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
Andrew Lunn7f854422016-01-06 20:11:18 +01001409 phydev = mdiobus_get_phy(tp->mdio_bus, 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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001541
1542 /* The bus registration will look for all the PHYs on the mdio bus.
1543 * Unfortunately, it does not ensure the PHY is powered up before
1544 * accessing the PHY ID registers. A chip reset is the
1545 * quickest way to bring the device back to an operational state..
1546 */
1547 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1548 tg3_bmcr_reset(tp);
1549
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001550 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001551 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001552 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001553 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001554 return i;
1555 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001556
Andrew Lunn7f854422016-01-06 20:11:18 +01001557 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsona9daf362008-05-25 23:49:44 -07001558
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001559 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001560 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001561 mdiobus_unregister(tp->mdio_bus);
1562 mdiobus_free(tp->mdio_bus);
1563 return -ENODEV;
1564 }
1565
1566 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001567 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001568 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001569 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001570 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001571 case PHY_ID_BCM50610:
1572 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001573 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001574 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001575 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001576 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001577 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001578 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001579 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001580 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001581 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001582 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001583 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001584 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001585 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001586 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001587 case PHY_ID_RTL8201E:
1588 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001589 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001590 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001591 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001592 break;
1593 }
1594
Joe Perches63c3a662011-04-26 08:12:10 +00001595 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001596
Joe Perches41535772013-02-16 11:20:04 +00001597 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001598 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001599
1600 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001601}
1602
1603static void tg3_mdio_fini(struct tg3 *tp)
1604{
Joe Perches63c3a662011-04-26 08:12:10 +00001605 if (tg3_flag(tp, MDIOBUS_INITED)) {
1606 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001607 mdiobus_unregister(tp->mdio_bus);
1608 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001609 }
1610}
1611
Matt Carlson95e28692008-05-25 23:44:14 -07001612/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001613static inline void tg3_generate_fw_event(struct tg3 *tp)
1614{
1615 u32 val;
1616
1617 val = tr32(GRC_RX_CPU_EVENT);
1618 val |= GRC_RX_CPU_DRIVER_EVENT;
1619 tw32_f(GRC_RX_CPU_EVENT, val);
1620
1621 tp->last_event_jiffies = jiffies;
1622}
1623
1624#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1625
1626/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001627static void tg3_wait_for_event_ack(struct tg3 *tp)
1628{
1629 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001630 unsigned int delay_cnt;
1631 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001632
Matt Carlson4ba526c2008-08-15 14:10:04 -07001633 /* If enough time has passed, no wait is necessary. */
1634 time_remain = (long)(tp->last_event_jiffies + 1 +
1635 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1636 (long)jiffies;
1637 if (time_remain < 0)
1638 return;
1639
1640 /* Check if we can shorten the wait time. */
1641 delay_cnt = jiffies_to_usecs(time_remain);
1642 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1643 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1644 delay_cnt = (delay_cnt >> 3) + 1;
1645
1646 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001647 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1648 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001649 if (pci_channel_offline(tp->pdev))
1650 break;
1651
Matt Carlson4ba526c2008-08-15 14:10:04 -07001652 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001653 }
1654}
1655
1656/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001657static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001658{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001659 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001660
1661 val = 0;
1662 if (!tg3_readphy(tp, MII_BMCR, &reg))
1663 val = reg << 16;
1664 if (!tg3_readphy(tp, MII_BMSR, &reg))
1665 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001666 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001667
1668 val = 0;
1669 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1670 val = reg << 16;
1671 if (!tg3_readphy(tp, MII_LPA, &reg))
1672 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001673 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001674
1675 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001676 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001677 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1678 val = reg << 16;
1679 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1680 val |= (reg & 0xffff);
1681 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001682 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001683
1684 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1685 val = reg << 16;
1686 else
1687 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001688 *data++ = val;
1689}
1690
1691/* tp->lock is held. */
1692static void tg3_ump_link_report(struct tg3 *tp)
1693{
1694 u32 data[4];
1695
1696 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1697 return;
1698
1699 tg3_phy_gather_ump_data(tp, data);
1700
1701 tg3_wait_for_event_ack(tp);
1702
1703 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1704 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1705 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1706 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1707 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1708 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001709
Matt Carlson4ba526c2008-08-15 14:10:04 -07001710 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001711}
1712
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001713/* tp->lock is held. */
1714static void tg3_stop_fw(struct tg3 *tp)
1715{
1716 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1717 /* Wait for RX cpu to ACK the previous event. */
1718 tg3_wait_for_event_ack(tp);
1719
1720 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1721
1722 tg3_generate_fw_event(tp);
1723
1724 /* Wait for RX cpu to ACK this event. */
1725 tg3_wait_for_event_ack(tp);
1726 }
1727}
1728
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001729/* tp->lock is held. */
1730static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1731{
1732 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1733 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1734
1735 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1736 switch (kind) {
1737 case RESET_KIND_INIT:
1738 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1739 DRV_STATE_START);
1740 break;
1741
1742 case RESET_KIND_SHUTDOWN:
1743 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1744 DRV_STATE_UNLOAD);
1745 break;
1746
1747 case RESET_KIND_SUSPEND:
1748 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1749 DRV_STATE_SUSPEND);
1750 break;
1751
1752 default:
1753 break;
1754 }
1755 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001756}
1757
1758/* tp->lock is held. */
1759static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1760{
1761 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1762 switch (kind) {
1763 case RESET_KIND_INIT:
1764 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1765 DRV_STATE_START_DONE);
1766 break;
1767
1768 case RESET_KIND_SHUTDOWN:
1769 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1770 DRV_STATE_UNLOAD_DONE);
1771 break;
1772
1773 default:
1774 break;
1775 }
1776 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001777}
1778
1779/* tp->lock is held. */
1780static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1781{
1782 if (tg3_flag(tp, ENABLE_ASF)) {
1783 switch (kind) {
1784 case RESET_KIND_INIT:
1785 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1786 DRV_STATE_START);
1787 break;
1788
1789 case RESET_KIND_SHUTDOWN:
1790 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1791 DRV_STATE_UNLOAD);
1792 break;
1793
1794 case RESET_KIND_SUSPEND:
1795 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1796 DRV_STATE_SUSPEND);
1797 break;
1798
1799 default:
1800 break;
1801 }
1802 }
1803}
1804
1805static int tg3_poll_fw(struct tg3 *tp)
1806{
1807 int i;
1808 u32 val;
1809
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001810 if (tg3_flag(tp, NO_FWARE_REPORTED))
1811 return 0;
1812
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001813 if (tg3_flag(tp, IS_SSB_CORE)) {
1814 /* We don't use firmware. */
1815 return 0;
1816 }
1817
Joe Perches41535772013-02-16 11:20:04 +00001818 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001819 /* Wait up to 20ms for init done. */
1820 for (i = 0; i < 200; i++) {
1821 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1822 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001823 if (pci_channel_offline(tp->pdev))
1824 return -ENODEV;
1825
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001826 udelay(100);
1827 }
1828 return -ENODEV;
1829 }
1830
1831 /* Wait for firmware initialization to complete. */
1832 for (i = 0; i < 100000; i++) {
1833 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1834 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1835 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001836 if (pci_channel_offline(tp->pdev)) {
1837 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1838 tg3_flag_set(tp, NO_FWARE_REPORTED);
1839 netdev_info(tp->dev, "No firmware running\n");
1840 }
1841
1842 break;
1843 }
1844
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001845 udelay(10);
1846 }
1847
1848 /* Chip might not be fitted with firmware. Some Sun onboard
1849 * parts are configured like that. So don't signal the timeout
1850 * of the above loop as an error, but do report the lack of
1851 * running firmware once.
1852 */
1853 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1854 tg3_flag_set(tp, NO_FWARE_REPORTED);
1855
1856 netdev_info(tp->dev, "No firmware running\n");
1857 }
1858
Joe Perches41535772013-02-16 11:20:04 +00001859 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001860 /* The 57765 A0 needs a little more
1861 * time to do some important work.
1862 */
1863 mdelay(10);
1864 }
1865
1866 return 0;
1867}
1868
Matt Carlson95e28692008-05-25 23:44:14 -07001869static void tg3_link_report(struct tg3 *tp)
1870{
1871 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001872 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001873 tg3_ump_link_report(tp);
1874 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001875 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1876 (tp->link_config.active_speed == SPEED_1000 ?
1877 1000 :
1878 (tp->link_config.active_speed == SPEED_100 ?
1879 100 : 10)),
1880 (tp->link_config.active_duplex == DUPLEX_FULL ?
1881 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001882
Joe Perches05dbe002010-02-17 19:44:19 +00001883 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1884 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1885 "on" : "off",
1886 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1887 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001888
1889 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1890 netdev_info(tp->dev, "EEE is %s\n",
1891 tp->setlpicnt ? "enabled" : "disabled");
1892
Matt Carlson95e28692008-05-25 23:44:14 -07001893 tg3_ump_link_report(tp);
1894 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001895
1896 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001897}
1898
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001899static u32 tg3_decode_flowctrl_1000T(u32 adv)
1900{
1901 u32 flowctrl = 0;
1902
1903 if (adv & ADVERTISE_PAUSE_CAP) {
1904 flowctrl |= FLOW_CTRL_RX;
1905 if (!(adv & ADVERTISE_PAUSE_ASYM))
1906 flowctrl |= FLOW_CTRL_TX;
1907 } else if (adv & ADVERTISE_PAUSE_ASYM)
1908 flowctrl |= FLOW_CTRL_TX;
1909
1910 return flowctrl;
1911}
1912
Matt Carlson95e28692008-05-25 23:44:14 -07001913static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1914{
1915 u16 miireg;
1916
Steve Glendinninge18ce342008-12-16 02:00:00 -08001917 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001918 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001919 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001920 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001921 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001922 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1923 else
1924 miireg = 0;
1925
1926 return miireg;
1927}
1928
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001929static u32 tg3_decode_flowctrl_1000X(u32 adv)
1930{
1931 u32 flowctrl = 0;
1932
1933 if (adv & ADVERTISE_1000XPAUSE) {
1934 flowctrl |= FLOW_CTRL_RX;
1935 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1936 flowctrl |= FLOW_CTRL_TX;
1937 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1938 flowctrl |= FLOW_CTRL_TX;
1939
1940 return flowctrl;
1941}
1942
Matt Carlson95e28692008-05-25 23:44:14 -07001943static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1944{
1945 u8 cap = 0;
1946
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001947 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1948 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1949 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1950 if (lcladv & ADVERTISE_1000XPAUSE)
1951 cap = FLOW_CTRL_RX;
1952 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001953 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001954 }
1955
1956 return cap;
1957}
1958
Matt Carlsonf51f3562008-05-25 23:45:08 -07001959static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001960{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001961 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001962 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001963 u32 old_rx_mode = tp->rx_mode;
1964 u32 old_tx_mode = tp->tx_mode;
1965
Joe Perches63c3a662011-04-26 08:12:10 +00001966 if (tg3_flag(tp, USE_PHYLIB))
Andrew Lunn7f854422016-01-06 20:11:18 +01001967 autoneg = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr)->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001968 else
1969 autoneg = tp->link_config.autoneg;
1970
Joe Perches63c3a662011-04-26 08:12:10 +00001971 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001972 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001973 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001974 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001975 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001976 } else
1977 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001978
Matt Carlsonf51f3562008-05-25 23:45:08 -07001979 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001980
Steve Glendinninge18ce342008-12-16 02:00:00 -08001981 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001982 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1983 else
1984 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1985
Matt Carlsonf51f3562008-05-25 23:45:08 -07001986 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001987 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001988
Steve Glendinninge18ce342008-12-16 02:00:00 -08001989 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001990 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1991 else
1992 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1993
Matt Carlsonf51f3562008-05-25 23:45:08 -07001994 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001995 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001996}
1997
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001998static void tg3_adjust_link(struct net_device *dev)
1999{
2000 u8 oldflowctrl, linkmesg = 0;
2001 u32 mac_mode, lcl_adv, rmt_adv;
2002 struct tg3 *tp = netdev_priv(dev);
Andrew Lunn7f854422016-01-06 20:11:18 +01002003 struct phy_device *phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002004
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002005 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002006
2007 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2008 MAC_MODE_HALF_DUPLEX);
2009
2010 oldflowctrl = tp->link_config.active_flowctrl;
2011
2012 if (phydev->link) {
2013 lcl_adv = 0;
2014 rmt_adv = 0;
2015
2016 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2017 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002018 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002019 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002020 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002021 else
2022 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002023
2024 if (phydev->duplex == DUPLEX_HALF)
2025 mac_mode |= MAC_MODE_HALF_DUPLEX;
2026 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002027 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002028 tp->link_config.flowctrl);
2029
2030 if (phydev->pause)
2031 rmt_adv = LPA_PAUSE_CAP;
2032 if (phydev->asym_pause)
2033 rmt_adv |= LPA_PAUSE_ASYM;
2034 }
2035
2036 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2037 } else
2038 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2039
2040 if (mac_mode != tp->mac_mode) {
2041 tp->mac_mode = mac_mode;
2042 tw32_f(MAC_MODE, tp->mac_mode);
2043 udelay(40);
2044 }
2045
Joe Perches41535772013-02-16 11:20:04 +00002046 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002047 if (phydev->speed == SPEED_10)
2048 tw32(MAC_MI_STAT,
2049 MAC_MI_STAT_10MBPS_MODE |
2050 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2051 else
2052 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2053 }
2054
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002055 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2056 tw32(MAC_TX_LENGTHS,
2057 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2058 (6 << TX_LENGTHS_IPG_SHIFT) |
2059 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2060 else
2061 tw32(MAC_TX_LENGTHS,
2062 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2063 (6 << TX_LENGTHS_IPG_SHIFT) |
2064 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2065
Matt Carlson34655ad2012-02-22 12:35:18 +00002066 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002067 phydev->speed != tp->link_config.active_speed ||
2068 phydev->duplex != tp->link_config.active_duplex ||
2069 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002070 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002071
Matt Carlson34655ad2012-02-22 12:35:18 +00002072 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002073 tp->link_config.active_speed = phydev->speed;
2074 tp->link_config.active_duplex = phydev->duplex;
2075
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002076 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002077
2078 if (linkmesg)
2079 tg3_link_report(tp);
2080}
2081
2082static int tg3_phy_init(struct tg3 *tp)
2083{
2084 struct phy_device *phydev;
2085
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002086 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002087 return 0;
2088
2089 /* Bring the PHY back to a known state. */
2090 tg3_bmcr_reset(tp);
2091
Andrew Lunn7f854422016-01-06 20:11:18 +01002092 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002093
2094 /* Attach the MAC to the PHY. */
Andrew Lunn84eff6d2016-01-06 20:11:10 +01002095 phydev = phy_connect(tp->dev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +00002096 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002097 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002098 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002099 return PTR_ERR(phydev);
2100 }
2101
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002102 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002103 switch (phydev->interface) {
2104 case PHY_INTERFACE_MODE_GMII:
2105 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002106 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002107 phydev->supported &= (PHY_GBIT_FEATURES |
2108 SUPPORTED_Pause |
2109 SUPPORTED_Asym_Pause);
2110 break;
2111 }
2112 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002113 case PHY_INTERFACE_MODE_MII:
2114 phydev->supported &= (PHY_BASIC_FEATURES |
2115 SUPPORTED_Pause |
2116 SUPPORTED_Asym_Pause);
2117 break;
2118 default:
Andrew Lunn7f854422016-01-06 20:11:18 +01002119 phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002120 return -EINVAL;
2121 }
2122
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002123 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002124
2125 phydev->advertising = phydev->supported;
2126
Andrew Lunn22209432016-01-06 20:11:13 +01002127 phy_attached_info(phydev);
2128
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002129 return 0;
2130}
2131
2132static void tg3_phy_start(struct tg3 *tp)
2133{
2134 struct phy_device *phydev;
2135
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002136 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002137 return;
2138
Andrew Lunn7f854422016-01-06 20:11:18 +01002139 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002140
Matt Carlson800960682010-08-02 11:26:06 +00002141 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2142 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002143 phydev->speed = tp->link_config.speed;
2144 phydev->duplex = tp->link_config.duplex;
2145 phydev->autoneg = tp->link_config.autoneg;
2146 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002147 }
2148
2149 phy_start(phydev);
2150
2151 phy_start_aneg(phydev);
2152}
2153
2154static void tg3_phy_stop(struct tg3 *tp)
2155{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002156 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002157 return;
2158
Andrew Lunn7f854422016-01-06 20:11:18 +01002159 phy_stop(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002160}
2161
2162static void tg3_phy_fini(struct tg3 *tp)
2163{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002164 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Andrew Lunn7f854422016-01-06 20:11:18 +01002165 phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002166 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002167 }
2168}
2169
Matt Carlson941ec902011-08-19 13:58:23 +00002170static int tg3_phy_set_extloopbk(struct tg3 *tp)
2171{
2172 int err;
2173 u32 val;
2174
2175 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2176 return 0;
2177
2178 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2179 /* Cannot do read-modify-write on 5401 */
2180 err = tg3_phy_auxctl_write(tp,
2181 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2182 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2183 0x4c20);
2184 goto done;
2185 }
2186
2187 err = tg3_phy_auxctl_read(tp,
2188 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2189 if (err)
2190 return err;
2191
2192 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2193 err = tg3_phy_auxctl_write(tp,
2194 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2195
2196done:
2197 return err;
2198}
2199
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002200static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2201{
2202 u32 phytest;
2203
2204 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2205 u32 phy;
2206
2207 tg3_writephy(tp, MII_TG3_FET_TEST,
2208 phytest | MII_TG3_FET_SHADOW_EN);
2209 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2210 if (enable)
2211 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2212 else
2213 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2214 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2215 }
2216 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2217 }
2218}
2219
Matt Carlson6833c042008-11-21 17:18:59 -08002220static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2221{
2222 u32 reg;
2223
Joe Perches63c3a662011-04-26 08:12:10 +00002224 if (!tg3_flag(tp, 5705_PLUS) ||
2225 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002226 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002227 return;
2228
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002229 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002230 tg3_phy_fet_toggle_apd(tp, enable);
2231 return;
2232 }
2233
Nithin Sujir3ab71072013-09-20 16:46:55 -07002234 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002235 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2236 MII_TG3_MISC_SHDW_SCR5_SDTL |
2237 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002238 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002239 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2240
Nithin Sujir3ab71072013-09-20 16:46:55 -07002241 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002242
2243
Nithin Sujir3ab71072013-09-20 16:46:55 -07002244 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002245 if (enable)
2246 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2247
Nithin Sujir3ab71072013-09-20 16:46:55 -07002248 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002249}
2250
Joe Perches953c96e2013-04-09 10:18:14 +00002251static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002252{
2253 u32 phy;
2254
Joe Perches63c3a662011-04-26 08:12:10 +00002255 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002256 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002257 return;
2258
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002259 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002260 u32 ephy;
2261
Matt Carlson535ef6e2009-08-25 10:09:36 +00002262 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2263 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2264
2265 tg3_writephy(tp, MII_TG3_FET_TEST,
2266 ephy | MII_TG3_FET_SHADOW_EN);
2267 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002268 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002269 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002270 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002271 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2272 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002273 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002274 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002275 }
2276 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002277 int ret;
2278
2279 ret = tg3_phy_auxctl_read(tp,
2280 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2281 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002282 if (enable)
2283 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2284 else
2285 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002286 tg3_phy_auxctl_write(tp,
2287 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002288 }
2289 }
2290}
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292static void tg3_phy_set_wirespeed(struct tg3 *tp)
2293{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002294 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 u32 val;
2296
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002297 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return;
2299
Matt Carlson15ee95c2011-04-20 07:57:40 +00002300 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2301 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002302 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2303 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002306static void tg3_phy_apply_otp(struct tg3 *tp)
2307{
2308 u32 otp, phy;
2309
2310 if (!tp->phy_otp)
2311 return;
2312
2313 otp = tp->phy_otp;
2314
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002315 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002316 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002317
2318 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2319 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2320 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2321
2322 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2323 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2324 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2325
2326 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2327 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2328 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2329
2330 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2331 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2332
2333 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2334 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2335
2336 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2337 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2338 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2339
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002340 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002341}
2342
Nithin Sujir400dfba2013-05-18 06:26:53 +00002343static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2344{
2345 u32 val;
2346 struct ethtool_eee *dest = &tp->eee;
2347
2348 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2349 return;
2350
2351 if (eee)
2352 dest = eee;
2353
2354 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2355 return;
2356
2357 /* Pull eee_active */
2358 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2359 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2360 dest->eee_active = 1;
2361 } else
2362 dest->eee_active = 0;
2363
2364 /* Pull lp advertised settings */
2365 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2366 return;
2367 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2368
2369 /* Pull advertised and eee_enabled settings */
2370 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2371 return;
2372 dest->eee_enabled = !!val;
2373 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2374
2375 /* Pull tx_lpi_enabled */
2376 val = tr32(TG3_CPMU_EEE_MODE);
2377 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2378
2379 /* Pull lpi timer value */
2380 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2381}
2382
Joe Perches953c96e2013-04-09 10:18:14 +00002383static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002384{
2385 u32 val;
2386
2387 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2388 return;
2389
2390 tp->setlpicnt = 0;
2391
2392 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002393 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002394 tp->link_config.active_duplex == DUPLEX_FULL &&
2395 (tp->link_config.active_speed == SPEED_100 ||
2396 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002397 u32 eeectl;
2398
2399 if (tp->link_config.active_speed == SPEED_1000)
2400 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2401 else
2402 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2403
2404 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2405
Nithin Sujir400dfba2013-05-18 06:26:53 +00002406 tg3_eee_pull_config(tp, NULL);
2407 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002408 tp->setlpicnt = 2;
2409 }
2410
2411 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002412 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002413 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002414 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002415 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002416 }
2417
Matt Carlson52b02d02010-10-14 10:37:41 +00002418 val = tr32(TG3_CPMU_EEE_MODE);
2419 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2420 }
2421}
2422
Matt Carlsonb0c59432011-05-19 12:12:48 +00002423static void tg3_phy_eee_enable(struct tg3 *tp)
2424{
2425 u32 val;
2426
2427 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002428 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2429 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002430 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002431 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002432 val = MII_TG3_DSP_TAP26_ALNOKO |
2433 MII_TG3_DSP_TAP26_RMRXSTO;
2434 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002435 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002436 }
2437
2438 val = tr32(TG3_CPMU_EEE_MODE);
2439 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2440}
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442static int tg3_wait_macro_done(struct tg3 *tp)
2443{
2444 int limit = 100;
2445
2446 while (limit--) {
2447 u32 tmp32;
2448
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002449 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if ((tmp32 & 0x1000) == 0)
2451 break;
2452 }
2453 }
Roel Kluind4675b52009-02-12 16:33:27 -08002454 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return -EBUSY;
2456
2457 return 0;
2458}
2459
2460static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2461{
2462 static const u32 test_pat[4][6] = {
2463 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2464 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2465 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2466 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2467 };
2468 int chan;
2469
2470 for (chan = 0; chan < 4; chan++) {
2471 int i;
2472
2473 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2474 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002475 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 for (i = 0; i < 6; i++)
2478 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2479 test_pat[chan][i]);
2480
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002481 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (tg3_wait_macro_done(tp)) {
2483 *resetp = 1;
2484 return -EBUSY;
2485 }
2486
2487 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2488 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002489 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (tg3_wait_macro_done(tp)) {
2491 *resetp = 1;
2492 return -EBUSY;
2493 }
2494
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002495 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (tg3_wait_macro_done(tp)) {
2497 *resetp = 1;
2498 return -EBUSY;
2499 }
2500
2501 for (i = 0; i < 6; i += 2) {
2502 u32 low, high;
2503
2504 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2505 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2506 tg3_wait_macro_done(tp)) {
2507 *resetp = 1;
2508 return -EBUSY;
2509 }
2510 low &= 0x7fff;
2511 high &= 0x000f;
2512 if (low != test_pat[chan][i] ||
2513 high != test_pat[chan][i+1]) {
2514 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2515 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2516 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2517
2518 return -EBUSY;
2519 }
2520 }
2521 }
2522
2523 return 0;
2524}
2525
2526static int tg3_phy_reset_chanpat(struct tg3 *tp)
2527{
2528 int chan;
2529
2530 for (chan = 0; chan < 4; chan++) {
2531 int i;
2532
2533 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2534 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002535 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 for (i = 0; i < 6; i++)
2537 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002538 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (tg3_wait_macro_done(tp))
2540 return -EBUSY;
2541 }
2542
2543 return 0;
2544}
2545
2546static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2547{
2548 u32 reg32, phy9_orig;
2549 int retries, do_phy_reset, err;
2550
2551 retries = 10;
2552 do_phy_reset = 1;
2553 do {
2554 if (do_phy_reset) {
2555 err = tg3_bmcr_reset(tp);
2556 if (err)
2557 return err;
2558 do_phy_reset = 0;
2559 }
2560
2561 /* Disable transmitter and interrupt. */
2562 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2563 continue;
2564
2565 reg32 |= 0x3000;
2566 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2567
2568 /* Set full-duplex, 1000 mbps. */
2569 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002570 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002573 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 continue;
2575
Matt Carlson221c5632011-06-13 13:39:01 +00002576 tg3_writephy(tp, MII_CTRL1000,
2577 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002579 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002580 if (err)
2581 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002584 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2587 if (!err)
2588 break;
2589 } while (--retries);
2590
2591 err = tg3_phy_reset_chanpat(tp);
2592 if (err)
2593 return err;
2594
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002595 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002598 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002600 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Matt Carlson221c5632011-06-13 13:39:01 +00002602 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002604 err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
2605 if (err)
2606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002608 reg32 &= ~0x3000;
2609 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2610
2611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002614static void tg3_carrier_off(struct tg3 *tp)
2615{
2616 netif_carrier_off(tp->dev);
2617 tp->link_up = false;
2618}
2619
Nithin Sujirce20f162013-04-09 08:48:04 +00002620static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2621{
2622 if (tg3_flag(tp, ENABLE_ASF))
2623 netdev_warn(tp->dev,
2624 "Management side-band traffic will be interrupted during phy settings change\n");
2625}
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627/* This will reset the tigon3 PHY if there is no valid
2628 * link unless the FORCE argument is non-zero.
2629 */
2630static int tg3_phy_reset(struct tg3 *tp)
2631{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002632 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 int err;
2634
Joe Perches41535772013-02-16 11:20:04 +00002635 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002636 val = tr32(GRC_MISC_CFG);
2637 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2638 udelay(40);
2639 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002640 err = tg3_readphy(tp, MII_BMSR, &val);
2641 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (err != 0)
2643 return -EBUSY;
2644
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002645 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002646 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002647 tg3_link_report(tp);
2648 }
2649
Joe Perches41535772013-02-16 11:20:04 +00002650 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2651 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2652 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 err = tg3_phy_reset_5703_4_5(tp);
2654 if (err)
2655 return err;
2656 goto out;
2657 }
2658
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002659 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002660 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2661 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002662 cpmuctrl = tr32(TG3_CPMU_CTRL);
2663 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2664 tw32(TG3_CPMU_CTRL,
2665 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2666 }
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 err = tg3_bmcr_reset(tp);
2669 if (err)
2670 return err;
2671
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002672 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002673 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2674 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002675
2676 tw32(TG3_CPMU_CTRL, cpmuctrl);
2677 }
2678
Joe Perches41535772013-02-16 11:20:04 +00002679 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2680 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002681 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2682 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2683 CPMU_LSPD_1000MB_MACCLK_12_5) {
2684 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2685 udelay(40);
2686 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2687 }
2688 }
2689
Joe Perches63c3a662011-04-26 08:12:10 +00002690 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002691 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002692 return 0;
2693
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002694 tg3_phy_apply_otp(tp);
2695
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002696 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002697 tg3_phy_toggle_apd(tp, true);
2698 else
2699 tg3_phy_toggle_apd(tp, false);
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002702 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002703 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002704 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2705 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002706 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002708
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002709 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002710 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2711 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002713
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002714 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002715 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002716 tg3_phydsp_write(tp, 0x000a, 0x310b);
2717 tg3_phydsp_write(tp, 0x201f, 0x9506);
2718 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002719 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002720 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002721 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002722 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002723 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2724 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2725 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2726 tg3_writephy(tp, MII_TG3_TEST1,
2727 MII_TG3_TEST1_TRIM_EN | 0x4);
2728 } else
2729 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2730
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002731 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002732 }
Michael Chanc424cb22006-04-29 18:56:34 -07002733 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /* Set Extended packet length bit (bit 14) on all chips that */
2736 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002737 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002739 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002740 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002742 err = tg3_phy_auxctl_read(tp,
2743 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2744 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002745 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2746 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
2748
2749 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2750 * jumbo frames transmission.
2751 */
Joe Perches63c3a662011-04-26 08:12:10 +00002752 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002753 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002754 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002755 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
2757
Joe Perches41535772013-02-16 11:20:04 +00002758 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002759 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002760 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002761 }
2762
Joe Perches41535772013-02-16 11:20:04 +00002763 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002764 tg3_phydsp_write(tp, 0xffb, 0x4000);
2765
Joe Perches953c96e2013-04-09 10:18:14 +00002766 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 tg3_phy_set_wirespeed(tp);
2768 return 0;
2769}
2770
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002771#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2772#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2773#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2774 TG3_GPIO_MSG_NEED_VAUX)
2775#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2776 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2777 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2778 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2779 (TG3_GPIO_MSG_DRVR_PRES << 12))
2780
2781#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2782 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2783 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2784 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2785 (TG3_GPIO_MSG_NEED_VAUX << 12))
2786
2787static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2788{
2789 u32 status, shift;
2790
Joe Perches41535772013-02-16 11:20:04 +00002791 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2792 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002793 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2794 else
2795 status = tr32(TG3_CPMU_DRV_STATUS);
2796
2797 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2798 status &= ~(TG3_GPIO_MSG_MASK << shift);
2799 status |= (newstat << shift);
2800
Joe Perches41535772013-02-16 11:20:04 +00002801 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2802 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002803 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2804 else
2805 tw32(TG3_CPMU_DRV_STATUS, status);
2806
2807 return status >> TG3_APE_GPIO_MSG_SHIFT;
2808}
2809
Matt Carlson520b2752011-06-13 13:39:02 +00002810static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2811{
2812 if (!tg3_flag(tp, IS_NIC))
2813 return 0;
2814
Joe Perches41535772013-02-16 11:20:04 +00002815 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2816 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2817 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002818 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2819 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002820
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002821 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2822
2823 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2824 TG3_GRC_LCLCTL_PWRSW_DELAY);
2825
2826 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2827 } else {
2828 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2829 TG3_GRC_LCLCTL_PWRSW_DELAY);
2830 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002831
Matt Carlson520b2752011-06-13 13:39:02 +00002832 return 0;
2833}
2834
2835static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2836{
2837 u32 grc_local_ctrl;
2838
2839 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002840 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2841 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002842 return;
2843
2844 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2845
2846 tw32_wait_f(GRC_LOCAL_CTRL,
2847 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2848 TG3_GRC_LCLCTL_PWRSW_DELAY);
2849
2850 tw32_wait_f(GRC_LOCAL_CTRL,
2851 grc_local_ctrl,
2852 TG3_GRC_LCLCTL_PWRSW_DELAY);
2853
2854 tw32_wait_f(GRC_LOCAL_CTRL,
2855 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2856 TG3_GRC_LCLCTL_PWRSW_DELAY);
2857}
2858
2859static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2860{
2861 if (!tg3_flag(tp, IS_NIC))
2862 return;
2863
Joe Perches41535772013-02-16 11:20:04 +00002864 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2865 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002866 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2867 (GRC_LCLCTRL_GPIO_OE0 |
2868 GRC_LCLCTRL_GPIO_OE1 |
2869 GRC_LCLCTRL_GPIO_OE2 |
2870 GRC_LCLCTRL_GPIO_OUTPUT0 |
2871 GRC_LCLCTRL_GPIO_OUTPUT1),
2872 TG3_GRC_LCLCTL_PWRSW_DELAY);
2873 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2874 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2875 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2876 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2877 GRC_LCLCTRL_GPIO_OE1 |
2878 GRC_LCLCTRL_GPIO_OE2 |
2879 GRC_LCLCTRL_GPIO_OUTPUT0 |
2880 GRC_LCLCTRL_GPIO_OUTPUT1 |
2881 tp->grc_local_ctrl;
2882 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2883 TG3_GRC_LCLCTL_PWRSW_DELAY);
2884
2885 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2886 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2887 TG3_GRC_LCLCTL_PWRSW_DELAY);
2888
2889 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2890 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2891 TG3_GRC_LCLCTL_PWRSW_DELAY);
2892 } else {
2893 u32 no_gpio2;
2894 u32 grc_local_ctrl = 0;
2895
2896 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002897 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002898 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2899 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2900 grc_local_ctrl,
2901 TG3_GRC_LCLCTL_PWRSW_DELAY);
2902 }
2903
2904 /* On 5753 and variants, GPIO2 cannot be used. */
2905 no_gpio2 = tp->nic_sram_data_cfg &
2906 NIC_SRAM_DATA_CFG_NO_GPIO2;
2907
2908 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2909 GRC_LCLCTRL_GPIO_OE1 |
2910 GRC_LCLCTRL_GPIO_OE2 |
2911 GRC_LCLCTRL_GPIO_OUTPUT1 |
2912 GRC_LCLCTRL_GPIO_OUTPUT2;
2913 if (no_gpio2) {
2914 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2915 GRC_LCLCTRL_GPIO_OUTPUT2);
2916 }
2917 tw32_wait_f(GRC_LOCAL_CTRL,
2918 tp->grc_local_ctrl | grc_local_ctrl,
2919 TG3_GRC_LCLCTL_PWRSW_DELAY);
2920
2921 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2922
2923 tw32_wait_f(GRC_LOCAL_CTRL,
2924 tp->grc_local_ctrl | grc_local_ctrl,
2925 TG3_GRC_LCLCTL_PWRSW_DELAY);
2926
2927 if (!no_gpio2) {
2928 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2929 tw32_wait_f(GRC_LOCAL_CTRL,
2930 tp->grc_local_ctrl | grc_local_ctrl,
2931 TG3_GRC_LCLCTL_PWRSW_DELAY);
2932 }
2933 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002934}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002935
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002936static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002937{
2938 u32 msg = 0;
2939
2940 /* Serialize power state transitions */
2941 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2942 return;
2943
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002944 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002945 msg = TG3_GPIO_MSG_NEED_VAUX;
2946
2947 msg = tg3_set_function_status(tp, msg);
2948
2949 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2950 goto done;
2951
2952 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2953 tg3_pwrsrc_switch_to_vaux(tp);
2954 else
2955 tg3_pwrsrc_die_with_vmain(tp);
2956
2957done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002958 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002959}
2960
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002961static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Matt Carlson683644b2011-03-09 16:58:23 +00002963 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Matt Carlson334355a2010-01-20 16:58:10 +00002965 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002966 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return;
2968
Joe Perches41535772013-02-16 11:20:04 +00002969 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2970 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2971 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002972 tg3_frob_aux_power_5717(tp, include_wol ?
2973 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002974 return;
2975 }
2976
2977 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002978 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002980 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002981
Michael Chanbc1c7562006-03-20 17:48:03 -08002982 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002983 if (dev_peer) {
2984 struct tg3 *tp_peer = netdev_priv(dev_peer);
2985
Joe Perches63c3a662011-04-26 08:12:10 +00002986 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002987 return;
2988
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002989 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002990 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002991 need_vaux = true;
2992 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002995 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2996 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002997 need_vaux = true;
2998
Matt Carlson520b2752011-06-13 13:39:02 +00002999 if (need_vaux)
3000 tg3_pwrsrc_switch_to_vaux(tp);
3001 else
3002 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003005static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3006{
3007 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3008 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003009 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003010 if (speed != SPEED_10)
3011 return 1;
3012 } else if (speed == SPEED_10)
3013 return 1;
3014
3015 return 0;
3016}
3017
Nithin Sujir44f3b502013-05-13 11:04:15 +00003018static bool tg3_phy_power_bug(struct tg3 *tp)
3019{
3020 switch (tg3_asic_rev(tp)) {
3021 case ASIC_REV_5700:
3022 case ASIC_REV_5704:
3023 return true;
3024 case ASIC_REV_5780:
3025 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3026 return true;
3027 return false;
3028 case ASIC_REV_5717:
3029 if (!tp->pci_fn)
3030 return true;
3031 return false;
3032 case ASIC_REV_5719:
3033 case ASIC_REV_5720:
3034 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3035 !tp->pci_fn)
3036 return true;
3037 return false;
3038 }
3039
3040 return false;
3041}
3042
Nithin Sujir989038e2013-08-30 17:01:36 -07003043static bool tg3_phy_led_bug(struct tg3 *tp)
3044{
3045 switch (tg3_asic_rev(tp)) {
3046 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003047 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003048 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3049 !tp->pci_fn)
3050 return true;
3051 return false;
3052 }
3053
3054 return false;
3055}
3056
Matt Carlson0a459aa2008-11-03 16:54:15 -08003057static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003058{
Matt Carlsonce057f02007-11-12 21:08:03 -08003059 u32 val;
3060
Nithin Sujir942d1af2013-04-09 08:48:07 +00003061 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3062 return;
3063
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003064 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003065 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003066 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3067 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3068
3069 sg_dig_ctrl |=
3070 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3071 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3072 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3073 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003074 return;
Michael Chan51297242007-02-13 12:17:57 -08003075 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003076
Joe Perches41535772013-02-16 11:20:04 +00003077 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003078 tg3_bmcr_reset(tp);
3079 val = tr32(GRC_MISC_CFG);
3080 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3081 udelay(40);
3082 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003083 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003084 u32 phytest;
3085 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3086 u32 phy;
3087
3088 tg3_writephy(tp, MII_ADVERTISE, 0);
3089 tg3_writephy(tp, MII_BMCR,
3090 BMCR_ANENABLE | BMCR_ANRESTART);
3091
3092 tg3_writephy(tp, MII_TG3_FET_TEST,
3093 phytest | MII_TG3_FET_SHADOW_EN);
3094 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3095 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3096 tg3_writephy(tp,
3097 MII_TG3_FET_SHDW_AUXMODE4,
3098 phy);
3099 }
3100 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3101 }
3102 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003103 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003104 if (!tg3_phy_led_bug(tp))
3105 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3106 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003107
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003108 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3109 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3110 MII_TG3_AUXCTL_PCTL_VREG_11V;
3111 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003112 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003113
Michael Chan15c3b692006-03-22 01:06:52 -08003114 /* The PHY should not be powered down on some chips because
3115 * of bugs.
3116 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003117 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003118 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003119
Joe Perches41535772013-02-16 11:20:04 +00003120 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3121 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003122 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3123 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3124 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3125 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3126 }
3127
Michael Chan15c3b692006-03-22 01:06:52 -08003128 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3129}
3130
Matt Carlson3f007892008-11-03 16:51:36 -08003131/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003132static int tg3_nvram_lock(struct tg3 *tp)
3133{
Joe Perches63c3a662011-04-26 08:12:10 +00003134 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003135 int i;
3136
3137 if (tp->nvram_lock_cnt == 0) {
3138 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3139 for (i = 0; i < 8000; i++) {
3140 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3141 break;
3142 udelay(20);
3143 }
3144 if (i == 8000) {
3145 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3146 return -ENODEV;
3147 }
3148 }
3149 tp->nvram_lock_cnt++;
3150 }
3151 return 0;
3152}
3153
3154/* tp->lock is held. */
3155static void tg3_nvram_unlock(struct tg3 *tp)
3156{
Joe Perches63c3a662011-04-26 08:12:10 +00003157 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003158 if (tp->nvram_lock_cnt > 0)
3159 tp->nvram_lock_cnt--;
3160 if (tp->nvram_lock_cnt == 0)
3161 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3162 }
3163}
3164
3165/* tp->lock is held. */
3166static void tg3_enable_nvram_access(struct tg3 *tp)
3167{
Joe Perches63c3a662011-04-26 08:12:10 +00003168 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003169 u32 nvaccess = tr32(NVRAM_ACCESS);
3170
3171 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3172 }
3173}
3174
3175/* tp->lock is held. */
3176static void tg3_disable_nvram_access(struct tg3 *tp)
3177{
Joe Perches63c3a662011-04-26 08:12:10 +00003178 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003179 u32 nvaccess = tr32(NVRAM_ACCESS);
3180
3181 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3182 }
3183}
3184
3185static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3186 u32 offset, u32 *val)
3187{
3188 u32 tmp;
3189 int i;
3190
3191 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3192 return -EINVAL;
3193
3194 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3195 EEPROM_ADDR_DEVID_MASK |
3196 EEPROM_ADDR_READ);
3197 tw32(GRC_EEPROM_ADDR,
3198 tmp |
3199 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3200 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3201 EEPROM_ADDR_ADDR_MASK) |
3202 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3203
3204 for (i = 0; i < 1000; i++) {
3205 tmp = tr32(GRC_EEPROM_ADDR);
3206
3207 if (tmp & EEPROM_ADDR_COMPLETE)
3208 break;
3209 msleep(1);
3210 }
3211 if (!(tmp & EEPROM_ADDR_COMPLETE))
3212 return -EBUSY;
3213
Matt Carlson62cedd12009-04-20 14:52:29 -07003214 tmp = tr32(GRC_EEPROM_DATA);
3215
3216 /*
3217 * The data will always be opposite the native endian
3218 * format. Perform a blind byteswap to compensate.
3219 */
3220 *val = swab32(tmp);
3221
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003222 return 0;
3223}
3224
Prashant Sreedharan66c965f2014-06-20 23:28:15 -07003225#define NVRAM_CMD_TIMEOUT 5000
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003226
3227static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3228{
3229 int i;
3230
3231 tw32(NVRAM_CMD, nvram_cmd);
3232 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
Prashant Sreedharan66c965f2014-06-20 23:28:15 -07003233 usleep_range(10, 40);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003234 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3235 udelay(10);
3236 break;
3237 }
3238 }
3239
3240 if (i == NVRAM_CMD_TIMEOUT)
3241 return -EBUSY;
3242
3243 return 0;
3244}
3245
3246static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3247{
Joe Perches63c3a662011-04-26 08:12:10 +00003248 if (tg3_flag(tp, NVRAM) &&
3249 tg3_flag(tp, NVRAM_BUFFERED) &&
3250 tg3_flag(tp, FLASH) &&
3251 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003252 (tp->nvram_jedecnum == JEDEC_ATMEL))
3253
3254 addr = ((addr / tp->nvram_pagesize) <<
3255 ATMEL_AT45DB0X1B_PAGE_POS) +
3256 (addr % tp->nvram_pagesize);
3257
3258 return addr;
3259}
3260
3261static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3262{
Joe Perches63c3a662011-04-26 08:12:10 +00003263 if (tg3_flag(tp, NVRAM) &&
3264 tg3_flag(tp, NVRAM_BUFFERED) &&
3265 tg3_flag(tp, FLASH) &&
3266 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003267 (tp->nvram_jedecnum == JEDEC_ATMEL))
3268
3269 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3270 tp->nvram_pagesize) +
3271 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3272
3273 return addr;
3274}
3275
Matt Carlsone4f34112009-02-25 14:25:00 +00003276/* NOTE: Data read in from NVRAM is byteswapped according to
3277 * the byteswapping settings for all other register accesses.
3278 * tg3 devices are BE devices, so on a BE machine, the data
3279 * returned will be exactly as it is seen in NVRAM. On a LE
3280 * machine, the 32-bit value will be byteswapped.
3281 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003282static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3283{
3284 int ret;
3285
Joe Perches63c3a662011-04-26 08:12:10 +00003286 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003287 return tg3_nvram_read_using_eeprom(tp, offset, val);
3288
3289 offset = tg3_nvram_phys_addr(tp, offset);
3290
3291 if (offset > NVRAM_ADDR_MSK)
3292 return -EINVAL;
3293
3294 ret = tg3_nvram_lock(tp);
3295 if (ret)
3296 return ret;
3297
3298 tg3_enable_nvram_access(tp);
3299
3300 tw32(NVRAM_ADDR, offset);
3301 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3302 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3303
3304 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003305 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003306
3307 tg3_disable_nvram_access(tp);
3308
3309 tg3_nvram_unlock(tp);
3310
3311 return ret;
3312}
3313
Matt Carlsona9dc5292009-02-25 14:25:30 +00003314/* Ensures NVRAM data is in bytestream format. */
3315static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003316{
3317 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003318 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003319 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003320 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003321 return res;
3322}
3323
Matt Carlsondbe9b922012-02-13 10:20:09 +00003324static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3325 u32 offset, u32 len, u8 *buf)
3326{
3327 int i, j, rc = 0;
3328 u32 val;
3329
3330 for (i = 0; i < len; i += 4) {
3331 u32 addr;
3332 __be32 data;
3333
3334 addr = offset + i;
3335
3336 memcpy(&data, buf + i, 4);
3337
3338 /*
3339 * The SEEPROM interface expects the data to always be opposite
3340 * the native endian format. We accomplish this by reversing
3341 * all the operations that would have been performed on the
3342 * data from a call to tg3_nvram_read_be32().
3343 */
3344 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3345
3346 val = tr32(GRC_EEPROM_ADDR);
3347 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3348
3349 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3350 EEPROM_ADDR_READ);
3351 tw32(GRC_EEPROM_ADDR, val |
3352 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3353 (addr & EEPROM_ADDR_ADDR_MASK) |
3354 EEPROM_ADDR_START |
3355 EEPROM_ADDR_WRITE);
3356
3357 for (j = 0; j < 1000; j++) {
3358 val = tr32(GRC_EEPROM_ADDR);
3359
3360 if (val & EEPROM_ADDR_COMPLETE)
3361 break;
3362 msleep(1);
3363 }
3364 if (!(val & EEPROM_ADDR_COMPLETE)) {
3365 rc = -EBUSY;
3366 break;
3367 }
3368 }
3369
3370 return rc;
3371}
3372
3373/* offset and length are dword aligned */
3374static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3375 u8 *buf)
3376{
3377 int ret = 0;
3378 u32 pagesize = tp->nvram_pagesize;
3379 u32 pagemask = pagesize - 1;
3380 u32 nvram_cmd;
3381 u8 *tmp;
3382
3383 tmp = kmalloc(pagesize, GFP_KERNEL);
3384 if (tmp == NULL)
3385 return -ENOMEM;
3386
3387 while (len) {
3388 int j;
3389 u32 phy_addr, page_off, size;
3390
3391 phy_addr = offset & ~pagemask;
3392
3393 for (j = 0; j < pagesize; j += 4) {
3394 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3395 (__be32 *) (tmp + j));
3396 if (ret)
3397 break;
3398 }
3399 if (ret)
3400 break;
3401
3402 page_off = offset & pagemask;
3403 size = pagesize;
3404 if (len < size)
3405 size = len;
3406
3407 len -= size;
3408
3409 memcpy(tmp + page_off, buf, size);
3410
3411 offset = offset + (pagesize - page_off);
3412
3413 tg3_enable_nvram_access(tp);
3414
3415 /*
3416 * Before we can erase the flash page, we need
3417 * to issue a special "write enable" command.
3418 */
3419 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3420
3421 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3422 break;
3423
3424 /* Erase the target page */
3425 tw32(NVRAM_ADDR, phy_addr);
3426
3427 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3428 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3429
3430 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3431 break;
3432
3433 /* Issue another write enable to start the write. */
3434 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3435
3436 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3437 break;
3438
3439 for (j = 0; j < pagesize; j += 4) {
3440 __be32 data;
3441
3442 data = *((__be32 *) (tmp + j));
3443
3444 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3445
3446 tw32(NVRAM_ADDR, phy_addr + j);
3447
3448 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3449 NVRAM_CMD_WR;
3450
3451 if (j == 0)
3452 nvram_cmd |= NVRAM_CMD_FIRST;
3453 else if (j == (pagesize - 4))
3454 nvram_cmd |= NVRAM_CMD_LAST;
3455
3456 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3457 if (ret)
3458 break;
3459 }
3460 if (ret)
3461 break;
3462 }
3463
3464 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3465 tg3_nvram_exec_cmd(tp, nvram_cmd);
3466
3467 kfree(tmp);
3468
3469 return ret;
3470}
3471
3472/* offset and length are dword aligned */
3473static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3474 u8 *buf)
3475{
3476 int i, ret = 0;
3477
3478 for (i = 0; i < len; i += 4, offset += 4) {
3479 u32 page_off, phy_addr, nvram_cmd;
3480 __be32 data;
3481
3482 memcpy(&data, buf + i, 4);
3483 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3484
3485 page_off = offset % tp->nvram_pagesize;
3486
3487 phy_addr = tg3_nvram_phys_addr(tp, offset);
3488
Matt Carlsondbe9b922012-02-13 10:20:09 +00003489 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3490
3491 if (page_off == 0 || i == 0)
3492 nvram_cmd |= NVRAM_CMD_FIRST;
3493 if (page_off == (tp->nvram_pagesize - 4))
3494 nvram_cmd |= NVRAM_CMD_LAST;
3495
3496 if (i == (len - 4))
3497 nvram_cmd |= NVRAM_CMD_LAST;
3498
Matt Carlson42278222012-02-13 15:20:11 +00003499 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3500 !tg3_flag(tp, FLASH) ||
3501 !tg3_flag(tp, 57765_PLUS))
3502 tw32(NVRAM_ADDR, phy_addr);
3503
Joe Perches41535772013-02-16 11:20:04 +00003504 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003505 !tg3_flag(tp, 5755_PLUS) &&
3506 (tp->nvram_jedecnum == JEDEC_ST) &&
3507 (nvram_cmd & NVRAM_CMD_FIRST)) {
3508 u32 cmd;
3509
3510 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3511 ret = tg3_nvram_exec_cmd(tp, cmd);
3512 if (ret)
3513 break;
3514 }
3515 if (!tg3_flag(tp, FLASH)) {
3516 /* We always do complete word writes to eeprom. */
3517 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3518 }
3519
3520 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3521 if (ret)
3522 break;
3523 }
3524 return ret;
3525}
3526
3527/* offset and length are dword aligned */
3528static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3529{
3530 int ret;
3531
3532 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3533 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3534 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3535 udelay(40);
3536 }
3537
3538 if (!tg3_flag(tp, NVRAM)) {
3539 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3540 } else {
3541 u32 grc_mode;
3542
3543 ret = tg3_nvram_lock(tp);
3544 if (ret)
3545 return ret;
3546
3547 tg3_enable_nvram_access(tp);
3548 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3549 tw32(NVRAM_WRITE1, 0x406);
3550
3551 grc_mode = tr32(GRC_MODE);
3552 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3553
3554 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3555 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3556 buf);
3557 } else {
3558 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3559 buf);
3560 }
3561
3562 grc_mode = tr32(GRC_MODE);
3563 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3564
3565 tg3_disable_nvram_access(tp);
3566 tg3_nvram_unlock(tp);
3567 }
3568
3569 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3570 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3571 udelay(40);
3572 }
3573
3574 return ret;
3575}
3576
Matt Carlson997b4f12011-08-31 11:44:53 +00003577#define RX_CPU_SCRATCH_BASE 0x30000
3578#define RX_CPU_SCRATCH_SIZE 0x04000
3579#define TX_CPU_SCRATCH_BASE 0x34000
3580#define TX_CPU_SCRATCH_SIZE 0x04000
3581
3582/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003583static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003584{
3585 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003586 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003587
Nithin Sujir837c45b2013-03-06 17:02:30 +00003588 for (i = 0; i < iters; i++) {
3589 tw32(cpu_base + CPU_STATE, 0xffffffff);
3590 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3591 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3592 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003593 if (pci_channel_offline(tp->pdev))
3594 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003595 }
3596
3597 return (i == iters) ? -EBUSY : 0;
3598}
3599
3600/* tp->lock is held. */
3601static int tg3_rxcpu_pause(struct tg3 *tp)
3602{
3603 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3604
3605 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3606 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3607 udelay(10);
3608
3609 return rc;
3610}
3611
3612/* tp->lock is held. */
3613static int tg3_txcpu_pause(struct tg3 *tp)
3614{
3615 return tg3_pause_cpu(tp, TX_CPU_BASE);
3616}
3617
3618/* tp->lock is held. */
3619static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3620{
3621 tw32(cpu_base + CPU_STATE, 0xffffffff);
3622 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3623}
3624
3625/* tp->lock is held. */
3626static void tg3_rxcpu_resume(struct tg3 *tp)
3627{
3628 tg3_resume_cpu(tp, RX_CPU_BASE);
3629}
3630
3631/* tp->lock is held. */
3632static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3633{
3634 int rc;
3635
3636 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003637
Joe Perches41535772013-02-16 11:20:04 +00003638 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003639 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3640
3641 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3642 return 0;
3643 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003644 if (cpu_base == RX_CPU_BASE) {
3645 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003646 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003647 /*
3648 * There is only an Rx CPU for the 5750 derivative in the
3649 * BCM4785.
3650 */
3651 if (tg3_flag(tp, IS_SSB_CORE))
3652 return 0;
3653
Nithin Sujir837c45b2013-03-06 17:02:30 +00003654 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003655 }
3656
Nithin Sujir837c45b2013-03-06 17:02:30 +00003657 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003658 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003659 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003660 return -ENODEV;
3661 }
3662
3663 /* Clear firmware's nvram arbitration. */
3664 if (tg3_flag(tp, NVRAM))
3665 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3666 return 0;
3667}
3668
Nithin Sujir31f11a92013-03-06 17:02:33 +00003669static int tg3_fw_data_len(struct tg3 *tp,
3670 const struct tg3_firmware_hdr *fw_hdr)
3671{
3672 int fw_len;
3673
3674 /* Non fragmented firmware have one firmware header followed by a
3675 * contiguous chunk of data to be written. The length field in that
3676 * header is not the length of data to be written but the complete
3677 * length of the bss. The data length is determined based on
3678 * tp->fw->size minus headers.
3679 *
3680 * Fragmented firmware have a main header followed by multiple
3681 * fragments. Each fragment is identical to non fragmented firmware
3682 * with a firmware header followed by a contiguous chunk of data. In
3683 * the main header, the length field is unused and set to 0xffffffff.
3684 * In each fragment header the length is the entire size of that
3685 * fragment i.e. fragment data + header length. Data length is
3686 * therefore length field in the header minus TG3_FW_HDR_LEN.
3687 */
3688 if (tp->fw_len == 0xffffffff)
3689 fw_len = be32_to_cpu(fw_hdr->len);
3690 else
3691 fw_len = tp->fw->size;
3692
3693 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3694}
3695
Matt Carlson997b4f12011-08-31 11:44:53 +00003696/* tp->lock is held. */
3697static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3698 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003699 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003700{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003701 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003702 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003703 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003704
3705 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3706 netdev_err(tp->dev,
3707 "%s: Trying to load TX cpu firmware which is 5705\n",
3708 __func__);
3709 return -EINVAL;
3710 }
3711
Nithin Sujirc4dab502013-03-06 17:02:34 +00003712 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003713 write_op = tg3_write_mem;
3714 else
3715 write_op = tg3_write_indirect_reg32;
3716
Nithin Sujirc4dab502013-03-06 17:02:34 +00003717 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3718 /* It is possible that bootcode is still loading at this point.
3719 * Get the nvram lock first before halting the cpu.
3720 */
3721 int lock_err = tg3_nvram_lock(tp);
3722 err = tg3_halt_cpu(tp, cpu_base);
3723 if (!lock_err)
3724 tg3_nvram_unlock(tp);
3725 if (err)
3726 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003727
Nithin Sujirc4dab502013-03-06 17:02:34 +00003728 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3729 write_op(tp, cpu_scratch_base + i, 0);
3730 tw32(cpu_base + CPU_STATE, 0xffffffff);
3731 tw32(cpu_base + CPU_MODE,
3732 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3733 } else {
3734 /* Subtract additional main header for fragmented firmware and
3735 * advance to the first fragment
3736 */
3737 total_len -= TG3_FW_HDR_LEN;
3738 fw_hdr++;
3739 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003740
Nithin Sujir31f11a92013-03-06 17:02:33 +00003741 do {
3742 u32 *fw_data = (u32 *)(fw_hdr + 1);
3743 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3744 write_op(tp, cpu_scratch_base +
3745 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3746 (i * sizeof(u32)),
3747 be32_to_cpu(fw_data[i]));
3748
3749 total_len -= be32_to_cpu(fw_hdr->len);
3750
3751 /* Advance to next fragment */
3752 fw_hdr = (struct tg3_firmware_hdr *)
3753 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3754 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003755
3756 err = 0;
3757
3758out:
3759 return err;
3760}
3761
3762/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003763static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3764{
3765 int i;
3766 const int iters = 5;
3767
3768 tw32(cpu_base + CPU_STATE, 0xffffffff);
3769 tw32_f(cpu_base + CPU_PC, pc);
3770
3771 for (i = 0; i < iters; i++) {
3772 if (tr32(cpu_base + CPU_PC) == pc)
3773 break;
3774 tw32(cpu_base + CPU_STATE, 0xffffffff);
3775 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3776 tw32_f(cpu_base + CPU_PC, pc);
3777 udelay(1000);
3778 }
3779
3780 return (i == iters) ? -EBUSY : 0;
3781}
3782
3783/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003784static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3785{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003786 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003787 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003788
Nithin Sujir77997ea2013-03-06 17:02:32 +00003789 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003790
3791 /* Firmware blob starts with version numbers, followed by
3792 start address and length. We are setting complete length.
3793 length = end_address_of_bss - start_address_of_text.
3794 Remainder is the blob to be loaded contiguously
3795 from start address. */
3796
Matt Carlson997b4f12011-08-31 11:44:53 +00003797 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3798 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003799 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003800 if (err)
3801 return err;
3802
3803 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3804 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003805 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003806 if (err)
3807 return err;
3808
3809 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003810 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3811 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003812 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003813 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3814 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003815 tr32(RX_CPU_BASE + CPU_PC),
3816 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003817 return -ENODEV;
3818 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003819
3820 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003821
3822 return 0;
3823}
3824
Nithin Sujirc4dab502013-03-06 17:02:34 +00003825static int tg3_validate_rxcpu_state(struct tg3 *tp)
3826{
3827 const int iters = 1000;
3828 int i;
3829 u32 val;
3830
3831 /* Wait for boot code to complete initialization and enter service
3832 * loop. It is then safe to download service patches
3833 */
3834 for (i = 0; i < iters; i++) {
3835 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3836 break;
3837
3838 udelay(10);
3839 }
3840
3841 if (i == iters) {
3842 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3843 return -EBUSY;
3844 }
3845
3846 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3847 if (val & 0xff) {
3848 netdev_warn(tp->dev,
3849 "Other patches exist. Not downloading EEE patch\n");
3850 return -EEXIST;
3851 }
3852
3853 return 0;
3854}
3855
3856/* tp->lock is held. */
3857static void tg3_load_57766_firmware(struct tg3 *tp)
3858{
3859 struct tg3_firmware_hdr *fw_hdr;
3860
3861 if (!tg3_flag(tp, NO_NVRAM))
3862 return;
3863
3864 if (tg3_validate_rxcpu_state(tp))
3865 return;
3866
3867 if (!tp->fw)
3868 return;
3869
3870 /* This firmware blob has a different format than older firmware
3871 * releases as given below. The main difference is we have fragmented
3872 * data to be written to non-contiguous locations.
3873 *
3874 * In the beginning we have a firmware header identical to other
3875 * firmware which consists of version, base addr and length. The length
3876 * here is unused and set to 0xffffffff.
3877 *
3878 * This is followed by a series of firmware fragments which are
3879 * individually identical to previous firmware. i.e. they have the
3880 * firmware header and followed by data for that fragment. The version
3881 * field of the individual fragment header is unused.
3882 */
3883
3884 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3885 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3886 return;
3887
3888 if (tg3_rxcpu_pause(tp))
3889 return;
3890
3891 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3892 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3893
3894 tg3_rxcpu_resume(tp);
3895}
3896
Matt Carlson997b4f12011-08-31 11:44:53 +00003897/* tp->lock is held. */
3898static int tg3_load_tso_firmware(struct tg3 *tp)
3899{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003900 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003901 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003902 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003903
Matt Carlson1caf13e2013-03-06 17:02:29 +00003904 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003905 return 0;
3906
Nithin Sujir77997ea2013-03-06 17:02:32 +00003907 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003908
3909 /* Firmware blob starts with version numbers, followed by
3910 start address and length. We are setting complete length.
3911 length = end_address_of_bss - start_address_of_text.
3912 Remainder is the blob to be loaded contiguously
3913 from start address. */
3914
Matt Carlson997b4f12011-08-31 11:44:53 +00003915 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003916
Joe Perches41535772013-02-16 11:20:04 +00003917 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003918 cpu_base = RX_CPU_BASE;
3919 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3920 } else {
3921 cpu_base = TX_CPU_BASE;
3922 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3923 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3924 }
3925
3926 err = tg3_load_firmware_cpu(tp, cpu_base,
3927 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003928 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003929 if (err)
3930 return err;
3931
3932 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003933 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3934 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003935 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003936 netdev_err(tp->dev,
3937 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003938 __func__, tr32(cpu_base + CPU_PC),
3939 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003940 return -ENODEV;
3941 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003942
3943 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003944 return 0;
3945}
3946
Michael Chanf022ae62014-01-03 10:09:11 -08003947/* tp->lock is held. */
3948static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
3949{
3950 u32 addr_high, addr_low;
3951
3952 addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
3953 addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
3954 (mac_addr[4] << 8) | mac_addr[5]);
3955
3956 if (index < 4) {
3957 tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
3958 tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
3959 } else {
3960 index -= 4;
3961 tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
3962 tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
3963 }
3964}
Matt Carlson997b4f12011-08-31 11:44:53 +00003965
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003966/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003967static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003968{
Michael Chanf022ae62014-01-03 10:09:11 -08003969 u32 addr_high;
Matt Carlson3f007892008-11-03 16:51:36 -08003970 int i;
3971
Matt Carlson3f007892008-11-03 16:51:36 -08003972 for (i = 0; i < 4; i++) {
3973 if (i == 1 && skip_mac_1)
3974 continue;
Michael Chanf022ae62014-01-03 10:09:11 -08003975 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003976 }
3977
Joe Perches41535772013-02-16 11:20:04 +00003978 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3979 tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chanf022ae62014-01-03 10:09:11 -08003980 for (i = 4; i < 16; i++)
3981 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003982 }
3983
3984 addr_high = (tp->dev->dev_addr[0] +
3985 tp->dev->dev_addr[1] +
3986 tp->dev->dev_addr[2] +
3987 tp->dev->dev_addr[3] +
3988 tp->dev->dev_addr[4] +
3989 tp->dev->dev_addr[5]) &
3990 TX_BACKOFF_SEED_MASK;
3991 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3992}
3993
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003994static void tg3_enable_register_access(struct tg3 *tp)
3995{
3996 /*
3997 * Make sure register accesses (indirect or otherwise) will function
3998 * correctly.
3999 */
4000 pci_write_config_dword(tp->pdev,
4001 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
4002}
4003
4004static int tg3_power_up(struct tg3 *tp)
4005{
Matt Carlsonbed98292011-07-13 09:27:29 +00004006 int err;
4007
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004008 tg3_enable_register_access(tp);
4009
Matt Carlsonbed98292011-07-13 09:27:29 +00004010 err = pci_set_power_state(tp->pdev, PCI_D0);
4011 if (!err) {
4012 /* Switch out of Vaux if it is a NIC */
4013 tg3_pwrsrc_switch_to_vmain(tp);
4014 } else {
4015 netdev_err(tp->dev, "Transition to D0 failed\n");
4016 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004017
Matt Carlsonbed98292011-07-13 09:27:29 +00004018 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004019}
4020
Joe Perches953c96e2013-04-09 10:18:14 +00004021static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004022
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004023static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024{
4025 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004026 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004028 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004029
4030 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004031 if (tg3_flag(tp, CLKREQ_BUG))
4032 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4033 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4036 tw32(TG3PCI_MISC_HOST_CTRL,
4037 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4038
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004039 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004040 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004041
Joe Perches63c3a662011-04-26 08:12:10 +00004042 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004043 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004044 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00004045 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004046 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004047 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004048
Andrew Lunn7f854422016-01-06 20:11:18 +01004049 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004050
Matt Carlson800960682010-08-02 11:26:06 +00004051 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004052
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004053 tp->link_config.speed = phydev->speed;
4054 tp->link_config.duplex = phydev->duplex;
4055 tp->link_config.autoneg = phydev->autoneg;
4056 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004057
4058 advertising = ADVERTISED_TP |
4059 ADVERTISED_Pause |
4060 ADVERTISED_Autoneg |
4061 ADVERTISED_10baseT_Half;
4062
Joe Perches63c3a662011-04-26 08:12:10 +00004063 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4064 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004065 advertising |=
4066 ADVERTISED_100baseT_Half |
4067 ADVERTISED_100baseT_Full |
4068 ADVERTISED_10baseT_Full;
4069 else
4070 advertising |= ADVERTISED_10baseT_Full;
4071 }
4072
4073 phydev->advertising = advertising;
4074
4075 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004076
4077 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004078 if (phyid != PHY_ID_BCMAC131) {
4079 phyid &= PHY_BCM_OUI_MASK;
4080 if (phyid == PHY_BCM_OUI_1 ||
4081 phyid == PHY_BCM_OUI_2 ||
4082 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004083 do_low_power = true;
4084 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004085 }
Matt Carlsondd477002008-05-25 23:45:58 -07004086 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004087 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004088
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004089 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson800960682010-08-02 11:26:06 +00004090 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Matt Carlson2855b9f2012-02-13 15:20:14 +00004092 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004093 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 }
4095
Joe Perches41535772013-02-16 11:20:04 +00004096 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004097 u32 val;
4098
4099 val = tr32(GRC_VCPU_EXT_CTRL);
4100 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004101 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004102 int i;
4103 u32 val;
4104
4105 for (i = 0; i < 200; i++) {
4106 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4107 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4108 break;
4109 msleep(1);
4110 }
4111 }
Joe Perches63c3a662011-04-26 08:12:10 +00004112 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004113 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4114 WOL_DRV_STATE_SHUTDOWN |
4115 WOL_DRV_WOL |
4116 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004117
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004118 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 u32 mac_mode;
4120
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004121 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004122 if (do_low_power &&
4123 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4124 tg3_phy_auxctl_write(tp,
4125 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4126 MII_TG3_AUXCTL_PCTL_WOL_EN |
4127 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4128 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004129 udelay(40);
4130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004132 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004133 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004134 else if (tp->phy_flags &
4135 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4136 if (tp->link_config.active_speed == SPEED_1000)
4137 mac_mode = MAC_MODE_PORT_MODE_GMII;
4138 else
4139 mac_mode = MAC_MODE_PORT_MODE_MII;
4140 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004141 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004143 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004144 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004145 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004146 SPEED_100 : SPEED_10;
4147 if (tg3_5700_link_polarity(tp, speed))
4148 mac_mode |= MAC_MODE_LINK_POLARITY;
4149 else
4150 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 } else {
4153 mac_mode = MAC_MODE_PORT_MODE_TBI;
4154 }
4155
Joe Perches63c3a662011-04-26 08:12:10 +00004156 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 tw32(MAC_LED_CTRL, tp->led_ctrl);
4158
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004159 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004160 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4161 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004162 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Joe Perches63c3a662011-04-26 08:12:10 +00004164 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004165 mac_mode |= MAC_MODE_APE_TX_EN |
4166 MAC_MODE_APE_RX_EN |
4167 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004168
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 tw32_f(MAC_MODE, mac_mode);
4170 udelay(100);
4171
4172 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4173 udelay(10);
4174 }
4175
Joe Perches63c3a662011-04-26 08:12:10 +00004176 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004177 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4178 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 u32 base_val;
4180
4181 base_val = tp->pci_clock_ctrl;
4182 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4183 CLOCK_CTRL_TXCLK_DISABLE);
4184
Michael Chanb401e9e2005-12-19 16:27:04 -08004185 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4186 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004187 } else if (tg3_flag(tp, 5780_CLASS) ||
4188 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004189 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004190 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004191 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 u32 newbits1, newbits2;
4193
Joe Perches41535772013-02-16 11:20:04 +00004194 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4195 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4197 CLOCK_CTRL_TXCLK_DISABLE |
4198 CLOCK_CTRL_ALTCLK);
4199 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004200 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 newbits1 = CLOCK_CTRL_625_CORE;
4202 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4203 } else {
4204 newbits1 = CLOCK_CTRL_ALTCLK;
4205 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4206 }
4207
Michael Chanb401e9e2005-12-19 16:27:04 -08004208 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4209 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Michael Chanb401e9e2005-12-19 16:27:04 -08004211 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4212 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Joe Perches63c3a662011-04-26 08:12:10 +00004214 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 u32 newbits3;
4216
Joe Perches41535772013-02-16 11:20:04 +00004217 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4218 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4220 CLOCK_CTRL_TXCLK_DISABLE |
4221 CLOCK_CTRL_44MHZ_CORE);
4222 } else {
4223 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4224 }
4225
Michael Chanb401e9e2005-12-19 16:27:04 -08004226 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4227 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 }
4229 }
4230
Joe Perches63c3a662011-04-26 08:12:10 +00004231 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004232 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004233
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004234 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
4236 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004237 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004238 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4239 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 u32 val = tr32(0x7d00);
4241
4242 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4243 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004244 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004245 int err;
4246
4247 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004249 if (!err)
4250 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 }
4253
Michael Chanbbadf502006-04-06 21:46:34 -07004254 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4255
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004256 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 return 0;
4259}
4260
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004261static void tg3_power_down(struct tg3 *tp)
4262{
Joe Perches63c3a662011-04-26 08:12:10 +00004263 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004264 pci_set_power_state(tp->pdev, PCI_D3hot);
4265}
4266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4268{
4269 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4270 case MII_TG3_AUX_STAT_10HALF:
4271 *speed = SPEED_10;
4272 *duplex = DUPLEX_HALF;
4273 break;
4274
4275 case MII_TG3_AUX_STAT_10FULL:
4276 *speed = SPEED_10;
4277 *duplex = DUPLEX_FULL;
4278 break;
4279
4280 case MII_TG3_AUX_STAT_100HALF:
4281 *speed = SPEED_100;
4282 *duplex = DUPLEX_HALF;
4283 break;
4284
4285 case MII_TG3_AUX_STAT_100FULL:
4286 *speed = SPEED_100;
4287 *duplex = DUPLEX_FULL;
4288 break;
4289
4290 case MII_TG3_AUX_STAT_1000HALF:
4291 *speed = SPEED_1000;
4292 *duplex = DUPLEX_HALF;
4293 break;
4294
4295 case MII_TG3_AUX_STAT_1000FULL:
4296 *speed = SPEED_1000;
4297 *duplex = DUPLEX_FULL;
4298 break;
4299
4300 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004301 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004302 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4303 SPEED_10;
4304 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4305 DUPLEX_HALF;
4306 break;
4307 }
Matt Carlsone7405222012-02-13 15:20:16 +00004308 *speed = SPEED_UNKNOWN;
4309 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312}
4313
Matt Carlson42b64a42011-05-19 12:12:49 +00004314static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
Matt Carlson42b64a42011-05-19 12:12:49 +00004316 int err = 0;
4317 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Matt Carlson42b64a42011-05-19 12:12:49 +00004319 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004320 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004321 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Matt Carlson42b64a42011-05-19 12:12:49 +00004323 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4324 if (err)
4325 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Matt Carlson4f272092011-12-14 11:09:57 +00004327 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4328 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004329
Joe Perches41535772013-02-16 11:20:04 +00004330 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4331 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004332 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004333
Matt Carlson4f272092011-12-14 11:09:57 +00004334 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4335 if (err)
4336 goto done;
4337 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004338
Matt Carlson42b64a42011-05-19 12:12:49 +00004339 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4340 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Matt Carlson42b64a42011-05-19 12:12:49 +00004342 tw32(TG3_CPMU_EEE_MODE,
4343 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004344
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004345 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004346 if (!err) {
4347 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004348
Matt Carlsona6b68da2010-12-06 08:28:52 +00004349 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004350 /* Advertise 100-BaseTX EEE ability */
4351 if (advertise & ADVERTISED_100baseT_Full)
4352 val |= MDIO_AN_EEE_ADV_100TX;
4353 /* Advertise 1000-BaseT EEE ability */
4354 if (advertise & ADVERTISED_1000baseT_Full)
4355 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004356
4357 if (!tp->eee.eee_enabled) {
4358 val = 0;
4359 tp->eee.advertised = 0;
4360 } else {
4361 tp->eee.advertised = advertise &
4362 (ADVERTISED_100baseT_Full |
4363 ADVERTISED_1000baseT_Full);
4364 }
4365
Matt Carlson42b64a42011-05-19 12:12:49 +00004366 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004367 if (err)
4368 val = 0;
4369
Joe Perches41535772013-02-16 11:20:04 +00004370 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004371 case ASIC_REV_5717:
4372 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004373 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004374 case ASIC_REV_5719:
4375 /* If we advertised any eee advertisements above... */
4376 if (val)
4377 val = MII_TG3_DSP_TAP26_ALNOKO |
4378 MII_TG3_DSP_TAP26_RMRXSTO |
4379 MII_TG3_DSP_TAP26_OPCSINPT;
4380 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4381 /* Fall through */
4382 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004383 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004384 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4385 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4386 MII_TG3_DSP_CH34TP2_HIBW01);
4387 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004388
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004389 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004390 if (!err)
4391 err = err2;
4392 }
4393
4394done:
4395 return err;
4396}
4397
4398static void tg3_phy_copper_begin(struct tg3 *tp)
4399{
Matt Carlsond13ba512012-02-22 12:35:19 +00004400 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4401 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4402 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004403
Nithin Sujir942d1af2013-04-09 08:48:07 +00004404 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4405 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004406 adv = ADVERTISED_10baseT_Half |
4407 ADVERTISED_10baseT_Full;
4408 if (tg3_flag(tp, WOL_SPEED_100MB))
4409 adv |= ADVERTISED_100baseT_Half |
4410 ADVERTISED_100baseT_Full;
Nithin Sujir7c786062013-12-06 09:53:17 -08004411 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
4412 if (!(tp->phy_flags &
4413 TG3_PHYFLG_DISABLE_1G_HD_ADV))
4414 adv |= ADVERTISED_1000baseT_Half;
4415 adv |= ADVERTISED_1000baseT_Full;
4416 }
Matt Carlson42b64a42011-05-19 12:12:49 +00004417
Matt Carlsond13ba512012-02-22 12:35:19 +00004418 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004419 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004420 adv = tp->link_config.advertising;
4421 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4422 adv &= ~(ADVERTISED_1000baseT_Half |
4423 ADVERTISED_1000baseT_Full);
4424
4425 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004426 }
4427
Matt Carlsond13ba512012-02-22 12:35:19 +00004428 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004429
Nithin Sujir942d1af2013-04-09 08:48:07 +00004430 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4431 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4432 /* Normally during power down we want to autonegotiate
4433 * the lowest possible speed for WOL. However, to avoid
4434 * link flap, we leave it untouched.
4435 */
4436 return;
4437 }
4438
Matt Carlsond13ba512012-02-22 12:35:19 +00004439 tg3_writephy(tp, MII_BMCR,
4440 BMCR_ANENABLE | BMCR_ANRESTART);
4441 } else {
4442 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 u32 bmcr, orig_bmcr;
4444
4445 tp->link_config.active_speed = tp->link_config.speed;
4446 tp->link_config.active_duplex = tp->link_config.duplex;
4447
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004448 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4449 /* With autoneg disabled, 5715 only links up when the
4450 * advertisement register has the configured speed
4451 * enabled.
4452 */
4453 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4454 }
4455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 bmcr = 0;
4457 switch (tp->link_config.speed) {
4458 default:
4459 case SPEED_10:
4460 break;
4461
4462 case SPEED_100:
4463 bmcr |= BMCR_SPEED100;
4464 break;
4465
4466 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004467 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471 if (tp->link_config.duplex == DUPLEX_FULL)
4472 bmcr |= BMCR_FULLDPLX;
4473
4474 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4475 (bmcr != orig_bmcr)) {
4476 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4477 for (i = 0; i < 1500; i++) {
4478 u32 tmp;
4479
4480 udelay(10);
4481 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4482 tg3_readphy(tp, MII_BMSR, &tmp))
4483 continue;
4484 if (!(tmp & BMSR_LSTATUS)) {
4485 udelay(40);
4486 break;
4487 }
4488 }
4489 tg3_writephy(tp, MII_BMCR, bmcr);
4490 udelay(40);
4491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 }
4493}
4494
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004495static int tg3_phy_pull_config(struct tg3 *tp)
4496{
4497 int err;
4498 u32 val;
4499
4500 err = tg3_readphy(tp, MII_BMCR, &val);
4501 if (err)
4502 goto done;
4503
4504 if (!(val & BMCR_ANENABLE)) {
4505 tp->link_config.autoneg = AUTONEG_DISABLE;
4506 tp->link_config.advertising = 0;
4507 tg3_flag_clear(tp, PAUSE_AUTONEG);
4508
4509 err = -EIO;
4510
4511 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4512 case 0:
4513 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4514 goto done;
4515
4516 tp->link_config.speed = SPEED_10;
4517 break;
4518 case BMCR_SPEED100:
4519 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4520 goto done;
4521
4522 tp->link_config.speed = SPEED_100;
4523 break;
4524 case BMCR_SPEED1000:
4525 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4526 tp->link_config.speed = SPEED_1000;
4527 break;
4528 }
4529 /* Fall through */
4530 default:
4531 goto done;
4532 }
4533
4534 if (val & BMCR_FULLDPLX)
4535 tp->link_config.duplex = DUPLEX_FULL;
4536 else
4537 tp->link_config.duplex = DUPLEX_HALF;
4538
4539 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4540
4541 err = 0;
4542 goto done;
4543 }
4544
4545 tp->link_config.autoneg = AUTONEG_ENABLE;
4546 tp->link_config.advertising = ADVERTISED_Autoneg;
4547 tg3_flag_set(tp, PAUSE_AUTONEG);
4548
4549 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4550 u32 adv;
4551
4552 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4553 if (err)
4554 goto done;
4555
4556 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4557 tp->link_config.advertising |= adv | ADVERTISED_TP;
4558
4559 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4560 } else {
4561 tp->link_config.advertising |= ADVERTISED_FIBRE;
4562 }
4563
4564 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4565 u32 adv;
4566
4567 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4568 err = tg3_readphy(tp, MII_CTRL1000, &val);
4569 if (err)
4570 goto done;
4571
4572 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4573 } else {
4574 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4575 if (err)
4576 goto done;
4577
4578 adv = tg3_decode_flowctrl_1000X(val);
4579 tp->link_config.flowctrl = adv;
4580
4581 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4582 adv = mii_adv_to_ethtool_adv_x(val);
4583 }
4584
4585 tp->link_config.advertising |= adv;
4586 }
4587
4588done:
4589 return err;
4590}
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592static int tg3_init_5401phy_dsp(struct tg3 *tp)
4593{
4594 int err;
4595
4596 /* Turn off tap power management. */
4597 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004598 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004600 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4601 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4602 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4603 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4604 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 udelay(40);
4607
4608 return err;
4609}
4610
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004611static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4612{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004613 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004614
4615 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4616 return true;
4617
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004618 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004619
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004620 if (tp->eee.eee_enabled) {
4621 if (tp->eee.advertised != eee.advertised ||
4622 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4623 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4624 return false;
4625 } else {
4626 /* EEE is disabled but we're advertising */
4627 if (eee.advertised)
4628 return false;
4629 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004630
4631 return true;
4632}
4633
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004634static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004636 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004637
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004638 advertising = tp->link_config.advertising;
4639 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004641 advmsk = ADVERTISE_ALL;
4642 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004643 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004644 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004647 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4648 return false;
4649
4650 if ((*lcladv & advmsk) != tgtadv)
4651 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004652
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004653 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 u32 tg3_ctrl;
4655
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004656 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004657
Matt Carlson221c5632011-06-13 13:39:01 +00004658 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004659 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Matt Carlson3198e072012-02-13 15:20:10 +00004661 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004662 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4663 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004664 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4665 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4666 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4667 } else {
4668 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4669 }
4670
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004671 if (tg3_ctrl != tgtadv)
4672 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004674
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004675 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004676}
4677
Matt Carlson859edb22011-12-08 14:40:16 +00004678static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4679{
4680 u32 lpeth = 0;
4681
4682 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4683 u32 val;
4684
4685 if (tg3_readphy(tp, MII_STAT1000, &val))
4686 return false;
4687
4688 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4689 }
4690
4691 if (tg3_readphy(tp, MII_LPA, rmtadv))
4692 return false;
4693
4694 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4695 tp->link_config.rmt_adv = lpeth;
4696
4697 return true;
4698}
4699
Joe Perches953c96e2013-04-09 10:18:14 +00004700static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004701{
4702 if (curr_link_up != tp->link_up) {
4703 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004704 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004705 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004706 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004707 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4708 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4709 }
4710
4711 tg3_link_report(tp);
4712 return true;
4713 }
4714
4715 return false;
4716}
4717
Michael Chan3310e242013-04-09 08:48:05 +00004718static void tg3_clear_mac_status(struct tg3 *tp)
4719{
4720 tw32(MAC_EVENT, 0);
4721
4722 tw32_f(MAC_STATUS,
4723 MAC_STATUS_SYNC_CHANGED |
4724 MAC_STATUS_CFG_CHANGED |
4725 MAC_STATUS_MI_COMPLETION |
4726 MAC_STATUS_LNKSTATE_CHANGED);
4727 udelay(40);
4728}
4729
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004730static void tg3_setup_eee(struct tg3 *tp)
4731{
4732 u32 val;
4733
4734 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4735 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4736 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4737 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4738
4739 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4740
4741 tw32_f(TG3_CPMU_EEE_CTRL,
4742 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4743
4744 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4745 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4746 TG3_CPMU_EEEMD_LPI_IN_RX |
4747 TG3_CPMU_EEEMD_EEE_ENABLE;
4748
4749 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4750 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4751
4752 if (tg3_flag(tp, ENABLE_APE))
4753 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4754
4755 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4756
4757 tw32_f(TG3_CPMU_EEE_DBTMR1,
4758 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4759 (tp->eee.tx_lpi_timer & 0xffff));
4760
4761 tw32_f(TG3_CPMU_EEE_DBTMR2,
4762 TG3_CPMU_DBTMR2_APE_TX_2047US |
4763 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4764}
4765
Joe Perches953c96e2013-04-09 10:18:14 +00004766static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767{
Joe Perches953c96e2013-04-09 10:18:14 +00004768 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004769 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004770 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 u16 current_speed;
4772 u8 current_duplex;
4773 int i, err;
4774
Michael Chan3310e242013-04-09 08:48:05 +00004775 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Matt Carlson8ef21422008-05-02 16:47:53 -07004777 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4778 tw32_f(MAC_MI_MODE,
4779 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4780 udelay(80);
4781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004783 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
4785 /* Some third-party PHYs need to be reset on link going
4786 * down.
4787 */
Joe Perches41535772013-02-16 11:20:04 +00004788 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4789 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4790 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004791 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 tg3_readphy(tp, MII_BMSR, &bmsr);
4793 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4794 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004795 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 }
4797 if (force_reset)
4798 tg3_phy_reset(tp);
4799
Matt Carlson79eb6902010-02-17 15:17:03 +00004800 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 tg3_readphy(tp, MII_BMSR, &bmsr);
4802 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004803 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 bmsr = 0;
4805
4806 if (!(bmsr & BMSR_LSTATUS)) {
4807 err = tg3_init_5401phy_dsp(tp);
4808 if (err)
4809 return err;
4810
4811 tg3_readphy(tp, MII_BMSR, &bmsr);
4812 for (i = 0; i < 1000; i++) {
4813 udelay(10);
4814 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4815 (bmsr & BMSR_LSTATUS)) {
4816 udelay(40);
4817 break;
4818 }
4819 }
4820
Matt Carlson79eb6902010-02-17 15:17:03 +00004821 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4822 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 !(bmsr & BMSR_LSTATUS) &&
4824 tp->link_config.active_speed == SPEED_1000) {
4825 err = tg3_phy_reset(tp);
4826 if (!err)
4827 err = tg3_init_5401phy_dsp(tp);
4828 if (err)
4829 return err;
4830 }
4831 }
Joe Perches41535772013-02-16 11:20:04 +00004832 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4833 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 /* 5701 {A0,B0} CRC bug workaround */
4835 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004836 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4837 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4838 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 }
4840
4841 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004842 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4843 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004845 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004847 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4849
Joe Perches41535772013-02-16 11:20:04 +00004850 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4851 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4853 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4854 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4855 else
4856 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4857 }
4858
Joe Perches953c96e2013-04-09 10:18:14 +00004859 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004860 current_speed = SPEED_UNKNOWN;
4861 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004862 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004863 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004865 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004866 err = tg3_phy_auxctl_read(tp,
4867 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4868 &val);
4869 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004870 tg3_phy_auxctl_write(tp,
4871 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4872 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 goto relink;
4874 }
4875 }
4876
4877 bmsr = 0;
4878 for (i = 0; i < 100; i++) {
4879 tg3_readphy(tp, MII_BMSR, &bmsr);
4880 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4881 (bmsr & BMSR_LSTATUS))
4882 break;
4883 udelay(40);
4884 }
4885
4886 if (bmsr & BMSR_LSTATUS) {
4887 u32 aux_stat, bmcr;
4888
4889 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4890 for (i = 0; i < 2000; i++) {
4891 udelay(10);
4892 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4893 aux_stat)
4894 break;
4895 }
4896
4897 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4898 &current_speed,
4899 &current_duplex);
4900
4901 bmcr = 0;
4902 for (i = 0; i < 200; i++) {
4903 tg3_readphy(tp, MII_BMCR, &bmcr);
4904 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4905 continue;
4906 if (bmcr && bmcr != 0x7fff)
4907 break;
4908 udelay(10);
4909 }
4910
Matt Carlsonef167e22007-12-20 20:10:01 -08004911 lcl_adv = 0;
4912 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
Matt Carlsonef167e22007-12-20 20:10:01 -08004914 tp->link_config.active_speed = current_speed;
4915 tp->link_config.active_duplex = current_duplex;
4916
4917 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004918 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4919
Matt Carlsonef167e22007-12-20 20:10:01 -08004920 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004921 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004922 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004923 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004924 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004925
4926 /* EEE settings changes take effect only after a phy
4927 * reset. If we have skipped a reset due to Link Flap
4928 * Avoidance being enabled, do it now.
4929 */
4930 if (!eee_config_ok &&
4931 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004932 !force_reset) {
4933 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004934 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 } else {
4937 if (!(bmcr & BMCR_ANENABLE) &&
4938 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004939 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004940 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 }
4942 }
4943
Joe Perches953c96e2013-04-09 10:18:14 +00004944 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004945 tp->link_config.active_duplex == DUPLEX_FULL) {
4946 u32 reg, bit;
4947
4948 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4949 reg = MII_TG3_FET_GEN_STAT;
4950 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4951 } else {
4952 reg = MII_TG3_EXT_STAT;
4953 bit = MII_TG3_EXT_STAT_MDIX;
4954 }
4955
4956 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4957 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4958
Matt Carlsonef167e22007-12-20 20:10:01 -08004959 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004964 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 tg3_phy_copper_begin(tp);
4966
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004967 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004968 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004969 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4970 current_speed = SPEED_1000;
4971 current_duplex = DUPLEX_FULL;
4972 tp->link_config.active_speed = current_speed;
4973 tp->link_config.active_duplex = current_duplex;
4974 }
4975
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004976 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004977 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4978 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004979 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
4981
4982 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004983 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 if (tp->link_config.active_speed == SPEED_100 ||
4985 tp->link_config.active_speed == SPEED_10)
4986 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4987 else
4988 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004989 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004990 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4991 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4993
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004994 /* In order for the 5750 core in BCM4785 chip to work properly
4995 * in RGMII mode, the Led Control Register must be set up.
4996 */
4997 if (tg3_flag(tp, RGMII_MODE)) {
4998 u32 led_ctrl = tr32(MAC_LED_CTRL);
4999 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
5000
5001 if (tp->link_config.active_speed == SPEED_10)
5002 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
5003 else if (tp->link_config.active_speed == SPEED_100)
5004 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5005 LED_CTRL_100MBPS_ON);
5006 else if (tp->link_config.active_speed == SPEED_1000)
5007 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5008 LED_CTRL_1000MBPS_ON);
5009
5010 tw32(MAC_LED_CTRL, led_ctrl);
5011 udelay(40);
5012 }
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5015 if (tp->link_config.active_duplex == DUPLEX_HALF)
5016 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5017
Joe Perches41535772013-02-16 11:20:04 +00005018 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005019 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005020 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005022 else
5023 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 }
5025
5026 /* ??? Without this setting Netgear GA302T PHY does not
5027 * ??? send/receive packets...
5028 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005029 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005030 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5032 tw32_f(MAC_MI_MODE, tp->mi_mode);
5033 udelay(80);
5034 }
5035
5036 tw32_f(MAC_MODE, tp->mac_mode);
5037 udelay(40);
5038
Matt Carlson52b02d02010-10-14 10:37:41 +00005039 tg3_phy_eee_adjust(tp, current_link_up);
5040
Joe Perches63c3a662011-04-26 08:12:10 +00005041 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 /* Polled via timer. */
5043 tw32_f(MAC_EVENT, 0);
5044 } else {
5045 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5046 }
5047 udelay(40);
5048
Joe Perches41535772013-02-16 11:20:04 +00005049 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005050 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005052 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 udelay(120);
5054 tw32_f(MAC_STATUS,
5055 (MAC_STATUS_SYNC_CHANGED |
5056 MAC_STATUS_CFG_CHANGED));
5057 udelay(40);
5058 tg3_write_mem(tp,
5059 NIC_SRAM_FIRMWARE_MBOX,
5060 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5061 }
5062
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005063 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005064 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005065 if (tp->link_config.active_speed == SPEED_100 ||
5066 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005067 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5068 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005069 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005070 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5071 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005072 }
5073
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005074 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
5076 return 0;
5077}
5078
5079struct tg3_fiber_aneginfo {
5080 int state;
5081#define ANEG_STATE_UNKNOWN 0
5082#define ANEG_STATE_AN_ENABLE 1
5083#define ANEG_STATE_RESTART_INIT 2
5084#define ANEG_STATE_RESTART 3
5085#define ANEG_STATE_DISABLE_LINK_OK 4
5086#define ANEG_STATE_ABILITY_DETECT_INIT 5
5087#define ANEG_STATE_ABILITY_DETECT 6
5088#define ANEG_STATE_ACK_DETECT_INIT 7
5089#define ANEG_STATE_ACK_DETECT 8
5090#define ANEG_STATE_COMPLETE_ACK_INIT 9
5091#define ANEG_STATE_COMPLETE_ACK 10
5092#define ANEG_STATE_IDLE_DETECT_INIT 11
5093#define ANEG_STATE_IDLE_DETECT 12
5094#define ANEG_STATE_LINK_OK 13
5095#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5096#define ANEG_STATE_NEXT_PAGE_WAIT 15
5097
5098 u32 flags;
5099#define MR_AN_ENABLE 0x00000001
5100#define MR_RESTART_AN 0x00000002
5101#define MR_AN_COMPLETE 0x00000004
5102#define MR_PAGE_RX 0x00000008
5103#define MR_NP_LOADED 0x00000010
5104#define MR_TOGGLE_TX 0x00000020
5105#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5106#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5107#define MR_LP_ADV_SYM_PAUSE 0x00000100
5108#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5109#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5110#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5111#define MR_LP_ADV_NEXT_PAGE 0x00001000
5112#define MR_TOGGLE_RX 0x00002000
5113#define MR_NP_RX 0x00004000
5114
5115#define MR_LINK_OK 0x80000000
5116
5117 unsigned long link_time, cur_time;
5118
5119 u32 ability_match_cfg;
5120 int ability_match_count;
5121
5122 char ability_match, idle_match, ack_match;
5123
5124 u32 txconfig, rxconfig;
5125#define ANEG_CFG_NP 0x00000080
5126#define ANEG_CFG_ACK 0x00000040
5127#define ANEG_CFG_RF2 0x00000020
5128#define ANEG_CFG_RF1 0x00000010
5129#define ANEG_CFG_PS2 0x00000001
5130#define ANEG_CFG_PS1 0x00008000
5131#define ANEG_CFG_HD 0x00004000
5132#define ANEG_CFG_FD 0x00002000
5133#define ANEG_CFG_INVAL 0x00001f06
5134
5135};
5136#define ANEG_OK 0
5137#define ANEG_DONE 1
5138#define ANEG_TIMER_ENAB 2
5139#define ANEG_FAILED -1
5140
5141#define ANEG_STATE_SETTLE_TIME 10000
5142
5143static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5144 struct tg3_fiber_aneginfo *ap)
5145{
Matt Carlson5be73b42007-12-20 20:09:29 -08005146 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 unsigned long delta;
5148 u32 rx_cfg_reg;
5149 int ret;
5150
5151 if (ap->state == ANEG_STATE_UNKNOWN) {
5152 ap->rxconfig = 0;
5153 ap->link_time = 0;
5154 ap->cur_time = 0;
5155 ap->ability_match_cfg = 0;
5156 ap->ability_match_count = 0;
5157 ap->ability_match = 0;
5158 ap->idle_match = 0;
5159 ap->ack_match = 0;
5160 }
5161 ap->cur_time++;
5162
5163 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5164 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5165
5166 if (rx_cfg_reg != ap->ability_match_cfg) {
5167 ap->ability_match_cfg = rx_cfg_reg;
5168 ap->ability_match = 0;
5169 ap->ability_match_count = 0;
5170 } else {
5171 if (++ap->ability_match_count > 1) {
5172 ap->ability_match = 1;
5173 ap->ability_match_cfg = rx_cfg_reg;
5174 }
5175 }
5176 if (rx_cfg_reg & ANEG_CFG_ACK)
5177 ap->ack_match = 1;
5178 else
5179 ap->ack_match = 0;
5180
5181 ap->idle_match = 0;
5182 } else {
5183 ap->idle_match = 1;
5184 ap->ability_match_cfg = 0;
5185 ap->ability_match_count = 0;
5186 ap->ability_match = 0;
5187 ap->ack_match = 0;
5188
5189 rx_cfg_reg = 0;
5190 }
5191
5192 ap->rxconfig = rx_cfg_reg;
5193 ret = ANEG_OK;
5194
Matt Carlson33f401a2010-04-05 10:19:27 +00005195 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 case ANEG_STATE_UNKNOWN:
5197 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5198 ap->state = ANEG_STATE_AN_ENABLE;
5199
5200 /* fallthru */
5201 case ANEG_STATE_AN_ENABLE:
5202 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5203 if (ap->flags & MR_AN_ENABLE) {
5204 ap->link_time = 0;
5205 ap->cur_time = 0;
5206 ap->ability_match_cfg = 0;
5207 ap->ability_match_count = 0;
5208 ap->ability_match = 0;
5209 ap->idle_match = 0;
5210 ap->ack_match = 0;
5211
5212 ap->state = ANEG_STATE_RESTART_INIT;
5213 } else {
5214 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5215 }
5216 break;
5217
5218 case ANEG_STATE_RESTART_INIT:
5219 ap->link_time = ap->cur_time;
5220 ap->flags &= ~(MR_NP_LOADED);
5221 ap->txconfig = 0;
5222 tw32(MAC_TX_AUTO_NEG, 0);
5223 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5224 tw32_f(MAC_MODE, tp->mac_mode);
5225 udelay(40);
5226
5227 ret = ANEG_TIMER_ENAB;
5228 ap->state = ANEG_STATE_RESTART;
5229
5230 /* fallthru */
5231 case ANEG_STATE_RESTART:
5232 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005233 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005235 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 break;
5238
5239 case ANEG_STATE_DISABLE_LINK_OK:
5240 ret = ANEG_DONE;
5241 break;
5242
5243 case ANEG_STATE_ABILITY_DETECT_INIT:
5244 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005245 ap->txconfig = ANEG_CFG_FD;
5246 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5247 if (flowctrl & ADVERTISE_1000XPAUSE)
5248 ap->txconfig |= ANEG_CFG_PS1;
5249 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5250 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5252 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5253 tw32_f(MAC_MODE, tp->mac_mode);
5254 udelay(40);
5255
5256 ap->state = ANEG_STATE_ABILITY_DETECT;
5257 break;
5258
5259 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005260 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 break;
5263
5264 case ANEG_STATE_ACK_DETECT_INIT:
5265 ap->txconfig |= ANEG_CFG_ACK;
5266 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5267 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5268 tw32_f(MAC_MODE, tp->mac_mode);
5269 udelay(40);
5270
5271 ap->state = ANEG_STATE_ACK_DETECT;
5272
5273 /* fallthru */
5274 case ANEG_STATE_ACK_DETECT:
5275 if (ap->ack_match != 0) {
5276 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5277 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5278 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5279 } else {
5280 ap->state = ANEG_STATE_AN_ENABLE;
5281 }
5282 } else if (ap->ability_match != 0 &&
5283 ap->rxconfig == 0) {
5284 ap->state = ANEG_STATE_AN_ENABLE;
5285 }
5286 break;
5287
5288 case ANEG_STATE_COMPLETE_ACK_INIT:
5289 if (ap->rxconfig & ANEG_CFG_INVAL) {
5290 ret = ANEG_FAILED;
5291 break;
5292 }
5293 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5294 MR_LP_ADV_HALF_DUPLEX |
5295 MR_LP_ADV_SYM_PAUSE |
5296 MR_LP_ADV_ASYM_PAUSE |
5297 MR_LP_ADV_REMOTE_FAULT1 |
5298 MR_LP_ADV_REMOTE_FAULT2 |
5299 MR_LP_ADV_NEXT_PAGE |
5300 MR_TOGGLE_RX |
5301 MR_NP_RX);
5302 if (ap->rxconfig & ANEG_CFG_FD)
5303 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5304 if (ap->rxconfig & ANEG_CFG_HD)
5305 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5306 if (ap->rxconfig & ANEG_CFG_PS1)
5307 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5308 if (ap->rxconfig & ANEG_CFG_PS2)
5309 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5310 if (ap->rxconfig & ANEG_CFG_RF1)
5311 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5312 if (ap->rxconfig & ANEG_CFG_RF2)
5313 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5314 if (ap->rxconfig & ANEG_CFG_NP)
5315 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5316
5317 ap->link_time = ap->cur_time;
5318
5319 ap->flags ^= (MR_TOGGLE_TX);
5320 if (ap->rxconfig & 0x0008)
5321 ap->flags |= MR_TOGGLE_RX;
5322 if (ap->rxconfig & ANEG_CFG_NP)
5323 ap->flags |= MR_NP_RX;
5324 ap->flags |= MR_PAGE_RX;
5325
5326 ap->state = ANEG_STATE_COMPLETE_ACK;
5327 ret = ANEG_TIMER_ENAB;
5328 break;
5329
5330 case ANEG_STATE_COMPLETE_ACK:
5331 if (ap->ability_match != 0 &&
5332 ap->rxconfig == 0) {
5333 ap->state = ANEG_STATE_AN_ENABLE;
5334 break;
5335 }
5336 delta = ap->cur_time - ap->link_time;
5337 if (delta > ANEG_STATE_SETTLE_TIME) {
5338 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5339 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5340 } else {
5341 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5342 !(ap->flags & MR_NP_RX)) {
5343 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5344 } else {
5345 ret = ANEG_FAILED;
5346 }
5347 }
5348 }
5349 break;
5350
5351 case ANEG_STATE_IDLE_DETECT_INIT:
5352 ap->link_time = ap->cur_time;
5353 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5354 tw32_f(MAC_MODE, tp->mac_mode);
5355 udelay(40);
5356
5357 ap->state = ANEG_STATE_IDLE_DETECT;
5358 ret = ANEG_TIMER_ENAB;
5359 break;
5360
5361 case ANEG_STATE_IDLE_DETECT:
5362 if (ap->ability_match != 0 &&
5363 ap->rxconfig == 0) {
5364 ap->state = ANEG_STATE_AN_ENABLE;
5365 break;
5366 }
5367 delta = ap->cur_time - ap->link_time;
5368 if (delta > ANEG_STATE_SETTLE_TIME) {
5369 /* XXX another gem from the Broadcom driver :( */
5370 ap->state = ANEG_STATE_LINK_OK;
5371 }
5372 break;
5373
5374 case ANEG_STATE_LINK_OK:
5375 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5376 ret = ANEG_DONE;
5377 break;
5378
5379 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5380 /* ??? unimplemented */
5381 break;
5382
5383 case ANEG_STATE_NEXT_PAGE_WAIT:
5384 /* ??? unimplemented */
5385 break;
5386
5387 default:
5388 ret = ANEG_FAILED;
5389 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391
5392 return ret;
5393}
5394
Matt Carlson5be73b42007-12-20 20:09:29 -08005395static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
5397 int res = 0;
5398 struct tg3_fiber_aneginfo aninfo;
5399 int status = ANEG_FAILED;
5400 unsigned int tick;
5401 u32 tmp;
5402
5403 tw32_f(MAC_TX_AUTO_NEG, 0);
5404
5405 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5406 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5407 udelay(40);
5408
5409 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5410 udelay(40);
5411
5412 memset(&aninfo, 0, sizeof(aninfo));
5413 aninfo.flags |= MR_AN_ENABLE;
5414 aninfo.state = ANEG_STATE_UNKNOWN;
5415 aninfo.cur_time = 0;
5416 tick = 0;
5417 while (++tick < 195000) {
5418 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5419 if (status == ANEG_DONE || status == ANEG_FAILED)
5420 break;
5421
5422 udelay(1);
5423 }
5424
5425 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5426 tw32_f(MAC_MODE, tp->mac_mode);
5427 udelay(40);
5428
Matt Carlson5be73b42007-12-20 20:09:29 -08005429 *txflags = aninfo.txconfig;
5430 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
5432 if (status == ANEG_DONE &&
5433 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5434 MR_LP_ADV_FULL_DUPLEX)))
5435 res = 1;
5436
5437 return res;
5438}
5439
5440static void tg3_init_bcm8002(struct tg3 *tp)
5441{
5442 u32 mac_status = tr32(MAC_STATUS);
5443 int i;
5444
5445 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005446 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 !(mac_status & MAC_STATUS_PCS_SYNCED))
5448 return;
5449
5450 /* Set PLL lock range. */
5451 tg3_writephy(tp, 0x16, 0x8007);
5452
5453 /* SW reset */
5454 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5455
5456 /* Wait for reset to complete. */
5457 /* XXX schedule_timeout() ... */
5458 for (i = 0; i < 500; i++)
5459 udelay(10);
5460
5461 /* Config mode; select PMA/Ch 1 regs. */
5462 tg3_writephy(tp, 0x10, 0x8411);
5463
5464 /* Enable auto-lock and comdet, select txclk for tx. */
5465 tg3_writephy(tp, 0x11, 0x0a10);
5466
5467 tg3_writephy(tp, 0x18, 0x00a0);
5468 tg3_writephy(tp, 0x16, 0x41ff);
5469
5470 /* Assert and deassert POR. */
5471 tg3_writephy(tp, 0x13, 0x0400);
5472 udelay(40);
5473 tg3_writephy(tp, 0x13, 0x0000);
5474
5475 tg3_writephy(tp, 0x11, 0x0a50);
5476 udelay(40);
5477 tg3_writephy(tp, 0x11, 0x0a10);
5478
5479 /* Wait for signal to stabilize */
5480 /* XXX schedule_timeout() ... */
5481 for (i = 0; i < 15000; i++)
5482 udelay(10);
5483
5484 /* Deselect the channel register so we can read the PHYID
5485 * later.
5486 */
5487 tg3_writephy(tp, 0x10, 0x8011);
5488}
5489
Joe Perches953c96e2013-04-09 10:18:14 +00005490static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005492 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005493 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 u32 sg_dig_ctrl, sg_dig_status;
5495 u32 serdes_cfg, expected_sg_dig_ctrl;
5496 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
5498 serdes_cfg = 0;
5499 expected_sg_dig_ctrl = 0;
5500 workaround = 0;
5501 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005502 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
Joe Perches41535772013-02-16 11:20:04 +00005504 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5505 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 workaround = 1;
5507 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5508 port_a = 0;
5509
5510 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5511 /* preserve bits 20-23 for voltage regulator */
5512 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5513 }
5514
5515 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5516
5517 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005518 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 if (workaround) {
5520 u32 val = serdes_cfg;
5521
5522 if (port_a)
5523 val |= 0xc010000;
5524 else
5525 val |= 0x4010000;
5526 tw32_f(MAC_SERDES_CFG, val);
5527 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005528
5529 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 }
5531 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5532 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005533 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 }
5535 goto out;
5536 }
5537
5538 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005539 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Matt Carlson82cd3d12007-12-20 20:09:00 -08005541 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5542 if (flowctrl & ADVERTISE_1000XPAUSE)
5543 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5544 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5545 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
5547 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005548 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005549 tp->serdes_counter &&
5550 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5551 MAC_STATUS_RCVD_CFG)) ==
5552 MAC_STATUS_PCS_SYNCED)) {
5553 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005554 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005555 goto out;
5556 }
5557restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 if (workaround)
5559 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005560 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 udelay(5);
5562 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5563
Michael Chan3d3ebe72006-09-27 15:59:15 -07005564 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005565 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5567 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005568 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 mac_status = tr32(MAC_STATUS);
5570
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005571 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005573 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Matt Carlson82cd3d12007-12-20 20:09:00 -08005575 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5576 local_adv |= ADVERTISE_1000XPAUSE;
5577 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5578 local_adv |= ADVERTISE_1000XPSE_ASYM;
5579
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005580 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005581 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005582 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005583 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
Matt Carlson859edb22011-12-08 14:40:16 +00005585 tp->link_config.rmt_adv =
5586 mii_adv_to_ethtool_adv_x(remote_adv);
5587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005589 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005590 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005591 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005592 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005593 if (tp->serdes_counter)
5594 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 else {
5596 if (workaround) {
5597 u32 val = serdes_cfg;
5598
5599 if (port_a)
5600 val |= 0xc010000;
5601 else
5602 val |= 0x4010000;
5603
5604 tw32_f(MAC_SERDES_CFG, val);
5605 }
5606
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005607 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 udelay(40);
5609
5610 /* Link parallel detection - link is up */
5611 /* only if we have PCS_SYNC and not */
5612 /* receiving config code words */
5613 mac_status = tr32(MAC_STATUS);
5614 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5615 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5616 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005617 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005618 tp->phy_flags |=
5619 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005620 tp->serdes_counter =
5621 SERDES_PARALLEL_DET_TIMEOUT;
5622 } else
5623 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 }
5625 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005626 } else {
5627 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005628 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 }
5630
5631out:
5632 return current_link_up;
5633}
5634
Joe Perches953c96e2013-04-09 10:18:14 +00005635static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636{
Joe Perches953c96e2013-04-09 10:18:14 +00005637 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005639 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005643 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005645
Matt Carlson5be73b42007-12-20 20:09:29 -08005646 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5647 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
Matt Carlson5be73b42007-12-20 20:09:29 -08005649 if (txflags & ANEG_CFG_PS1)
5650 local_adv |= ADVERTISE_1000XPAUSE;
5651 if (txflags & ANEG_CFG_PS2)
5652 local_adv |= ADVERTISE_1000XPSE_ASYM;
5653
5654 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5655 remote_adv |= LPA_1000XPAUSE;
5656 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5657 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
Matt Carlson859edb22011-12-08 14:40:16 +00005659 tp->link_config.rmt_adv =
5660 mii_adv_to_ethtool_adv_x(remote_adv);
5661
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 tg3_setup_flow_control(tp, local_adv, remote_adv);
5663
Joe Perches953c96e2013-04-09 10:18:14 +00005664 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 }
5666 for (i = 0; i < 30; i++) {
5667 udelay(20);
5668 tw32_f(MAC_STATUS,
5669 (MAC_STATUS_SYNC_CHANGED |
5670 MAC_STATUS_CFG_CHANGED));
5671 udelay(40);
5672 if ((tr32(MAC_STATUS) &
5673 (MAC_STATUS_SYNC_CHANGED |
5674 MAC_STATUS_CFG_CHANGED)) == 0)
5675 break;
5676 }
5677
5678 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005679 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5681 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005682 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005684 tg3_setup_flow_control(tp, 0, 0);
5685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005687 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5690 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005691
5692 tw32_f(MAC_MODE, tp->mac_mode);
5693 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 }
5695
5696out:
5697 return current_link_up;
5698}
5699
Joe Perches953c96e2013-04-09 10:18:14 +00005700static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701{
5702 u32 orig_pause_cfg;
5703 u16 orig_active_speed;
5704 u8 orig_active_duplex;
5705 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005706 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 int i;
5708
Matt Carlson8d018622007-12-20 20:05:44 -08005709 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 orig_active_speed = tp->link_config.active_speed;
5711 orig_active_duplex = tp->link_config.active_duplex;
5712
Joe Perches63c3a662011-04-26 08:12:10 +00005713 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005714 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005715 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 mac_status = tr32(MAC_STATUS);
5717 mac_status &= (MAC_STATUS_PCS_SYNCED |
5718 MAC_STATUS_SIGNAL_DET |
5719 MAC_STATUS_CFG_CHANGED |
5720 MAC_STATUS_RCVD_CFG);
5721 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5722 MAC_STATUS_SIGNAL_DET)) {
5723 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5724 MAC_STATUS_CFG_CHANGED));
5725 return 0;
5726 }
5727 }
5728
5729 tw32_f(MAC_TX_AUTO_NEG, 0);
5730
5731 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5732 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5733 tw32_f(MAC_MODE, tp->mac_mode);
5734 udelay(40);
5735
Matt Carlson79eb6902010-02-17 15:17:03 +00005736 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 tg3_init_bcm8002(tp);
5738
5739 /* Enable link change event even when serdes polling. */
5740 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5741 udelay(40);
5742
Joe Perches953c96e2013-04-09 10:18:14 +00005743 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005744 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 mac_status = tr32(MAC_STATUS);
5746
Joe Perches63c3a662011-04-26 08:12:10 +00005747 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5749 else
5750 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5751
Matt Carlson898a56f2009-08-28 14:02:40 +00005752 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005754 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
5756 for (i = 0; i < 100; i++) {
5757 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5758 MAC_STATUS_CFG_CHANGED));
5759 udelay(5);
5760 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005761 MAC_STATUS_CFG_CHANGED |
5762 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 break;
5764 }
5765
5766 mac_status = tr32(MAC_STATUS);
5767 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005768 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005769 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5770 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 tw32_f(MAC_MODE, (tp->mac_mode |
5772 MAC_MODE_SEND_CONFIGS));
5773 udelay(1);
5774 tw32_f(MAC_MODE, tp->mac_mode);
5775 }
5776 }
5777
Joe Perches953c96e2013-04-09 10:18:14 +00005778 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 tp->link_config.active_speed = SPEED_1000;
5780 tp->link_config.active_duplex = DUPLEX_FULL;
5781 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5782 LED_CTRL_LNKLED_OVERRIDE |
5783 LED_CTRL_1000MBPS_ON));
5784 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005785 tp->link_config.active_speed = SPEED_UNKNOWN;
5786 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5788 LED_CTRL_LNKLED_OVERRIDE |
5789 LED_CTRL_TRAFFIC_OVERRIDE));
5790 }
5791
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005792 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005793 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 if (orig_pause_cfg != now_pause_cfg ||
5795 orig_active_speed != tp->link_config.active_speed ||
5796 orig_active_duplex != tp->link_config.active_duplex)
5797 tg3_link_report(tp);
5798 }
5799
5800 return 0;
5801}
5802
Joe Perches953c96e2013-04-09 10:18:14 +00005803static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005804{
Joe Perches953c96e2013-04-09 10:18:14 +00005805 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005806 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005807 u16 current_speed = SPEED_UNKNOWN;
5808 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005809 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005810 u32 local_adv, remote_adv, sgsr;
5811
5812 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5813 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5814 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5815 (sgsr & SERDES_TG3_SGMII_MODE)) {
5816
5817 if (force_reset)
5818 tg3_phy_reset(tp);
5819
5820 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5821
5822 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5823 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5824 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005825 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005826 if (sgsr & SERDES_TG3_SPEED_1000) {
5827 current_speed = SPEED_1000;
5828 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5829 } else if (sgsr & SERDES_TG3_SPEED_100) {
5830 current_speed = SPEED_100;
5831 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5832 } else {
5833 current_speed = SPEED_10;
5834 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5835 }
5836
5837 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5838 current_duplex = DUPLEX_FULL;
5839 else
5840 current_duplex = DUPLEX_HALF;
5841 }
5842
5843 tw32_f(MAC_MODE, tp->mac_mode);
5844 udelay(40);
5845
5846 tg3_clear_mac_status(tp);
5847
5848 goto fiber_setup_done;
5849 }
Michael Chan747e8f82005-07-25 12:33:22 -07005850
5851 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5852 tw32_f(MAC_MODE, tp->mac_mode);
5853 udelay(40);
5854
Michael Chan3310e242013-04-09 08:48:05 +00005855 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005856
5857 if (force_reset)
5858 tg3_phy_reset(tp);
5859
Matt Carlson859edb22011-12-08 14:40:16 +00005860 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005861
5862 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5863 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005864 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005865 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5866 bmsr |= BMSR_LSTATUS;
5867 else
5868 bmsr &= ~BMSR_LSTATUS;
5869 }
Michael Chan747e8f82005-07-25 12:33:22 -07005870
5871 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5872
5873 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005874 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005875 /* do nothing, just check for link up at the end */
5876 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005877 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005878
5879 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005880 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5881 ADVERTISE_1000XPAUSE |
5882 ADVERTISE_1000XPSE_ASYM |
5883 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005884
Matt Carlson28011cf2011-11-16 18:36:59 -05005885 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005886 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005887
Matt Carlson28011cf2011-11-16 18:36:59 -05005888 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5889 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005890 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5891 tg3_writephy(tp, MII_BMCR, bmcr);
5892
5893 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005894 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005895 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005896
5897 return err;
5898 }
5899 } else {
5900 u32 new_bmcr;
5901
5902 bmcr &= ~BMCR_SPEED1000;
5903 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5904
5905 if (tp->link_config.duplex == DUPLEX_FULL)
5906 new_bmcr |= BMCR_FULLDPLX;
5907
5908 if (new_bmcr != bmcr) {
5909 /* BMCR_SPEED1000 is a reserved bit that needs
5910 * to be set on write.
5911 */
5912 new_bmcr |= BMCR_SPEED1000;
5913
5914 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005915 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005916 u32 adv;
5917
5918 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5919 adv &= ~(ADVERTISE_1000XFULL |
5920 ADVERTISE_1000XHALF |
5921 ADVERTISE_SLCT);
5922 tg3_writephy(tp, MII_ADVERTISE, adv);
5923 tg3_writephy(tp, MII_BMCR, bmcr |
5924 BMCR_ANRESTART |
5925 BMCR_ANENABLE);
5926 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005927 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005928 }
5929 tg3_writephy(tp, MII_BMCR, new_bmcr);
5930 bmcr = new_bmcr;
5931 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5932 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005933 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005934 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5935 bmsr |= BMSR_LSTATUS;
5936 else
5937 bmsr &= ~BMSR_LSTATUS;
5938 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005939 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005940 }
5941 }
5942
5943 if (bmsr & BMSR_LSTATUS) {
5944 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005945 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005946 if (bmcr & BMCR_FULLDPLX)
5947 current_duplex = DUPLEX_FULL;
5948 else
5949 current_duplex = DUPLEX_HALF;
5950
Matt Carlsonef167e22007-12-20 20:10:01 -08005951 local_adv = 0;
5952 remote_adv = 0;
5953
Michael Chan747e8f82005-07-25 12:33:22 -07005954 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005955 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005956
5957 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5958 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5959 common = local_adv & remote_adv;
5960 if (common & (ADVERTISE_1000XHALF |
5961 ADVERTISE_1000XFULL)) {
5962 if (common & ADVERTISE_1000XFULL)
5963 current_duplex = DUPLEX_FULL;
5964 else
5965 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005966
5967 tp->link_config.rmt_adv =
5968 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005969 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005970 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005971 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005972 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005973 }
Michael Chan747e8f82005-07-25 12:33:22 -07005974 }
5975 }
5976
Michael Chan85730a62013-04-09 08:48:06 +00005977fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005978 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005979 tg3_setup_flow_control(tp, local_adv, remote_adv);
5980
Michael Chan747e8f82005-07-25 12:33:22 -07005981 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5982 if (tp->link_config.active_duplex == DUPLEX_HALF)
5983 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5984
5985 tw32_f(MAC_MODE, tp->mac_mode);
5986 udelay(40);
5987
5988 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5989
5990 tp->link_config.active_speed = current_speed;
5991 tp->link_config.active_duplex = current_duplex;
5992
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005993 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005994 return err;
5995}
5996
5997static void tg3_serdes_parallel_detect(struct tg3 *tp)
5998{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005999 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07006000 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07006001 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07006002 return;
6003 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006004
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006005 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07006006 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
6007 u32 bmcr;
6008
6009 tg3_readphy(tp, MII_BMCR, &bmcr);
6010 if (bmcr & BMCR_ANENABLE) {
6011 u32 phy1, phy2;
6012
6013 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006014 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6015 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006016
6017 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006018 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6019 MII_TG3_DSP_EXP1_INT_STAT);
6020 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6021 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006022
6023 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6024 /* We have signal detect and not receiving
6025 * config code words, link is up by parallel
6026 * detection.
6027 */
6028
6029 bmcr &= ~BMCR_ANENABLE;
6030 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6031 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006032 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006033 }
6034 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006035 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006036 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006037 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006038 u32 phy2;
6039
6040 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006041 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6042 MII_TG3_DSP_EXP1_INT_STAT);
6043 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006044 if (phy2 & 0x20) {
6045 u32 bmcr;
6046
6047 /* Config code words received, turn on autoneg. */
6048 tg3_readphy(tp, MII_BMCR, &bmcr);
6049 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6050
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006051 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006052
6053 }
6054 }
6055}
6056
Joe Perches953c96e2013-04-09 10:18:14 +00006057static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006059 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 int err;
6061
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006062 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006064 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006065 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006066 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
Joe Perches41535772013-02-16 11:20:04 +00006069 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006070 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006071
6072 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6073 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6074 scale = 65;
6075 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6076 scale = 6;
6077 else
6078 scale = 12;
6079
6080 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6081 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6082 tw32(GRC_MISC_CFG, val);
6083 }
6084
Matt Carlsonf2096f92011-04-05 14:22:48 +00006085 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6086 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006087 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6088 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006089 val |= tr32(MAC_TX_LENGTHS) &
6090 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6091 TX_LENGTHS_CNT_DWN_VAL_MSK);
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 if (tp->link_config.active_speed == SPEED_1000 &&
6094 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006095 tw32(MAC_TX_LENGTHS, val |
6096 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006098 tw32(MAC_TX_LENGTHS, val |
6099 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
Joe Perches63c3a662011-04-26 08:12:10 +00006101 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006102 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006104 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 } else {
6106 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6107 }
6108 }
6109
Joe Perches63c3a662011-04-26 08:12:10 +00006110 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006111 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006112 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006113 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6114 tp->pwrmgmt_thresh;
6115 else
6116 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6117 tw32(PCIE_PWR_MGMT_THRESH, val);
6118 }
6119
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 return err;
6121}
6122
Matt Carlsonbe947302012-12-03 19:36:57 +00006123/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006124static u64 tg3_refclk_read(struct tg3 *tp)
6125{
6126 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6127 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6128}
6129
6130/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006131static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6132{
Nithin Sujir92e64572013-07-29 13:58:38 -07006133 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6134
6135 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006136 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6137 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006138 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006139}
6140
Matt Carlson7d41e492012-12-03 19:36:58 +00006141static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6142static inline void tg3_full_unlock(struct tg3 *tp);
6143static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6144{
6145 struct tg3 *tp = netdev_priv(dev);
6146
6147 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6148 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006149 SOF_TIMESTAMPING_SOFTWARE;
6150
6151 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006152 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006153 SOF_TIMESTAMPING_RX_HARDWARE |
6154 SOF_TIMESTAMPING_RAW_HARDWARE;
6155 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006156
6157 if (tp->ptp_clock)
6158 info->phc_index = ptp_clock_index(tp->ptp_clock);
6159 else
6160 info->phc_index = -1;
6161
6162 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6163
6164 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6165 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6166 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6167 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6168 return 0;
6169}
6170
6171static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6172{
6173 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6174 bool neg_adj = false;
6175 u32 correction = 0;
6176
6177 if (ppb < 0) {
6178 neg_adj = true;
6179 ppb = -ppb;
6180 }
6181
6182 /* Frequency adjustment is performed using hardware with a 24 bit
6183 * accumulator and a programmable correction value. On each clk, the
6184 * correction value gets added to the accumulator and when it
6185 * overflows, the time counter is incremented/decremented.
6186 *
6187 * So conversion from ppb to correction value is
6188 * ppb * (1 << 24) / 1000000000
6189 */
6190 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6191 TG3_EAV_REF_CLK_CORRECT_MASK;
6192
6193 tg3_full_lock(tp, 0);
6194
6195 if (correction)
6196 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6197 TG3_EAV_REF_CLK_CORRECT_EN |
6198 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6199 else
6200 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6201
6202 tg3_full_unlock(tp);
6203
6204 return 0;
6205}
6206
6207static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6208{
6209 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6210
6211 tg3_full_lock(tp, 0);
6212 tp->ptp_adjust += delta;
6213 tg3_full_unlock(tp);
6214
6215 return 0;
6216}
6217
Richard Cochranf578b412015-03-29 23:11:57 +02006218static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Matt Carlson7d41e492012-12-03 19:36:58 +00006219{
6220 u64 ns;
Matt Carlson7d41e492012-12-03 19:36:58 +00006221 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6222
6223 tg3_full_lock(tp, 0);
6224 ns = tg3_refclk_read(tp);
6225 ns += tp->ptp_adjust;
6226 tg3_full_unlock(tp);
6227
Richard Cochran7a20efb2015-03-31 23:08:08 +02006228 *ts = ns_to_timespec64(ns);
Matt Carlson7d41e492012-12-03 19:36:58 +00006229
6230 return 0;
6231}
6232
6233static int tg3_ptp_settime(struct ptp_clock_info *ptp,
Richard Cochranf578b412015-03-29 23:11:57 +02006234 const struct timespec64 *ts)
Matt Carlson7d41e492012-12-03 19:36:58 +00006235{
6236 u64 ns;
6237 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6238
Richard Cochranf578b412015-03-29 23:11:57 +02006239 ns = timespec64_to_ns(ts);
Matt Carlson7d41e492012-12-03 19:36:58 +00006240
6241 tg3_full_lock(tp, 0);
6242 tg3_refclk_write(tp, ns);
6243 tp->ptp_adjust = 0;
6244 tg3_full_unlock(tp);
6245
6246 return 0;
6247}
6248
6249static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6250 struct ptp_clock_request *rq, int on)
6251{
Nithin Sujir92e64572013-07-29 13:58:38 -07006252 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6253 u32 clock_ctl;
6254 int rval = 0;
6255
6256 switch (rq->type) {
6257 case PTP_CLK_REQ_PEROUT:
6258 if (rq->perout.index != 0)
6259 return -EINVAL;
6260
6261 tg3_full_lock(tp, 0);
6262 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6263 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6264
6265 if (on) {
6266 u64 nsec;
6267
6268 nsec = rq->perout.start.sec * 1000000000ULL +
6269 rq->perout.start.nsec;
6270
6271 if (rq->perout.period.sec || rq->perout.period.nsec) {
6272 netdev_warn(tp->dev,
6273 "Device supports only a one-shot timesync output, period must be 0\n");
6274 rval = -EINVAL;
6275 goto err_out;
6276 }
6277
6278 if (nsec & (1ULL << 63)) {
6279 netdev_warn(tp->dev,
6280 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6281 rval = -EINVAL;
6282 goto err_out;
6283 }
6284
6285 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6286 tw32(TG3_EAV_WATCHDOG0_MSB,
6287 TG3_EAV_WATCHDOG0_EN |
6288 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6289
6290 tw32(TG3_EAV_REF_CLCK_CTL,
6291 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6292 } else {
6293 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6294 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6295 }
6296
6297err_out:
6298 tg3_full_unlock(tp);
6299 return rval;
6300
6301 default:
6302 break;
6303 }
6304
Matt Carlson7d41e492012-12-03 19:36:58 +00006305 return -EOPNOTSUPP;
6306}
6307
6308static const struct ptp_clock_info tg3_ptp_caps = {
6309 .owner = THIS_MODULE,
6310 .name = "tg3 clock",
6311 .max_adj = 250000000,
6312 .n_alarm = 0,
6313 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006314 .n_per_out = 1,
Richard Cochran4986b4f02014-03-20 22:21:55 +01006315 .n_pins = 0,
Matt Carlson7d41e492012-12-03 19:36:58 +00006316 .pps = 0,
6317 .adjfreq = tg3_ptp_adjfreq,
6318 .adjtime = tg3_ptp_adjtime,
Richard Cochranf578b412015-03-29 23:11:57 +02006319 .gettime64 = tg3_ptp_gettime,
6320 .settime64 = tg3_ptp_settime,
Matt Carlson7d41e492012-12-03 19:36:58 +00006321 .enable = tg3_ptp_enable,
6322};
6323
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006324static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6325 struct skb_shared_hwtstamps *timestamp)
6326{
6327 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6328 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6329 tp->ptp_adjust);
6330}
6331
Matt Carlsonbe947302012-12-03 19:36:57 +00006332/* tp->lock must be held */
6333static void tg3_ptp_init(struct tg3 *tp)
6334{
6335 if (!tg3_flag(tp, PTP_CAPABLE))
6336 return;
6337
6338 /* Initialize the hardware clock to the system time. */
6339 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6340 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006341 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006342}
6343
6344/* tp->lock must be held */
6345static void tg3_ptp_resume(struct tg3 *tp)
6346{
6347 if (!tg3_flag(tp, PTP_CAPABLE))
6348 return;
6349
6350 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6351 tp->ptp_adjust = 0;
6352}
6353
6354static void tg3_ptp_fini(struct tg3 *tp)
6355{
6356 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6357 return;
6358
Matt Carlson7d41e492012-12-03 19:36:58 +00006359 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006360 tp->ptp_clock = NULL;
6361 tp->ptp_adjust = 0;
6362}
6363
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006364static inline int tg3_irq_sync(struct tg3 *tp)
6365{
6366 return tp->irq_sync;
6367}
6368
Matt Carlson97bd8e42011-04-13 11:05:04 +00006369static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6370{
6371 int i;
6372
6373 dst = (u32 *)((u8 *)dst + off);
6374 for (i = 0; i < len; i += sizeof(u32))
6375 *dst++ = tr32(off + i);
6376}
6377
6378static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6379{
6380 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6381 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6382 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6383 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6384 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6385 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6386 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6387 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6388 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6389 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6390 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6391 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6392 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6393 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6394 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6395 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6396 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6397 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6398 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6399
Joe Perches63c3a662011-04-26 08:12:10 +00006400 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006401 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6402
6403 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6404 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6405 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6406 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6407 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6408 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6409 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6410 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6411
Joe Perches63c3a662011-04-26 08:12:10 +00006412 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006413 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6414 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6415 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6416 }
6417
6418 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6419 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6420 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6421 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6422 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6423
Joe Perches63c3a662011-04-26 08:12:10 +00006424 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006425 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6426}
6427
6428static void tg3_dump_state(struct tg3 *tp)
6429{
6430 int i;
6431 u32 *regs;
6432
6433 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006434 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006435 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006436
Joe Perches63c3a662011-04-26 08:12:10 +00006437 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006438 /* Read up to but not including private PCI registers */
6439 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6440 regs[i / sizeof(u32)] = tr32(i);
6441 } else
6442 tg3_dump_legacy_regs(tp, regs);
6443
6444 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6445 if (!regs[i + 0] && !regs[i + 1] &&
6446 !regs[i + 2] && !regs[i + 3])
6447 continue;
6448
6449 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6450 i * 4,
6451 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6452 }
6453
6454 kfree(regs);
6455
6456 for (i = 0; i < tp->irq_cnt; i++) {
6457 struct tg3_napi *tnapi = &tp->napi[i];
6458
6459 /* SW status block */
6460 netdev_err(tp->dev,
6461 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6462 i,
6463 tnapi->hw_status->status,
6464 tnapi->hw_status->status_tag,
6465 tnapi->hw_status->rx_jumbo_consumer,
6466 tnapi->hw_status->rx_consumer,
6467 tnapi->hw_status->rx_mini_consumer,
6468 tnapi->hw_status->idx[0].rx_producer,
6469 tnapi->hw_status->idx[0].tx_consumer);
6470
6471 netdev_err(tp->dev,
6472 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6473 i,
6474 tnapi->last_tag, tnapi->last_irq_tag,
6475 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6476 tnapi->rx_rcb_ptr,
6477 tnapi->prodring.rx_std_prod_idx,
6478 tnapi->prodring.rx_std_cons_idx,
6479 tnapi->prodring.rx_jmb_prod_idx,
6480 tnapi->prodring.rx_jmb_cons_idx);
6481 }
6482}
6483
Michael Chandf3e6542006-05-26 17:48:07 -07006484/* This is called whenever we suspect that the system chipset is re-
6485 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6486 * is bogus tx completions. We try to recover by setting the
6487 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6488 * in the workqueue.
6489 */
6490static void tg3_tx_recover(struct tg3 *tp)
6491{
Joe Perches63c3a662011-04-26 08:12:10 +00006492 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006493 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6494
Matt Carlson5129c3a2010-04-05 10:19:23 +00006495 netdev_warn(tp->dev,
6496 "The system may be re-ordering memory-mapped I/O "
6497 "cycles to the network device, attempting to recover. "
6498 "Please report the problem to the driver maintainer "
6499 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006500
Joe Perches63c3a662011-04-26 08:12:10 +00006501 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006502}
6503
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006504static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006505{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006506 /* Tell compiler to fetch tx indices from memory. */
6507 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006508 return tnapi->tx_pending -
6509 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006510}
6511
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512/* Tigon3 never reports partial packet sends. So we do not
6513 * need special logic to handle SKBs that have not had all
6514 * of their frags sent yet, like SunGEM does.
6515 */
Matt Carlson17375d22009-08-28 14:02:18 +00006516static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517{
Matt Carlson17375d22009-08-28 14:02:18 +00006518 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006519 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006520 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006521 struct netdev_queue *txq;
6522 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006523 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006524
Joe Perches63c3a662011-04-26 08:12:10 +00006525 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006526 index--;
6527
6528 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
6530 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006531 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006533 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
Michael Chandf3e6542006-05-26 17:48:07 -07006535 if (unlikely(skb == NULL)) {
6536 tg3_tx_recover(tp);
6537 return;
6538 }
6539
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006540 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6541 struct skb_shared_hwtstamps timestamp;
6542 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6543 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6544
6545 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6546
6547 skb_tstamp_tx(skb, &timestamp);
6548 }
6549
Alexander Duyckf4188d82009-12-02 16:48:38 +00006550 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006551 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006552 skb_headlen(skb),
6553 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
6555 ri->skb = NULL;
6556
Matt Carlsone01ee142011-07-27 14:20:50 +00006557 while (ri->fragmented) {
6558 ri->fragmented = false;
6559 sw_idx = NEXT_TX(sw_idx);
6560 ri = &tnapi->tx_buffers[sw_idx];
6561 }
6562
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 sw_idx = NEXT_TX(sw_idx);
6564
6565 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006566 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006567 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6568 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006569
6570 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006571 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006572 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006573 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006574
6575 while (ri->fragmented) {
6576 ri->fragmented = false;
6577 sw_idx = NEXT_TX(sw_idx);
6578 ri = &tnapi->tx_buffers[sw_idx];
6579 }
6580
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 sw_idx = NEXT_TX(sw_idx);
6582 }
6583
Tom Herbert298376d2011-11-28 16:33:30 +00006584 pkts_compl++;
6585 bytes_compl += skb->len;
6586
Eric W. Biederman497a27b2014-03-11 14:18:14 -07006587 dev_kfree_skb_any(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006588
6589 if (unlikely(tx_bug)) {
6590 tg3_tx_recover(tp);
6591 return;
6592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 }
6594
Tom Herbert5cb917b2012-03-05 19:53:50 +00006595 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006596
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006597 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
Michael Chan1b2a7202006-08-07 21:46:02 -07006599 /* Need to make the tx_cons update visible to tg3_start_xmit()
6600 * before checking for netif_queue_stopped(). Without the
6601 * memory barrier, there is a small possibility that tg3_start_xmit()
6602 * will miss it and cause the queue to be stopped forever.
6603 */
6604 smp_mb();
6605
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006606 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006607 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006608 __netif_tx_lock(txq, smp_processor_id());
6609 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006610 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006611 netif_tx_wake_queue(txq);
6612 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614}
6615
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006616static void tg3_frag_free(bool is_frag, void *data)
6617{
6618 if (is_frag)
Alexander Duycke51423d2015-05-06 21:12:31 -07006619 skb_free_frag(data);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006620 else
6621 kfree(data);
6622}
6623
Eric Dumazet9205fd92011-11-18 06:47:01 +00006624static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006625{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006626 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6627 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6628
Eric Dumazet9205fd92011-11-18 06:47:01 +00006629 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006630 return;
6631
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006632 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006633 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006634 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006635 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006636}
6637
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006638
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639/* Returns size of skb allocated or < 0 on error.
6640 *
6641 * We only need to fill in the address because the other members
6642 * of the RX descriptor are invariant, see tg3_init_rings.
6643 *
6644 * Note the purposeful assymetry of cpu vs. chip accesses. For
6645 * posting buffers we only dirty the first cache line of the RX
6646 * descriptor (containing the address). Whereas for the RX status
6647 * buffers the cpu only reads the last cacheline of the RX descriptor
6648 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6649 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006650static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006651 u32 opaque_key, u32 dest_idx_unmasked,
6652 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653{
6654 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006655 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006656 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006658 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 switch (opaque_key) {
6661 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006662 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006663 desc = &tpr->rx_std[dest_idx];
6664 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006665 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 break;
6667
6668 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006669 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006670 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006671 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006672 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 break;
6674
6675 default:
6676 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
6679 /* Do not overwrite any of the map or rp information
6680 * until we are sure we can commit to a new buffer.
6681 *
6682 * Callers depend upon this behavior and assume that
6683 * we leave everything unchanged if we fail.
6684 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006685 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6686 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006687 if (skb_size <= PAGE_SIZE) {
6688 data = netdev_alloc_frag(skb_size);
6689 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006690 } else {
6691 data = kmalloc(skb_size, GFP_ATOMIC);
6692 *frag_size = 0;
6693 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006694 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 return -ENOMEM;
6696
Eric Dumazet9205fd92011-11-18 06:47:01 +00006697 mapping = pci_map_single(tp->pdev,
6698 data + TG3_RX_OFFSET(tp),
6699 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006701 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006702 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006703 return -EIO;
6704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705
Eric Dumazet9205fd92011-11-18 06:47:01 +00006706 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006707 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 desc->addr_hi = ((u64)mapping >> 32);
6710 desc->addr_lo = ((u64)mapping & 0xffffffff);
6711
Eric Dumazet9205fd92011-11-18 06:47:01 +00006712 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713}
6714
6715/* We only need to move over in the address because the other
6716 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006717 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 */
Matt Carlsona3896162009-11-13 13:03:44 +00006719static void tg3_recycle_rx(struct tg3_napi *tnapi,
6720 struct tg3_rx_prodring_set *dpr,
6721 u32 opaque_key, int src_idx,
6722 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723{
Matt Carlson17375d22009-08-28 14:02:18 +00006724 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6726 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006727 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006728 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
6730 switch (opaque_key) {
6731 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006732 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006733 dest_desc = &dpr->rx_std[dest_idx];
6734 dest_map = &dpr->rx_std_buffers[dest_idx];
6735 src_desc = &spr->rx_std[src_idx];
6736 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 break;
6738
6739 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006740 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006741 dest_desc = &dpr->rx_jmb[dest_idx].std;
6742 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6743 src_desc = &spr->rx_jmb[src_idx].std;
6744 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 break;
6746
6747 default:
6748 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
Eric Dumazet9205fd92011-11-18 06:47:01 +00006751 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006752 dma_unmap_addr_set(dest_map, mapping,
6753 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 dest_desc->addr_hi = src_desc->addr_hi;
6755 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006756
6757 /* Ensure that the update to the skb happens after the physical
6758 * addresses have been transferred to the new BD location.
6759 */
6760 smp_wmb();
6761
Eric Dumazet9205fd92011-11-18 06:47:01 +00006762 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763}
6764
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765/* The RX ring scheme is composed of multiple rings which post fresh
6766 * buffers to the chip, and one special ring the chip uses to report
6767 * status back to the host.
6768 *
6769 * The special ring reports the status of received packets to the
6770 * host. The chip does not write into the original descriptor the
6771 * RX buffer was obtained from. The chip simply takes the original
6772 * descriptor as provided by the host, updates the status and length
6773 * field, then writes this into the next status ring entry.
6774 *
6775 * Each ring the host uses to post buffers to the chip is described
6776 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6777 * it is first placed into the on-chip ram. When the packet's length
6778 * is known, it walks down the TG3_BDINFO entries to select the ring.
6779 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6780 * which is within the range of the new packet's length is chosen.
6781 *
6782 * The "separate ring for rx status" scheme may sound queer, but it makes
6783 * sense from a cache coherency perspective. If only the host writes
6784 * to the buffer post rings, and only the chip writes to the rx status
6785 * rings, then cache lines never move beyond shared-modified state.
6786 * If both the host and chip were to write into the same ring, cache line
6787 * eviction could occur since both entities want it in an exclusive state.
6788 */
Matt Carlson17375d22009-08-28 14:02:18 +00006789static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790{
Matt Carlson17375d22009-08-28 14:02:18 +00006791 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006792 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006793 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006794 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006795 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006797 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006799 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 /*
6801 * We need to order the read of hw_idx and the read of
6802 * the opaque cookie.
6803 */
6804 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 work_mask = 0;
6806 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006807 std_prod_idx = tpr->rx_std_prod_idx;
6808 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006810 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006811 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 unsigned int len;
6813 struct sk_buff *skb;
6814 dma_addr_t dma_addr;
6815 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006816 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006817 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818
6819 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6820 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6821 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006822 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006823 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006824 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006825 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006826 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006828 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006829 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006830 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006831 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006832 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
6835 work_mask |= opaque_key;
6836
Michael Chand7b95312014-02-28 15:05:10 -08006837 if (desc->err_vlan & RXD_ERR_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006839 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 desc_idx, *post_ptr);
6841 drop_it_no_recycle:
6842 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006843 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 goto next_pkt;
6845 }
6846
Eric Dumazet9205fd92011-11-18 06:47:01 +00006847 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006848 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6849 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006851 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6852 RXD_FLAG_PTPSTAT_PTPV1 ||
6853 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6854 RXD_FLAG_PTPSTAT_PTPV2) {
6855 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6856 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6857 }
6858
Matt Carlsond2757fc2010-04-12 06:58:27 +00006859 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006861 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862
Eric Dumazet9205fd92011-11-18 06:47:01 +00006863 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006864 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 if (skb_size < 0)
6866 goto drop_it;
6867
Matt Carlson287be122009-08-28 13:58:46 +00006868 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 PCI_DMA_FROMDEVICE);
6870
Eric Dumazet9205fd92011-11-18 06:47:01 +00006871 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006872 * after the usage of the old DMA mapping.
6873 */
6874 smp_wmb();
6875
Eric Dumazet9205fd92011-11-18 06:47:01 +00006876 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006877
Ivan Vecera85aec732013-11-06 14:02:36 +01006878 skb = build_skb(data, frag_size);
6879 if (!skb) {
6880 tg3_frag_free(frag_size != 0, data);
6881 goto drop_it_no_recycle;
6882 }
6883 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006885 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 desc_idx, *post_ptr);
6887
Eric Dumazet9205fd92011-11-18 06:47:01 +00006888 skb = netdev_alloc_skb(tp->dev,
6889 len + TG3_RAW_IP_ALIGN);
6890 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 goto drop_it_no_recycle;
6892
Eric Dumazet9205fd92011-11-18 06:47:01 +00006893 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006895 memcpy(skb->data,
6896 data + TG3_RX_OFFSET(tp),
6897 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 }
6900
Eric Dumazet9205fd92011-11-18 06:47:01 +00006901 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006902 if (tstamp)
6903 tg3_hwclock_to_timestamp(tp, tstamp,
6904 skb_hwtstamps(skb));
6905
Michał Mirosławdc668912011-04-07 03:35:07 +00006906 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6908 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6909 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6910 skb->ip_summed = CHECKSUM_UNNECESSARY;
6911 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006912 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913
6914 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006915
6916 if (len > (tp->dev->mtu + ETH_HLEN) &&
Vlad Yasevich7d3083e2014-09-30 19:39:36 -04006917 skb->protocol != htons(ETH_P_8021Q) &&
6918 skb->protocol != htons(ETH_P_8021AD)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07006919 dev_kfree_skb_any(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006920 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006921 }
6922
Matt Carlson9dc7a112010-04-12 06:58:28 +00006923 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006924 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006925 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006926 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006927
Matt Carlsonbf933c82011-01-25 15:58:49 +00006928 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 received++;
6931 budget--;
6932
6933next_pkt:
6934 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006935
6936 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006937 tpr->rx_std_prod_idx = std_prod_idx &
6938 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006939 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6940 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006941 work_mask &= ~RXD_OPAQUE_RING_STD;
6942 rx_std_posted = 0;
6943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006945 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006946 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006947
6948 /* Refresh hw_idx to see if there is new work */
6949 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006950 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006951 rmb();
6952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 }
6954
6955 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006956 tnapi->rx_rcb_ptr = sw_idx;
6957 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958
6959 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006960 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006961 /* Sync BD data before updating mailbox */
6962 wmb();
6963
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006964 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006965 tpr->rx_std_prod_idx = std_prod_idx &
6966 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006967 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6968 tpr->rx_std_prod_idx);
6969 }
6970 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006971 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6972 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006973 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6974 tpr->rx_jmb_prod_idx);
6975 }
6976 mmiowb();
6977 } else if (work_mask) {
6978 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6979 * updated before the producer indices can be updated.
6980 */
6981 smp_wmb();
6982
Matt Carlson2c49a442010-09-30 10:34:35 +00006983 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6984 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006985
Michael Chan7ae52892012-03-21 15:38:33 +00006986 if (tnapi != &tp->napi[1]) {
6987 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006988 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991
6992 return received;
6993}
6994
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006995static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006998 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006999 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
7000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 if (sblk->status & SD_STATUS_LINK_CHG) {
7002 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007003 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07007004 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00007005 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007006 tw32_f(MAC_STATUS,
7007 (MAC_STATUS_SYNC_CHANGED |
7008 MAC_STATUS_CFG_CHANGED |
7009 MAC_STATUS_MI_COMPLETION |
7010 MAC_STATUS_LNKSTATE_CHANGED));
7011 udelay(40);
7012 } else
Joe Perches953c96e2013-04-09 10:18:14 +00007013 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007014 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 }
7016 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007017}
7018
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007019static int tg3_rx_prodring_xfer(struct tg3 *tp,
7020 struct tg3_rx_prodring_set *dpr,
7021 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007022{
7023 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007024 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007025
7026 while (1) {
7027 src_prod_idx = spr->rx_std_prod_idx;
7028
7029 /* Make sure updates to the rx_std_buffers[] entries and the
7030 * standard producer index are seen in the correct order.
7031 */
7032 smp_rmb();
7033
7034 if (spr->rx_std_cons_idx == src_prod_idx)
7035 break;
7036
7037 if (spr->rx_std_cons_idx < src_prod_idx)
7038 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7039 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007040 cpycnt = tp->rx_std_ring_mask + 1 -
7041 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007042
Matt Carlson2c49a442010-09-30 10:34:35 +00007043 cpycnt = min(cpycnt,
7044 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007045
7046 si = spr->rx_std_cons_idx;
7047 di = dpr->rx_std_prod_idx;
7048
Matt Carlsone92967b2010-02-12 14:47:06 +00007049 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007050 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007051 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007052 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007053 break;
7054 }
7055 }
7056
7057 if (!cpycnt)
7058 break;
7059
7060 /* Ensure that updates to the rx_std_buffers ring and the
7061 * shadowed hardware producer ring from tg3_recycle_skb() are
7062 * ordered correctly WRT the skb check above.
7063 */
7064 smp_rmb();
7065
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007066 memcpy(&dpr->rx_std_buffers[di],
7067 &spr->rx_std_buffers[si],
7068 cpycnt * sizeof(struct ring_info));
7069
7070 for (i = 0; i < cpycnt; i++, di++, si++) {
7071 struct tg3_rx_buffer_desc *sbd, *dbd;
7072 sbd = &spr->rx_std[si];
7073 dbd = &dpr->rx_std[di];
7074 dbd->addr_hi = sbd->addr_hi;
7075 dbd->addr_lo = sbd->addr_lo;
7076 }
7077
Matt Carlson2c49a442010-09-30 10:34:35 +00007078 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7079 tp->rx_std_ring_mask;
7080 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7081 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007082 }
7083
7084 while (1) {
7085 src_prod_idx = spr->rx_jmb_prod_idx;
7086
7087 /* Make sure updates to the rx_jmb_buffers[] entries and
7088 * the jumbo producer index are seen in the correct order.
7089 */
7090 smp_rmb();
7091
7092 if (spr->rx_jmb_cons_idx == src_prod_idx)
7093 break;
7094
7095 if (spr->rx_jmb_cons_idx < src_prod_idx)
7096 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7097 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007098 cpycnt = tp->rx_jmb_ring_mask + 1 -
7099 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007100
7101 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007102 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007103
7104 si = spr->rx_jmb_cons_idx;
7105 di = dpr->rx_jmb_prod_idx;
7106
Matt Carlsone92967b2010-02-12 14:47:06 +00007107 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007108 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007109 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007110 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007111 break;
7112 }
7113 }
7114
7115 if (!cpycnt)
7116 break;
7117
7118 /* Ensure that updates to the rx_jmb_buffers ring and the
7119 * shadowed hardware producer ring from tg3_recycle_skb() are
7120 * ordered correctly WRT the skb check above.
7121 */
7122 smp_rmb();
7123
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007124 memcpy(&dpr->rx_jmb_buffers[di],
7125 &spr->rx_jmb_buffers[si],
7126 cpycnt * sizeof(struct ring_info));
7127
7128 for (i = 0; i < cpycnt; i++, di++, si++) {
7129 struct tg3_rx_buffer_desc *sbd, *dbd;
7130 sbd = &spr->rx_jmb[si].std;
7131 dbd = &dpr->rx_jmb[di].std;
7132 dbd->addr_hi = sbd->addr_hi;
7133 dbd->addr_lo = sbd->addr_lo;
7134 }
7135
Matt Carlson2c49a442010-09-30 10:34:35 +00007136 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7137 tp->rx_jmb_ring_mask;
7138 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7139 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007140 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007141
7142 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007143}
7144
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007145static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7146{
7147 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148
7149 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007150 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007151 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007152 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007153 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 }
7155
Matt Carlsonf891ea12012-04-24 13:37:01 +00007156 if (!tnapi->rx_rcb_prod_idx)
7157 return work_done;
7158
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 /* run RX thread, within the bounds set by NAPI.
7160 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007161 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007163 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007164 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
Joe Perches63c3a662011-04-26 08:12:10 +00007166 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007167 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007168 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007169 u32 std_prod_idx = dpr->rx_std_prod_idx;
7170 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007171
Michael Chan7ae52892012-03-21 15:38:33 +00007172 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007173 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007174 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007175 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007176
7177 wmb();
7178
Matt Carlsone4af1af2010-02-12 14:47:05 +00007179 if (std_prod_idx != dpr->rx_std_prod_idx)
7180 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7181 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007182
Matt Carlsone4af1af2010-02-12 14:47:05 +00007183 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7184 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7185 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007186
7187 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007188
7189 if (err)
7190 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007191 }
7192
David S. Miller6f535762007-10-11 18:08:29 -07007193 return work_done;
7194}
David S. Millerf7383c22005-05-18 22:50:53 -07007195
Matt Carlsondb219972011-11-04 09:15:03 +00007196static inline void tg3_reset_task_schedule(struct tg3 *tp)
7197{
7198 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7199 schedule_work(&tp->reset_task);
7200}
7201
7202static inline void tg3_reset_task_cancel(struct tg3 *tp)
7203{
7204 cancel_work_sync(&tp->reset_task);
7205 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007206 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007207}
7208
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007209static int tg3_poll_msix(struct napi_struct *napi, int budget)
7210{
7211 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7212 struct tg3 *tp = tnapi->tp;
7213 int work_done = 0;
7214 struct tg3_hw_status *sblk = tnapi->hw_status;
7215
7216 while (1) {
7217 work_done = tg3_poll_work(tnapi, work_done, budget);
7218
Joe Perches63c3a662011-04-26 08:12:10 +00007219 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007220 goto tx_recovery;
7221
7222 if (unlikely(work_done >= budget))
7223 break;
7224
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007225 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007226 * to tell the hw how much work has been processed,
7227 * so we must read it before checking for more work.
7228 */
7229 tnapi->last_tag = sblk->status_tag;
7230 tnapi->last_irq_tag = tnapi->last_tag;
7231 rmb();
7232
7233 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007234 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7235 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007236
7237 /* This test here is not race free, but will reduce
7238 * the number of interrupts by looping again.
7239 */
7240 if (tnapi == &tp->napi[1] && tp->rx_refill)
7241 continue;
7242
Eric Dumazet24d2e4a2015-03-05 10:41:34 -08007243 napi_complete_done(napi, work_done);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007244 /* Reenable interrupts. */
7245 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007246
7247 /* This test here is synchronized by napi_schedule()
7248 * and napi_complete() to close the race condition.
7249 */
7250 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7251 tw32(HOSTCC_MODE, tp->coalesce_mode |
7252 HOSTCC_MODE_ENABLE |
7253 tnapi->coal_now);
7254 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007255 mmiowb();
7256 break;
7257 }
7258 }
7259
7260 return work_done;
7261
7262tx_recovery:
7263 /* work_done is guaranteed to be less than budget. */
7264 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007265 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007266 return work_done;
7267}
7268
Matt Carlsone64de4e2011-04-13 11:05:05 +00007269static void tg3_process_error(struct tg3 *tp)
7270{
7271 u32 val;
7272 bool real_error = false;
7273
Joe Perches63c3a662011-04-26 08:12:10 +00007274 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007275 return;
7276
7277 /* Check Flow Attention register */
7278 val = tr32(HOSTCC_FLOW_ATTN);
7279 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7280 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7281 real_error = true;
7282 }
7283
7284 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7285 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7286 real_error = true;
7287 }
7288
7289 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7290 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7291 real_error = true;
7292 }
7293
7294 if (!real_error)
7295 return;
7296
7297 tg3_dump_state(tp);
7298
Joe Perches63c3a662011-04-26 08:12:10 +00007299 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007300 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007301}
7302
David S. Miller6f535762007-10-11 18:08:29 -07007303static int tg3_poll(struct napi_struct *napi, int budget)
7304{
Matt Carlson8ef04422009-08-28 14:01:37 +00007305 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7306 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007307 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007308 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007309
7310 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007311 if (sblk->status & SD_STATUS_ERROR)
7312 tg3_process_error(tp);
7313
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007314 tg3_poll_link(tp);
7315
Matt Carlson17375d22009-08-28 14:02:18 +00007316 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007317
Joe Perches63c3a662011-04-26 08:12:10 +00007318 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007319 goto tx_recovery;
7320
7321 if (unlikely(work_done >= budget))
7322 break;
7323
Joe Perches63c3a662011-04-26 08:12:10 +00007324 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007325 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007326 * to tell the hw how much work has been processed,
7327 * so we must read it before checking for more work.
7328 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007329 tnapi->last_tag = sblk->status_tag;
7330 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007331 rmb();
7332 } else
7333 sblk->status &= ~SD_STATUS_UPDATED;
7334
Matt Carlson17375d22009-08-28 14:02:18 +00007335 if (likely(!tg3_has_work(tnapi))) {
Eric Dumazet24d2e4a2015-03-05 10:41:34 -08007336 napi_complete_done(napi, work_done);
Matt Carlson17375d22009-08-28 14:02:18 +00007337 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007338 break;
7339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 }
7341
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007342 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007343
7344tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007345 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007346 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007347 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007348 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349}
7350
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007351static void tg3_napi_disable(struct tg3 *tp)
7352{
7353 int i;
7354
7355 for (i = tp->irq_cnt - 1; i >= 0; i--)
7356 napi_disable(&tp->napi[i].napi);
7357}
7358
7359static void tg3_napi_enable(struct tg3 *tp)
7360{
7361 int i;
7362
7363 for (i = 0; i < tp->irq_cnt; i++)
7364 napi_enable(&tp->napi[i].napi);
7365}
7366
7367static void tg3_napi_init(struct tg3 *tp)
7368{
7369 int i;
7370
7371 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7372 for (i = 1; i < tp->irq_cnt; i++)
7373 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7374}
7375
7376static void tg3_napi_fini(struct tg3 *tp)
7377{
7378 int i;
7379
7380 for (i = 0; i < tp->irq_cnt; i++)
7381 netif_napi_del(&tp->napi[i].napi);
7382}
7383
7384static inline void tg3_netif_stop(struct tg3 *tp)
7385{
Florian Westphal860e9532016-05-03 16:33:13 +02007386 netif_trans_update(tp->dev); /* prevent tx timeout */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007387 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007388 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007389 netif_tx_disable(tp->dev);
7390}
7391
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007392/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007393static inline void tg3_netif_start(struct tg3 *tp)
7394{
Matt Carlsonbe947302012-12-03 19:36:57 +00007395 tg3_ptp_resume(tp);
7396
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007397 /* NOTE: unconditional netif_tx_wake_all_queues is only
7398 * appropriate so long as all callers are assured to
7399 * have free tx slots (such as after tg3_init_hw)
7400 */
7401 netif_tx_wake_all_queues(tp->dev);
7402
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007403 if (tp->link_up)
7404 netif_carrier_on(tp->dev);
7405
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007406 tg3_napi_enable(tp);
7407 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7408 tg3_enable_ints(tp);
7409}
7410
David S. Millerf47c11e2005-06-24 20:18:35 -07007411static void tg3_irq_quiesce(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007412 __releases(tp->lock)
7413 __acquires(tp->lock)
David S. Millerf47c11e2005-06-24 20:18:35 -07007414{
Matt Carlson4f125f42009-09-01 12:55:02 +00007415 int i;
7416
David S. Millerf47c11e2005-06-24 20:18:35 -07007417 BUG_ON(tp->irq_sync);
7418
7419 tp->irq_sync = 1;
7420 smp_mb();
7421
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007422 spin_unlock_bh(&tp->lock);
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);
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007426
7427 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007428}
7429
David S. Millerf47c11e2005-06-24 20:18:35 -07007430/* Fully shutdown all tg3 driver activity elsewhere in the system.
7431 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7432 * with as well. Most of the time, this is not necessary except when
7433 * shutting down the device.
7434 */
7435static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7436{
Michael Chan46966542007-07-11 19:47:19 -07007437 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007438 if (irq_sync)
7439 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007440}
7441
7442static inline void tg3_full_unlock(struct tg3 *tp)
7443{
David S. Millerf47c11e2005-06-24 20:18:35 -07007444 spin_unlock_bh(&tp->lock);
7445}
7446
Michael Chanfcfa0a32006-03-20 22:28:41 -08007447/* One-shot MSI handler - Chip automatically disables interrupt
7448 * after sending MSI so driver doesn't have to do it.
7449 */
David Howells7d12e782006-10-05 14:55:46 +01007450static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007451{
Matt Carlson09943a12009-08-28 14:01:57 +00007452 struct tg3_napi *tnapi = dev_id;
7453 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007454
Matt Carlson898a56f2009-08-28 14:02:40 +00007455 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007456 if (tnapi->rx_rcb)
7457 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007458
7459 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007460 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007461
7462 return IRQ_HANDLED;
7463}
7464
Michael Chan88b06bc22005-04-21 17:13:25 -07007465/* MSI ISR - No need to check for interrupt sharing and no need to
7466 * flush status block and interrupt mailbox. PCI ordering rules
7467 * guarantee that MSI will arrive after the status block.
7468 */
David Howells7d12e782006-10-05 14:55:46 +01007469static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007470{
Matt Carlson09943a12009-08-28 14:01:57 +00007471 struct tg3_napi *tnapi = dev_id;
7472 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007473
Matt Carlson898a56f2009-08-28 14:02:40 +00007474 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007475 if (tnapi->rx_rcb)
7476 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007477 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007478 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007479 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007480 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007481 * NIC to stop sending us irqs, engaging "in-intr-handler"
7482 * event coalescing.
7483 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007484 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007485 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007486 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007487
Michael Chan88b06bc22005-04-21 17:13:25 -07007488 return IRQ_RETVAL(1);
7489}
7490
David Howells7d12e782006-10-05 14:55:46 +01007491static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492{
Matt Carlson09943a12009-08-28 14:01:57 +00007493 struct tg3_napi *tnapi = dev_id;
7494 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007495 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 unsigned int handled = 1;
7497
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 /* In INTx mode, it is possible for the interrupt to arrive at
7499 * the CPU before the status block posted prior to the interrupt.
7500 * Reading the PCI State register will confirm whether the
7501 * interrupt is ours and will flush the status block.
7502 */
Michael Chand18edcb2007-03-24 20:57:11 -07007503 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007504 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007505 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7506 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007507 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007508 }
Michael Chand18edcb2007-03-24 20:57:11 -07007509 }
7510
7511 /*
7512 * Writing any value to intr-mbox-0 clears PCI INTA# and
7513 * chip-internal interrupt pending events.
7514 * Writing non-zero to intr-mbox-0 additional tells the
7515 * NIC to stop sending us irqs, engaging "in-intr-handler"
7516 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007517 *
7518 * Flush the mailbox to de-assert the IRQ immediately to prevent
7519 * spurious interrupts. The flush impacts performance but
7520 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007521 */
Michael Chanc04cb342007-05-07 00:26:15 -07007522 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007523 if (tg3_irq_sync(tp))
7524 goto out;
7525 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007526 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007527 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007528 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007529 } else {
7530 /* No work, shared interrupt perhaps? re-enable
7531 * interrupts, and flush that PCI write
7532 */
7533 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7534 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007535 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007536out:
David S. Millerfac9b832005-05-18 22:46:34 -07007537 return IRQ_RETVAL(handled);
7538}
7539
David Howells7d12e782006-10-05 14:55:46 +01007540static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007541{
Matt Carlson09943a12009-08-28 14:01:57 +00007542 struct tg3_napi *tnapi = dev_id;
7543 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007544 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007545 unsigned int handled = 1;
7546
David S. Millerfac9b832005-05-18 22:46:34 -07007547 /* In INTx mode, it is possible for the interrupt to arrive at
7548 * the CPU before the status block posted prior to the interrupt.
7549 * Reading the PCI State register will confirm whether the
7550 * interrupt is ours and will flush the status block.
7551 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007552 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007553 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007554 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7555 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 }
Michael Chand18edcb2007-03-24 20:57:11 -07007558 }
7559
7560 /*
7561 * writing any value to intr-mbox-0 clears PCI INTA# and
7562 * chip-internal interrupt pending events.
7563 * writing non-zero to intr-mbox-0 additional tells the
7564 * NIC to stop sending us irqs, engaging "in-intr-handler"
7565 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007566 *
7567 * Flush the mailbox to de-assert the IRQ immediately to prevent
7568 * spurious interrupts. The flush impacts performance but
7569 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007570 */
Michael Chanc04cb342007-05-07 00:26:15 -07007571 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007572
7573 /*
7574 * In a shared interrupt configuration, sometimes other devices'
7575 * interrupts will scream. We record the current status tag here
7576 * so that the above check can report that the screaming interrupts
7577 * are unhandled. Eventually they will be silenced.
7578 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007579 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007580
Michael Chand18edcb2007-03-24 20:57:11 -07007581 if (tg3_irq_sync(tp))
7582 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007583
Matt Carlson72334482009-08-28 14:03:01 +00007584 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007585
Matt Carlson09943a12009-08-28 14:01:57 +00007586 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007587
David S. Millerf47c11e2005-06-24 20:18:35 -07007588out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 return IRQ_RETVAL(handled);
7590}
7591
Michael Chan79381092005-04-21 17:13:59 -07007592/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007593static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007594{
Matt Carlson09943a12009-08-28 14:01:57 +00007595 struct tg3_napi *tnapi = dev_id;
7596 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007597 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007598
Michael Chanf9804dd2005-09-27 12:13:10 -07007599 if ((sblk->status & SD_STATUS_UPDATED) ||
7600 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007601 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007602 return IRQ_RETVAL(1);
7603 }
7604 return IRQ_RETVAL(0);
7605}
7606
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607#ifdef CONFIG_NET_POLL_CONTROLLER
7608static void tg3_poll_controller(struct net_device *dev)
7609{
Matt Carlson4f125f42009-09-01 12:55:02 +00007610 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007611 struct tg3 *tp = netdev_priv(dev);
7612
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007613 if (tg3_irq_sync(tp))
7614 return;
7615
Matt Carlson4f125f42009-09-01 12:55:02 +00007616 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007617 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618}
7619#endif
7620
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621static void tg3_tx_timeout(struct net_device *dev)
7622{
7623 struct tg3 *tp = netdev_priv(dev);
7624
Michael Chanb0408752007-02-13 12:18:30 -08007625 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007626 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007627 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629
Matt Carlsondb219972011-11-04 09:15:03 +00007630 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631}
7632
Michael Chanc58ec932005-09-17 00:46:27 -07007633/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7634static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7635{
7636 u32 base = (u32) mapping & 0xffffffff;
7637
Nithin Sujir37567912013-12-19 17:44:11 -08007638 return base + len + 8 < base;
Michael Chanc58ec932005-09-17 00:46:27 -07007639}
7640
Michael Chan0f0d1512013-05-13 11:04:16 +00007641/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7642 * of any 4GB boundaries: 4G, 8G, etc
7643 */
7644static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7645 u32 len, u32 mss)
7646{
7647 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7648 u32 base = (u32) mapping & 0xffffffff;
7649
7650 return ((base + len + (mss & 0x3fff)) < base);
7651 }
7652 return 0;
7653}
7654
Michael Chan72f2afb2006-03-06 19:28:35 -08007655/* Test for DMA addresses > 40-bit */
7656static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7657 int len)
7658{
7659#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007660 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007661 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007662 return 0;
7663#else
7664 return 0;
7665#endif
7666}
7667
Matt Carlsond1a3b732011-07-27 14:20:51 +00007668static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007669 dma_addr_t mapping, u32 len, u32 flags,
7670 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007671{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007672 txbd->addr_hi = ((u64) mapping >> 32);
7673 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7674 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7675 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677
Matt Carlson84b67b22011-07-27 14:20:52 +00007678static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007679 dma_addr_t map, u32 len, u32 flags,
7680 u32 mss, u32 vlan)
7681{
7682 struct tg3 *tp = tnapi->tp;
7683 bool hwbug = false;
7684
7685 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007686 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007687
7688 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007689 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007690
Michael Chan0f0d1512013-05-13 11:04:16 +00007691 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7692 hwbug = true;
7693
Matt Carlsond1a3b732011-07-27 14:20:51 +00007694 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007695 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007696
Matt Carlsona4cb4282011-12-14 11:09:58 +00007697 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007698 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007699 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007700 while (len > tp->dma_limit && *budget) {
7701 u32 frag_len = tp->dma_limit;
7702 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007703
Matt Carlsonb9e45482011-11-04 09:14:59 +00007704 /* Avoid the 8byte DMA problem */
7705 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007706 len += tp->dma_limit / 2;
7707 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007708 }
7709
Matt Carlsonb9e45482011-11-04 09:14:59 +00007710 tnapi->tx_buffers[*entry].fragmented = true;
7711
7712 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7713 frag_len, tmp_flag, mss, vlan);
7714 *budget -= 1;
7715 prvidx = *entry;
7716 *entry = NEXT_TX(*entry);
7717
Matt Carlsone31aa982011-07-27 14:20:53 +00007718 map += frag_len;
7719 }
7720
7721 if (len) {
7722 if (*budget) {
7723 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7724 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007725 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007726 *entry = NEXT_TX(*entry);
7727 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007728 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007729 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007730 }
7731 }
7732 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007733 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7734 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007735 *entry = NEXT_TX(*entry);
7736 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007737
7738 return hwbug;
7739}
7740
Matt Carlson0d681b22011-07-27 14:20:49 +00007741static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007742{
7743 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007744 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007745 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007746
Matt Carlson0d681b22011-07-27 14:20:49 +00007747 skb = txb->skb;
7748 txb->skb = NULL;
7749
Matt Carlson432aa7e2011-05-19 12:12:45 +00007750 pci_unmap_single(tnapi->tp->pdev,
7751 dma_unmap_addr(txb, mapping),
7752 skb_headlen(skb),
7753 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007754
7755 while (txb->fragmented) {
7756 txb->fragmented = false;
7757 entry = NEXT_TX(entry);
7758 txb = &tnapi->tx_buffers[entry];
7759 }
7760
Matt Carlsonba1142e2011-11-04 09:15:00 +00007761 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007762 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007763
7764 entry = NEXT_TX(entry);
7765 txb = &tnapi->tx_buffers[entry];
7766
7767 pci_unmap_page(tnapi->tp->pdev,
7768 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007769 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007770
7771 while (txb->fragmented) {
7772 txb->fragmented = false;
7773 entry = NEXT_TX(entry);
7774 txb = &tnapi->tx_buffers[entry];
7775 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007776 }
7777}
7778
Michael Chan72f2afb2006-03-06 19:28:35 -08007779/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007780static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007781 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007782 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007783 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007785 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007786 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007787 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007788 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789
Joe Perches41535772013-02-16 11:20:04 +00007790 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007791 new_skb = skb_copy(skb, GFP_ATOMIC);
7792 else {
7793 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7794
7795 new_skb = skb_copy_expand(skb,
7796 skb_headroom(skb) + more_headroom,
7797 skb_tailroom(skb), GFP_ATOMIC);
7798 }
7799
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007801 ret = -1;
7802 } else {
7803 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007804 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7805 PCI_DMA_TODEVICE);
7806 /* Make sure the mapping succeeded */
7807 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007808 dev_kfree_skb_any(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007809 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007810 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007811 u32 save_entry = *entry;
7812
Matt Carlson92cd3a12011-07-27 14:20:47 +00007813 base_flags |= TXD_FLAG_END;
7814
Matt Carlson84b67b22011-07-27 14:20:52 +00007815 tnapi->tx_buffers[*entry].skb = new_skb;
7816 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007817 mapping, new_addr);
7818
Matt Carlson84b67b22011-07-27 14:20:52 +00007819 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007820 new_skb->len, base_flags,
7821 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007822 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007823 dev_kfree_skb_any(new_skb);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007824 ret = -1;
7825 }
Michael Chanc58ec932005-09-17 00:46:27 -07007826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 }
7828
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007829 dev_kfree_skb_any(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007830 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007831 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832}
7833
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307834static bool tg3_tso_bug_gso_check(struct tg3_napi *tnapi, struct sk_buff *skb)
7835{
7836 /* Check if we will never have enough descriptors,
7837 * as gso_segs can be more than current ring size
7838 */
7839 return skb_shinfo(skb)->gso_segs < tnapi->tx_pending / 3;
7840}
7841
Matt Carlson2ffcc982011-05-19 12:12:44 +00007842static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007843
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007844/* Use GSO to workaround all TSO packets that meet HW bug conditions
7845 * indicated in tg3_tx_frag_set()
Michael Chan52c0fd82006-06-29 20:15:54 -07007846 */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007847static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
7848 struct netdev_queue *txq, struct sk_buff *skb)
Michael Chan52c0fd82006-06-29 20:15:54 -07007849{
7850 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007851 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007852
7853 /* Estimate the number of fragments in the worst case */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007854 if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
7855 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007856
7857 /* netif_tx_stop_queue() must be done before checking
7858 * checking tx index in tg3_tx_avail() below, because in
7859 * tg3_tx(), we update tx index before checking for
7860 * netif_tx_queue_stopped().
7861 */
7862 smp_mb();
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007863 if (tg3_tx_avail(tnapi) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007864 return NETDEV_TX_BUSY;
7865
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007866 netif_tx_wake_queue(txq);
Michael Chan52c0fd82006-06-29 20:15:54 -07007867 }
7868
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007869 segs = skb_gso_segment(skb, tp->dev->features &
7870 ~(NETIF_F_TSO | NETIF_F_TSO6));
Prashant Sreedharan40c1dea2014-06-18 18:38:13 -07007871 if (IS_ERR(segs) || !segs)
Michael Chan52c0fd82006-06-29 20:15:54 -07007872 goto tg3_tso_bug_end;
7873
7874 do {
7875 nskb = segs;
7876 segs = segs->next;
7877 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007878 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007879 } while (segs);
7880
7881tg3_tso_bug_end:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007882 dev_kfree_skb_any(skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007883
7884 return NETDEV_TX_OK;
7885}
Michael Chan52c0fd82006-06-29 20:15:54 -07007886
Michael Chand71c0dc2014-05-11 20:22:53 -07007887/* hard_start_xmit for all devices */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007888static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007889{
7890 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007891 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007892 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007893 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007894 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007895 struct tg3_napi *tnapi;
7896 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007897 unsigned int last;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007898 struct iphdr *iph = NULL;
7899 struct tcphdr *tcph = NULL;
7900 __sum16 tcp_csum = 0, ip_csum = 0;
7901 __be16 ip_tot_len = 0;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007902
Matt Carlson24f4efd2009-11-13 13:03:35 +00007903 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7904 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007905 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007906 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907
Matt Carlson84b67b22011-07-27 14:20:52 +00007908 budget = tg3_tx_avail(tnapi);
7909
Michael Chan00b70502006-06-17 21:58:45 -07007910 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007911 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007912 * interrupt. Furthermore, IRQ processing runs lockless so we have
7913 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007915 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007916 if (!netif_tx_queue_stopped(txq)) {
7917 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007918
7919 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007920 netdev_err(dev,
7921 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 return NETDEV_TX_BUSY;
7924 }
7925
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007926 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 base_flags = 0;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007928
Matt Carlsonbe98da62010-07-11 09:31:46 +00007929 mss = skb_shinfo(skb)->gso_size;
7930 if (mss) {
Matt Carlson34195c32010-07-11 09:31:42 +00007931 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932
françois romieu105dcb52014-03-29 12:26:29 +01007933 if (skb_cow_head(skb, 0))
Eric Dumazet48855432011-10-24 07:53:03 +00007934 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
Matt Carlson34195c32010-07-11 09:31:42 +00007936 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007937 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938
Eric Dumazeta5a11952012-01-23 01:22:09 +00007939 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007940
Vlad Yasevich476c1882014-09-18 10:31:17 -04007941 /* HW/FW can not correctly segment packets that have been
7942 * vlan encapsulated.
7943 */
7944 if (skb->protocol == htons(ETH_P_8021Q) ||
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307945 skb->protocol == htons(ETH_P_8021AD)) {
7946 if (tg3_tso_bug_gso_check(tnapi, skb))
7947 return tg3_tso_bug(tp, tnapi, txq, skb);
7948 goto drop;
7949 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04007950
Eric Dumazeta5a11952012-01-23 01:22:09 +00007951 if (!skb_is_gso_v6(skb)) {
Michael Chand71c0dc2014-05-11 20:22:53 -07007952 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307953 tg3_flag(tp, TSO_BUG)) {
7954 if (tg3_tso_bug_gso_check(tnapi, skb))
7955 return tg3_tso_bug(tp, tnapi, txq, skb);
7956 goto drop;
7957 }
Michael Chand3f6f3a2014-05-11 20:22:54 -07007958 ip_csum = iph->check;
7959 ip_tot_len = iph->tot_len;
Matt Carlson34195c32010-07-11 09:31:42 +00007960 iph->check = 0;
7961 iph->tot_len = htons(mss + hdr_len);
7962 }
7963
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7965 TXD_FLAG_CPU_POST_DMA);
7966
Michael Chand3f6f3a2014-05-11 20:22:54 -07007967 tcph = tcp_hdr(skb);
7968 tcp_csum = tcph->check;
7969
Joe Perches63c3a662011-04-26 08:12:10 +00007970 if (tg3_flag(tp, HW_TSO_1) ||
7971 tg3_flag(tp, HW_TSO_2) ||
7972 tg3_flag(tp, HW_TSO_3)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07007973 tcph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007975 } else {
7976 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
7977 0, IPPROTO_TCP, 0);
7978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979
Joe Perches63c3a662011-04-26 08:12:10 +00007980 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007981 mss |= (hdr_len & 0xc) << 12;
7982 if (hdr_len & 0x10)
7983 base_flags |= 0x00000010;
7984 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007985 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007986 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007987 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007988 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007989 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990 int tsflags;
7991
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007992 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 mss |= (tsflags << 11);
7994 }
7995 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007996 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 int tsflags;
7998
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007999 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000 base_flags |= tsflags << 12;
8001 }
8002 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04008003 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
8004 /* HW/FW can not correctly checksum packets that have been
8005 * vlan encapsulated.
8006 */
8007 if (skb->protocol == htons(ETH_P_8021Q) ||
8008 skb->protocol == htons(ETH_P_8021AD)) {
8009 if (skb_checksum_help(skb))
8010 goto drop;
8011 } else {
8012 base_flags |= TXD_FLAG_TCPUDP_CSUM;
8013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00008015
Matt Carlson93a700a2011-08-31 11:44:54 +00008016 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
8017 !mss && skb->len > VLAN_ETH_FRAME_LEN)
8018 base_flags |= TXD_FLAG_JMB_PKT;
8019
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008020 if (skb_vlan_tag_present(skb)) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008021 base_flags |= TXD_FLAG_VLAN;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008022 vlan = skb_vlan_tag_get(skb);
Matt Carlson92cd3a12011-07-27 14:20:47 +00008023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00008025 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
8026 tg3_flag(tp, TX_TSTAMP_EN)) {
8027 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
8028 base_flags |= TXD_FLAG_HWTSTAMP;
8029 }
8030
Alexander Duyckf4188d82009-12-02 16:48:38 +00008031 len = skb_headlen(skb);
8032
8033 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00008034 if (pci_dma_mapping_error(tp->pdev, mapping))
8035 goto drop;
8036
David S. Miller90079ce2008-09-11 04:52:51 -07008037
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008038 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008039 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040
8041 would_hit_hwbug = 0;
8042
Joe Perches63c3a662011-04-26 08:12:10 +00008043 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07008044 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045
Matt Carlson84b67b22011-07-27 14:20:52 +00008046 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00008047 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008048 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008049 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008050 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008051 u32 tmp_mss = mss;
8052
8053 if (!tg3_flag(tp, HW_TSO_1) &&
8054 !tg3_flag(tp, HW_TSO_2) &&
8055 !tg3_flag(tp, HW_TSO_3))
8056 tmp_mss = 0;
8057
Matt Carlsonc5665a52012-02-13 10:20:12 +00008058 /* Now loop through additional data
8059 * fragments, and queue them.
8060 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008061 last = skb_shinfo(skb)->nr_frags - 1;
8062 for (i = 0; i <= last; i++) {
8063 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8064
Eric Dumazet9e903e02011-10-18 21:00:24 +00008065 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008066 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008067 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008069 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008070 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008071 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008072 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008073 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074
Matt Carlsonb9e45482011-11-04 09:14:59 +00008075 if (!budget ||
8076 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008077 len, base_flags |
8078 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008079 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008080 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008081 break;
8082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083 }
8084 }
8085
8086 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008087 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05308089 if (mss && tg3_tso_bug_gso_check(tnapi, skb)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07008090 /* If it's a TSO packet, do GSO instead of
8091 * allocating and copying to a large linear SKB
8092 */
8093 if (ip_tot_len) {
8094 iph->check = ip_csum;
8095 iph->tot_len = ip_tot_len;
8096 }
8097 tcph->check = tcp_csum;
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07008098 return tg3_tso_bug(tp, tnapi, txq, skb);
Michael Chand3f6f3a2014-05-11 20:22:54 -07008099 }
8100
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101 /* If the workaround fails due to memory/mapping
8102 * failure, silently drop this packet.
8103 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008104 entry = tnapi->tx_prod;
8105 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008106 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008107 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008108 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109 }
8110
Richard Cochrand515b452011-06-19 03:31:41 +00008111 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008112 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008113
Michael Chan6541b802012-03-04 14:48:14 +00008114 /* Sync BD data before updating mailbox */
8115 wmb();
8116
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008117 tnapi->tx_prod = entry;
8118 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008119 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008120
8121 /* netif_tx_stop_queue() must be done before checking
8122 * checking tx index in tg3_tx_avail() below, because in
8123 * tg3_tx(), we update tx index before checking for
8124 * netif_tx_queue_stopped().
8125 */
8126 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008127 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008128 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130
Prashant Sreedharan2c7c9ea2014-10-13 09:21:42 -07008131 if (!skb->xmit_more || netif_xmit_stopped(txq)) {
8132 /* Packets are ready, update Tx producer idx on card. */
8133 tw32_tx_mbox(tnapi->prodmbox, entry);
8134 mmiowb();
8135 }
8136
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008138
8139dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008140 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008141 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008142drop:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07008143 dev_kfree_skb_any(skb);
Eric Dumazet48855432011-10-24 07:53:03 +00008144drop_nofree:
8145 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008146 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147}
8148
Matt Carlson6e01b202011-08-19 13:58:20 +00008149static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8150{
8151 if (enable) {
8152 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8153 MAC_MODE_PORT_MODE_MASK);
8154
8155 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8156
8157 if (!tg3_flag(tp, 5705_PLUS))
8158 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8159
8160 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8161 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8162 else
8163 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8164 } else {
8165 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8166
8167 if (tg3_flag(tp, 5705_PLUS) ||
8168 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008169 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008170 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8171 }
8172
8173 tw32(MAC_MODE, tp->mac_mode);
8174 udelay(40);
8175}
8176
Matt Carlson941ec902011-08-19 13:58:23 +00008177static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008178{
Matt Carlson941ec902011-08-19 13:58:23 +00008179 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008180
8181 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008182 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008183
Matt Carlson941ec902011-08-19 13:58:23 +00008184 if (extlpbk && tg3_phy_set_extloopbk(tp))
8185 return -EIO;
8186
8187 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008188 switch (speed) {
8189 case SPEED_10:
8190 break;
8191 case SPEED_100:
8192 bmcr |= BMCR_SPEED100;
8193 break;
8194 case SPEED_1000:
8195 default:
8196 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8197 speed = SPEED_100;
8198 bmcr |= BMCR_SPEED100;
8199 } else {
8200 speed = SPEED_1000;
8201 bmcr |= BMCR_SPEED1000;
8202 }
8203 }
8204
Matt Carlson941ec902011-08-19 13:58:23 +00008205 if (extlpbk) {
8206 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8207 tg3_readphy(tp, MII_CTRL1000, &val);
8208 val |= CTL1000_AS_MASTER |
8209 CTL1000_ENABLE_MASTER;
8210 tg3_writephy(tp, MII_CTRL1000, val);
8211 } else {
8212 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8213 MII_TG3_FET_PTEST_TRIM_2;
8214 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8215 }
8216 } else
8217 bmcr |= BMCR_LOOPBACK;
8218
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008219 tg3_writephy(tp, MII_BMCR, bmcr);
8220
8221 /* The write needs to be flushed for the FETs */
8222 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8223 tg3_readphy(tp, MII_BMCR, &bmcr);
8224
8225 udelay(40);
8226
8227 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008228 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008229 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008230 MII_TG3_FET_PTEST_FRC_TX_LINK |
8231 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8232
8233 /* The write needs to be flushed for the AC131 */
8234 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8235 }
8236
8237 /* Reset to prevent losing 1st rx packet intermittently */
8238 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8239 tg3_flag(tp, 5780_CLASS)) {
8240 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8241 udelay(10);
8242 tw32_f(MAC_RX_MODE, tp->rx_mode);
8243 }
8244
8245 mac_mode = tp->mac_mode &
8246 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8247 if (speed == SPEED_1000)
8248 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8249 else
8250 mac_mode |= MAC_MODE_PORT_MODE_MII;
8251
Joe Perches41535772013-02-16 11:20:04 +00008252 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008253 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8254
8255 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8256 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8257 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8258 mac_mode |= MAC_MODE_LINK_POLARITY;
8259
8260 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8261 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8262 }
8263
8264 tw32(MAC_MODE, mac_mode);
8265 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008266
8267 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008268}
8269
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008270static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008271{
8272 struct tg3 *tp = netdev_priv(dev);
8273
8274 if (features & NETIF_F_LOOPBACK) {
8275 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8276 return;
8277
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008278 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008279 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008280 netif_carrier_on(tp->dev);
8281 spin_unlock_bh(&tp->lock);
8282 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8283 } else {
8284 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8285 return;
8286
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008287 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008288 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008289 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008290 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008291 spin_unlock_bh(&tp->lock);
8292 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8293 }
8294}
8295
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008296static netdev_features_t tg3_fix_features(struct net_device *dev,
8297 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008298{
8299 struct tg3 *tp = netdev_priv(dev);
8300
Joe Perches63c3a662011-04-26 08:12:10 +00008301 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008302 features &= ~NETIF_F_ALL_TSO;
8303
8304 return features;
8305}
8306
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008307static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008308{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008309 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008310
8311 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8312 tg3_set_loopback(dev, features);
8313
8314 return 0;
8315}
8316
Matt Carlson21f581a2009-08-28 14:00:25 +00008317static void tg3_rx_prodring_free(struct tg3 *tp,
8318 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320 int i;
8321
Matt Carlson8fea32b2010-09-15 08:59:58 +00008322 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008323 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008324 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008325 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008326 tp->rx_pkt_map_sz);
8327
Joe Perches63c3a662011-04-26 08:12:10 +00008328 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008329 for (i = tpr->rx_jmb_cons_idx;
8330 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008331 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008332 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008333 TG3_RX_JMB_MAP_SZ);
8334 }
8335 }
8336
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008337 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339
Matt Carlson2c49a442010-09-30 10:34:35 +00008340 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008341 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008342 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343
Joe Perches63c3a662011-04-26 08:12:10 +00008344 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008345 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008346 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008347 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 }
8349}
8350
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008351/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352 *
8353 * The chip has been shut down and the driver detached from
8354 * the networking, so no interrupts or new tx packets will
8355 * end up in the driver. tp->{tx,}lock are held and thus
8356 * we may not sleep.
8357 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008358static int tg3_rx_prodring_alloc(struct tg3 *tp,
8359 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008360{
Matt Carlson287be122009-08-28 13:58:46 +00008361 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008363 tpr->rx_std_cons_idx = 0;
8364 tpr->rx_std_prod_idx = 0;
8365 tpr->rx_jmb_cons_idx = 0;
8366 tpr->rx_jmb_prod_idx = 0;
8367
Matt Carlson8fea32b2010-09-15 08:59:58 +00008368 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008369 memset(&tpr->rx_std_buffers[0], 0,
8370 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008371 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008372 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008373 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008374 goto done;
8375 }
8376
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008378 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379
Matt Carlson287be122009-08-28 13:58:46 +00008380 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008381 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008382 tp->dev->mtu > ETH_DATA_LEN)
8383 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8384 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008385
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 /* Initialize invariants of the rings, we only set this
8387 * stuff once. This works because the card does not
8388 * write into the rx buffer posting rings.
8389 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008390 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008391 struct tg3_rx_buffer_desc *rxd;
8392
Matt Carlson21f581a2009-08-28 14:00:25 +00008393 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008394 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8396 rxd->opaque = (RXD_OPAQUE_RING_STD |
8397 (i << RXD_OPAQUE_INDEX_SHIFT));
8398 }
8399
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008400 /* Now allocate fresh SKBs for each rx ring. */
8401 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008402 unsigned int frag_size;
8403
8404 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8405 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008406 netdev_warn(tp->dev,
8407 "Using a smaller RX standard ring. Only "
8408 "%d out of %d buffers were allocated "
8409 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008410 if (i == 0)
8411 goto initfail;
8412 tp->rx_pending = i;
8413 break;
8414 }
8415 }
8416
Joe Perches63c3a662011-04-26 08:12:10 +00008417 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008418 goto done;
8419
Matt Carlson2c49a442010-09-30 10:34:35 +00008420 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008421
Joe Perches63c3a662011-04-26 08:12:10 +00008422 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008423 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424
Matt Carlson2c49a442010-09-30 10:34:35 +00008425 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008426 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427
Matt Carlson0d86df82010-02-17 15:17:00 +00008428 rxd = &tpr->rx_jmb[i].std;
8429 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8430 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8431 RXD_FLAG_JUMBO;
8432 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8433 (i << RXD_OPAQUE_INDEX_SHIFT));
8434 }
8435
8436 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008437 unsigned int frag_size;
8438
8439 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8440 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008441 netdev_warn(tp->dev,
8442 "Using a smaller RX jumbo ring. Only %d "
8443 "out of %d buffers were allocated "
8444 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008445 if (i == 0)
8446 goto initfail;
8447 tp->rx_jumbo_pending = i;
8448 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449 }
8450 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008451
8452done:
Michael Chan32d8c572006-07-25 16:38:29 -07008453 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008454
8455initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008456 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008457 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458}
8459
Matt Carlson21f581a2009-08-28 14:00:25 +00008460static void tg3_rx_prodring_fini(struct tg3 *tp,
8461 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462{
Matt Carlson21f581a2009-08-28 14:00:25 +00008463 kfree(tpr->rx_std_buffers);
8464 tpr->rx_std_buffers = NULL;
8465 kfree(tpr->rx_jmb_buffers);
8466 tpr->rx_jmb_buffers = NULL;
8467 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008468 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8469 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008470 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008472 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008473 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8474 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008475 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008477}
8478
Matt Carlson21f581a2009-08-28 14:00:25 +00008479static int tg3_rx_prodring_init(struct tg3 *tp,
8480 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008481{
Matt Carlson2c49a442010-09-30 10:34:35 +00008482 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8483 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008484 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008485 return -ENOMEM;
8486
Matt Carlson4bae65c2010-11-24 08:31:52 +00008487 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8488 TG3_RX_STD_RING_BYTES(tp),
8489 &tpr->rx_std_mapping,
8490 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008491 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008492 goto err_out;
8493
Joe Perches63c3a662011-04-26 08:12:10 +00008494 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008495 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008496 GFP_KERNEL);
8497 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008498 goto err_out;
8499
Matt Carlson4bae65c2010-11-24 08:31:52 +00008500 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8501 TG3_RX_JMB_RING_BYTES(tp),
8502 &tpr->rx_jmb_mapping,
8503 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008504 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008505 goto err_out;
8506 }
8507
8508 return 0;
8509
8510err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008511 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008512 return -ENOMEM;
8513}
8514
8515/* Free up pending packets in all rx/tx rings.
8516 *
8517 * The chip has been shut down and the driver detached from
8518 * the networking, so no interrupts or new tx packets will
8519 * end up in the driver. tp->{tx,}lock is not held and we are not
8520 * in an interrupt context and thus may sleep.
8521 */
8522static void tg3_free_rings(struct tg3 *tp)
8523{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008524 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008525
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008526 for (j = 0; j < tp->irq_cnt; j++) {
8527 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008528
Matt Carlson8fea32b2010-09-15 08:59:58 +00008529 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008530
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008531 if (!tnapi->tx_buffers)
8532 continue;
8533
Matt Carlson0d681b22011-07-27 14:20:49 +00008534 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8535 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008536
Matt Carlson0d681b22011-07-27 14:20:49 +00008537 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008538 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008539
Matt Carlsonba1142e2011-11-04 09:15:00 +00008540 tg3_tx_skb_unmap(tnapi, i,
8541 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008542
8543 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008544 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008545 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008546 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008547}
8548
8549/* Initialize tx/rx rings for packet processing.
8550 *
8551 * The chip has been shut down and the driver detached from
8552 * the networking, so no interrupts or new tx packets will
8553 * end up in the driver. tp->{tx,}lock are held and thus
8554 * we may not sleep.
8555 */
8556static int tg3_init_rings(struct tg3 *tp)
8557{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008558 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008559
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008560 /* Free up all the SKBs. */
8561 tg3_free_rings(tp);
8562
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008563 for (i = 0; i < tp->irq_cnt; i++) {
8564 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008565
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008566 tnapi->last_tag = 0;
8567 tnapi->last_irq_tag = 0;
8568 tnapi->hw_status->status = 0;
8569 tnapi->hw_status->status_tag = 0;
8570 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8571
8572 tnapi->tx_prod = 0;
8573 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008574 if (tnapi->tx_ring)
8575 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008576
8577 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008578 if (tnapi->rx_rcb)
8579 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008580
Thadeu Lima de Souza Cascardoa620a6b2014-11-25 14:21:11 -02008581 if (tnapi->prodring.rx_std &&
8582 tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008583 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008584 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008585 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008586 }
Matt Carlson72334482009-08-28 14:03:01 +00008587
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008588 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008589}
8590
Michael Chan49a359e2012-09-28 07:12:37 +00008591static void tg3_mem_tx_release(struct tg3 *tp)
8592{
8593 int i;
8594
8595 for (i = 0; i < tp->irq_max; i++) {
8596 struct tg3_napi *tnapi = &tp->napi[i];
8597
8598 if (tnapi->tx_ring) {
8599 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8600 tnapi->tx_ring, tnapi->tx_desc_mapping);
8601 tnapi->tx_ring = NULL;
8602 }
8603
8604 kfree(tnapi->tx_buffers);
8605 tnapi->tx_buffers = NULL;
8606 }
8607}
8608
8609static int tg3_mem_tx_acquire(struct tg3 *tp)
8610{
8611 int i;
8612 struct tg3_napi *tnapi = &tp->napi[0];
8613
8614 /* If multivector TSS is enabled, vector 0 does not handle
8615 * tx interrupts. Don't allocate any resources for it.
8616 */
8617 if (tg3_flag(tp, ENABLE_TSS))
8618 tnapi++;
8619
8620 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8621 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8622 TG3_TX_RING_SIZE, GFP_KERNEL);
8623 if (!tnapi->tx_buffers)
8624 goto err_out;
8625
8626 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8627 TG3_TX_RING_BYTES,
8628 &tnapi->tx_desc_mapping,
8629 GFP_KERNEL);
8630 if (!tnapi->tx_ring)
8631 goto err_out;
8632 }
8633
8634 return 0;
8635
8636err_out:
8637 tg3_mem_tx_release(tp);
8638 return -ENOMEM;
8639}
8640
8641static void tg3_mem_rx_release(struct tg3 *tp)
8642{
8643 int i;
8644
8645 for (i = 0; i < tp->irq_max; i++) {
8646 struct tg3_napi *tnapi = &tp->napi[i];
8647
8648 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8649
8650 if (!tnapi->rx_rcb)
8651 continue;
8652
8653 dma_free_coherent(&tp->pdev->dev,
8654 TG3_RX_RCB_RING_BYTES(tp),
8655 tnapi->rx_rcb,
8656 tnapi->rx_rcb_mapping);
8657 tnapi->rx_rcb = NULL;
8658 }
8659}
8660
8661static int tg3_mem_rx_acquire(struct tg3 *tp)
8662{
8663 unsigned int i, limit;
8664
8665 limit = tp->rxq_cnt;
8666
8667 /* If RSS is enabled, we need a (dummy) producer ring
8668 * set on vector zero. This is the true hw prodring.
8669 */
8670 if (tg3_flag(tp, ENABLE_RSS))
8671 limit++;
8672
8673 for (i = 0; i < limit; i++) {
8674 struct tg3_napi *tnapi = &tp->napi[i];
8675
8676 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8677 goto err_out;
8678
8679 /* If multivector RSS is enabled, vector 0
8680 * does not handle rx or tx interrupts.
8681 * Don't allocate any resources for it.
8682 */
8683 if (!i && tg3_flag(tp, ENABLE_RSS))
8684 continue;
8685
Joe Perchesede23fa2013-08-26 22:45:23 -07008686 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8687 TG3_RX_RCB_RING_BYTES(tp),
8688 &tnapi->rx_rcb_mapping,
8689 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008690 if (!tnapi->rx_rcb)
8691 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008692 }
8693
8694 return 0;
8695
8696err_out:
8697 tg3_mem_rx_release(tp);
8698 return -ENOMEM;
8699}
8700
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008701/*
8702 * Must not be invoked with interrupt sources disabled and
8703 * the hardware shutdown down.
8704 */
8705static void tg3_free_consistent(struct tg3 *tp)
8706{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008707 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008708
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008709 for (i = 0; i < tp->irq_cnt; i++) {
8710 struct tg3_napi *tnapi = &tp->napi[i];
8711
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008712 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008713 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8714 tnapi->hw_status,
8715 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008716 tnapi->hw_status = NULL;
8717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008719
Michael Chan49a359e2012-09-28 07:12:37 +00008720 tg3_mem_rx_release(tp);
8721 tg3_mem_tx_release(tp);
8722
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008724 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8725 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 tp->hw_stats = NULL;
8727 }
8728}
8729
8730/*
8731 * Must not be invoked with interrupt sources disabled and
8732 * the hardware shutdown down. Can sleep.
8733 */
8734static int tg3_alloc_consistent(struct tg3 *tp)
8735{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008736 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008737
Joe Perchesede23fa2013-08-26 22:45:23 -07008738 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8739 sizeof(struct tg3_hw_stats),
8740 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 if (!tp->hw_stats)
8742 goto err_out;
8743
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008744 for (i = 0; i < tp->irq_cnt; i++) {
8745 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008746 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008747
Joe Perchesede23fa2013-08-26 22:45:23 -07008748 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8749 TG3_HW_STATUS_SIZE,
8750 &tnapi->status_mapping,
8751 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008752 if (!tnapi->hw_status)
8753 goto err_out;
8754
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008755 sblk = tnapi->hw_status;
8756
Michael Chan49a359e2012-09-28 07:12:37 +00008757 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008758 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008759
Michael Chan49a359e2012-09-28 07:12:37 +00008760 /*
8761 * When RSS is enabled, the status block format changes
8762 * slightly. The "rx_jumbo_consumer", "reserved",
8763 * and "rx_mini_consumer" members get mapped to the
8764 * other three rx return ring producer indexes.
8765 */
8766 switch (i) {
8767 case 1:
8768 prodptr = &sblk->idx[0].rx_producer;
8769 break;
8770 case 2:
8771 prodptr = &sblk->rx_jumbo_consumer;
8772 break;
8773 case 3:
8774 prodptr = &sblk->reserved;
8775 break;
8776 case 4:
8777 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008778 break;
8779 }
Michael Chan49a359e2012-09-28 07:12:37 +00008780 tnapi->rx_rcb_prod_idx = prodptr;
8781 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008782 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008783 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008784 }
8785
Michael Chan49a359e2012-09-28 07:12:37 +00008786 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8787 goto err_out;
8788
Linus Torvalds1da177e2005-04-16 15:20:36 -07008789 return 0;
8790
8791err_out:
8792 tg3_free_consistent(tp);
8793 return -ENOMEM;
8794}
8795
8796#define MAX_WAIT_CNT 1000
8797
8798/* To stop a block, clear the enable bit and poll till it
8799 * clears. tp->lock is held.
8800 */
Joe Perches953c96e2013-04-09 10:18:14 +00008801static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008802{
8803 unsigned int i;
8804 u32 val;
8805
Joe Perches63c3a662011-04-26 08:12:10 +00008806 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807 switch (ofs) {
8808 case RCVLSC_MODE:
8809 case DMAC_MODE:
8810 case MBFREE_MODE:
8811 case BUFMGR_MODE:
8812 case MEMARB_MODE:
8813 /* We can't enable/disable these bits of the
8814 * 5705/5750, just say success.
8815 */
8816 return 0;
8817
8818 default:
8819 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821 }
8822
8823 val = tr32(ofs);
8824 val &= ~enable_bit;
8825 tw32_f(ofs, val);
8826
8827 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008828 if (pci_channel_offline(tp->pdev)) {
8829 dev_err(&tp->pdev->dev,
8830 "tg3_stop_block device offline, "
8831 "ofs=%lx enable_bit=%x\n",
8832 ofs, enable_bit);
8833 return -ENODEV;
8834 }
8835
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 udelay(100);
8837 val = tr32(ofs);
8838 if ((val & enable_bit) == 0)
8839 break;
8840 }
8841
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008842 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008843 dev_err(&tp->pdev->dev,
8844 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8845 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 return -ENODEV;
8847 }
8848
8849 return 0;
8850}
8851
8852/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008853static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854{
8855 int i, err;
8856
8857 tg3_disable_ints(tp);
8858
Gavin Shan6d446ec2013-06-25 15:24:32 +08008859 if (pci_channel_offline(tp->pdev)) {
8860 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8861 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8862 err = -ENODEV;
8863 goto err_no_dev;
8864 }
8865
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 tp->rx_mode &= ~RX_MODE_ENABLE;
8867 tw32_f(MAC_RX_MODE, tp->rx_mode);
8868 udelay(10);
8869
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008870 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8871 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8872 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8873 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8874 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8875 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008877 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8878 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8879 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8880 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8881 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8882 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8883 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884
8885 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8886 tw32_f(MAC_MODE, tp->mac_mode);
8887 udelay(40);
8888
8889 tp->tx_mode &= ~TX_MODE_ENABLE;
8890 tw32_f(MAC_TX_MODE, tp->tx_mode);
8891
8892 for (i = 0; i < MAX_WAIT_CNT; i++) {
8893 udelay(100);
8894 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8895 break;
8896 }
8897 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008898 dev_err(&tp->pdev->dev,
8899 "%s timed out, TX_MODE_ENABLE will not clear "
8900 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008901 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902 }
8903
Michael Chane6de8ad2005-05-05 14:42:41 -07008904 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008905 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8906 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
8908 tw32(FTQ_RESET, 0xffffffff);
8909 tw32(FTQ_RESET, 0x00000000);
8910
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008911 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8912 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913
Gavin Shan6d446ec2013-06-25 15:24:32 +08008914err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008915 for (i = 0; i < tp->irq_cnt; i++) {
8916 struct tg3_napi *tnapi = &tp->napi[i];
8917 if (tnapi->hw_status)
8918 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 return err;
8922}
8923
Michael Chanee6a99b2007-07-18 21:49:10 -07008924/* Save PCI command register before chip reset */
8925static void tg3_save_pci_state(struct tg3 *tp)
8926{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008927 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008928}
8929
8930/* Restore PCI state after chip reset */
8931static void tg3_restore_pci_state(struct tg3 *tp)
8932{
8933 u32 val;
8934
8935 /* Re-enable indirect register accesses. */
8936 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8937 tp->misc_host_ctrl);
8938
8939 /* Set MAX PCI retry to zero. */
8940 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008941 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008942 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008943 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008944 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008945 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008946 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008947 PCISTATE_ALLOW_APE_SHMEM_WR |
8948 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008949 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8950
Matt Carlson8a6eac92007-10-21 16:17:55 -07008951 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008952
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008953 if (!tg3_flag(tp, PCI_EXPRESS)) {
8954 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8955 tp->pci_cacheline_sz);
8956 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8957 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008958 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008959
Michael Chanee6a99b2007-07-18 21:49:10 -07008960 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008961 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008962 u16 pcix_cmd;
8963
8964 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8965 &pcix_cmd);
8966 pcix_cmd &= ~PCI_X_CMD_ERO;
8967 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8968 pcix_cmd);
8969 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008970
Joe Perches63c3a662011-04-26 08:12:10 +00008971 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008972
8973 /* Chip reset on 5780 will reset MSI enable bit,
8974 * so need to restore it.
8975 */
Joe Perches63c3a662011-04-26 08:12:10 +00008976 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008977 u16 ctrl;
8978
8979 pci_read_config_word(tp->pdev,
8980 tp->msi_cap + PCI_MSI_FLAGS,
8981 &ctrl);
8982 pci_write_config_word(tp->pdev,
8983 tp->msi_cap + PCI_MSI_FLAGS,
8984 ctrl | PCI_MSI_FLAGS_ENABLE);
8985 val = tr32(MSGINT_MODE);
8986 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8987 }
8988 }
8989}
8990
Nithin Sujirf82995b2014-01-03 10:09:13 -08008991static void tg3_override_clk(struct tg3 *tp)
8992{
8993 u32 val;
8994
8995 switch (tg3_asic_rev(tp)) {
8996 case ASIC_REV_5717:
8997 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
8998 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
8999 TG3_CPMU_MAC_ORIDE_ENABLE);
9000 break;
9001
9002 case ASIC_REV_5719:
9003 case ASIC_REV_5720:
9004 tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9005 break;
9006
9007 default:
9008 return;
9009 }
9010}
9011
9012static void tg3_restore_clk(struct tg3 *tp)
9013{
9014 u32 val;
9015
9016 switch (tg3_asic_rev(tp)) {
9017 case ASIC_REV_5717:
9018 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9019 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
9020 val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
9021 break;
9022
9023 case ASIC_REV_5719:
9024 case ASIC_REV_5720:
9025 val = tr32(TG3_CPMU_CLCK_ORIDE);
9026 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9027 break;
9028
9029 default:
9030 return;
9031 }
9032}
9033
Linus Torvalds1da177e2005-04-16 15:20:36 -07009034/* tp->lock is held. */
9035static int tg3_chip_reset(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009036 __releases(tp->lock)
9037 __acquires(tp->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038{
9039 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07009040 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00009041 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01009043 if (!pci_device_is_present(tp->pdev))
9044 return -ENODEV;
9045
David S. Millerf49639e2006-06-09 11:58:36 -07009046 tg3_nvram_lock(tp);
9047
Matt Carlson77b483f2008-08-15 14:07:24 -07009048 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
9049
David S. Millerf49639e2006-06-09 11:58:36 -07009050 /* No matching tg3_nvram_unlock() after this because
9051 * chip reset below will undo the nvram lock.
9052 */
9053 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009054
Michael Chanee6a99b2007-07-18 21:49:10 -07009055 /* GRC_MISC_CFG core clock reset will clear the memory
9056 * enable bit in PCI register 4 and the MSI enable bit
9057 * on some chips, so we save relevant registers here.
9058 */
9059 tg3_save_pci_state(tp);
9060
Joe Perches41535772013-02-16 11:20:04 +00009061 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009062 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08009063 tw32(GRC_FASTBOOT_PC, 0);
9064
Linus Torvalds1da177e2005-04-16 15:20:36 -07009065 /*
9066 * We must avoid the readl() that normally takes place.
9067 * It locks machines, causes machine checks, and other
9068 * fun things. So, temporarily disable the 5701
9069 * hardware workaround, while we do the reset.
9070 */
Michael Chan1ee582d2005-08-09 20:16:46 -07009071 write_op = tp->write32;
9072 if (write_op == tg3_write_flush_reg32)
9073 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009074
Michael Chand18edcb2007-03-24 20:57:11 -07009075 /* Prevent the irq handler from reading or writing PCI registers
9076 * during chip reset when the memory enable bit in the PCI command
9077 * register may be cleared. The chip does not generate interrupt
9078 * at this time, but the irq handler may still be called due to irq
9079 * sharing or irqpoll.
9080 */
Joe Perches63c3a662011-04-26 08:12:10 +00009081 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009082 for (i = 0; i < tp->irq_cnt; i++) {
9083 struct tg3_napi *tnapi = &tp->napi[i];
9084 if (tnapi->hw_status) {
9085 tnapi->hw_status->status = 0;
9086 tnapi->hw_status->status_tag = 0;
9087 }
9088 tnapi->last_tag = 0;
9089 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07009090 }
Michael Chand18edcb2007-03-24 20:57:11 -07009091 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00009092
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009093 tg3_full_unlock(tp);
9094
Matt Carlson4f125f42009-09-01 12:55:02 +00009095 for (i = 0; i < tp->irq_cnt; i++)
9096 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07009097
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009098 tg3_full_lock(tp, 0);
9099
Joe Perches41535772013-02-16 11:20:04 +00009100 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00009101 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9102 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
9103 }
9104
Linus Torvalds1da177e2005-04-16 15:20:36 -07009105 /* do the reset */
9106 val = GRC_MISC_CFG_CORECLK_RESET;
9107
Joe Perches63c3a662011-04-26 08:12:10 +00009108 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00009109 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00009110 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009111 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00009112 tr32(TG3_PCIE_PHY_TSTCTL) ==
9113 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
9114 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9115
Joe Perches41535772013-02-16 11:20:04 +00009116 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117 tw32(GRC_MISC_CFG, (1 << 29));
9118 val |= (1 << 29);
9119 }
9120 }
9121
Joe Perches41535772013-02-16 11:20:04 +00009122 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009123 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9124 tw32(GRC_VCPU_EXT_CTRL,
9125 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9126 }
9127
Nithin Sujirf82995b2014-01-03 10:09:13 -08009128 /* Set the clock to the highest frequency to avoid timeouts. With link
9129 * aware mode, the clock speed could be slow and bootcode does not
9130 * complete within the expected time. Override the clock to allow the
9131 * bootcode to finish sooner and then restore it.
9132 */
9133 tg3_override_clk(tp);
9134
Matt Carlsonf37500d2010-08-02 11:25:59 +00009135 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009136 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009137 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009138
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139 tw32(GRC_MISC_CFG, val);
9140
Michael Chan1ee582d2005-08-09 20:16:46 -07009141 /* restore 5701 hardware bug workaround write method */
9142 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143
9144 /* Unfortunately, we have to delay before the PCI read back.
9145 * Some 575X chips even will not respond to a PCI cfg access
9146 * when the reset command is given to the chip.
9147 *
9148 * How do these hardware designers expect things to work
9149 * properly if the PCI write is posted for a long period
9150 * of time? It is always necessary to have some method by
9151 * which a register read back can occur to push the write
9152 * out which does the reset.
9153 *
9154 * For most tg3 variants the trick below was working.
9155 * Ho hum...
9156 */
9157 udelay(120);
9158
9159 /* Flush PCI posted writes. The normal MMIO registers
9160 * are inaccessible at this time so this is the only
9161 * way to make this reliably (actually, this is no longer
9162 * the case, see above). I tried to use indirect
9163 * register read/write but this upset some 5701 variants.
9164 */
9165 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9166
9167 udelay(120);
9168
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009169 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009170 u16 val16;
9171
Joe Perches41535772013-02-16 11:20:04 +00009172 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009173 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174 u32 cfg_val;
9175
9176 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009177 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178 udelay(100);
9179
9180 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9181 pci_write_config_dword(tp->pdev, 0xc4,
9182 cfg_val | (1 << 15));
9183 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009184
Matt Carlsone7126992009-08-25 10:08:16 +00009185 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009186 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009187 /*
9188 * Older PCIe devices only support the 128 byte
9189 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009190 */
Joe Perches63c3a662011-04-26 08:12:10 +00009191 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009192 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9193 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009194
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009195 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009196 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009197 PCI_EXP_DEVSTA_CED |
9198 PCI_EXP_DEVSTA_NFED |
9199 PCI_EXP_DEVSTA_FED |
9200 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009201 }
9202
Michael Chanee6a99b2007-07-18 21:49:10 -07009203 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204
Joe Perches63c3a662011-04-26 08:12:10 +00009205 tg3_flag_clear(tp, CHIP_RESETTING);
9206 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009207
Michael Chanee6a99b2007-07-18 21:49:10 -07009208 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009209 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009210 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009211 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212
Joe Perches41535772013-02-16 11:20:04 +00009213 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009214 tg3_stop_fw(tp);
9215 tw32(0x5000, 0x400);
9216 }
9217
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009218 if (tg3_flag(tp, IS_SSB_CORE)) {
9219 /*
9220 * BCM4785: In order to avoid repercussions from using
9221 * potentially defective internal ROM, stop the Rx RISC CPU,
9222 * which is not required.
9223 */
9224 tg3_stop_fw(tp);
9225 tg3_halt_cpu(tp, RX_CPU_BASE);
9226 }
9227
Nithin Sujirfb03a432013-05-21 12:57:32 +00009228 err = tg3_poll_fw(tp);
9229 if (err)
9230 return err;
9231
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232 tw32(GRC_MODE, tp->grc_mode);
9233
Joe Perches41535772013-02-16 11:20:04 +00009234 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009235 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236
9237 tw32(0xc4, val | (1 << 15));
9238 }
9239
9240 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009241 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009243 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9245 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9246 }
9247
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009248 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009249 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009250 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009251 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009252 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009253 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009255 val = 0;
9256
9257 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258 udelay(40);
9259
Matt Carlson77b483f2008-08-15 14:07:24 -07009260 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9261
Matt Carlson0a9140c2009-08-28 12:27:50 +00009262 tg3_mdio_start(tp);
9263
Joe Perches63c3a662011-04-26 08:12:10 +00009264 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009265 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9266 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009267 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009268 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269
9270 tw32(0x7c00, val | (1 << 25));
9271 }
9272
Nithin Sujirf82995b2014-01-03 10:09:13 -08009273 tg3_restore_clk(tp);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009274
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009276 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009277 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9278 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9279
Joe Perches63c3a662011-04-26 08:12:10 +00009280 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9282 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9283 u32 nic_cfg;
9284
9285 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9286 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009287 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009288 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009289 if (tg3_flag(tp, 5750_PLUS))
9290 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009291
9292 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9293 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9294 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9295 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9296 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297 }
9298 }
9299
9300 return 0;
9301}
9302
Matt Carlson65ec6982012-02-28 23:33:37 +00009303static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9304static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Michael Chane565eec2014-01-03 10:09:12 -08009305static void __tg3_set_rx_mode(struct net_device *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009306
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009308static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309{
9310 int err;
9311
9312 tg3_stop_fw(tp);
9313
Michael Chan944d9802005-05-29 14:57:48 -07009314 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009316 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 err = tg3_chip_reset(tp);
9318
Joe Perches953c96e2013-04-09 10:18:14 +00009319 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009320
Michael Chan944d9802005-05-29 14:57:48 -07009321 tg3_write_sig_legacy(tp, kind);
9322 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323
Matt Carlson92feeab2011-12-08 14:40:14 +00009324 if (tp->hw_stats) {
9325 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009326 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009327 tg3_get_estats(tp, &tp->estats_prev);
9328
9329 /* And make sure the next sample is new data */
9330 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9331 }
9332
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009333 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334}
9335
Linus Torvalds1da177e2005-04-16 15:20:36 -07009336static int tg3_set_mac_addr(struct net_device *dev, void *p)
9337{
9338 struct tg3 *tp = netdev_priv(dev);
9339 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009340 int err = 0;
9341 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342
Michael Chanf9804dd2005-09-27 12:13:10 -07009343 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009344 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009345
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9347
Michael Chane75f7c92006-03-20 21:33:26 -08009348 if (!netif_running(dev))
9349 return 0;
9350
Joe Perches63c3a662011-04-26 08:12:10 +00009351 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009352 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009353
Michael Chan986e0ae2007-05-05 12:10:20 -07009354 addr0_high = tr32(MAC_ADDR_0_HIGH);
9355 addr0_low = tr32(MAC_ADDR_0_LOW);
9356 addr1_high = tr32(MAC_ADDR_1_HIGH);
9357 addr1_low = tr32(MAC_ADDR_1_LOW);
9358
9359 /* Skip MAC addr 1 if ASF is using it. */
9360 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9361 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009362 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009363 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009364 spin_lock_bh(&tp->lock);
9365 __tg3_set_mac_addr(tp, skip_mac_1);
Michael Chane565eec2014-01-03 10:09:12 -08009366 __tg3_set_rx_mode(dev);
Michael Chan986e0ae2007-05-05 12:10:20 -07009367 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368
Michael Chanb9ec6c12006-07-25 16:37:27 -07009369 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009370}
9371
9372/* tp->lock is held. */
9373static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9374 dma_addr_t mapping, u32 maxlen_flags,
9375 u32 nic_addr)
9376{
9377 tg3_write_mem(tp,
9378 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9379 ((u64) mapping >> 32));
9380 tg3_write_mem(tp,
9381 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9382 ((u64) mapping & 0xffffffff));
9383 tg3_write_mem(tp,
9384 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9385 maxlen_flags);
9386
Joe Perches63c3a662011-04-26 08:12:10 +00009387 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 tg3_write_mem(tp,
9389 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9390 nic_addr);
9391}
9392
Michael Chana489b6d2012-09-28 07:12:39 +00009393
9394static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009395{
Michael Chana489b6d2012-09-28 07:12:39 +00009396 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009397
Joe Perches63c3a662011-04-26 08:12:10 +00009398 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009399 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9400 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9401 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009402 } else {
9403 tw32(HOSTCC_TXCOL_TICKS, 0);
9404 tw32(HOSTCC_TXMAX_FRAMES, 0);
9405 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009406
9407 for (; i < tp->txq_cnt; i++) {
9408 u32 reg;
9409
9410 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9411 tw32(reg, ec->tx_coalesce_usecs);
9412 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9413 tw32(reg, ec->tx_max_coalesced_frames);
9414 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9415 tw32(reg, ec->tx_max_coalesced_frames_irq);
9416 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009417 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009418
Michael Chana489b6d2012-09-28 07:12:39 +00009419 for (; i < tp->irq_max - 1; i++) {
9420 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9421 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9422 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9423 }
9424}
9425
9426static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9427{
9428 int i = 0;
9429 u32 limit = tp->rxq_cnt;
9430
Joe Perches63c3a662011-04-26 08:12:10 +00009431 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009432 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9433 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9434 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009435 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009436 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009437 tw32(HOSTCC_RXCOL_TICKS, 0);
9438 tw32(HOSTCC_RXMAX_FRAMES, 0);
9439 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009440 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009441
Michael Chana489b6d2012-09-28 07:12:39 +00009442 for (; i < limit; i++) {
9443 u32 reg;
9444
9445 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9446 tw32(reg, ec->rx_coalesce_usecs);
9447 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9448 tw32(reg, ec->rx_max_coalesced_frames);
9449 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9450 tw32(reg, ec->rx_max_coalesced_frames_irq);
9451 }
9452
9453 for (; i < tp->irq_max - 1; i++) {
9454 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9455 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9456 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9457 }
9458}
9459
9460static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9461{
9462 tg3_coal_tx_init(tp, ec);
9463 tg3_coal_rx_init(tp, ec);
9464
Joe Perches63c3a662011-04-26 08:12:10 +00009465 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009466 u32 val = ec->stats_block_coalesce_usecs;
9467
Matt Carlsonb6080e12009-09-01 13:12:00 +00009468 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9469 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9470
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009471 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009472 val = 0;
9473
9474 tw32(HOSTCC_STAT_COAL_TICKS, val);
9475 }
9476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477
9478/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009479static void tg3_tx_rcbs_disable(struct tg3 *tp)
9480{
9481 u32 txrcb, limit;
9482
9483 /* Disable all transmit rings but the first. */
9484 if (!tg3_flag(tp, 5705_PLUS))
9485 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9486 else if (tg3_flag(tp, 5717_PLUS))
9487 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9488 else if (tg3_flag(tp, 57765_CLASS) ||
9489 tg3_asic_rev(tp) == ASIC_REV_5762)
9490 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9491 else
9492 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9493
9494 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9495 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9496 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9497 BDINFO_FLAGS_DISABLED);
9498}
9499
9500/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009501static void tg3_tx_rcbs_init(struct tg3 *tp)
9502{
9503 int i = 0;
9504 u32 txrcb = NIC_SRAM_SEND_RCB;
9505
9506 if (tg3_flag(tp, ENABLE_TSS))
9507 i++;
9508
9509 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9510 struct tg3_napi *tnapi = &tp->napi[i];
9511
9512 if (!tnapi->tx_ring)
9513 continue;
9514
9515 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9516 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9517 NIC_SRAM_TX_BUFFER_DESC);
9518 }
9519}
9520
9521/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009522static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9523{
9524 u32 rxrcb, limit;
9525
9526 /* Disable all receive return rings but the first. */
9527 if (tg3_flag(tp, 5717_PLUS))
9528 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9529 else if (!tg3_flag(tp, 5705_PLUS))
9530 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9531 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9532 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9533 tg3_flag(tp, 57765_CLASS))
9534 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9535 else
9536 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9537
9538 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9539 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9540 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9541 BDINFO_FLAGS_DISABLED);
9542}
9543
9544/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009545static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9546{
9547 int i = 0;
9548 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9549
9550 if (tg3_flag(tp, ENABLE_RSS))
9551 i++;
9552
9553 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9554 struct tg3_napi *tnapi = &tp->napi[i];
9555
9556 if (!tnapi->rx_rcb)
9557 continue;
9558
9559 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9560 (tp->rx_ret_ring_mask + 1) <<
9561 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9562 }
9563}
9564
9565/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009566static void tg3_rings_reset(struct tg3 *tp)
9567{
9568 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009569 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009570 struct tg3_napi *tnapi = &tp->napi[0];
9571
Nithin Sujir328947f2013-05-23 11:11:24 +00009572 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009573
Nithin Sujir328947f2013-05-23 11:11:24 +00009574 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009575
9576 /* Disable interrupts */
9577 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009578 tp->napi[0].chk_msi_cnt = 0;
9579 tp->napi[0].last_rx_cons = 0;
9580 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009581
9582 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009583 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009584 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009585 tp->napi[i].tx_prod = 0;
9586 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009587 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009588 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009589 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9590 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009591 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009592 tp->napi[i].last_rx_cons = 0;
9593 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009594 }
Joe Perches63c3a662011-04-26 08:12:10 +00009595 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009596 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009597 } else {
9598 tp->napi[0].tx_prod = 0;
9599 tp->napi[0].tx_cons = 0;
9600 tw32_mailbox(tp->napi[0].prodmbox, 0);
9601 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9602 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009603
9604 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009605 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009606 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9607 for (i = 0; i < 16; i++)
9608 tw32_tx_mbox(mbox + i * 8, 0);
9609 }
9610
Matt Carlson2d31eca2009-09-01 12:53:31 +00009611 /* Clear status block in ram. */
9612 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9613
9614 /* Set status block DMA address */
9615 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9616 ((u64) tnapi->status_mapping >> 32));
9617 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9618 ((u64) tnapi->status_mapping & 0xffffffff));
9619
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009620 stblk = HOSTCC_STATBLCK_RING1;
9621
9622 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9623 u64 mapping = (u64)tnapi->status_mapping;
9624 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9625 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009626 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009627
9628 /* Clear status block in ram. */
9629 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009630 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009631
9632 tg3_tx_rcbs_init(tp);
9633 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009634}
9635
Matt Carlsoneb07a942011-04-20 07:57:36 +00009636static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9637{
9638 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9639
Joe Perches63c3a662011-04-26 08:12:10 +00009640 if (!tg3_flag(tp, 5750_PLUS) ||
9641 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009642 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9643 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009644 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009645 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009646 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9647 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009648 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9649 else
9650 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9651
9652 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9653 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9654
9655 val = min(nic_rep_thresh, host_rep_thresh);
9656 tw32(RCVBDI_STD_THRESH, val);
9657
Joe Perches63c3a662011-04-26 08:12:10 +00009658 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009659 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9660
Joe Perches63c3a662011-04-26 08:12:10 +00009661 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009662 return;
9663
Matt Carlson513aa6e2011-11-21 15:01:18 +00009664 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009665
9666 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9667
9668 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9669 tw32(RCVBDI_JUMBO_THRESH, val);
9670
Joe Perches63c3a662011-04-26 08:12:10 +00009671 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009672 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9673}
9674
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009675static inline u32 calc_crc(unsigned char *buf, int len)
9676{
9677 u32 reg;
9678 u32 tmp;
9679 int j, k;
9680
9681 reg = 0xffffffff;
9682
9683 for (j = 0; j < len; j++) {
9684 reg ^= buf[j];
9685
9686 for (k = 0; k < 8; k++) {
9687 tmp = reg & 0x01;
9688
9689 reg >>= 1;
9690
9691 if (tmp)
9692 reg ^= 0xedb88320;
9693 }
9694 }
9695
9696 return ~reg;
9697}
9698
9699static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9700{
9701 /* accept or reject all multicast frames */
9702 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9703 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9704 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9705 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9706}
9707
9708static void __tg3_set_rx_mode(struct net_device *dev)
9709{
9710 struct tg3 *tp = netdev_priv(dev);
9711 u32 rx_mode;
9712
9713 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9714 RX_MODE_KEEP_VLAN_TAG);
9715
9716#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9717 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9718 * flag clear.
9719 */
9720 if (!tg3_flag(tp, ENABLE_ASF))
9721 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9722#endif
9723
9724 if (dev->flags & IFF_PROMISC) {
9725 /* Promiscuous mode. */
9726 rx_mode |= RX_MODE_PROMISC;
9727 } else if (dev->flags & IFF_ALLMULTI) {
9728 /* Accept all multicast. */
9729 tg3_set_multi(tp, 1);
9730 } else if (netdev_mc_empty(dev)) {
9731 /* Reject all multicast. */
9732 tg3_set_multi(tp, 0);
9733 } else {
9734 /* Accept one or more multicast(s). */
9735 struct netdev_hw_addr *ha;
9736 u32 mc_filter[4] = { 0, };
9737 u32 regidx;
9738 u32 bit;
9739 u32 crc;
9740
9741 netdev_for_each_mc_addr(ha, dev) {
9742 crc = calc_crc(ha->addr, ETH_ALEN);
9743 bit = ~crc & 0x7f;
9744 regidx = (bit & 0x60) >> 5;
9745 bit &= 0x1f;
9746 mc_filter[regidx] |= (1 << bit);
9747 }
9748
9749 tw32(MAC_HASH_REG_0, mc_filter[0]);
9750 tw32(MAC_HASH_REG_1, mc_filter[1]);
9751 tw32(MAC_HASH_REG_2, mc_filter[2]);
9752 tw32(MAC_HASH_REG_3, mc_filter[3]);
9753 }
9754
Michael Chane565eec2014-01-03 10:09:12 -08009755 if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
9756 rx_mode |= RX_MODE_PROMISC;
9757 } else if (!(dev->flags & IFF_PROMISC)) {
9758 /* Add all entries into to the mac addr filter list */
9759 int i = 0;
9760 struct netdev_hw_addr *ha;
9761
9762 netdev_for_each_uc_addr(ha, dev) {
9763 __tg3_set_one_mac_addr(tp, ha->addr,
9764 i + TG3_UCAST_ADDR_IDX(tp));
9765 i++;
9766 }
9767 }
9768
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009769 if (rx_mode != tp->rx_mode) {
9770 tp->rx_mode = rx_mode;
9771 tw32_f(MAC_RX_MODE, rx_mode);
9772 udelay(10);
9773 }
9774}
9775
Michael Chan91024262012-09-28 07:12:38 +00009776static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009777{
9778 int i;
9779
9780 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009781 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009782}
9783
9784static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009785{
9786 int i;
9787
9788 if (!tg3_flag(tp, SUPPORT_MSIX))
9789 return;
9790
Michael Chan0b3ba052012-11-14 14:44:29 +00009791 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009792 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009793 return;
9794 }
9795
9796 /* Validate table against current IRQ count */
9797 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009798 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009799 break;
9800 }
9801
9802 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009803 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009804}
9805
Matt Carlson90415472011-12-16 13:33:23 +00009806static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009807{
9808 int i = 0;
9809 u32 reg = MAC_RSS_INDIR_TBL_0;
9810
9811 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9812 u32 val = tp->rss_ind_tbl[i];
9813 i++;
9814 for (; i % 8; i++) {
9815 val <<= 4;
9816 val |= tp->rss_ind_tbl[i];
9817 }
9818 tw32(reg, val);
9819 reg += 4;
9820 }
9821}
9822
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009823static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9824{
9825 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9826 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9827 else
9828 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9829}
9830
Matt Carlson2d31eca2009-09-01 12:53:31 +00009831/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009832static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833{
9834 u32 val, rdmac_mode;
9835 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009836 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837
9838 tg3_disable_ints(tp);
9839
9840 tg3_stop_fw(tp);
9841
9842 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9843
Joe Perches63c3a662011-04-26 08:12:10 +00009844 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009845 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009847 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9848 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9849 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009850 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009851 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9852 }
9853
Nithin Sujir400dfba2013-05-18 06:26:53 +00009854 /* Enable MAC control of LPI */
9855 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9856 tg3_setup_eee(tp);
9857
Matt Carlson603f1172010-02-12 14:47:10 +00009858 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009859 tg3_phy_reset(tp);
9860
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861 err = tg3_chip_reset(tp);
9862 if (err)
9863 return err;
9864
9865 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9866
Joe Perches41535772013-02-16 11:20:04 +00009867 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009868 val = tr32(TG3_CPMU_CTRL);
9869 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9870 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009871
9872 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9873 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9874 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9875 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9876
9877 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9878 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9879 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9880 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9881
9882 val = tr32(TG3_CPMU_HST_ACC);
9883 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9884 val |= CPMU_HST_ACC_MACCLK_6_25;
9885 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009886 }
9887
Joe Perches41535772013-02-16 11:20:04 +00009888 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009889 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9890 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9891 PCIE_PWR_MGMT_L1_THRESH_4MS;
9892 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009893
9894 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9895 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9896
9897 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009898
Matt Carlsonf40386c2009-11-02 14:24:02 +00009899 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9900 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009901 }
9902
Joe Perches63c3a662011-04-26 08:12:10 +00009903 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009904 u32 grc_mode = tr32(GRC_MODE);
9905
9906 /* Access the lower 1K of PL PCIE block registers. */
9907 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9908 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9909
9910 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9911 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9912 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9913
9914 tw32(GRC_MODE, grc_mode);
9915 }
9916
Matt Carlson55086ad2011-12-14 11:09:59 +00009917 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009918 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009919 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009920
Matt Carlson5093eed2010-11-24 08:31:45 +00009921 /* Access the lower 1K of PL PCIE block registers. */
9922 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9923 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009924
Matt Carlson5093eed2010-11-24 08:31:45 +00009925 val = tr32(TG3_PCIE_TLDLPL_PORT +
9926 TG3_PCIE_PL_LO_PHYCTL5);
9927 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9928 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009929
Matt Carlson5093eed2010-11-24 08:31:45 +00009930 tw32(GRC_MODE, grc_mode);
9931 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009932
Joe Perches41535772013-02-16 11:20:04 +00009933 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009934 u32 grc_mode;
9935
9936 /* Fix transmit hangs */
9937 val = tr32(TG3_CPMU_PADRNG_CTL);
9938 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9939 tw32(TG3_CPMU_PADRNG_CTL, val);
9940
9941 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009942
9943 /* Access the lower 1K of DL PCIE block registers. */
9944 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9945 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9946
9947 val = tr32(TG3_PCIE_TLDLPL_PORT +
9948 TG3_PCIE_DL_LO_FTSMAX);
9949 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9950 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9951 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9952
9953 tw32(GRC_MODE, grc_mode);
9954 }
9955
Matt Carlsona977dbe2010-04-12 06:58:26 +00009956 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9957 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9958 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9959 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009960 }
9961
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 /* This works around an issue with Athlon chipsets on
9963 * B3 tigon3 silicon. This bit has no effect on any
9964 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009965 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009966 */
Joe Perches63c3a662011-04-26 08:12:10 +00009967 if (!tg3_flag(tp, CPMU_PRESENT)) {
9968 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009969 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9970 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972
Joe Perches41535772013-02-16 11:20:04 +00009973 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009974 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975 val = tr32(TG3PCI_PCISTATE);
9976 val |= PCISTATE_RETRY_SAME_DMA;
9977 tw32(TG3PCI_PCISTATE, val);
9978 }
9979
Joe Perches63c3a662011-04-26 08:12:10 +00009980 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009981 /* Allow reads and writes to the
9982 * APE register and memory space.
9983 */
9984 val = tr32(TG3PCI_PCISTATE);
9985 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009986 PCISTATE_ALLOW_APE_SHMEM_WR |
9987 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009988 tw32(TG3PCI_PCISTATE, val);
9989 }
9990
Joe Perches41535772013-02-16 11:20:04 +00009991 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992 /* Enable some hw fixes. */
9993 val = tr32(TG3PCI_MSI_DATA);
9994 val |= (1 << 26) | (1 << 28) | (1 << 29);
9995 tw32(TG3PCI_MSI_DATA, val);
9996 }
9997
9998 /* Descriptor ring init may make accesses to the
9999 * NIC SRAM area to setup the TX descriptors, so we
10000 * can only do this after the hardware has been
10001 * successfully reset.
10002 */
Michael Chan32d8c572006-07-25 16:38:29 -070010003 err = tg3_init_rings(tp);
10004 if (err)
10005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006
Joe Perches63c3a662011-04-26 08:12:10 +000010007 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010008 val = tr32(TG3PCI_DMA_RW_CTRL) &
10009 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +000010010 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +000010011 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +000010012 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +000010013 tg3_asic_rev(tp) != ASIC_REV_5717 &&
10014 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +000010015 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010016 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +000010017 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
10018 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -070010019 /* This value is determined during the probe time DMA
10020 * engine test, tg3_test_dma.
10021 */
10022 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
10023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010024
10025 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
10026 GRC_MODE_4X_NIC_SEND_RINGS |
10027 GRC_MODE_NO_TX_PHDR_CSUM |
10028 GRC_MODE_NO_RX_PHDR_CSUM);
10029 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -070010030
10031 /* Pseudo-header checksum is done by hardware logic and not
10032 * the offload processers, so make the chip do the pseudo-
10033 * header checksums on receive. For transmit it is more
10034 * convenient to do the pseudo-header checksum in software
10035 * as Linux does that on transmit for us in all cases.
10036 */
10037 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010038
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000010039 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
10040 if (tp->rxptpctl)
10041 tw32(TG3_RX_PTP_CTL,
10042 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
10043
10044 if (tg3_flag(tp, PTP_CAPABLE))
10045 val |= GRC_MODE_TIME_SYNC_ENABLE;
10046
10047 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010048
10049 /* Setup the timer prescalar register. Clock is always 66Mhz. */
10050 val = tr32(GRC_MISC_CFG);
10051 val &= ~0xff;
10052 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
10053 tw32(GRC_MISC_CFG, val);
10054
10055 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +000010056 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +000010058 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +000010060 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
10062 else
10063 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
10064 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
10065 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +000010066 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067 int fw_len;
10068
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080010069 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
10071 tw32(BUFMGR_MB_POOL_ADDR,
10072 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
10073 tw32(BUFMGR_MB_POOL_SIZE,
10074 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
10075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010076
Michael Chan0f893dc2005-07-25 12:30:38 -070010077 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010078 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10079 tp->bufmgr_config.mbuf_read_dma_low_water);
10080 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10081 tp->bufmgr_config.mbuf_mac_rx_low_water);
10082 tw32(BUFMGR_MB_HIGH_WATER,
10083 tp->bufmgr_config.mbuf_high_water);
10084 } else {
10085 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10086 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
10087 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10088 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
10089 tw32(BUFMGR_MB_HIGH_WATER,
10090 tp->bufmgr_config.mbuf_high_water_jumbo);
10091 }
10092 tw32(BUFMGR_DMA_LOW_WATER,
10093 tp->bufmgr_config.dma_low_water);
10094 tw32(BUFMGR_DMA_HIGH_WATER,
10095 tp->bufmgr_config.dma_high_water);
10096
Matt Carlsond309a462010-09-30 10:34:31 +000010097 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +000010098 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +000010099 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +000010100 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080010101 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000010102 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10103 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +000010104 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +000010105 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106 for (i = 0; i < 2000; i++) {
10107 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
10108 break;
10109 udelay(10);
10110 }
10111 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +000010112 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113 return -ENODEV;
10114 }
10115
Joe Perches41535772013-02-16 11:20:04 +000010116 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +000010117 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -070010118
Matt Carlsoneb07a942011-04-20 07:57:36 +000010119 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120
10121 /* Initialize TG3_BDINFO's at:
10122 * RCVDBDI_STD_BD: standard eth size rx ring
10123 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
10124 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
10125 *
10126 * like so:
10127 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
10128 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
10129 * ring attribute flags
10130 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
10131 *
10132 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
10133 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
10134 *
10135 * The size of each ring is fixed in the firmware, but the location is
10136 * configurable.
10137 */
10138 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010139 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010141 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010142 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010143 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10144 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010146 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010147 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10149 BDINFO_FLAGS_DISABLED);
10150
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010151 /* Program the jumbo buffer descriptor ring control
10152 * blocks on those devices that have them.
10153 */
Joe Perches41535772013-02-16 11:20:04 +000010154 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010155 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156
Joe Perches63c3a662011-04-26 08:12:10 +000010157 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010159 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010161 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010162 val = TG3_RX_JMB_RING_SIZE(tp) <<
10163 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010165 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010166 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010167 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010168 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010169 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10170 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171 } else {
10172 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10173 BDINFO_FLAGS_DISABLED);
10174 }
10175
Joe Perches63c3a662011-04-26 08:12:10 +000010176 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010177 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010178 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10179 val |= (TG3_RX_STD_DMA_SZ << 2);
10180 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010181 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010182 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010183 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010184
10185 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186
Matt Carlson411da642009-11-13 13:03:46 +000010187 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +000010188 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189
Joe Perches63c3a662011-04-26 08:12:10 +000010190 tpr->rx_jmb_prod_idx =
10191 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +000010192 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193
Matt Carlson2d31eca2009-09-01 12:53:31 +000010194 tg3_rings_reset(tp);
10195
Linus Torvalds1da177e2005-04-16 15:20:36 -070010196 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010197 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198
10199 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010200 tw32(MAC_RX_MTU_SIZE,
10201 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010202
10203 /* The slot time is changed by tg3_setup_phy if we
10204 * run at gigabit with half duplex.
10205 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010206 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10207 (6 << TX_LENGTHS_IPG_SHIFT) |
10208 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10209
Joe Perches41535772013-02-16 11:20:04 +000010210 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10211 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010212 val |= tr32(MAC_TX_LENGTHS) &
10213 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10214 TX_LENGTHS_CNT_DWN_VAL_MSK);
10215
10216 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217
10218 /* Receive rules. */
10219 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10220 tw32(RCVLPC_CONFIG, 0x0181);
10221
10222 /* Calculate RDMAC_MODE setting early, we need it to determine
10223 * the RCVLPC_STATE_ENABLE mask.
10224 */
10225 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10226 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10227 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10228 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10229 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010230
Joe Perches41535772013-02-16 11:20:04 +000010231 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010232 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10233
Joe Perches41535772013-02-16 11:20:04 +000010234 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10235 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10236 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010237 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10238 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10239 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10240
Joe Perches41535772013-02-16 11:20:04 +000010241 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10242 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010243 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010244 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10246 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010247 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010248 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10249 }
10250 }
10251
Joe Perches63c3a662011-04-26 08:12:10 +000010252 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010253 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10254
Joe Perches41535772013-02-16 11:20:04 +000010255 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010256 tp->dma_limit = 0;
10257 if (tp->dev->mtu <= ETH_DATA_LEN) {
10258 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10259 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10260 }
10261 }
10262
Joe Perches63c3a662011-04-26 08:12:10 +000010263 if (tg3_flag(tp, HW_TSO_1) ||
10264 tg3_flag(tp, HW_TSO_2) ||
10265 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010266 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10267
Matt Carlson108a6c12011-05-19 12:12:47 +000010268 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010269 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10270 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010271 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010272
Joe Perches41535772013-02-16 11:20:04 +000010273 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10274 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010275 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10276
Joe Perches41535772013-02-16 11:20:04 +000010277 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10278 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10279 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10280 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010281 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010282 u32 tgtreg;
10283
Joe Perches41535772013-02-16 11:20:04 +000010284 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010285 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10286 else
10287 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10288
10289 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010290 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10291 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010292 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10293 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10294 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10295 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10296 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10297 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010298 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010299 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010300 }
10301
Joe Perches41535772013-02-16 11:20:04 +000010302 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10303 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10304 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010305 u32 tgtreg;
10306
Joe Perches41535772013-02-16 11:20:04 +000010307 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010308 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10309 else
10310 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10311
10312 val = tr32(tgtreg);
10313 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010314 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10315 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10316 }
10317
Linus Torvalds1da177e2005-04-16 15:20:36 -070010318 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010319 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010320 val = tr32(RCVLPC_STATS_ENABLE);
10321 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10322 tw32(RCVLPC_STATS_ENABLE, val);
10323 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010324 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010325 val = tr32(RCVLPC_STATS_ENABLE);
10326 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10327 tw32(RCVLPC_STATS_ENABLE, val);
10328 } else {
10329 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10330 }
10331 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10332 tw32(SNDDATAI_STATSENAB, 0xffffff);
10333 tw32(SNDDATAI_STATSCTRL,
10334 (SNDDATAI_SCTRL_ENABLE |
10335 SNDDATAI_SCTRL_FASTUPD));
10336
10337 /* Setup host coalescing engine. */
10338 tw32(HOSTCC_MODE, 0);
10339 for (i = 0; i < 2000; i++) {
10340 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10341 break;
10342 udelay(10);
10343 }
10344
Michael Chand244c892005-07-05 14:42:33 -070010345 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010346
Joe Perches63c3a662011-04-26 08:12:10 +000010347 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010348 /* Status/statistics block address. See tg3_timer,
10349 * the tg3_periodic_fetch_stats call there, and
10350 * tg3_get_stats to see how this works for 5705/5750 chips.
10351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10353 ((u64) tp->stats_mapping >> 32));
10354 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10355 ((u64) tp->stats_mapping & 0xffffffff));
10356 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010357
Linus Torvalds1da177e2005-04-16 15:20:36 -070010358 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010359
10360 /* Clear statistics and status block memory areas */
10361 for (i = NIC_SRAM_STATS_BLK;
10362 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10363 i += sizeof(u32)) {
10364 tg3_write_mem(tp, i, 0);
10365 udelay(40);
10366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010367 }
10368
10369 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10370
10371 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10372 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010373 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010374 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10375
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010376 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10377 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010378 /* reset to prevent losing 1st rx packet intermittently */
10379 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10380 udelay(10);
10381 }
10382
Matt Carlson3bda1252008-08-15 14:08:22 -070010383 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010384 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10385 MAC_MODE_FHDE_ENABLE;
10386 if (tg3_flag(tp, ENABLE_APE))
10387 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010388 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010389 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010390 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010391 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10393 udelay(40);
10394
Michael Chan314fba32005-04-21 17:07:04 -070010395 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010396 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010397 * register to preserve the GPIO settings for LOMs. The GPIOs,
10398 * whether used as inputs or outputs, are set by boot code after
10399 * reset.
10400 */
Joe Perches63c3a662011-04-26 08:12:10 +000010401 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010402 u32 gpio_mask;
10403
Michael Chan9d26e212006-12-07 00:21:14 -080010404 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10405 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10406 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010407
Joe Perches41535772013-02-16 11:20:04 +000010408 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010409 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10410 GRC_LCLCTRL_GPIO_OUTPUT3;
10411
Joe Perches41535772013-02-16 11:20:04 +000010412 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010413 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10414
Gary Zambranoaaf84462007-05-05 11:51:45 -070010415 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010416 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10417
10418 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010419 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010420 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10421 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10424 udelay(100);
10425
Matt Carlsonc3b50032012-01-17 15:27:23 +000010426 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010427 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010428 val |= MSGINT_MODE_ENABLE;
10429 if (tp->irq_cnt > 1)
10430 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010431 if (!tg3_flag(tp, 1SHOT_MSI))
10432 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010433 tw32(MSGINT_MODE, val);
10434 }
10435
Joe Perches63c3a662011-04-26 08:12:10 +000010436 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10438 udelay(40);
10439 }
10440
10441 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10442 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10443 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10444 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10445 WDMAC_MODE_LNGREAD_ENAB);
10446
Joe Perches41535772013-02-16 11:20:04 +000010447 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10448 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010449 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010450 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10451 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010452 /* nothing */
10453 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010454 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010455 val |= WDMAC_MODE_RX_ACCEL;
10456 }
10457 }
10458
Michael Chand9ab5ad2006-03-20 22:27:35 -080010459 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010460 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010461 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -080010462
Joe Perches41535772013-02-16 11:20:04 +000010463 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010464 val |= WDMAC_MODE_BURST_ALL_DATA;
10465
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466 tw32_f(WDMAC_MODE, val);
10467 udelay(40);
10468
Joe Perches63c3a662011-04-26 08:12:10 +000010469 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010470 u16 pcix_cmd;
10471
10472 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10473 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010474 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010475 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10476 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010477 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010478 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10479 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010480 }
Matt Carlson9974a352007-10-07 23:27:28 -070010481 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10482 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010483 }
10484
10485 tw32_f(RDMAC_MODE, rdmac_mode);
10486 udelay(40);
10487
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010488 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10489 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010490 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10491 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10492 break;
10493 }
10494 if (i < TG3_NUM_RDMA_CHANNELS) {
10495 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010496 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010497 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010498 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010499 }
10500 }
10501
Linus Torvalds1da177e2005-04-16 15:20:36 -070010502 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010503 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010504 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010505
Joe Perches41535772013-02-16 11:20:04 +000010506 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010507 tw32(SNDDATAC_MODE,
10508 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10509 else
10510 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10511
Linus Torvalds1da177e2005-04-16 15:20:36 -070010512 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10513 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010514 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010515 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010516 val |= RCVDBDI_MODE_LRG_RING_SZ;
10517 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010518 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010519 if (tg3_flag(tp, HW_TSO_1) ||
10520 tg3_flag(tp, HW_TSO_2) ||
10521 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010522 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010523 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010524 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010525 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10526 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010527 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10528
Joe Perches41535772013-02-16 11:20:04 +000010529 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530 err = tg3_load_5701_a0_firmware_fix(tp);
10531 if (err)
10532 return err;
10533 }
10534
Nithin Sujirc4dab502013-03-06 17:02:34 +000010535 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10536 /* Ignore any errors for the firmware download. If download
10537 * fails, the device will operate with EEE disabled
10538 */
10539 tg3_load_57766_firmware(tp);
10540 }
10541
Joe Perches63c3a662011-04-26 08:12:10 +000010542 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010543 err = tg3_load_tso_firmware(tp);
10544 if (err)
10545 return err;
10546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010547
10548 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010549
Joe Perches63c3a662011-04-26 08:12:10 +000010550 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010551 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010552 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010553
Joe Perches41535772013-02-16 11:20:04 +000010554 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10555 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010556 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10557 tp->tx_mode &= ~val;
10558 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10559 }
10560
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561 tw32_f(MAC_TX_MODE, tp->tx_mode);
10562 udelay(100);
10563
Joe Perches63c3a662011-04-26 08:12:10 +000010564 if (tg3_flag(tp, ENABLE_RSS)) {
Eric Dumazet39648352014-11-16 06:23:08 -080010565 u32 rss_key[10];
10566
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010567 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010568
Eric Dumazet39648352014-11-16 06:23:08 -080010569 netdev_rss_key_fill(rss_key, 10 * sizeof(u32));
10570
10571 for (i = 0; i < 10 ; i++)
10572 tw32(MAC_RSS_HASH_KEY_0 + i*4, rss_key[i]);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010573 }
10574
Linus Torvalds1da177e2005-04-16 15:20:36 -070010575 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010576 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010577 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10578
Nithin Sujir378b72c2013-07-29 13:58:39 -070010579 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10580 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10581
Joe Perches63c3a662011-04-26 08:12:10 +000010582 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010583 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10584 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10585 RX_MODE_RSS_IPV6_HASH_EN |
10586 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10587 RX_MODE_RSS_IPV4_HASH_EN |
10588 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10589
Linus Torvalds1da177e2005-04-16 15:20:36 -070010590 tw32_f(MAC_RX_MODE, tp->rx_mode);
10591 udelay(10);
10592
Linus Torvalds1da177e2005-04-16 15:20:36 -070010593 tw32(MAC_LED_CTRL, tp->led_ctrl);
10594
10595 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010596 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10598 udelay(10);
10599 }
10600 tw32_f(MAC_RX_MODE, tp->rx_mode);
10601 udelay(10);
10602
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010603 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010604 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10605 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606 /* Set drive transmission level to 1.2V */
10607 /* only if the signal pre-emphasis bit is not set */
10608 val = tr32(MAC_SERDES_CFG);
10609 val &= 0xfffff000;
10610 val |= 0x880;
10611 tw32(MAC_SERDES_CFG, val);
10612 }
Joe Perches41535772013-02-16 11:20:04 +000010613 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010614 tw32(MAC_SERDES_CFG, 0x616000);
10615 }
10616
10617 /* Prevent chip from dropping frames when flow control
10618 * is enabled.
10619 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010620 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010621 val = 1;
10622 else
10623 val = 2;
10624 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010625
Joe Perches41535772013-02-16 11:20:04 +000010626 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010627 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010628 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010629 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010630 }
10631
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010632 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010633 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010634 u32 tmp;
10635
10636 tmp = tr32(SERDES_RX_CTRL);
10637 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10638 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10639 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10640 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10641 }
10642
Joe Perches63c3a662011-04-26 08:12:10 +000010643 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010644 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson800960682010-08-02 11:26:06 +000010645 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010646
Joe Perches953c96e2013-04-09 10:18:14 +000010647 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010648 if (err)
10649 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010650
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010651 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10652 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010653 u32 tmp;
10654
10655 /* Clear CRC stats. */
10656 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10657 tg3_writephy(tp, MII_TG3_TEST1,
10658 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010659 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010661 }
10662 }
10663
10664 __tg3_set_rx_mode(tp->dev);
10665
10666 /* Initialize receive rules. */
10667 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10668 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10669 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10670 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10671
Joe Perches63c3a662011-04-26 08:12:10 +000010672 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010673 limit = 8;
10674 else
10675 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010676 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010677 limit -= 4;
10678 switch (limit) {
10679 case 16:
10680 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10681 case 15:
10682 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10683 case 14:
10684 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10685 case 13:
10686 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10687 case 12:
10688 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10689 case 11:
10690 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10691 case 10:
10692 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10693 case 9:
10694 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10695 case 8:
10696 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10697 case 7:
10698 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10699 case 6:
10700 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10701 case 5:
10702 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10703 case 4:
10704 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10705 case 3:
10706 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10707 case 2:
10708 case 1:
10709
10710 default:
10711 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010713
Joe Perches63c3a662011-04-26 08:12:10 +000010714 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010715 /* Write our heartbeat update interval to APE. */
10716 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10717 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010718
Linus Torvalds1da177e2005-04-16 15:20:36 -070010719 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10720
Linus Torvalds1da177e2005-04-16 15:20:36 -070010721 return 0;
10722}
10723
10724/* Called at device open time to get the chip ready for
10725 * packet processing. Invoked with tp->lock held.
10726 */
Joe Perches953c96e2013-04-09 10:18:14 +000010727static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010729 /* Chip may have been just powered on. If so, the boot code may still
10730 * be running initialization. Wait for it to finish to avoid races in
10731 * accessing the hardware.
10732 */
10733 tg3_enable_register_access(tp);
10734 tg3_poll_fw(tp);
10735
Linus Torvalds1da177e2005-04-16 15:20:36 -070010736 tg3_switch_clocks(tp);
10737
10738 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10739
Matt Carlson2f751b62008-08-04 23:17:34 -070010740 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741}
10742
Michael Chanaed93e02012-07-16 16:24:02 +000010743static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10744{
10745 int i;
10746
10747 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10748 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10749
10750 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10751 off += len;
10752
10753 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10754 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10755 memset(ocir, 0, TG3_OCIR_LEN);
10756 }
10757}
10758
10759/* sysfs attributes for hwmon */
10760static ssize_t tg3_show_temp(struct device *dev,
10761 struct device_attribute *devattr, char *buf)
10762{
Michael Chanaed93e02012-07-16 16:24:02 +000010763 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010764 struct tg3 *tp = dev_get_drvdata(dev);
Michael Chanaed93e02012-07-16 16:24:02 +000010765 u32 temperature;
10766
10767 spin_lock_bh(&tp->lock);
10768 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10769 sizeof(temperature));
10770 spin_unlock_bh(&tp->lock);
Jean Delvared3d11fe2015-09-01 18:07:41 +020010771 return sprintf(buf, "%u\n", temperature * 1000);
Michael Chanaed93e02012-07-16 16:24:02 +000010772}
10773
10774
10775static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10776 TG3_TEMP_SENSOR_OFFSET);
10777static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10778 TG3_TEMP_CAUTION_OFFSET);
10779static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10780 TG3_TEMP_MAX_OFFSET);
10781
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010782static struct attribute *tg3_attrs[] = {
Michael Chanaed93e02012-07-16 16:24:02 +000010783 &sensor_dev_attr_temp1_input.dev_attr.attr,
10784 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10785 &sensor_dev_attr_temp1_max.dev_attr.attr,
10786 NULL
10787};
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010788ATTRIBUTE_GROUPS(tg3);
Michael Chanaed93e02012-07-16 16:24:02 +000010789
Michael Chanaed93e02012-07-16 16:24:02 +000010790static void tg3_hwmon_close(struct tg3 *tp)
10791{
Michael Chanaed93e02012-07-16 16:24:02 +000010792 if (tp->hwmon_dev) {
10793 hwmon_device_unregister(tp->hwmon_dev);
10794 tp->hwmon_dev = NULL;
Michael Chanaed93e02012-07-16 16:24:02 +000010795 }
Michael Chanaed93e02012-07-16 16:24:02 +000010796}
10797
10798static void tg3_hwmon_open(struct tg3 *tp)
10799{
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010800 int i;
Michael Chanaed93e02012-07-16 16:24:02 +000010801 u32 size = 0;
10802 struct pci_dev *pdev = tp->pdev;
10803 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10804
10805 tg3_sd_scan_scratchpad(tp, ocirs);
10806
10807 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10808 if (!ocirs[i].src_data_length)
10809 continue;
10810
10811 size += ocirs[i].src_hdr_length;
10812 size += ocirs[i].src_data_length;
10813 }
10814
10815 if (!size)
10816 return;
10817
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010818 tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
10819 tp, tg3_groups);
Michael Chanaed93e02012-07-16 16:24:02 +000010820 if (IS_ERR(tp->hwmon_dev)) {
10821 tp->hwmon_dev = NULL;
10822 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
Michael Chanaed93e02012-07-16 16:24:02 +000010823 }
Michael Chanaed93e02012-07-16 16:24:02 +000010824}
10825
10826
Linus Torvalds1da177e2005-04-16 15:20:36 -070010827#define TG3_STAT_ADD32(PSTAT, REG) \
10828do { u32 __val = tr32(REG); \
10829 (PSTAT)->low += __val; \
10830 if ((PSTAT)->low < __val) \
10831 (PSTAT)->high += 1; \
10832} while (0)
10833
10834static void tg3_periodic_fetch_stats(struct tg3 *tp)
10835{
10836 struct tg3_hw_stats *sp = tp->hw_stats;
10837
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010838 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010839 return;
10840
10841 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10842 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10843 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10844 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10845 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10846 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10847 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10848 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10849 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10850 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10851 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10852 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10853 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010854 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010855 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10856 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10857 u32 val;
10858
10859 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010860 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010861 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010862 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010864
10865 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10866 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10867 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10868 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10869 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10870 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10871 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10872 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10873 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10874 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10875 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10876 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10877 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10878 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010879
10880 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010881 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
Nithin Sujir94962f72013-12-06 09:53:19 -080010882 tg3_asic_rev(tp) != ASIC_REV_5762 &&
Joe Perches41535772013-02-16 11:20:04 +000010883 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10884 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010885 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10886 } else {
10887 u32 val = tr32(HOSTCC_FLOW_ATTN);
10888 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10889 if (val) {
10890 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10891 sp->rx_discards.low += val;
10892 if (sp->rx_discards.low < val)
10893 sp->rx_discards.high += 1;
10894 }
10895 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10896 }
Michael Chan463d3052006-05-22 16:36:27 -070010897 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010898}
10899
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010900static void tg3_chk_missed_msi(struct tg3 *tp)
10901{
10902 u32 i;
10903
10904 for (i = 0; i < tp->irq_cnt; i++) {
10905 struct tg3_napi *tnapi = &tp->napi[i];
10906
10907 if (tg3_has_work(tnapi)) {
10908 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10909 tnapi->last_tx_cons == tnapi->tx_cons) {
10910 if (tnapi->chk_msi_cnt < 1) {
10911 tnapi->chk_msi_cnt++;
10912 return;
10913 }
Matt Carlson7f230732011-08-31 11:44:48 +000010914 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010915 }
10916 }
10917 tnapi->chk_msi_cnt = 0;
10918 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10919 tnapi->last_tx_cons = tnapi->tx_cons;
10920 }
10921}
10922
Linus Torvalds1da177e2005-04-16 15:20:36 -070010923static void tg3_timer(unsigned long __opaque)
10924{
10925 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010926
David S. Millerf47c11e2005-06-24 20:18:35 -070010927 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010928
Prashant Sreedharan4fd190a2015-01-14 11:33:49 -080010929 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING)) {
10930 spin_unlock(&tp->lock);
10931 goto restart_timer;
10932 }
10933
Joe Perches41535772013-02-16 11:20:04 +000010934 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010935 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010936 tg3_chk_missed_msi(tp);
10937
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010938 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10939 /* BCM4785: Flush posted writes from GbE to host memory. */
10940 tr32(HOSTCC_MODE);
10941 }
10942
Joe Perches63c3a662011-04-26 08:12:10 +000010943 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010944 /* All of this garbage is because when using non-tagged
10945 * IRQ status the mailbox/status_block protocol the chip
10946 * uses with the cpu is race prone.
10947 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010948 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010949 tw32(GRC_LOCAL_CTRL,
10950 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10951 } else {
10952 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010953 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010955
David S. Millerfac9b832005-05-18 22:46:34 -070010956 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010957 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010958 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010959 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010961 }
10962
Linus Torvalds1da177e2005-04-16 15:20:36 -070010963 /* This part only runs once per second. */
10964 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010965 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010966 tg3_periodic_fetch_stats(tp);
10967
Matt Carlsonb0c59432011-05-19 12:12:48 +000010968 if (tp->setlpicnt && !--tp->setlpicnt)
10969 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010970
Joe Perches63c3a662011-04-26 08:12:10 +000010971 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010972 u32 mac_stat;
10973 int phy_event;
10974
10975 mac_stat = tr32(MAC_STATUS);
10976
10977 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010978 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010979 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10980 phy_event = 1;
10981 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10982 phy_event = 1;
10983
10984 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010985 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010986 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010987 u32 mac_stat = tr32(MAC_STATUS);
10988 int need_setup = 0;
10989
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010990 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010991 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10992 need_setup = 1;
10993 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010994 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010995 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10996 MAC_STATUS_SIGNAL_DET))) {
10997 need_setup = 1;
10998 }
10999 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070011000 if (!tp->serdes_counter) {
11001 tw32_f(MAC_MODE,
11002 (tp->mac_mode &
11003 ~MAC_MODE_PORT_MODE_MASK));
11004 udelay(40);
11005 tw32_f(MAC_MODE, tp->mac_mode);
11006 udelay(40);
11007 }
Joe Perches953c96e2013-04-09 10:18:14 +000011008 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011009 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011010 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011011 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070011012 tg3_serdes_parallel_detect(tp);
Nithin Sujir1743b832014-01-03 10:09:14 -080011013 } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
11014 u32 cpmu = tr32(TG3_CPMU_STATUS);
11015 bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
11016 TG3_CPMU_STATUS_LINK_MASK);
11017
11018 if (link_up != tp->link_up)
11019 tg3_setup_phy(tp, false);
Matt Carlson57d8b882010-06-05 17:24:35 +000011020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011021
11022 tp->timer_counter = tp->timer_multiplier;
11023 }
11024
Michael Chan130b8e42006-09-27 16:00:40 -070011025 /* Heartbeat is only sent once every 2 seconds.
11026 *
11027 * The heartbeat is to tell the ASF firmware that the host
11028 * driver is still alive. In the event that the OS crashes,
11029 * ASF needs to reset the hardware to free up the FIFO space
11030 * that may be filled with rx packets destined for the host.
11031 * If the FIFO is full, ASF will no longer function properly.
11032 *
11033 * Unintended resets have been reported on real time kernels
11034 * where the timer doesn't run on time. Netpoll will also have
11035 * same problem.
11036 *
11037 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
11038 * to check the ring condition when the heartbeat is expiring
11039 * before doing the reset. This will prevent most unintended
11040 * resets.
11041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011042 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000011043 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070011044 tg3_wait_for_event_ack(tp);
11045
Michael Chanbbadf502006-04-06 21:46:34 -070011046 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070011047 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070011048 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011049 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
11050 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070011051
11052 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011053 }
11054 tp->asf_counter = tp->asf_multiplier;
11055 }
11056
David S. Millerf47c11e2005-06-24 20:18:35 -070011057 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011058
Michael Chanf475f162006-03-27 23:20:14 -080011059restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011060 tp->timer.expires = jiffies + tp->timer_offset;
11061 add_timer(&tp->timer);
11062}
11063
Bill Pemberton229b1ad2012-12-03 09:22:59 -050011064static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000011065{
11066 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000011067 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000011068 !tg3_flag(tp, 57765_CLASS))
11069 tp->timer_offset = HZ;
11070 else
11071 tp->timer_offset = HZ / 10;
11072
11073 BUG_ON(tp->timer_offset > HZ);
11074
11075 tp->timer_multiplier = (HZ / tp->timer_offset);
11076 tp->asf_multiplier = (HZ / tp->timer_offset) *
11077 TG3_FW_UPDATE_FREQ_SEC;
11078
11079 init_timer(&tp->timer);
11080 tp->timer.data = (unsigned long) tp;
11081 tp->timer.function = tg3_timer;
11082}
11083
11084static void tg3_timer_start(struct tg3 *tp)
11085{
11086 tp->asf_counter = tp->asf_multiplier;
11087 tp->timer_counter = tp->timer_multiplier;
11088
11089 tp->timer.expires = jiffies + tp->timer_offset;
11090 add_timer(&tp->timer);
11091}
11092
11093static void tg3_timer_stop(struct tg3 *tp)
11094{
11095 del_timer_sync(&tp->timer);
11096}
11097
11098/* Restart hardware after configuration changes, self-test, etc.
11099 * Invoked with tp->lock held.
11100 */
Joe Perches953c96e2013-04-09 10:18:14 +000011101static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000011102 __releases(tp->lock)
11103 __acquires(tp->lock)
11104{
11105 int err;
11106
11107 err = tg3_init_hw(tp, reset_phy);
11108 if (err) {
11109 netdev_err(tp->dev,
11110 "Failed to re-initialize device, aborting\n");
11111 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11112 tg3_full_unlock(tp);
11113 tg3_timer_stop(tp);
11114 tp->irq_sync = 0;
11115 tg3_napi_enable(tp);
11116 dev_close(tp->dev);
11117 tg3_full_lock(tp, 0);
11118 }
11119 return err;
11120}
11121
11122static void tg3_reset_task(struct work_struct *work)
11123{
11124 struct tg3 *tp = container_of(work, struct tg3, reset_task);
11125 int err;
11126
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011127 rtnl_lock();
Matt Carlson21f76382012-02-22 12:35:21 +000011128 tg3_full_lock(tp, 0);
11129
11130 if (!netif_running(tp->dev)) {
11131 tg3_flag_clear(tp, RESET_TASK_PENDING);
11132 tg3_full_unlock(tp);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011133 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011134 return;
11135 }
11136
11137 tg3_full_unlock(tp);
11138
11139 tg3_phy_stop(tp);
11140
11141 tg3_netif_stop(tp);
11142
11143 tg3_full_lock(tp, 1);
11144
11145 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11146 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11147 tp->write32_rx_mbox = tg3_write_flush_reg32;
11148 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11149 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11150 }
11151
11152 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011153 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011154 if (err)
11155 goto out;
11156
11157 tg3_netif_start(tp);
11158
11159out:
11160 tg3_full_unlock(tp);
11161
11162 if (!err)
11163 tg3_phy_start(tp);
11164
11165 tg3_flag_clear(tp, RESET_TASK_PENDING);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011166 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011167}
11168
Matt Carlson4f125f42009-09-01 12:55:02 +000011169static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011170{
David Howells7d12e782006-10-05 14:55:46 +010011171 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011172 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011173 char *name;
11174 struct tg3_napi *tnapi = &tp->napi[irq_num];
11175
11176 if (tp->irq_cnt == 1)
11177 name = tp->dev->name;
11178 else {
11179 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011180 if (tnapi->tx_buffers && tnapi->rx_rcb)
11181 snprintf(name, IFNAMSIZ,
11182 "%s-txrx-%d", tp->dev->name, irq_num);
11183 else if (tnapi->tx_buffers)
11184 snprintf(name, IFNAMSIZ,
11185 "%s-tx-%d", tp->dev->name, irq_num);
11186 else if (tnapi->rx_rcb)
11187 snprintf(name, IFNAMSIZ,
11188 "%s-rx-%d", tp->dev->name, irq_num);
11189 else
11190 snprintf(name, IFNAMSIZ,
11191 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011192 name[IFNAMSIZ-1] = 0;
11193 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011194
Joe Perches63c3a662011-04-26 08:12:10 +000011195 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011196 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011197 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011198 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011199 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011200 } else {
11201 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011202 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011203 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011204 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011205 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011206
11207 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011208}
11209
Michael Chan79381092005-04-21 17:13:59 -070011210static int tg3_test_interrupt(struct tg3 *tp)
11211{
Matt Carlson09943a12009-08-28 14:01:57 +000011212 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011213 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011214 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011215 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011216
Michael Chand4bc3922005-05-29 14:59:20 -070011217 if (!netif_running(dev))
11218 return -ENODEV;
11219
Michael Chan79381092005-04-21 17:13:59 -070011220 tg3_disable_ints(tp);
11221
Matt Carlson4f125f42009-09-01 12:55:02 +000011222 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011223
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011224 /*
11225 * Turn off MSI one shot mode. Otherwise this test has no
11226 * observable way to know whether the interrupt was delivered.
11227 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011228 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011229 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11230 tw32(MSGINT_MODE, val);
11231 }
11232
Matt Carlson4f125f42009-09-01 12:55:02 +000011233 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011234 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011235 if (err)
11236 return err;
11237
Matt Carlson898a56f2009-08-28 14:02:40 +000011238 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011239 tg3_enable_ints(tp);
11240
11241 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011242 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011243
11244 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011245 u32 int_mbox, misc_host_ctrl;
11246
Matt Carlson898a56f2009-08-28 14:02:40 +000011247 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011248 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11249
11250 if ((int_mbox != 0) ||
11251 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11252 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011253 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011254 }
11255
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011256 if (tg3_flag(tp, 57765_PLUS) &&
11257 tnapi->hw_status->status_tag != tnapi->last_tag)
11258 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11259
Michael Chan79381092005-04-21 17:13:59 -070011260 msleep(10);
11261 }
11262
11263 tg3_disable_ints(tp);
11264
Matt Carlson4f125f42009-09-01 12:55:02 +000011265 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011266
Matt Carlson4f125f42009-09-01 12:55:02 +000011267 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011268
11269 if (err)
11270 return err;
11271
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011272 if (intr_ok) {
11273 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011274 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011275 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11276 tw32(MSGINT_MODE, val);
11277 }
Michael Chan79381092005-04-21 17:13:59 -070011278 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011279 }
Michael Chan79381092005-04-21 17:13:59 -070011280
11281 return -EIO;
11282}
11283
11284/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11285 * successfully restored
11286 */
11287static int tg3_test_msi(struct tg3 *tp)
11288{
Michael Chan79381092005-04-21 17:13:59 -070011289 int err;
11290 u16 pci_cmd;
11291
Joe Perches63c3a662011-04-26 08:12:10 +000011292 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011293 return 0;
11294
11295 /* Turn off SERR reporting in case MSI terminates with Master
11296 * Abort.
11297 */
11298 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11299 pci_write_config_word(tp->pdev, PCI_COMMAND,
11300 pci_cmd & ~PCI_COMMAND_SERR);
11301
11302 err = tg3_test_interrupt(tp);
11303
11304 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11305
11306 if (!err)
11307 return 0;
11308
11309 /* other failures */
11310 if (err != -EIO)
11311 return err;
11312
11313 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011314 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11315 "to INTx mode. Please report this failure to the PCI "
11316 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011317
Matt Carlson4f125f42009-09-01 12:55:02 +000011318 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011319
Michael Chan79381092005-04-21 17:13:59 -070011320 pci_disable_msi(tp->pdev);
11321
Joe Perches63c3a662011-04-26 08:12:10 +000011322 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011323 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011324
Matt Carlson4f125f42009-09-01 12:55:02 +000011325 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011326 if (err)
11327 return err;
11328
11329 /* Need to reset the chip because the MSI cycle may have terminated
11330 * with Master Abort.
11331 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011332 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011333
Michael Chan944d9802005-05-29 14:57:48 -070011334 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011335 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011336
David S. Millerf47c11e2005-06-24 20:18:35 -070011337 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011338
11339 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011340 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011341
11342 return err;
11343}
11344
Matt Carlson9e9fd122009-01-19 16:57:45 -080011345static int tg3_request_firmware(struct tg3 *tp)
11346{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011347 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011348
11349 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011350 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11351 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011352 return -ENOENT;
11353 }
11354
Nithin Sujir77997ea2013-03-06 17:02:32 +000011355 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011356
11357 /* Firmware blob starts with version numbers, followed by
11358 * start address and _full_ length including BSS sections
11359 * (which must be longer than the actual data, of course
11360 */
11361
Nithin Sujir77997ea2013-03-06 17:02:32 +000011362 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11363 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011364 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11365 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011366 release_firmware(tp->fw);
11367 tp->fw = NULL;
11368 return -EINVAL;
11369 }
11370
11371 /* We no longer need firmware; we have it. */
11372 tp->fw_needed = NULL;
11373 return 0;
11374}
11375
Michael Chan91024262012-09-28 07:12:38 +000011376static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011377{
Michael Chan91024262012-09-28 07:12:38 +000011378 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011379
Michael Chan91024262012-09-28 07:12:38 +000011380 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011381 /* We want as many rx rings enabled as there are cpus.
11382 * In multiqueue MSI-X mode, the first MSI-X vector
11383 * only deals with link interrupts, etc, so we add
11384 * one to the number of vectors we are requesting.
11385 */
Michael Chan91024262012-09-28 07:12:38 +000011386 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011387 }
Matt Carlson679563f2009-09-01 12:55:46 +000011388
Michael Chan91024262012-09-28 07:12:38 +000011389 return irq_cnt;
11390}
11391
11392static bool tg3_enable_msix(struct tg3 *tp)
11393{
11394 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011395 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011396
Michael Chan09681692012-09-28 07:12:42 +000011397 tp->txq_cnt = tp->txq_req;
11398 tp->rxq_cnt = tp->rxq_req;
11399 if (!tp->rxq_cnt)
11400 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011401 if (tp->rxq_cnt > tp->rxq_max)
11402 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011403
11404 /* Disable multiple TX rings by default. Simple round-robin hardware
11405 * scheduling of the TX rings can cause starvation of rings with
11406 * small packets when other rings have TSO or jumbo packets.
11407 */
11408 if (!tp->txq_req)
11409 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011410
11411 tp->irq_cnt = tg3_irq_count(tp);
11412
Matt Carlson679563f2009-09-01 12:55:46 +000011413 for (i = 0; i < tp->irq_max; i++) {
11414 msix_ent[i].entry = i;
11415 msix_ent[i].vector = 0;
11416 }
11417
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011418 rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011419 if (rc < 0) {
11420 return false;
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011421 } else if (rc < tp->irq_cnt) {
Joe Perches05dbe002010-02-17 19:44:19 +000011422 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11423 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011424 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011425 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011426 if (tp->txq_cnt)
11427 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011428 }
11429
11430 for (i = 0; i < tp->irq_max; i++)
11431 tp->napi[i].irq_vec = msix_ent[i].vector;
11432
Michael Chan49a359e2012-09-28 07:12:37 +000011433 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011434 pci_disable_msix(tp->pdev);
11435 return false;
11436 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011437
Michael Chan91024262012-09-28 07:12:38 +000011438 if (tp->irq_cnt == 1)
11439 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011440
Michael Chan91024262012-09-28 07:12:38 +000011441 tg3_flag_set(tp, ENABLE_RSS);
11442
11443 if (tp->txq_cnt > 1)
11444 tg3_flag_set(tp, ENABLE_TSS);
11445
11446 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011447
Matt Carlson679563f2009-09-01 12:55:46 +000011448 return true;
11449}
11450
Matt Carlson07b01732009-08-28 14:01:15 +000011451static void tg3_ints_init(struct tg3 *tp)
11452{
Joe Perches63c3a662011-04-26 08:12:10 +000011453 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11454 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011455 /* All MSI supporting chips should support tagged
11456 * status. Assert that this is the case.
11457 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011458 netdev_warn(tp->dev,
11459 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011460 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011461 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011462
Joe Perches63c3a662011-04-26 08:12:10 +000011463 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11464 tg3_flag_set(tp, USING_MSIX);
11465 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11466 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011467
Joe Perches63c3a662011-04-26 08:12:10 +000011468 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011469 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011470 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011471 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011472 if (!tg3_flag(tp, 1SHOT_MSI))
11473 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011474 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11475 }
11476defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011477 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011478 tp->irq_cnt = 1;
11479 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011480 }
11481
11482 if (tp->irq_cnt == 1) {
11483 tp->txq_cnt = 1;
11484 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011485 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011486 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011487 }
Matt Carlson07b01732009-08-28 14:01:15 +000011488}
11489
11490static void tg3_ints_fini(struct tg3 *tp)
11491{
Joe Perches63c3a662011-04-26 08:12:10 +000011492 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011493 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011494 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011495 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011496 tg3_flag_clear(tp, USING_MSI);
11497 tg3_flag_clear(tp, USING_MSIX);
11498 tg3_flag_clear(tp, ENABLE_RSS);
11499 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011500}
11501
Matt Carlsonbe947302012-12-03 19:36:57 +000011502static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11503 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011504{
Michael Chand8f4cd32012-09-28 07:12:40 +000011505 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011506 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011507
Matt Carlson679563f2009-09-01 12:55:46 +000011508 /*
11509 * Setup interrupts first so we know how
11510 * many NAPI resources to allocate
11511 */
11512 tg3_ints_init(tp);
11513
Matt Carlson90415472011-12-16 13:33:23 +000011514 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011515
Linus Torvalds1da177e2005-04-16 15:20:36 -070011516 /* The placement of this call is tied
11517 * to the setup and use of Host TX descriptors.
11518 */
11519 err = tg3_alloc_consistent(tp);
11520 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011521 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011522
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011523 tg3_napi_init(tp);
11524
Matt Carlsonfed97812009-09-01 13:10:19 +000011525 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011526
Matt Carlson4f125f42009-09-01 12:55:02 +000011527 for (i = 0; i < tp->irq_cnt; i++) {
11528 struct tg3_napi *tnapi = &tp->napi[i];
11529 err = tg3_request_irq(tp, i);
11530 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011531 for (i--; i >= 0; i--) {
11532 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011533 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011534 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011535 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011536 }
11537 }
Matt Carlson07b01732009-08-28 14:01:15 +000011538
David S. Millerf47c11e2005-06-24 20:18:35 -070011539 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011540
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011541 if (init)
11542 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11543
Michael Chand8f4cd32012-09-28 07:12:40 +000011544 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011545 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011546 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011547 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011548 }
11549
David S. Millerf47c11e2005-06-24 20:18:35 -070011550 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011551
Matt Carlson07b01732009-08-28 14:01:15 +000011552 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011553 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011554
Michael Chand8f4cd32012-09-28 07:12:40 +000011555 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011556 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011557
Michael Chan79381092005-04-21 17:13:59 -070011558 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011559 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011560 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011561 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011562 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011563
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011564 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011565 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011566
Joe Perches63c3a662011-04-26 08:12:10 +000011567 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011568 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011569
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011570 tw32(PCIE_TRANSACTION_CFG,
11571 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011572 }
Michael Chan79381092005-04-21 17:13:59 -070011573 }
11574
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011575 tg3_phy_start(tp);
11576
Michael Chanaed93e02012-07-16 16:24:02 +000011577 tg3_hwmon_open(tp);
11578
David S. Millerf47c11e2005-06-24 20:18:35 -070011579 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580
Matt Carlson21f76382012-02-22 12:35:21 +000011581 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011582 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583 tg3_enable_ints(tp);
11584
Ivan Vecera20d14a52015-01-08 16:13:07 +010011585 tg3_ptp_resume(tp);
Matt Carlsonbe947302012-12-03 19:36:57 +000011586
David S. Millerf47c11e2005-06-24 20:18:35 -070011587 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011588
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011589 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011590
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011591 /*
11592 * Reset loopback feature if it was turned on while the device was down
11593 * make sure that it's installed properly now.
11594 */
11595 if (dev->features & NETIF_F_LOOPBACK)
11596 tg3_set_loopback(dev, dev->features);
11597
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011599
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011600out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011601 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11602 struct tg3_napi *tnapi = &tp->napi[i];
11603 free_irq(tnapi->irq_vec, tnapi);
11604 }
Matt Carlson07b01732009-08-28 14:01:15 +000011605
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011606out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011607 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011608 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011609 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011610
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011611out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011612 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011613
Matt Carlson07b01732009-08-28 14:01:15 +000011614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011615}
11616
Michael Chan65138592012-09-28 07:12:41 +000011617static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011618{
Matt Carlson4f125f42009-09-01 12:55:02 +000011619 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011620
Matt Carlsondb219972011-11-04 09:15:03 +000011621 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011622 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011623
Matt Carlson21f76382012-02-22 12:35:21 +000011624 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011625
Michael Chanaed93e02012-07-16 16:24:02 +000011626 tg3_hwmon_close(tp);
11627
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011628 tg3_phy_stop(tp);
11629
David S. Millerf47c11e2005-06-24 20:18:35 -070011630 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011631
11632 tg3_disable_ints(tp);
11633
Michael Chan944d9802005-05-29 14:57:48 -070011634 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011635 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011636 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011637
David S. Millerf47c11e2005-06-24 20:18:35 -070011638 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011639
Matt Carlson4f125f42009-09-01 12:55:02 +000011640 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11641 struct tg3_napi *tnapi = &tp->napi[i];
11642 free_irq(tnapi->irq_vec, tnapi);
11643 }
Matt Carlson07b01732009-08-28 14:01:15 +000011644
11645 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011646
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011647 tg3_napi_fini(tp);
11648
Linus Torvalds1da177e2005-04-16 15:20:36 -070011649 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011650}
11651
Michael Chand8f4cd32012-09-28 07:12:40 +000011652static int tg3_open(struct net_device *dev)
11653{
11654 struct tg3 *tp = netdev_priv(dev);
11655 int err;
11656
Ivan Vecera0486a062014-09-01 14:21:57 +020011657 if (tp->pcierr_recovery) {
11658 netdev_err(dev, "Failed to open device. PCI error recovery "
11659 "in progress\n");
11660 return -EAGAIN;
11661 }
11662
Michael Chand8f4cd32012-09-28 07:12:40 +000011663 if (tp->fw_needed) {
11664 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011665 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11666 if (err) {
11667 netdev_warn(tp->dev, "EEE capability disabled\n");
11668 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11669 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11670 netdev_warn(tp->dev, "EEE capability restored\n");
11671 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11672 }
11673 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011674 if (err)
11675 return err;
11676 } else if (err) {
11677 netdev_warn(tp->dev, "TSO capability disabled\n");
11678 tg3_flag_clear(tp, TSO_CAPABLE);
11679 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11680 netdev_notice(tp->dev, "TSO capability restored\n");
11681 tg3_flag_set(tp, TSO_CAPABLE);
11682 }
11683 }
11684
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011685 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011686
11687 err = tg3_power_up(tp);
11688 if (err)
11689 return err;
11690
11691 tg3_full_lock(tp, 0);
11692
11693 tg3_disable_ints(tp);
11694 tg3_flag_clear(tp, INIT_COMPLETE);
11695
11696 tg3_full_unlock(tp);
11697
Nithin Sujir942d1af2013-04-09 08:48:07 +000011698 err = tg3_start(tp,
11699 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11700 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011701 if (err) {
11702 tg3_frob_aux_power(tp, false);
11703 pci_set_power_state(tp->pdev, PCI_D3hot);
11704 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011705
Linus Torvalds1da177e2005-04-16 15:20:36 -070011706 return err;
11707}
11708
11709static int tg3_close(struct net_device *dev)
11710{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011711 struct tg3 *tp = netdev_priv(dev);
11712
Ivan Vecera0486a062014-09-01 14:21:57 +020011713 if (tp->pcierr_recovery) {
11714 netdev_err(dev, "Failed to close device. PCI error recovery "
11715 "in progress\n");
11716 return -EAGAIN;
11717 }
11718
Michael Chan65138592012-09-28 07:12:41 +000011719 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011720
11721 /* Clear stats across close / open calls */
11722 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11723 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011724
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011725 if (pci_device_is_present(tp->pdev)) {
11726 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011727
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011728 tg3_carrier_off(tp);
11729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011730 return 0;
11731}
11732
11733static inline u64 get_stat64(tg3_stat64_t *val)
11734{
11735 return ((u64)val->high << 32) | ((u64)val->low);
11736}
11737
11738static u64 tg3_calc_crc_errors(struct tg3 *tp)
11739{
11740 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11741
11742 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011743 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11744 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011745 u32 val;
11746
11747 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11748 tg3_writephy(tp, MII_TG3_TEST1,
11749 val | MII_TG3_TEST1_CRC_EN);
11750 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11751 } else
11752 val = 0;
11753
11754 tp->phy_crc_errors += val;
11755
11756 return tp->phy_crc_errors;
11757 }
11758
11759 return get_stat64(&hw_stats->rx_fcs_errors);
11760}
11761
11762#define ESTAT_ADD(member) \
11763 estats->member = old_estats->member + \
11764 get_stat64(&hw_stats->member)
11765
11766static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11767{
11768 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11769 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11770
11771 ESTAT_ADD(rx_octets);
11772 ESTAT_ADD(rx_fragments);
11773 ESTAT_ADD(rx_ucast_packets);
11774 ESTAT_ADD(rx_mcast_packets);
11775 ESTAT_ADD(rx_bcast_packets);
11776 ESTAT_ADD(rx_fcs_errors);
11777 ESTAT_ADD(rx_align_errors);
11778 ESTAT_ADD(rx_xon_pause_rcvd);
11779 ESTAT_ADD(rx_xoff_pause_rcvd);
11780 ESTAT_ADD(rx_mac_ctrl_rcvd);
11781 ESTAT_ADD(rx_xoff_entered);
11782 ESTAT_ADD(rx_frame_too_long_errors);
11783 ESTAT_ADD(rx_jabbers);
11784 ESTAT_ADD(rx_undersize_packets);
11785 ESTAT_ADD(rx_in_length_errors);
11786 ESTAT_ADD(rx_out_length_errors);
11787 ESTAT_ADD(rx_64_or_less_octet_packets);
11788 ESTAT_ADD(rx_65_to_127_octet_packets);
11789 ESTAT_ADD(rx_128_to_255_octet_packets);
11790 ESTAT_ADD(rx_256_to_511_octet_packets);
11791 ESTAT_ADD(rx_512_to_1023_octet_packets);
11792 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11793 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11794 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11795 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11796 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11797
11798 ESTAT_ADD(tx_octets);
11799 ESTAT_ADD(tx_collisions);
11800 ESTAT_ADD(tx_xon_sent);
11801 ESTAT_ADD(tx_xoff_sent);
11802 ESTAT_ADD(tx_flow_control);
11803 ESTAT_ADD(tx_mac_errors);
11804 ESTAT_ADD(tx_single_collisions);
11805 ESTAT_ADD(tx_mult_collisions);
11806 ESTAT_ADD(tx_deferred);
11807 ESTAT_ADD(tx_excessive_collisions);
11808 ESTAT_ADD(tx_late_collisions);
11809 ESTAT_ADD(tx_collide_2times);
11810 ESTAT_ADD(tx_collide_3times);
11811 ESTAT_ADD(tx_collide_4times);
11812 ESTAT_ADD(tx_collide_5times);
11813 ESTAT_ADD(tx_collide_6times);
11814 ESTAT_ADD(tx_collide_7times);
11815 ESTAT_ADD(tx_collide_8times);
11816 ESTAT_ADD(tx_collide_9times);
11817 ESTAT_ADD(tx_collide_10times);
11818 ESTAT_ADD(tx_collide_11times);
11819 ESTAT_ADD(tx_collide_12times);
11820 ESTAT_ADD(tx_collide_13times);
11821 ESTAT_ADD(tx_collide_14times);
11822 ESTAT_ADD(tx_collide_15times);
11823 ESTAT_ADD(tx_ucast_packets);
11824 ESTAT_ADD(tx_mcast_packets);
11825 ESTAT_ADD(tx_bcast_packets);
11826 ESTAT_ADD(tx_carrier_sense_errors);
11827 ESTAT_ADD(tx_discards);
11828 ESTAT_ADD(tx_errors);
11829
11830 ESTAT_ADD(dma_writeq_full);
11831 ESTAT_ADD(dma_write_prioq_full);
11832 ESTAT_ADD(rxbds_empty);
11833 ESTAT_ADD(rx_discards);
11834 ESTAT_ADD(rx_errors);
11835 ESTAT_ADD(rx_threshold_hit);
11836
11837 ESTAT_ADD(dma_readq_full);
11838 ESTAT_ADD(dma_read_prioq_full);
11839 ESTAT_ADD(tx_comp_queue_full);
11840
11841 ESTAT_ADD(ring_set_send_prod_index);
11842 ESTAT_ADD(ring_status_update);
11843 ESTAT_ADD(nic_irqs);
11844 ESTAT_ADD(nic_avoided_irqs);
11845 ESTAT_ADD(nic_tx_threshold_hit);
11846
Matt Carlson4452d092011-05-19 12:12:51 +000011847 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011848}
11849
Matt Carlson65ec6982012-02-28 23:33:37 +000011850static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011851{
Eric Dumazet511d2222010-07-07 20:44:24 +000011852 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011853 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11854
Linus Torvalds1da177e2005-04-16 15:20:36 -070011855 stats->rx_packets = old_stats->rx_packets +
11856 get_stat64(&hw_stats->rx_ucast_packets) +
11857 get_stat64(&hw_stats->rx_mcast_packets) +
11858 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011859
Linus Torvalds1da177e2005-04-16 15:20:36 -070011860 stats->tx_packets = old_stats->tx_packets +
11861 get_stat64(&hw_stats->tx_ucast_packets) +
11862 get_stat64(&hw_stats->tx_mcast_packets) +
11863 get_stat64(&hw_stats->tx_bcast_packets);
11864
11865 stats->rx_bytes = old_stats->rx_bytes +
11866 get_stat64(&hw_stats->rx_octets);
11867 stats->tx_bytes = old_stats->tx_bytes +
11868 get_stat64(&hw_stats->tx_octets);
11869
11870 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011871 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011872 stats->tx_errors = old_stats->tx_errors +
11873 get_stat64(&hw_stats->tx_errors) +
11874 get_stat64(&hw_stats->tx_mac_errors) +
11875 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11876 get_stat64(&hw_stats->tx_discards);
11877
11878 stats->multicast = old_stats->multicast +
11879 get_stat64(&hw_stats->rx_mcast_packets);
11880 stats->collisions = old_stats->collisions +
11881 get_stat64(&hw_stats->tx_collisions);
11882
11883 stats->rx_length_errors = old_stats->rx_length_errors +
11884 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11885 get_stat64(&hw_stats->rx_undersize_packets);
11886
Linus Torvalds1da177e2005-04-16 15:20:36 -070011887 stats->rx_frame_errors = old_stats->rx_frame_errors +
11888 get_stat64(&hw_stats->rx_align_errors);
11889 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11890 get_stat64(&hw_stats->tx_discards);
11891 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11892 get_stat64(&hw_stats->tx_carrier_sense_errors);
11893
11894 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011895 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896
John W. Linville4f63b872005-09-12 14:43:18 -070011897 stats->rx_missed_errors = old_stats->rx_missed_errors +
11898 get_stat64(&hw_stats->rx_discards);
11899
Eric Dumazetb0057c52010-10-10 19:55:52 +000011900 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011901 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011902}
11903
Linus Torvalds1da177e2005-04-16 15:20:36 -070011904static int tg3_get_regs_len(struct net_device *dev)
11905{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011906 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011907}
11908
11909static void tg3_get_regs(struct net_device *dev,
11910 struct ethtool_regs *regs, void *_p)
11911{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011912 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011913
11914 regs->version = 0;
11915
Matt Carlson97bd8e42011-04-13 11:05:04 +000011916 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011917
Matt Carlson800960682010-08-02 11:26:06 +000011918 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011919 return;
11920
David S. Millerf47c11e2005-06-24 20:18:35 -070011921 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011922
Matt Carlson97bd8e42011-04-13 11:05:04 +000011923 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011924
David S. Millerf47c11e2005-06-24 20:18:35 -070011925 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926}
11927
11928static int tg3_get_eeprom_len(struct net_device *dev)
11929{
11930 struct tg3 *tp = netdev_priv(dev);
11931
11932 return tp->nvram_size;
11933}
11934
Linus Torvalds1da177e2005-04-16 15:20:36 -070011935static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11936{
11937 struct tg3 *tp = netdev_priv(dev);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011938 int ret, cpmu_restore = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011939 u8 *pd;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011940 u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011941 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942
Joe Perches63c3a662011-04-26 08:12:10 +000011943 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011944 return -EINVAL;
11945
Linus Torvalds1da177e2005-04-16 15:20:36 -070011946 offset = eeprom->offset;
11947 len = eeprom->len;
11948 eeprom->len = 0;
11949
11950 eeprom->magic = TG3_EEPROM_MAGIC;
11951
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011952 /* Override clock, link aware and link idle modes */
11953 if (tg3_flag(tp, CPMU_PRESENT)) {
11954 cpmu_val = tr32(TG3_CPMU_CTRL);
11955 if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
11956 CPMU_CTRL_LINK_IDLE_MODE)) {
11957 tw32(TG3_CPMU_CTRL, cpmu_val &
11958 ~(CPMU_CTRL_LINK_AWARE_MODE |
11959 CPMU_CTRL_LINK_IDLE_MODE));
11960 cpmu_restore = 1;
11961 }
11962 }
11963 tg3_override_clk(tp);
11964
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965 if (offset & 3) {
11966 /* adjustments to start on required 4 byte boundary */
11967 b_offset = offset & 3;
11968 b_count = 4 - b_offset;
11969 if (b_count > len) {
11970 /* i.e. offset=1 len=2 */
11971 b_count = len;
11972 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011973 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011974 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011975 goto eeprom_done;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011976 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011977 len -= b_count;
11978 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011979 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011980 }
11981
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011982 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011983 pd = &data[eeprom->len];
11984 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011985 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011986 if (ret) {
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011987 if (i)
11988 i -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011989 eeprom->len += i;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011990 goto eeprom_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011992 memcpy(pd + i, &val, 4);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011993 if (need_resched()) {
11994 if (signal_pending(current)) {
11995 eeprom->len += i;
11996 ret = -EINTR;
11997 goto eeprom_done;
11998 }
11999 cond_resched();
12000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012001 }
12002 eeprom->len += i;
12003
12004 if (len & 3) {
12005 /* read last bytes not ending on 4 byte boundary */
12006 pd = &data[eeprom->len];
12007 b_count = len & 3;
12008 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012009 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012010 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012011 goto eeprom_done;
Al Virob9fc7dc2007-12-17 22:59:57 -080012012 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012013 eeprom->len += b_count;
12014 }
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012015 ret = 0;
12016
12017eeprom_done:
12018 /* Restore clock, link aware and link idle modes */
12019 tg3_restore_clk(tp);
12020 if (cpmu_restore)
12021 tw32(TG3_CPMU_CTRL, cpmu_val);
12022
12023 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012024}
12025
Linus Torvalds1da177e2005-04-16 15:20:36 -070012026static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
12027{
12028 struct tg3 *tp = netdev_priv(dev);
12029 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080012030 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012031 u8 *buf;
Arnd Bergmanne434e042016-01-29 12:39:15 +010012032 __be32 start = 0, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012033
Joe Perches63c3a662011-04-26 08:12:10 +000012034 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000012035 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012036 return -EINVAL;
12037
12038 offset = eeprom->offset;
12039 len = eeprom->len;
12040
12041 if ((b_offset = (offset & 3))) {
12042 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012043 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012044 if (ret)
12045 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012046 len += b_offset;
12047 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070012048 if (len < 4)
12049 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012050 }
12051
12052 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070012053 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012054 /* adjustments to end on required 4 byte boundary */
12055 odd_len = 1;
12056 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012057 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058 if (ret)
12059 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012060 }
12061
12062 buf = data;
12063 if (b_offset || odd_len) {
12064 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012065 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012066 return -ENOMEM;
12067 if (b_offset)
12068 memcpy(buf, &start, 4);
12069 if (odd_len)
12070 memcpy(buf+len-4, &end, 4);
12071 memcpy(buf + b_offset, data, eeprom->len);
12072 }
12073
12074 ret = tg3_nvram_write_block(tp, offset, len, buf);
12075
12076 if (buf != data)
12077 kfree(buf);
12078
12079 return ret;
12080}
12081
12082static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12083{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012084 struct tg3 *tp = netdev_priv(dev);
12085
Joe Perches63c3a662011-04-26 08:12:10 +000012086 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012087 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012088 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012089 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012090 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012091 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012092 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012093
Linus Torvalds1da177e2005-04-16 15:20:36 -070012094 cmd->supported = (SUPPORTED_Autoneg);
12095
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012096 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012097 cmd->supported |= (SUPPORTED_1000baseT_Half |
12098 SUPPORTED_1000baseT_Full);
12099
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012100 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012101 cmd->supported |= (SUPPORTED_100baseT_Half |
12102 SUPPORTED_100baseT_Full |
12103 SUPPORTED_10baseT_Half |
12104 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080012105 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070012106 cmd->port = PORT_TP;
12107 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012108 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070012109 cmd->port = PORT_FIBRE;
12110 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012111
Linus Torvalds1da177e2005-04-16 15:20:36 -070012112 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000012113 if (tg3_flag(tp, PAUSE_AUTONEG)) {
12114 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
12115 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12116 cmd->advertising |= ADVERTISED_Pause;
12117 } else {
12118 cmd->advertising |= ADVERTISED_Pause |
12119 ADVERTISED_Asym_Pause;
12120 }
12121 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12122 cmd->advertising |= ADVERTISED_Asym_Pause;
12123 }
12124 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012125 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000012126 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012127 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000012128 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012129 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
12130 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
12131 cmd->eth_tp_mdix = ETH_TP_MDI_X;
12132 else
12133 cmd->eth_tp_mdix = ETH_TP_MDI;
12134 }
Matt Carlson64c22182010-10-14 10:37:44 +000012135 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000012136 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
12137 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012138 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012139 }
Matt Carlson882e9792009-09-01 13:21:36 +000012140 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012141 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012142 cmd->autoneg = tp->link_config.autoneg;
12143 cmd->maxtxpkt = 0;
12144 cmd->maxrxpkt = 0;
12145 return 0;
12146}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012147
Linus Torvalds1da177e2005-04-16 15:20:36 -070012148static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12149{
12150 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000012151 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012152
Joe Perches63c3a662011-04-26 08:12:10 +000012153 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012154 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012155 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012156 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012157 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012158 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012159 }
12160
Matt Carlson7e5856b2009-02-25 14:23:01 +000012161 if (cmd->autoneg != AUTONEG_ENABLE &&
12162 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012163 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012164
12165 if (cmd->autoneg == AUTONEG_DISABLE &&
12166 cmd->duplex != DUPLEX_FULL &&
12167 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012168 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012169
Matt Carlson7e5856b2009-02-25 14:23:01 +000012170 if (cmd->autoneg == AUTONEG_ENABLE) {
12171 u32 mask = ADVERTISED_Autoneg |
12172 ADVERTISED_Pause |
12173 ADVERTISED_Asym_Pause;
12174
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012175 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012176 mask |= ADVERTISED_1000baseT_Half |
12177 ADVERTISED_1000baseT_Full;
12178
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012179 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012180 mask |= ADVERTISED_100baseT_Half |
12181 ADVERTISED_100baseT_Full |
12182 ADVERTISED_10baseT_Half |
12183 ADVERTISED_10baseT_Full |
12184 ADVERTISED_TP;
12185 else
12186 mask |= ADVERTISED_FIBRE;
12187
12188 if (cmd->advertising & ~mask)
12189 return -EINVAL;
12190
12191 mask &= (ADVERTISED_1000baseT_Half |
12192 ADVERTISED_1000baseT_Full |
12193 ADVERTISED_100baseT_Half |
12194 ADVERTISED_100baseT_Full |
12195 ADVERTISED_10baseT_Half |
12196 ADVERTISED_10baseT_Full);
12197
12198 cmd->advertising &= mask;
12199 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012200 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012201 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012202 return -EINVAL;
12203
12204 if (cmd->duplex != DUPLEX_FULL)
12205 return -EINVAL;
12206 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012207 if (speed != SPEED_100 &&
12208 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012209 return -EINVAL;
12210 }
12211 }
12212
David S. Millerf47c11e2005-06-24 20:18:35 -070012213 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012214
12215 tp->link_config.autoneg = cmd->autoneg;
12216 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012217 tp->link_config.advertising = (cmd->advertising |
12218 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012219 tp->link_config.speed = SPEED_UNKNOWN;
12220 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012221 } else {
12222 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012223 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012224 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012225 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012226
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012227 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12228
Nithin Sujirce20f162013-04-09 08:48:04 +000012229 tg3_warn_mgmt_link_flap(tp);
12230
Linus Torvalds1da177e2005-04-16 15:20:36 -070012231 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012232 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012233
David S. Millerf47c11e2005-06-24 20:18:35 -070012234 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012235
Linus Torvalds1da177e2005-04-16 15:20:36 -070012236 return 0;
12237}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012238
Linus Torvalds1da177e2005-04-16 15:20:36 -070012239static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12240{
12241 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012242
Rick Jones68aad782011-11-07 13:29:27 +000012243 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12244 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12245 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12246 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012247}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012248
Linus Torvalds1da177e2005-04-16 15:20:36 -070012249static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12250{
12251 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012252
Joe Perches63c3a662011-04-26 08:12:10 +000012253 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012254 wol->supported = WAKE_MAGIC;
12255 else
12256 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012257 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012258 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012259 wol->wolopts = WAKE_MAGIC;
12260 memset(&wol->sopass, 0, sizeof(wol->sopass));
12261}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012262
Linus Torvalds1da177e2005-04-16 15:20:36 -070012263static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12264{
12265 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012266 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012267
Linus Torvalds1da177e2005-04-16 15:20:36 -070012268 if (wol->wolopts & ~WAKE_MAGIC)
12269 return -EINVAL;
12270 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012271 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012272 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012273
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012274 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12275
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012276 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012277 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012278 else
Joe Perches63c3a662011-04-26 08:12:10 +000012279 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012280
Linus Torvalds1da177e2005-04-16 15:20:36 -070012281 return 0;
12282}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012283
Linus Torvalds1da177e2005-04-16 15:20:36 -070012284static u32 tg3_get_msglevel(struct net_device *dev)
12285{
12286 struct tg3 *tp = netdev_priv(dev);
12287 return tp->msg_enable;
12288}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012289
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290static void tg3_set_msglevel(struct net_device *dev, u32 value)
12291{
12292 struct tg3 *tp = netdev_priv(dev);
12293 tp->msg_enable = value;
12294}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012295
Linus Torvalds1da177e2005-04-16 15:20:36 -070012296static int tg3_nway_reset(struct net_device *dev)
12297{
12298 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012299 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012300
Linus Torvalds1da177e2005-04-16 15:20:36 -070012301 if (!netif_running(dev))
12302 return -EAGAIN;
12303
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012304 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012305 return -EINVAL;
12306
Nithin Sujirce20f162013-04-09 08:48:04 +000012307 tg3_warn_mgmt_link_flap(tp);
12308
Joe Perches63c3a662011-04-26 08:12:10 +000012309 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012310 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012311 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012312 r = phy_start_aneg(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012313 } else {
12314 u32 bmcr;
12315
12316 spin_lock_bh(&tp->lock);
12317 r = -EINVAL;
12318 tg3_readphy(tp, MII_BMCR, &bmcr);
12319 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12320 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012321 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012322 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12323 BMCR_ANENABLE);
12324 r = 0;
12325 }
12326 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012328
Linus Torvalds1da177e2005-04-16 15:20:36 -070012329 return r;
12330}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012331
Linus Torvalds1da177e2005-04-16 15:20:36 -070012332static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12333{
12334 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012335
Matt Carlson2c49a442010-09-30 10:34:35 +000012336 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012337 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012338 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012339 else
12340 ering->rx_jumbo_max_pending = 0;
12341
12342 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012343
12344 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012345 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012346 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12347 else
12348 ering->rx_jumbo_pending = 0;
12349
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012350 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012351}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012352
Linus Torvalds1da177e2005-04-16 15:20:36 -070012353static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12354{
12355 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012356 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012357
Matt Carlson2c49a442010-09-30 10:34:35 +000012358 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12359 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012360 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12361 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012362 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012363 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012364 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012365
Michael Chanbbe832c2005-06-24 20:20:04 -070012366 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012367 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012368 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012369 irq_sync = 1;
12370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012371
Michael Chanbbe832c2005-06-24 20:20:04 -070012372 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012373
Linus Torvalds1da177e2005-04-16 15:20:36 -070012374 tp->rx_pending = ering->rx_pending;
12375
Joe Perches63c3a662011-04-26 08:12:10 +000012376 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012377 tp->rx_pending > 63)
12378 tp->rx_pending = 63;
Ivan Veceraba67b512014-04-17 14:51:08 +020012379
12380 if (tg3_flag(tp, JUMBO_RING_ENABLE))
12381 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012382
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012383 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012384 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012385
12386 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012387 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012388 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012389 if (!err)
12390 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012391 }
12392
David S. Millerf47c11e2005-06-24 20:18:35 -070012393 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012394
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012395 if (irq_sync && !err)
12396 tg3_phy_start(tp);
12397
Michael Chanb9ec6c12006-07-25 16:37:27 -070012398 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012399}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012400
Linus Torvalds1da177e2005-04-16 15:20:36 -070012401static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12402{
12403 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012404
Joe Perches63c3a662011-04-26 08:12:10 +000012405 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012406
Matt Carlson4a2db502011-12-08 14:40:17 +000012407 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012408 epause->rx_pause = 1;
12409 else
12410 epause->rx_pause = 0;
12411
Matt Carlson4a2db502011-12-08 14:40:17 +000012412 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012413 epause->tx_pause = 1;
12414 else
12415 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012416}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012417
Linus Torvalds1da177e2005-04-16 15:20:36 -070012418static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12419{
12420 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012421 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012422
Nithin Sujirce20f162013-04-09 08:48:04 +000012423 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12424 tg3_warn_mgmt_link_flap(tp);
12425
Joe Perches63c3a662011-04-26 08:12:10 +000012426 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012427 u32 newadv;
12428 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012429
Andrew Lunn7f854422016-01-06 20:11:18 +010012430 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012431
Matt Carlson27121682010-02-17 15:16:57 +000012432 if (!(phydev->supported & SUPPORTED_Pause) ||
12433 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012434 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012435 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012436
Matt Carlson27121682010-02-17 15:16:57 +000012437 tp->link_config.flowctrl = 0;
12438 if (epause->rx_pause) {
12439 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012440
Matt Carlson27121682010-02-17 15:16:57 +000012441 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012442 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012443 newadv = ADVERTISED_Pause;
12444 } else
12445 newadv = ADVERTISED_Pause |
12446 ADVERTISED_Asym_Pause;
12447 } else if (epause->tx_pause) {
12448 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12449 newadv = ADVERTISED_Asym_Pause;
12450 } else
12451 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012452
Matt Carlson27121682010-02-17 15:16:57 +000012453 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012454 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012455 else
Joe Perches63c3a662011-04-26 08:12:10 +000012456 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012457
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012458 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012459 u32 oldadv = phydev->advertising &
12460 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12461 if (oldadv != newadv) {
12462 phydev->advertising &=
12463 ~(ADVERTISED_Pause |
12464 ADVERTISED_Asym_Pause);
12465 phydev->advertising |= newadv;
12466 if (phydev->autoneg) {
12467 /*
12468 * Always renegotiate the link to
12469 * inform our link partner of our
12470 * flow control settings, even if the
12471 * flow control is forced. Let
12472 * tg3_adjust_link() do the final
12473 * flow control setup.
12474 */
12475 return phy_start_aneg(phydev);
12476 }
12477 }
12478
12479 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012480 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012481 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012482 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012483 ~(ADVERTISED_Pause |
12484 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012485 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012486 }
12487 } else {
12488 int irq_sync = 0;
12489
12490 if (netif_running(dev)) {
12491 tg3_netif_stop(tp);
12492 irq_sync = 1;
12493 }
12494
12495 tg3_full_lock(tp, irq_sync);
12496
12497 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012498 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012499 else
Joe Perches63c3a662011-04-26 08:12:10 +000012500 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012501 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012502 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012503 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012504 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012505 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012506 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012507 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012508 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012509
12510 if (netif_running(dev)) {
12511 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012512 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012513 if (!err)
12514 tg3_netif_start(tp);
12515 }
12516
12517 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012519
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012520 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12521
Michael Chanb9ec6c12006-07-25 16:37:27 -070012522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012523}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012524
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012525static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012526{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012527 switch (sset) {
12528 case ETH_SS_TEST:
12529 return TG3_NUM_TEST;
12530 case ETH_SS_STATS:
12531 return TG3_NUM_STATS;
12532 default:
12533 return -EOPNOTSUPP;
12534 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012535}
12536
Matt Carlson90415472011-12-16 13:33:23 +000012537static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12538 u32 *rules __always_unused)
12539{
12540 struct tg3 *tp = netdev_priv(dev);
12541
12542 if (!tg3_flag(tp, SUPPORT_MSIX))
12543 return -EOPNOTSUPP;
12544
12545 switch (info->cmd) {
12546 case ETHTOOL_GRXRINGS:
12547 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012548 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012549 else {
12550 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012551 if (info->data > TG3_RSS_MAX_NUM_QS)
12552 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012553 }
12554
12555 /* The first interrupt vector only
12556 * handles link interrupts.
12557 */
12558 info->data -= 1;
12559 return 0;
12560
12561 default:
12562 return -EOPNOTSUPP;
12563 }
12564}
12565
12566static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12567{
12568 u32 size = 0;
12569 struct tg3 *tp = netdev_priv(dev);
12570
12571 if (tg3_flag(tp, SUPPORT_MSIX))
12572 size = TG3_RSS_INDIR_TBL_SIZE;
12573
12574 return size;
12575}
12576
Eyal Perry892311f2014-12-02 18:12:10 +020012577static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012578{
12579 struct tg3 *tp = netdev_priv(dev);
12580 int i;
12581
Eyal Perry892311f2014-12-02 18:12:10 +020012582 if (hfunc)
12583 *hfunc = ETH_RSS_HASH_TOP;
12584 if (!indir)
12585 return 0;
12586
Matt Carlson90415472011-12-16 13:33:23 +000012587 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12588 indir[i] = tp->rss_ind_tbl[i];
12589
12590 return 0;
12591}
12592
Eyal Perry892311f2014-12-02 18:12:10 +020012593static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
12594 const u8 hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012595{
12596 struct tg3 *tp = netdev_priv(dev);
12597 size_t i;
12598
Eyal Perry892311f2014-12-02 18:12:10 +020012599 /* We require at least one supported parameter to be changed and no
12600 * change in any of the unsupported parameters
12601 */
12602 if (key ||
12603 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
12604 return -EOPNOTSUPP;
12605
12606 if (!indir)
12607 return 0;
12608
Matt Carlson90415472011-12-16 13:33:23 +000012609 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12610 tp->rss_ind_tbl[i] = indir[i];
12611
12612 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12613 return 0;
12614
12615 /* It is legal to write the indirection
12616 * table while the device is running.
12617 */
12618 tg3_full_lock(tp, 0);
12619 tg3_rss_write_indir_tbl(tp);
12620 tg3_full_unlock(tp);
12621
12622 return 0;
12623}
12624
Michael Chan09681692012-09-28 07:12:42 +000012625static void tg3_get_channels(struct net_device *dev,
12626 struct ethtool_channels *channel)
12627{
12628 struct tg3 *tp = netdev_priv(dev);
12629 u32 deflt_qs = netif_get_num_default_rss_queues();
12630
12631 channel->max_rx = tp->rxq_max;
12632 channel->max_tx = tp->txq_max;
12633
12634 if (netif_running(dev)) {
12635 channel->rx_count = tp->rxq_cnt;
12636 channel->tx_count = tp->txq_cnt;
12637 } else {
12638 if (tp->rxq_req)
12639 channel->rx_count = tp->rxq_req;
12640 else
12641 channel->rx_count = min(deflt_qs, tp->rxq_max);
12642
12643 if (tp->txq_req)
12644 channel->tx_count = tp->txq_req;
12645 else
12646 channel->tx_count = min(deflt_qs, tp->txq_max);
12647 }
12648}
12649
12650static int tg3_set_channels(struct net_device *dev,
12651 struct ethtool_channels *channel)
12652{
12653 struct tg3 *tp = netdev_priv(dev);
12654
12655 if (!tg3_flag(tp, SUPPORT_MSIX))
12656 return -EOPNOTSUPP;
12657
12658 if (channel->rx_count > tp->rxq_max ||
12659 channel->tx_count > tp->txq_max)
12660 return -EINVAL;
12661
12662 tp->rxq_req = channel->rx_count;
12663 tp->txq_req = channel->tx_count;
12664
12665 if (!netif_running(dev))
12666 return 0;
12667
12668 tg3_stop(tp);
12669
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012670 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012671
Matt Carlsonbe947302012-12-03 19:36:57 +000012672 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012673
12674 return 0;
12675}
12676
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012677static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012678{
12679 switch (stringset) {
12680 case ETH_SS_STATS:
12681 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12682 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012683 case ETH_SS_TEST:
12684 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012686 default:
12687 WARN_ON(1); /* we need a WARN() */
12688 break;
12689 }
12690}
12691
stephen hemminger81b87092011-04-04 08:43:50 +000012692static int tg3_set_phys_id(struct net_device *dev,
12693 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012694{
12695 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012696
12697 if (!netif_running(tp->dev))
12698 return -EAGAIN;
12699
stephen hemminger81b87092011-04-04 08:43:50 +000012700 switch (state) {
12701 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012702 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012703
stephen hemminger81b87092011-04-04 08:43:50 +000012704 case ETHTOOL_ID_ON:
12705 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12706 LED_CTRL_1000MBPS_ON |
12707 LED_CTRL_100MBPS_ON |
12708 LED_CTRL_10MBPS_ON |
12709 LED_CTRL_TRAFFIC_OVERRIDE |
12710 LED_CTRL_TRAFFIC_BLINK |
12711 LED_CTRL_TRAFFIC_LED);
12712 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012713
stephen hemminger81b87092011-04-04 08:43:50 +000012714 case ETHTOOL_ID_OFF:
12715 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12716 LED_CTRL_TRAFFIC_OVERRIDE);
12717 break;
Michael Chan4009a932005-09-05 17:52:54 -070012718
stephen hemminger81b87092011-04-04 08:43:50 +000012719 case ETHTOOL_ID_INACTIVE:
12720 tw32(MAC_LED_CTRL, tp->led_ctrl);
12721 break;
Michael Chan4009a932005-09-05 17:52:54 -070012722 }
stephen hemminger81b87092011-04-04 08:43:50 +000012723
Michael Chan4009a932005-09-05 17:52:54 -070012724 return 0;
12725}
12726
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012727static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012728 struct ethtool_stats *estats, u64 *tmp_stats)
12729{
12730 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012731
Matt Carlsonb546e462012-02-13 15:20:09 +000012732 if (tp->hw_stats)
12733 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12734 else
12735 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012736}
12737
Matt Carlson535a4902011-07-20 10:20:56 +000012738static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012739{
12740 int i;
12741 __be32 *buf;
12742 u32 offset = 0, len = 0;
12743 u32 magic, val;
12744
Joe Perches63c3a662011-04-26 08:12:10 +000012745 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012746 return NULL;
12747
12748 if (magic == TG3_EEPROM_MAGIC) {
12749 for (offset = TG3_NVM_DIR_START;
12750 offset < TG3_NVM_DIR_END;
12751 offset += TG3_NVM_DIRENT_SIZE) {
12752 if (tg3_nvram_read(tp, offset, &val))
12753 return NULL;
12754
12755 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12756 TG3_NVM_DIRTYPE_EXTVPD)
12757 break;
12758 }
12759
12760 if (offset != TG3_NVM_DIR_END) {
12761 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12762 if (tg3_nvram_read(tp, offset + 4, &offset))
12763 return NULL;
12764
12765 offset = tg3_nvram_logical_addr(tp, offset);
12766 }
12767 }
12768
12769 if (!offset || !len) {
12770 offset = TG3_NVM_VPD_OFF;
12771 len = TG3_NVM_VPD_LEN;
12772 }
12773
12774 buf = kmalloc(len, GFP_KERNEL);
12775 if (buf == NULL)
12776 return NULL;
12777
12778 if (magic == TG3_EEPROM_MAGIC) {
12779 for (i = 0; i < len; i += 4) {
12780 /* The data is in little-endian format in NVRAM.
12781 * Use the big-endian read routines to preserve
12782 * the byte order as it exists in NVRAM.
12783 */
12784 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12785 goto error;
12786 }
12787 } else {
12788 u8 *ptr;
12789 ssize_t cnt;
12790 unsigned int pos = 0;
12791
12792 ptr = (u8 *)&buf[0];
12793 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12794 cnt = pci_read_vpd(tp->pdev, pos,
12795 len - pos, ptr);
12796 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12797 cnt = 0;
12798 else if (cnt < 0)
12799 goto error;
12800 }
12801 if (pos != len)
12802 goto error;
12803 }
12804
Matt Carlson535a4902011-07-20 10:20:56 +000012805 *vpdlen = len;
12806
Matt Carlsonc3e94502011-04-13 11:05:08 +000012807 return buf;
12808
12809error:
12810 kfree(buf);
12811 return NULL;
12812}
12813
Michael Chan566f86a2005-05-29 14:56:58 -070012814#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012815#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12816#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12817#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012818#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12819#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012820#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012821#define NVRAM_SELFBOOT_HW_SIZE 0x20
12822#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012823
12824static int tg3_test_nvram(struct tg3 *tp)
12825{
Matt Carlson535a4902011-07-20 10:20:56 +000012826 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012827 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012828 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012829
Joe Perches63c3a662011-04-26 08:12:10 +000012830 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012831 return 0;
12832
Matt Carlsone4f34112009-02-25 14:25:00 +000012833 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012834 return -EIO;
12835
Michael Chan1b277772006-03-20 22:27:48 -080012836 if (magic == TG3_EEPROM_MAGIC)
12837 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012838 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012839 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12840 TG3_EEPROM_SB_FORMAT_1) {
12841 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12842 case TG3_EEPROM_SB_REVISION_0:
12843 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12844 break;
12845 case TG3_EEPROM_SB_REVISION_2:
12846 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12847 break;
12848 case TG3_EEPROM_SB_REVISION_3:
12849 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12850 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012851 case TG3_EEPROM_SB_REVISION_4:
12852 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12853 break;
12854 case TG3_EEPROM_SB_REVISION_5:
12855 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12856 break;
12857 case TG3_EEPROM_SB_REVISION_6:
12858 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12859 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012860 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012861 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012862 }
12863 } else
Michael Chan1b277772006-03-20 22:27:48 -080012864 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012865 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12866 size = NVRAM_SELFBOOT_HW_SIZE;
12867 else
Michael Chan1b277772006-03-20 22:27:48 -080012868 return -EIO;
12869
12870 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012871 if (buf == NULL)
12872 return -ENOMEM;
12873
Michael Chan1b277772006-03-20 22:27:48 -080012874 err = -EIO;
12875 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012876 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12877 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012878 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012879 }
Michael Chan1b277772006-03-20 22:27:48 -080012880 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012881 goto out;
12882
Michael Chan1b277772006-03-20 22:27:48 -080012883 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012884 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012885 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012886 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012887 u8 *buf8 = (u8 *) buf, csum8 = 0;
12888
Al Virob9fc7dc2007-12-17 22:59:57 -080012889 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012890 TG3_EEPROM_SB_REVISION_2) {
12891 /* For rev 2, the csum doesn't include the MBA. */
12892 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12893 csum8 += buf8[i];
12894 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12895 csum8 += buf8[i];
12896 } else {
12897 for (i = 0; i < size; i++)
12898 csum8 += buf8[i];
12899 }
Michael Chan1b277772006-03-20 22:27:48 -080012900
Adrian Bunkad96b482006-04-05 22:21:04 -070012901 if (csum8 == 0) {
12902 err = 0;
12903 goto out;
12904 }
12905
12906 err = -EIO;
12907 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012908 }
Michael Chan566f86a2005-05-29 14:56:58 -070012909
Al Virob9fc7dc2007-12-17 22:59:57 -080012910 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012911 TG3_EEPROM_MAGIC_HW) {
12912 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012913 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012914 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012915
12916 /* Separate the parity bits and the data bytes. */
12917 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12918 if ((i == 0) || (i == 8)) {
12919 int l;
12920 u8 msk;
12921
12922 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12923 parity[k++] = buf8[i] & msk;
12924 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012925 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012926 int l;
12927 u8 msk;
12928
12929 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12930 parity[k++] = buf8[i] & msk;
12931 i++;
12932
12933 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12934 parity[k++] = buf8[i] & msk;
12935 i++;
12936 }
12937 data[j++] = buf8[i];
12938 }
12939
12940 err = -EIO;
12941 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12942 u8 hw8 = hweight8(data[i]);
12943
12944 if ((hw8 & 0x1) && parity[i])
12945 goto out;
12946 else if (!(hw8 & 0x1) && !parity[i])
12947 goto out;
12948 }
12949 err = 0;
12950 goto out;
12951 }
12952
Matt Carlson01c3a392011-03-09 16:58:20 +000012953 err = -EIO;
12954
Michael Chan566f86a2005-05-29 14:56:58 -070012955 /* Bootstrap checksum at offset 0x10 */
12956 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012957 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012958 goto out;
12959
12960 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12961 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012962 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012963 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012964
Matt Carlsonc3e94502011-04-13 11:05:08 +000012965 kfree(buf);
12966
Matt Carlson535a4902011-07-20 10:20:56 +000012967 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012968 if (!buf)
12969 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012970
Matt Carlson535a4902011-07-20 10:20:56 +000012971 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012972 if (i > 0) {
12973 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12974 if (j < 0)
12975 goto out;
12976
Matt Carlson535a4902011-07-20 10:20:56 +000012977 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012978 goto out;
12979
12980 i += PCI_VPD_LRDT_TAG_SIZE;
12981 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12982 PCI_VPD_RO_KEYWORD_CHKSUM);
12983 if (j > 0) {
12984 u8 csum8 = 0;
12985
12986 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12987
12988 for (i = 0; i <= j; i++)
12989 csum8 += ((u8 *)buf)[i];
12990
12991 if (csum8)
12992 goto out;
12993 }
12994 }
12995
Michael Chan566f86a2005-05-29 14:56:58 -070012996 err = 0;
12997
12998out:
12999 kfree(buf);
13000 return err;
13001}
13002
Michael Chanca430072005-05-29 14:57:23 -070013003#define TG3_SERDES_TIMEOUT_SEC 2
13004#define TG3_COPPER_TIMEOUT_SEC 6
13005
13006static int tg3_test_link(struct tg3 *tp)
13007{
13008 int i, max;
13009
13010 if (!netif_running(tp->dev))
13011 return -ENODEV;
13012
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013013 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070013014 max = TG3_SERDES_TIMEOUT_SEC;
13015 else
13016 max = TG3_COPPER_TIMEOUT_SEC;
13017
13018 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000013019 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070013020 return 0;
13021
13022 if (msleep_interruptible(1000))
13023 break;
13024 }
13025
13026 return -EIO;
13027}
13028
Michael Chana71116d2005-05-29 14:58:11 -070013029/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080013030static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070013031{
Michael Chanb16250e2006-09-27 16:10:14 -070013032 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070013033 u32 offset, read_mask, write_mask, val, save_val, read_val;
13034 static struct {
13035 u16 offset;
13036 u16 flags;
13037#define TG3_FL_5705 0x1
13038#define TG3_FL_NOT_5705 0x2
13039#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070013040#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070013041 u32 read_mask;
13042 u32 write_mask;
13043 } reg_tbl[] = {
13044 /* MAC Control Registers */
13045 { MAC_MODE, TG3_FL_NOT_5705,
13046 0x00000000, 0x00ef6f8c },
13047 { MAC_MODE, TG3_FL_5705,
13048 0x00000000, 0x01ef6b8c },
13049 { MAC_STATUS, TG3_FL_NOT_5705,
13050 0x03800107, 0x00000000 },
13051 { MAC_STATUS, TG3_FL_5705,
13052 0x03800100, 0x00000000 },
13053 { MAC_ADDR_0_HIGH, 0x0000,
13054 0x00000000, 0x0000ffff },
13055 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013056 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070013057 { MAC_RX_MTU_SIZE, 0x0000,
13058 0x00000000, 0x0000ffff },
13059 { MAC_TX_MODE, 0x0000,
13060 0x00000000, 0x00000070 },
13061 { MAC_TX_LENGTHS, 0x0000,
13062 0x00000000, 0x00003fff },
13063 { MAC_RX_MODE, TG3_FL_NOT_5705,
13064 0x00000000, 0x000007fc },
13065 { MAC_RX_MODE, TG3_FL_5705,
13066 0x00000000, 0x000007dc },
13067 { MAC_HASH_REG_0, 0x0000,
13068 0x00000000, 0xffffffff },
13069 { MAC_HASH_REG_1, 0x0000,
13070 0x00000000, 0xffffffff },
13071 { MAC_HASH_REG_2, 0x0000,
13072 0x00000000, 0xffffffff },
13073 { MAC_HASH_REG_3, 0x0000,
13074 0x00000000, 0xffffffff },
13075
13076 /* Receive Data and Receive BD Initiator Control Registers. */
13077 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
13078 0x00000000, 0xffffffff },
13079 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
13080 0x00000000, 0xffffffff },
13081 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
13082 0x00000000, 0x00000003 },
13083 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
13084 0x00000000, 0xffffffff },
13085 { RCVDBDI_STD_BD+0, 0x0000,
13086 0x00000000, 0xffffffff },
13087 { RCVDBDI_STD_BD+4, 0x0000,
13088 0x00000000, 0xffffffff },
13089 { RCVDBDI_STD_BD+8, 0x0000,
13090 0x00000000, 0xffff0002 },
13091 { RCVDBDI_STD_BD+0xc, 0x0000,
13092 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013093
Michael Chana71116d2005-05-29 14:58:11 -070013094 /* Receive BD Initiator Control Registers. */
13095 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
13096 0x00000000, 0xffffffff },
13097 { RCVBDI_STD_THRESH, TG3_FL_5705,
13098 0x00000000, 0x000003ff },
13099 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
13100 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013101
Michael Chana71116d2005-05-29 14:58:11 -070013102 /* Host Coalescing Control Registers. */
13103 { HOSTCC_MODE, TG3_FL_NOT_5705,
13104 0x00000000, 0x00000004 },
13105 { HOSTCC_MODE, TG3_FL_5705,
13106 0x00000000, 0x000000f6 },
13107 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
13108 0x00000000, 0xffffffff },
13109 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
13110 0x00000000, 0x000003ff },
13111 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
13112 0x00000000, 0xffffffff },
13113 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
13114 0x00000000, 0x000003ff },
13115 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
13116 0x00000000, 0xffffffff },
13117 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13118 0x00000000, 0x000000ff },
13119 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
13120 0x00000000, 0xffffffff },
13121 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13122 0x00000000, 0x000000ff },
13123 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
13124 0x00000000, 0xffffffff },
13125 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
13126 0x00000000, 0xffffffff },
13127 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13128 0x00000000, 0xffffffff },
13129 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13130 0x00000000, 0x000000ff },
13131 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13132 0x00000000, 0xffffffff },
13133 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13134 0x00000000, 0x000000ff },
13135 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
13136 0x00000000, 0xffffffff },
13137 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
13138 0x00000000, 0xffffffff },
13139 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
13140 0x00000000, 0xffffffff },
13141 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
13142 0x00000000, 0xffffffff },
13143 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
13144 0x00000000, 0xffffffff },
13145 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
13146 0xffffffff, 0x00000000 },
13147 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
13148 0xffffffff, 0x00000000 },
13149
13150 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070013151 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013152 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070013153 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013154 0x00000000, 0x007fffff },
13155 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
13156 0x00000000, 0x0000003f },
13157 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
13158 0x00000000, 0x000001ff },
13159 { BUFMGR_MB_HIGH_WATER, 0x0000,
13160 0x00000000, 0x000001ff },
13161 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
13162 0xffffffff, 0x00000000 },
13163 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
13164 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013165
Michael Chana71116d2005-05-29 14:58:11 -070013166 /* Mailbox Registers */
13167 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
13168 0x00000000, 0x000001ff },
13169 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
13170 0x00000000, 0x000001ff },
13171 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
13172 0x00000000, 0x000007ff },
13173 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
13174 0x00000000, 0x000001ff },
13175
13176 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13177 };
13178
Michael Chanb16250e2006-09-27 16:10:14 -070013179 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013180 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013181 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013182 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013183 is_5750 = 1;
13184 }
Michael Chana71116d2005-05-29 14:58:11 -070013185
13186 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13187 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13188 continue;
13189
13190 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13191 continue;
13192
Joe Perches63c3a662011-04-26 08:12:10 +000013193 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013194 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13195 continue;
13196
Michael Chanb16250e2006-09-27 16:10:14 -070013197 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13198 continue;
13199
Michael Chana71116d2005-05-29 14:58:11 -070013200 offset = (u32) reg_tbl[i].offset;
13201 read_mask = reg_tbl[i].read_mask;
13202 write_mask = reg_tbl[i].write_mask;
13203
13204 /* Save the original register content */
13205 save_val = tr32(offset);
13206
13207 /* Determine the read-only value. */
13208 read_val = save_val & read_mask;
13209
13210 /* Write zero to the register, then make sure the read-only bits
13211 * are not changed and the read/write bits are all zeros.
13212 */
13213 tw32(offset, 0);
13214
13215 val = tr32(offset);
13216
13217 /* Test the read-only and read/write bits. */
13218 if (((val & read_mask) != read_val) || (val & write_mask))
13219 goto out;
13220
13221 /* Write ones to all the bits defined by RdMask and WrMask, then
13222 * make sure the read-only bits are not changed and the
13223 * read/write bits are all ones.
13224 */
13225 tw32(offset, read_mask | write_mask);
13226
13227 val = tr32(offset);
13228
13229 /* Test the read-only bits. */
13230 if ((val & read_mask) != read_val)
13231 goto out;
13232
13233 /* Test the read/write bits. */
13234 if ((val & write_mask) != write_mask)
13235 goto out;
13236
13237 tw32(offset, save_val);
13238 }
13239
13240 return 0;
13241
13242out:
Michael Chan9f88f292006-12-07 00:22:54 -080013243 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013244 netdev_err(tp->dev,
13245 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013246 tw32(offset, save_val);
13247 return -EIO;
13248}
13249
Michael Chan7942e1d2005-05-29 14:58:36 -070013250static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13251{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013252 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013253 int i;
13254 u32 j;
13255
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013256 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013257 for (j = 0; j < len; j += 4) {
13258 u32 val;
13259
13260 tg3_write_mem(tp, offset + j, test_pattern[i]);
13261 tg3_read_mem(tp, offset + j, &val);
13262 if (val != test_pattern[i])
13263 return -EIO;
13264 }
13265 }
13266 return 0;
13267}
13268
13269static int tg3_test_memory(struct tg3 *tp)
13270{
13271 static struct mem_entry {
13272 u32 offset;
13273 u32 len;
13274 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013275 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013276 { 0x00002000, 0x1c000},
13277 { 0xffffffff, 0x00000}
13278 }, mem_tbl_5705[] = {
13279 { 0x00000100, 0x0000c},
13280 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013281 { 0x00004000, 0x00800},
13282 { 0x00006000, 0x01000},
13283 { 0x00008000, 0x02000},
13284 { 0x00010000, 0x0e000},
13285 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013286 }, mem_tbl_5755[] = {
13287 { 0x00000200, 0x00008},
13288 { 0x00004000, 0x00800},
13289 { 0x00006000, 0x00800},
13290 { 0x00008000, 0x02000},
13291 { 0x00010000, 0x0c000},
13292 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013293 }, mem_tbl_5906[] = {
13294 { 0x00000200, 0x00008},
13295 { 0x00004000, 0x00400},
13296 { 0x00006000, 0x00400},
13297 { 0x00008000, 0x01000},
13298 { 0x00010000, 0x01000},
13299 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013300 }, mem_tbl_5717[] = {
13301 { 0x00000200, 0x00008},
13302 { 0x00010000, 0x0a000},
13303 { 0x00020000, 0x13c00},
13304 { 0xffffffff, 0x00000}
13305 }, mem_tbl_57765[] = {
13306 { 0x00000200, 0x00008},
13307 { 0x00004000, 0x00800},
13308 { 0x00006000, 0x09800},
13309 { 0x00010000, 0x0a000},
13310 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013311 };
13312 struct mem_entry *mem_tbl;
13313 int err = 0;
13314 int i;
13315
Joe Perches63c3a662011-04-26 08:12:10 +000013316 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013317 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013318 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013319 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013320 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013321 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013322 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013323 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013324 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013325 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013326 mem_tbl = mem_tbl_5705;
13327 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013328 mem_tbl = mem_tbl_570x;
13329
13330 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013331 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13332 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013333 break;
13334 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013335
Michael Chan7942e1d2005-05-29 14:58:36 -070013336 return err;
13337}
13338
Matt Carlsonbb158d62011-04-25 12:42:47 +000013339#define TG3_TSO_MSS 500
13340
13341#define TG3_TSO_IP_HDR_LEN 20
13342#define TG3_TSO_TCP_HDR_LEN 20
13343#define TG3_TSO_TCP_OPT_LEN 12
13344
13345static const u8 tg3_tso_header[] = {
133460x08, 0x00,
133470x45, 0x00, 0x00, 0x00,
133480x00, 0x00, 0x40, 0x00,
133490x40, 0x06, 0x00, 0x00,
133500x0a, 0x00, 0x00, 0x01,
133510x0a, 0x00, 0x00, 0x02,
133520x0d, 0x00, 0xe0, 0x00,
133530x00, 0x00, 0x01, 0x00,
133540x00, 0x00, 0x02, 0x00,
133550x80, 0x10, 0x10, 0x00,
133560x14, 0x09, 0x00, 0x00,
133570x01, 0x01, 0x08, 0x0a,
133580x11, 0x11, 0x11, 0x11,
133590x11, 0x11, 0x11, 0x11,
13360};
Michael Chan9f40dea2005-09-05 17:53:06 -070013361
Matt Carlson28a45952011-08-19 13:58:22 +000013362static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013363{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013364 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013365 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013366 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013367 struct sk_buff *skb;
13368 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013369 dma_addr_t map;
13370 int num_pkts, tx_len, rx_len, i, err;
13371 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013372 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013373 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013374
Matt Carlsonc8873402010-02-12 14:47:11 +000013375 tnapi = &tp->napi[0];
13376 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013377 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013378 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013379 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013380 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013381 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013382 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013383 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013384
Michael Chanc76949a2005-05-29 14:58:59 -070013385 err = -EIO;
13386
Matt Carlson4852a862011-04-13 11:05:07 +000013387 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013388 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013389 if (!skb)
13390 return -ENOMEM;
13391
Michael Chanc76949a2005-05-29 14:58:59 -070013392 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013393 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13394 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013395
Matt Carlson4852a862011-04-13 11:05:07 +000013396 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013397
Matt Carlson28a45952011-08-19 13:58:22 +000013398 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013399 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13400
13401 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13402 TG3_TSO_TCP_OPT_LEN;
13403
13404 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13405 sizeof(tg3_tso_header));
13406 mss = TG3_TSO_MSS;
13407
13408 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13409 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13410
13411 /* Set the total length field in the IP header */
13412 iph->tot_len = htons((u16)(mss + hdr_len));
13413
13414 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13415 TXD_FLAG_CPU_POST_DMA);
13416
Joe Perches63c3a662011-04-26 08:12:10 +000013417 if (tg3_flag(tp, HW_TSO_1) ||
13418 tg3_flag(tp, HW_TSO_2) ||
13419 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013420 struct tcphdr *th;
13421 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13422 th = (struct tcphdr *)&tx_data[val];
13423 th->check = 0;
13424 } else
13425 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13426
Joe Perches63c3a662011-04-26 08:12:10 +000013427 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013428 mss |= (hdr_len & 0xc) << 12;
13429 if (hdr_len & 0x10)
13430 base_flags |= 0x00000010;
13431 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013432 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013433 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013434 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013435 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013436 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13437 } else {
13438 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13439 }
13440
13441 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13442 } else {
13443 num_pkts = 1;
13444 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013445
13446 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13447 tx_len > VLAN_ETH_FRAME_LEN)
13448 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013449 }
13450
13451 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013452 tx_data[i] = (u8) (i & 0xff);
13453
Alexander Duyckf4188d82009-12-02 16:48:38 +000013454 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13455 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013456 dev_kfree_skb(skb);
13457 return -EIO;
13458 }
Michael Chanc76949a2005-05-29 14:58:59 -070013459
Matt Carlson0d681b22011-07-27 14:20:49 +000013460 val = tnapi->tx_prod;
13461 tnapi->tx_buffers[val].skb = skb;
13462 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13463
Michael Chanc76949a2005-05-29 14:58:59 -070013464 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013465 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013466
13467 udelay(10);
13468
Matt Carlson898a56f2009-08-28 14:02:40 +000013469 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013470
Matt Carlson84b67b22011-07-27 14:20:52 +000013471 budget = tg3_tx_avail(tnapi);
13472 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013473 base_flags | TXD_FLAG_END, mss, 0)) {
13474 tnapi->tx_buffers[val].skb = NULL;
13475 dev_kfree_skb(skb);
13476 return -EIO;
13477 }
Michael Chanc76949a2005-05-29 14:58:59 -070013478
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013479 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013480
Michael Chan6541b802012-03-04 14:48:14 +000013481 /* Sync BD data before updating mailbox */
13482 wmb();
13483
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013484 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13485 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013486
13487 udelay(10);
13488
Matt Carlson303fc922009-11-02 14:27:34 +000013489 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13490 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013491 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013492 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013493
13494 udelay(10);
13495
Matt Carlson898a56f2009-08-28 14:02:40 +000013496 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13497 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013498 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013499 (rx_idx == (rx_start_idx + num_pkts)))
13500 break;
13501 }
13502
Matt Carlsonba1142e2011-11-04 09:15:00 +000013503 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013504 dev_kfree_skb(skb);
13505
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013506 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013507 goto out;
13508
13509 if (rx_idx != rx_start_idx + num_pkts)
13510 goto out;
13511
Matt Carlsonbb158d62011-04-25 12:42:47 +000013512 val = data_off;
13513 while (rx_idx != rx_start_idx) {
13514 desc = &rnapi->rx_rcb[rx_start_idx++];
13515 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13516 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013517
Matt Carlsonbb158d62011-04-25 12:42:47 +000013518 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13519 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013520 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013521
Matt Carlsonbb158d62011-04-25 12:42:47 +000013522 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13523 - ETH_FCS_LEN;
13524
Matt Carlson28a45952011-08-19 13:58:22 +000013525 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013526 if (rx_len != tx_len)
13527 goto out;
13528
13529 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13530 if (opaque_key != RXD_OPAQUE_RING_STD)
13531 goto out;
13532 } else {
13533 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13534 goto out;
13535 }
13536 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13537 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013538 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013539 goto out;
13540 }
13541
13542 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013543 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013544 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13545 mapping);
13546 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013547 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013548 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13549 mapping);
13550 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013551 goto out;
13552
Matt Carlsonbb158d62011-04-25 12:42:47 +000013553 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13554 PCI_DMA_FROMDEVICE);
13555
Eric Dumazet9205fd92011-11-18 06:47:01 +000013556 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013557 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013558 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013559 goto out;
13560 }
Matt Carlson4852a862011-04-13 11:05:07 +000013561 }
13562
Michael Chanc76949a2005-05-29 14:58:59 -070013563 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013564
Eric Dumazet9205fd92011-11-18 06:47:01 +000013565 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013566out:
13567 return err;
13568}
13569
Matt Carlson00c266b2011-04-25 12:42:46 +000013570#define TG3_STD_LOOPBACK_FAILED 1
13571#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013572#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013573#define TG3_LOOPBACK_FAILED \
13574 (TG3_STD_LOOPBACK_FAILED | \
13575 TG3_JMB_LOOPBACK_FAILED | \
13576 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013577
Matt Carlson941ec902011-08-19 13:58:23 +000013578static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013579{
Matt Carlson28a45952011-08-19 13:58:22 +000013580 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013581 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013582 u32 jmb_pkt_sz = 9000;
13583
13584 if (tp->dma_limit)
13585 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013586
Matt Carlsonab789042011-01-25 15:58:54 +000013587 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13588 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13589
Matt Carlson28a45952011-08-19 13:58:22 +000013590 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013591 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13592 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013593 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013594 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013595 goto done;
13596 }
13597
Joe Perches953c96e2013-04-09 10:18:14 +000013598 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013599 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013600 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13601 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013602 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013603 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013604 goto done;
13605 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013606
Joe Perches63c3a662011-04-26 08:12:10 +000013607 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013608 int i;
13609
13610 /* Reroute all rx packets to the 1st queue */
13611 for (i = MAC_RSS_INDIR_TBL_0;
13612 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13613 tw32(i, 0x0);
13614 }
13615
Matt Carlson6e01b202011-08-19 13:58:20 +000013616 /* HW errata - mac loopback fails in some cases on 5780.
13617 * Normal traffic and PHY loopback are not affected by
13618 * errata. Also, the MAC loopback test is deprecated for
13619 * all newer ASIC revisions.
13620 */
Joe Perches41535772013-02-16 11:20:04 +000013621 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013622 !tg3_flag(tp, CPMU_PRESENT)) {
13623 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013624
Matt Carlson28a45952011-08-19 13:58:22 +000013625 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013626 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013627
13628 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_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013631
13632 tg3_mac_loopback(tp, false);
13633 }
Matt Carlson4852a862011-04-13 11:05:07 +000013634
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013635 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013636 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013637 int i;
13638
Matt Carlson941ec902011-08-19 13:58:23 +000013639 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013640
13641 /* Wait for link */
13642 for (i = 0; i < 100; i++) {
13643 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13644 break;
13645 mdelay(1);
13646 }
13647
Matt Carlson28a45952011-08-19 13:58:22 +000013648 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013649 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013650 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013651 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013652 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013653 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013654 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013655 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013656
Matt Carlson941ec902011-08-19 13:58:23 +000013657 if (do_extlpbk) {
13658 tg3_phy_lpbk_set(tp, 0, true);
13659
13660 /* All link indications report up, but the hardware
13661 * isn't really ready for about 20 msec. Double it
13662 * to be sure.
13663 */
13664 mdelay(40);
13665
13666 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013667 data[TG3_EXT_LOOPB_TEST] |=
13668 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013669 if (tg3_flag(tp, TSO_CAPABLE) &&
13670 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013671 data[TG3_EXT_LOOPB_TEST] |=
13672 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013673 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013674 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013675 data[TG3_EXT_LOOPB_TEST] |=
13676 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013677 }
13678
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013679 /* Re-enable gphy autopowerdown. */
13680 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13681 tg3_phy_toggle_apd(tp, true);
13682 }
Matt Carlson6833c042008-11-21 17:18:59 -080013683
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013684 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13685 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013686
Matt Carlsonab789042011-01-25 15:58:54 +000013687done:
13688 tp->phy_flags |= eee_cap;
13689
Michael Chan9f40dea2005-09-05 17:53:06 -070013690 return err;
13691}
13692
Michael Chan4cafd3f2005-05-29 14:56:34 -070013693static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13694 u64 *data)
13695{
Michael Chan566f86a2005-05-29 14:56:58 -070013696 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013697 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013698
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013699 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13700 if (tg3_power_up(tp)) {
13701 etest->flags |= ETH_TEST_FL_FAILED;
13702 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13703 return;
13704 }
13705 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013706 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013707
Michael Chan566f86a2005-05-29 14:56:58 -070013708 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13709
13710 if (tg3_test_nvram(tp) != 0) {
13711 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013712 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013713 }
Matt Carlson941ec902011-08-19 13:58:23 +000013714 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013715 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013716 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013717 }
Michael Chana71116d2005-05-29 14:58:11 -070013718 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013719 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013720
Michael Chanbbe832c2005-06-24 20:20:04 -070013721 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013722 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013723 tg3_netif_stop(tp);
13724 irq_sync = 1;
13725 }
13726
13727 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013728 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013729 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013730 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013731 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013732 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013733 if (!err)
13734 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013735
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013736 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080013737 tg3_phy_reset(tp);
13738
Michael Chana71116d2005-05-29 14:58:11 -070013739 if (tg3_test_registers(tp) != 0) {
13740 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013741 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013742 }
Matt Carlson28a45952011-08-19 13:58:22 +000013743
Michael Chan7942e1d2005-05-29 14:58:36 -070013744 if (tg3_test_memory(tp) != 0) {
13745 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013746 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013747 }
Matt Carlson28a45952011-08-19 13:58:22 +000013748
Matt Carlson941ec902011-08-19 13:58:23 +000013749 if (doextlpbk)
13750 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13751
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013752 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013753 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013754
David S. Millerf47c11e2005-06-24 20:18:35 -070013755 tg3_full_unlock(tp);
13756
Michael Chand4bc3922005-05-29 14:59:20 -070013757 if (tg3_test_interrupt(tp) != 0) {
13758 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013759 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013760 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013761
13762 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013763
Michael Chana71116d2005-05-29 14:58:11 -070013764 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13765 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013766 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013767 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013768 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013769 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013770 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013771
13772 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013773
13774 if (irq_sync && !err2)
13775 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013776 }
Matt Carlson800960682010-08-02 11:26:06 +000013777 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013778 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013779
Michael Chan4cafd3f2005-05-29 14:56:34 -070013780}
13781
Ben Hutchings72608992013-11-18 22:59:43 +000013782static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013783{
13784 struct tg3 *tp = netdev_priv(dev);
13785 struct hwtstamp_config stmpconf;
13786
13787 if (!tg3_flag(tp, PTP_CAPABLE))
Ben Hutchings72608992013-11-18 22:59:43 +000013788 return -EOPNOTSUPP;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013789
13790 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13791 return -EFAULT;
13792
13793 if (stmpconf.flags)
13794 return -EINVAL;
13795
Ben Hutchings58b187c2013-11-14 00:40:56 +000013796 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13797 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013798 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013799
13800 switch (stmpconf.rx_filter) {
13801 case HWTSTAMP_FILTER_NONE:
13802 tp->rxptpctl = 0;
13803 break;
13804 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13805 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13806 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13807 break;
13808 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13809 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13810 TG3_RX_PTP_CTL_SYNC_EVNT;
13811 break;
13812 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13813 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13814 TG3_RX_PTP_CTL_DELAY_REQ;
13815 break;
13816 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13817 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13818 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13819 break;
13820 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13821 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13822 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13823 break;
13824 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13825 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13826 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13827 break;
13828 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13829 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13830 TG3_RX_PTP_CTL_SYNC_EVNT;
13831 break;
13832 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13833 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13834 TG3_RX_PTP_CTL_SYNC_EVNT;
13835 break;
13836 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13837 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13838 TG3_RX_PTP_CTL_SYNC_EVNT;
13839 break;
13840 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13841 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13842 TG3_RX_PTP_CTL_DELAY_REQ;
13843 break;
13844 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13845 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13846 TG3_RX_PTP_CTL_DELAY_REQ;
13847 break;
13848 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13849 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13850 TG3_RX_PTP_CTL_DELAY_REQ;
13851 break;
13852 default:
13853 return -ERANGE;
13854 }
13855
13856 if (netif_running(dev) && tp->rxptpctl)
13857 tw32(TG3_RX_PTP_CTL,
13858 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13859
Ben Hutchings58b187c2013-11-14 00:40:56 +000013860 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13861 tg3_flag_set(tp, TX_TSTAMP_EN);
13862 else
13863 tg3_flag_clear(tp, TX_TSTAMP_EN);
13864
Matt Carlson0a633ac2012-12-03 19:36:59 +000013865 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13866 -EFAULT : 0;
13867}
13868
Ben Hutchings72608992013-11-18 22:59:43 +000013869static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
13870{
13871 struct tg3 *tp = netdev_priv(dev);
13872 struct hwtstamp_config stmpconf;
13873
13874 if (!tg3_flag(tp, PTP_CAPABLE))
13875 return -EOPNOTSUPP;
13876
13877 stmpconf.flags = 0;
13878 stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
13879 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
13880
13881 switch (tp->rxptpctl) {
13882 case 0:
13883 stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
13884 break;
13885 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
13886 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
13887 break;
13888 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13889 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
13890 break;
13891 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13892 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
13893 break;
13894 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13895 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13896 break;
13897 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13898 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
13899 break;
13900 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13901 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
13902 break;
13903 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13904 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
13905 break;
13906 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13907 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
13908 break;
13909 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13910 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
13911 break;
13912 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13913 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
13914 break;
13915 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13916 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
13917 break;
13918 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13919 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
13920 break;
13921 default:
13922 WARN_ON_ONCE(1);
13923 return -ERANGE;
13924 }
13925
13926 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13927 -EFAULT : 0;
13928}
13929
Linus Torvalds1da177e2005-04-16 15:20:36 -070013930static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13931{
13932 struct mii_ioctl_data *data = if_mii(ifr);
13933 struct tg3 *tp = netdev_priv(dev);
13934 int err;
13935
Joe Perches63c3a662011-04-26 08:12:10 +000013936 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013937 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013938 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013939 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010013940 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Richard Cochran28b04112010-07-17 08:48:55 +000013941 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013942 }
13943
Matt Carlson33f401a2010-04-05 10:19:27 +000013944 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013945 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013946 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013947
13948 /* fallthru */
13949 case SIOCGMIIREG: {
13950 u32 mii_regval;
13951
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013952 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013953 break; /* We have no PHY */
13954
Matt Carlson34eea5a2011-04-20 07:57:38 +000013955 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013956 return -EAGAIN;
13957
David S. Millerf47c11e2005-06-24 20:18:35 -070013958 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013959 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13960 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013961 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013962
13963 data->val_out = mii_regval;
13964
13965 return err;
13966 }
13967
13968 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013969 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013970 break; /* We have no PHY */
13971
Matt Carlson34eea5a2011-04-20 07:57:38 +000013972 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013973 return -EAGAIN;
13974
David S. Millerf47c11e2005-06-24 20:18:35 -070013975 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013976 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13977 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013978 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013979
13980 return err;
13981
Matt Carlson0a633ac2012-12-03 19:36:59 +000013982 case SIOCSHWTSTAMP:
Ben Hutchings72608992013-11-18 22:59:43 +000013983 return tg3_hwtstamp_set(dev, ifr);
13984
13985 case SIOCGHWTSTAMP:
13986 return tg3_hwtstamp_get(dev, ifr);
Matt Carlson0a633ac2012-12-03 19:36:59 +000013987
Linus Torvalds1da177e2005-04-16 15:20:36 -070013988 default:
13989 /* do nothing */
13990 break;
13991 }
13992 return -EOPNOTSUPP;
13993}
13994
David S. Miller15f98502005-05-18 22:49:26 -070013995static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13996{
13997 struct tg3 *tp = netdev_priv(dev);
13998
13999 memcpy(ec, &tp->coal, sizeof(*ec));
14000 return 0;
14001}
14002
Michael Chand244c892005-07-05 14:42:33 -070014003static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
14004{
14005 struct tg3 *tp = netdev_priv(dev);
14006 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
14007 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
14008
Joe Perches63c3a662011-04-26 08:12:10 +000014009 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014010 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
14011 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
14012 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
14013 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
14014 }
14015
14016 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
14017 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
14018 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
14019 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
14020 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
14021 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
14022 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
14023 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
14024 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
14025 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
14026 return -EINVAL;
14027
14028 /* No rx interrupts will be generated if both are zero */
14029 if ((ec->rx_coalesce_usecs == 0) &&
14030 (ec->rx_max_coalesced_frames == 0))
14031 return -EINVAL;
14032
14033 /* No tx interrupts will be generated if both are zero */
14034 if ((ec->tx_coalesce_usecs == 0) &&
14035 (ec->tx_max_coalesced_frames == 0))
14036 return -EINVAL;
14037
14038 /* Only copy relevant parameters, ignore all others. */
14039 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
14040 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
14041 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
14042 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
14043 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
14044 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
14045 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
14046 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
14047 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
14048
14049 if (netif_running(dev)) {
14050 tg3_full_lock(tp, 0);
14051 __tg3_set_coalesce(tp, &tp->coal);
14052 tg3_full_unlock(tp);
14053 }
14054 return 0;
14055}
14056
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014057static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
14058{
14059 struct tg3 *tp = netdev_priv(dev);
14060
14061 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14062 netdev_warn(tp->dev, "Board does not support EEE!\n");
14063 return -EOPNOTSUPP;
14064 }
14065
14066 if (edata->advertised != tp->eee.advertised) {
14067 netdev_warn(tp->dev,
14068 "Direct manipulation of EEE advertisement is not supported\n");
14069 return -EINVAL;
14070 }
14071
14072 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
14073 netdev_warn(tp->dev,
14074 "Maximal Tx Lpi timer supported is %#x(u)\n",
14075 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
14076 return -EINVAL;
14077 }
14078
14079 tp->eee = *edata;
14080
14081 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
14082 tg3_warn_mgmt_link_flap(tp);
14083
14084 if (netif_running(tp->dev)) {
14085 tg3_full_lock(tp, 0);
14086 tg3_setup_eee(tp);
14087 tg3_phy_reset(tp);
14088 tg3_full_unlock(tp);
14089 }
14090
14091 return 0;
14092}
14093
14094static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
14095{
14096 struct tg3 *tp = netdev_priv(dev);
14097
14098 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14099 netdev_warn(tp->dev,
14100 "Board does not support EEE!\n");
14101 return -EOPNOTSUPP;
14102 }
14103
14104 *edata = tp->eee;
14105 return 0;
14106}
14107
Jeff Garzik7282d492006-09-13 14:30:00 -040014108static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014109 .get_settings = tg3_get_settings,
14110 .set_settings = tg3_set_settings,
14111 .get_drvinfo = tg3_get_drvinfo,
14112 .get_regs_len = tg3_get_regs_len,
14113 .get_regs = tg3_get_regs,
14114 .get_wol = tg3_get_wol,
14115 .set_wol = tg3_set_wol,
14116 .get_msglevel = tg3_get_msglevel,
14117 .set_msglevel = tg3_set_msglevel,
14118 .nway_reset = tg3_nway_reset,
14119 .get_link = ethtool_op_get_link,
14120 .get_eeprom_len = tg3_get_eeprom_len,
14121 .get_eeprom = tg3_get_eeprom,
14122 .set_eeprom = tg3_set_eeprom,
14123 .get_ringparam = tg3_get_ringparam,
14124 .set_ringparam = tg3_set_ringparam,
14125 .get_pauseparam = tg3_get_pauseparam,
14126 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070014127 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014128 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000014129 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014130 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070014131 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070014132 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070014133 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000014134 .get_rxnfc = tg3_get_rxnfc,
14135 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
Ben Hutchingsfe62d002014-05-15 01:25:27 +010014136 .get_rxfh = tg3_get_rxfh,
14137 .set_rxfh = tg3_set_rxfh,
Michael Chan09681692012-09-28 07:12:42 +000014138 .get_channels = tg3_get_channels,
14139 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000014140 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014141 .get_eee = tg3_get_eee,
14142 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014143};
14144
David S. Millerb4017c52012-03-01 17:57:40 -050014145static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
14146 struct rtnl_link_stats64 *stats)
14147{
14148 struct tg3 *tp = netdev_priv(dev);
14149
David S. Millerb4017c52012-03-01 17:57:40 -050014150 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000014151 if (!tp->hw_stats) {
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014152 *stats = tp->net_stats_prev;
Michael Chan0f566b22012-07-29 19:15:44 +000014153 spin_unlock_bh(&tp->lock);
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014154 return stats;
Michael Chan0f566b22012-07-29 19:15:44 +000014155 }
14156
David S. Millerb4017c52012-03-01 17:57:40 -050014157 tg3_get_nstats(tp, stats);
14158 spin_unlock_bh(&tp->lock);
14159
14160 return stats;
14161}
14162
Matt Carlsonccd5ba92012-02-13 10:20:08 +000014163static void tg3_set_rx_mode(struct net_device *dev)
14164{
14165 struct tg3 *tp = netdev_priv(dev);
14166
14167 if (!netif_running(dev))
14168 return;
14169
14170 tg3_full_lock(tp, 0);
14171 __tg3_set_rx_mode(dev);
14172 tg3_full_unlock(tp);
14173}
14174
Matt Carlsonfaf16272012-02-13 10:20:07 +000014175static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
14176 int new_mtu)
14177{
14178 dev->mtu = new_mtu;
14179
14180 if (new_mtu > ETH_DATA_LEN) {
14181 if (tg3_flag(tp, 5780_CLASS)) {
14182 netdev_update_features(dev);
14183 tg3_flag_clear(tp, TSO_CAPABLE);
14184 } else {
14185 tg3_flag_set(tp, JUMBO_RING_ENABLE);
14186 }
14187 } else {
14188 if (tg3_flag(tp, 5780_CLASS)) {
14189 tg3_flag_set(tp, TSO_CAPABLE);
14190 netdev_update_features(dev);
14191 }
14192 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
14193 }
14194}
14195
14196static int tg3_change_mtu(struct net_device *dev, int new_mtu)
14197{
14198 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000014199 int err;
14200 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000014201
14202 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
14203 return -EINVAL;
14204
14205 if (!netif_running(dev)) {
14206 /* We'll just catch it later when the
14207 * device is up'd.
14208 */
14209 tg3_set_mtu(dev, tp, new_mtu);
14210 return 0;
14211 }
14212
14213 tg3_phy_stop(tp);
14214
14215 tg3_netif_stop(tp);
14216
Nithin Sujirc6993df2014-02-06 14:13:05 -080014217 tg3_set_mtu(dev, tp, new_mtu);
14218
Matt Carlsonfaf16272012-02-13 10:20:07 +000014219 tg3_full_lock(tp, 1);
14220
14221 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14222
Michael Chan2fae5e32012-03-04 14:48:15 +000014223 /* Reset PHY, otherwise the read DMA engine will be in a mode that
14224 * breaks all requests to 256 bytes.
14225 */
Joe Perches41535772013-02-16 11:20:04 +000014226 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000014227 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000014228
14229 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000014230
14231 if (!err)
14232 tg3_netif_start(tp);
14233
14234 tg3_full_unlock(tp);
14235
14236 if (!err)
14237 tg3_phy_start(tp);
14238
14239 return err;
14240}
14241
14242static const struct net_device_ops tg3_netdev_ops = {
14243 .ndo_open = tg3_open,
14244 .ndo_stop = tg3_close,
14245 .ndo_start_xmit = tg3_start_xmit,
14246 .ndo_get_stats64 = tg3_get_stats64,
14247 .ndo_validate_addr = eth_validate_addr,
14248 .ndo_set_rx_mode = tg3_set_rx_mode,
14249 .ndo_set_mac_address = tg3_set_mac_addr,
14250 .ndo_do_ioctl = tg3_ioctl,
14251 .ndo_tx_timeout = tg3_tx_timeout,
14252 .ndo_change_mtu = tg3_change_mtu,
14253 .ndo_fix_features = tg3_fix_features,
14254 .ndo_set_features = tg3_set_features,
14255#ifdef CONFIG_NET_POLL_CONTROLLER
14256 .ndo_poll_controller = tg3_poll_controller,
14257#endif
14258};
14259
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014260static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014261{
Michael Chan1b277772006-03-20 22:27:48 -080014262 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014263
14264 tp->nvram_size = EEPROM_CHIP_SIZE;
14265
Matt Carlsone4f34112009-02-25 14:25:00 +000014266 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267 return;
14268
Michael Chanb16250e2006-09-27 16:10:14 -070014269 if ((magic != TG3_EEPROM_MAGIC) &&
14270 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14271 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014272 return;
14273
14274 /*
14275 * Size the chip by reading offsets at increasing powers of two.
14276 * When we encounter our validation signature, we know the addressing
14277 * has wrapped around, and thus have our chip size.
14278 */
Michael Chan1b277772006-03-20 22:27:48 -080014279 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014280
14281 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014282 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014283 return;
14284
Michael Chan18201802006-03-20 22:29:15 -080014285 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014286 break;
14287
14288 cursize <<= 1;
14289 }
14290
14291 tp->nvram_size = cursize;
14292}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014293
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014294static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014295{
14296 u32 val;
14297
Joe Perches63c3a662011-04-26 08:12:10 +000014298 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014299 return;
14300
14301 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014302 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014303 tg3_get_eeprom_size(tp);
14304 return;
14305 }
14306
Matt Carlson6d348f22009-02-25 14:25:52 +000014307 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014308 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014309 /* This is confusing. We want to operate on the
14310 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14311 * call will read from NVRAM and byteswap the data
14312 * according to the byteswapping settings for all
14313 * other register accesses. This ensures the data we
14314 * want will always reside in the lower 16-bits.
14315 * However, the data in NVRAM is in LE format, which
14316 * means the data from the NVRAM read will always be
14317 * opposite the endianness of the CPU. The 16-bit
14318 * byteswap then brings the data to CPU endianness.
14319 */
14320 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014321 return;
14322 }
14323 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014324 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014325}
14326
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014327static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014328{
14329 u32 nvcfg1;
14330
14331 nvcfg1 = tr32(NVRAM_CFG1);
14332 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014333 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014334 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014335 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14336 tw32(NVRAM_CFG1, nvcfg1);
14337 }
14338
Joe Perches41535772013-02-16 11:20:04 +000014339 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014340 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014342 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14343 tp->nvram_jedecnum = JEDEC_ATMEL;
14344 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014345 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014346 break;
14347 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14348 tp->nvram_jedecnum = JEDEC_ATMEL;
14349 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14350 break;
14351 case FLASH_VENDOR_ATMEL_EEPROM:
14352 tp->nvram_jedecnum = JEDEC_ATMEL;
14353 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014354 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014355 break;
14356 case FLASH_VENDOR_ST:
14357 tp->nvram_jedecnum = JEDEC_ST;
14358 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014359 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014360 break;
14361 case FLASH_VENDOR_SAIFUN:
14362 tp->nvram_jedecnum = JEDEC_SAIFUN;
14363 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14364 break;
14365 case FLASH_VENDOR_SST_SMALL:
14366 case FLASH_VENDOR_SST_LARGE:
14367 tp->nvram_jedecnum = JEDEC_SST;
14368 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 }
Matt Carlson8590a602009-08-28 12:29:16 +000014371 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014372 tp->nvram_jedecnum = JEDEC_ATMEL;
14373 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014374 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014375 }
14376}
14377
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014378static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014379{
14380 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14381 case FLASH_5752PAGE_SIZE_256:
14382 tp->nvram_pagesize = 256;
14383 break;
14384 case FLASH_5752PAGE_SIZE_512:
14385 tp->nvram_pagesize = 512;
14386 break;
14387 case FLASH_5752PAGE_SIZE_1K:
14388 tp->nvram_pagesize = 1024;
14389 break;
14390 case FLASH_5752PAGE_SIZE_2K:
14391 tp->nvram_pagesize = 2048;
14392 break;
14393 case FLASH_5752PAGE_SIZE_4K:
14394 tp->nvram_pagesize = 4096;
14395 break;
14396 case FLASH_5752PAGE_SIZE_264:
14397 tp->nvram_pagesize = 264;
14398 break;
14399 case FLASH_5752PAGE_SIZE_528:
14400 tp->nvram_pagesize = 528;
14401 break;
14402 }
14403}
14404
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014405static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014406{
14407 u32 nvcfg1;
14408
14409 nvcfg1 = tr32(NVRAM_CFG1);
14410
Michael Chane6af3012005-04-21 17:12:05 -070014411 /* NVRAM protection for TPM */
14412 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014413 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014414
Michael Chan361b4ac2005-04-21 17:11:21 -070014415 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014416 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14417 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14418 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014419 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014420 break;
14421 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14422 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014423 tg3_flag_set(tp, NVRAM_BUFFERED);
14424 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014425 break;
14426 case FLASH_5752VENDOR_ST_M45PE10:
14427 case FLASH_5752VENDOR_ST_M45PE20:
14428 case FLASH_5752VENDOR_ST_M45PE40:
14429 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014430 tg3_flag_set(tp, NVRAM_BUFFERED);
14431 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014432 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014433 }
14434
Joe Perches63c3a662011-04-26 08:12:10 +000014435 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014436 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014437 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014438 /* For eeprom, set pagesize to maximum eeprom size */
14439 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14440
14441 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14442 tw32(NVRAM_CFG1, nvcfg1);
14443 }
14444}
14445
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014446static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014447{
Matt Carlson989a9d22007-05-05 11:51:05 -070014448 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014449
14450 nvcfg1 = tr32(NVRAM_CFG1);
14451
14452 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014453 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014454 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014455 protect = 1;
14456 }
Michael Chand3c7b882006-03-23 01:28:25 -080014457
Matt Carlson989a9d22007-05-05 11:51:05 -070014458 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14459 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014460 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14461 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14462 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14463 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14464 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014465 tg3_flag_set(tp, NVRAM_BUFFERED);
14466 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014467 tp->nvram_pagesize = 264;
14468 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14469 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14470 tp->nvram_size = (protect ? 0x3e200 :
14471 TG3_NVRAM_SIZE_512KB);
14472 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14473 tp->nvram_size = (protect ? 0x1f200 :
14474 TG3_NVRAM_SIZE_256KB);
14475 else
14476 tp->nvram_size = (protect ? 0x1f200 :
14477 TG3_NVRAM_SIZE_128KB);
14478 break;
14479 case FLASH_5752VENDOR_ST_M45PE10:
14480 case FLASH_5752VENDOR_ST_M45PE20:
14481 case FLASH_5752VENDOR_ST_M45PE40:
14482 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014483 tg3_flag_set(tp, NVRAM_BUFFERED);
14484 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014485 tp->nvram_pagesize = 256;
14486 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14487 tp->nvram_size = (protect ?
14488 TG3_NVRAM_SIZE_64KB :
14489 TG3_NVRAM_SIZE_128KB);
14490 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14491 tp->nvram_size = (protect ?
14492 TG3_NVRAM_SIZE_64KB :
14493 TG3_NVRAM_SIZE_256KB);
14494 else
14495 tp->nvram_size = (protect ?
14496 TG3_NVRAM_SIZE_128KB :
14497 TG3_NVRAM_SIZE_512KB);
14498 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014499 }
14500}
14501
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014502static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014503{
14504 u32 nvcfg1;
14505
14506 nvcfg1 = tr32(NVRAM_CFG1);
14507
14508 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014509 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14510 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14511 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14512 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14513 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014514 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014515 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014516
Matt Carlson8590a602009-08-28 12:29:16 +000014517 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14518 tw32(NVRAM_CFG1, nvcfg1);
14519 break;
14520 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14521 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14522 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14523 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14524 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014525 tg3_flag_set(tp, NVRAM_BUFFERED);
14526 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014527 tp->nvram_pagesize = 264;
14528 break;
14529 case FLASH_5752VENDOR_ST_M45PE10:
14530 case FLASH_5752VENDOR_ST_M45PE20:
14531 case FLASH_5752VENDOR_ST_M45PE40:
14532 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014533 tg3_flag_set(tp, NVRAM_BUFFERED);
14534 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014535 tp->nvram_pagesize = 256;
14536 break;
Michael Chan1b277772006-03-20 22:27:48 -080014537 }
14538}
14539
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014540static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014541{
14542 u32 nvcfg1, protect = 0;
14543
14544 nvcfg1 = tr32(NVRAM_CFG1);
14545
14546 /* NVRAM protection for TPM */
14547 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014548 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014549 protect = 1;
14550 }
14551
14552 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14553 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014554 case FLASH_5761VENDOR_ATMEL_ADB021D:
14555 case FLASH_5761VENDOR_ATMEL_ADB041D:
14556 case FLASH_5761VENDOR_ATMEL_ADB081D:
14557 case FLASH_5761VENDOR_ATMEL_ADB161D:
14558 case FLASH_5761VENDOR_ATMEL_MDB021D:
14559 case FLASH_5761VENDOR_ATMEL_MDB041D:
14560 case FLASH_5761VENDOR_ATMEL_MDB081D:
14561 case FLASH_5761VENDOR_ATMEL_MDB161D:
14562 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014563 tg3_flag_set(tp, NVRAM_BUFFERED);
14564 tg3_flag_set(tp, FLASH);
14565 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014566 tp->nvram_pagesize = 256;
14567 break;
14568 case FLASH_5761VENDOR_ST_A_M45PE20:
14569 case FLASH_5761VENDOR_ST_A_M45PE40:
14570 case FLASH_5761VENDOR_ST_A_M45PE80:
14571 case FLASH_5761VENDOR_ST_A_M45PE16:
14572 case FLASH_5761VENDOR_ST_M_M45PE20:
14573 case FLASH_5761VENDOR_ST_M_M45PE40:
14574 case FLASH_5761VENDOR_ST_M_M45PE80:
14575 case FLASH_5761VENDOR_ST_M_M45PE16:
14576 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014577 tg3_flag_set(tp, NVRAM_BUFFERED);
14578 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014579 tp->nvram_pagesize = 256;
14580 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014581 }
14582
14583 if (protect) {
14584 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14585 } else {
14586 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014587 case FLASH_5761VENDOR_ATMEL_ADB161D:
14588 case FLASH_5761VENDOR_ATMEL_MDB161D:
14589 case FLASH_5761VENDOR_ST_A_M45PE16:
14590 case FLASH_5761VENDOR_ST_M_M45PE16:
14591 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14592 break;
14593 case FLASH_5761VENDOR_ATMEL_ADB081D:
14594 case FLASH_5761VENDOR_ATMEL_MDB081D:
14595 case FLASH_5761VENDOR_ST_A_M45PE80:
14596 case FLASH_5761VENDOR_ST_M_M45PE80:
14597 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14598 break;
14599 case FLASH_5761VENDOR_ATMEL_ADB041D:
14600 case FLASH_5761VENDOR_ATMEL_MDB041D:
14601 case FLASH_5761VENDOR_ST_A_M45PE40:
14602 case FLASH_5761VENDOR_ST_M_M45PE40:
14603 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14604 break;
14605 case FLASH_5761VENDOR_ATMEL_ADB021D:
14606 case FLASH_5761VENDOR_ATMEL_MDB021D:
14607 case FLASH_5761VENDOR_ST_A_M45PE20:
14608 case FLASH_5761VENDOR_ST_M_M45PE20:
14609 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14610 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014611 }
14612 }
14613}
14614
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014615static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014616{
14617 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014618 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014619 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14620}
14621
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014622static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014623{
14624 u32 nvcfg1;
14625
14626 nvcfg1 = tr32(NVRAM_CFG1);
14627
14628 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14629 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14630 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14631 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014632 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014633 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14634
14635 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14636 tw32(NVRAM_CFG1, nvcfg1);
14637 return;
14638 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14639 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14640 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14641 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14642 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14643 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14644 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14645 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014646 tg3_flag_set(tp, NVRAM_BUFFERED);
14647 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014648
14649 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14650 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14651 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14652 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14653 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14654 break;
14655 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14656 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14657 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14658 break;
14659 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14660 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14661 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14662 break;
14663 }
14664 break;
14665 case FLASH_5752VENDOR_ST_M45PE10:
14666 case FLASH_5752VENDOR_ST_M45PE20:
14667 case FLASH_5752VENDOR_ST_M45PE40:
14668 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014669 tg3_flag_set(tp, NVRAM_BUFFERED);
14670 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014671
14672 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14673 case FLASH_5752VENDOR_ST_M45PE10:
14674 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14675 break;
14676 case FLASH_5752VENDOR_ST_M45PE20:
14677 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14678 break;
14679 case FLASH_5752VENDOR_ST_M45PE40:
14680 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14681 break;
14682 }
14683 break;
14684 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014685 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014686 return;
14687 }
14688
Matt Carlsona1b950d2009-09-01 13:20:17 +000014689 tg3_nvram_get_pagesize(tp, nvcfg1);
14690 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014691 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014692}
14693
14694
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014695static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014696{
14697 u32 nvcfg1;
14698
14699 nvcfg1 = tr32(NVRAM_CFG1);
14700
14701 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14702 case FLASH_5717VENDOR_ATMEL_EEPROM:
14703 case FLASH_5717VENDOR_MICRO_EEPROM:
14704 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014705 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014706 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14707
14708 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14709 tw32(NVRAM_CFG1, nvcfg1);
14710 return;
14711 case FLASH_5717VENDOR_ATMEL_MDB011D:
14712 case FLASH_5717VENDOR_ATMEL_ADB011B:
14713 case FLASH_5717VENDOR_ATMEL_ADB011D:
14714 case FLASH_5717VENDOR_ATMEL_MDB021D:
14715 case FLASH_5717VENDOR_ATMEL_ADB021B:
14716 case FLASH_5717VENDOR_ATMEL_ADB021D:
14717 case FLASH_5717VENDOR_ATMEL_45USPT:
14718 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014719 tg3_flag_set(tp, NVRAM_BUFFERED);
14720 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014721
14722 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14723 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014724 /* Detect size with tg3_nvram_get_size() */
14725 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014726 case FLASH_5717VENDOR_ATMEL_ADB021B:
14727 case FLASH_5717VENDOR_ATMEL_ADB021D:
14728 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14729 break;
14730 default:
14731 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14732 break;
14733 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014734 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014735 case FLASH_5717VENDOR_ST_M_M25PE10:
14736 case FLASH_5717VENDOR_ST_A_M25PE10:
14737 case FLASH_5717VENDOR_ST_M_M45PE10:
14738 case FLASH_5717VENDOR_ST_A_M45PE10:
14739 case FLASH_5717VENDOR_ST_M_M25PE20:
14740 case FLASH_5717VENDOR_ST_A_M25PE20:
14741 case FLASH_5717VENDOR_ST_M_M45PE20:
14742 case FLASH_5717VENDOR_ST_A_M45PE20:
14743 case FLASH_5717VENDOR_ST_25USPT:
14744 case FLASH_5717VENDOR_ST_45USPT:
14745 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014746 tg3_flag_set(tp, NVRAM_BUFFERED);
14747 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014748
14749 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14750 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014751 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014752 /* Detect size with tg3_nvram_get_size() */
14753 break;
14754 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014755 case FLASH_5717VENDOR_ST_A_M45PE20:
14756 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14757 break;
14758 default:
14759 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14760 break;
14761 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014762 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014763 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014764 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014765 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014766 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014767
14768 tg3_nvram_get_pagesize(tp, nvcfg1);
14769 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014770 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014771}
14772
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014773static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014774{
14775 u32 nvcfg1, nvmpinstrp;
14776
14777 nvcfg1 = tr32(NVRAM_CFG1);
14778 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14779
Joe Perches41535772013-02-16 11:20:04 +000014780 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014781 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14782 tg3_flag_set(tp, NO_NVRAM);
14783 return;
14784 }
14785
14786 switch (nvmpinstrp) {
14787 case FLASH_5762_EEPROM_HD:
14788 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014789 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014790 case FLASH_5762_EEPROM_LD:
14791 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014792 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014793 case FLASH_5720VENDOR_M_ST_M45PE20:
14794 /* This pinstrap supports multiple sizes, so force it
14795 * to read the actual size from location 0xf0.
14796 */
14797 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14798 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014799 }
14800 }
14801
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014802 switch (nvmpinstrp) {
14803 case FLASH_5720_EEPROM_HD:
14804 case FLASH_5720_EEPROM_LD:
14805 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014806 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014807
14808 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14809 tw32(NVRAM_CFG1, nvcfg1);
14810 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14811 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14812 else
14813 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14814 return;
14815 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14816 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14817 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14818 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14819 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14820 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14821 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14822 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14823 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14824 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14825 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14826 case FLASH_5720VENDOR_ATMEL_45USPT:
14827 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014828 tg3_flag_set(tp, NVRAM_BUFFERED);
14829 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014830
14831 switch (nvmpinstrp) {
14832 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14833 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14834 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14835 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14836 break;
14837 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14838 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14839 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14840 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14841 break;
14842 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14843 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14844 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14845 break;
14846 default:
Joe Perches41535772013-02-16 11:20:04 +000014847 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014848 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014849 break;
14850 }
14851 break;
14852 case FLASH_5720VENDOR_M_ST_M25PE10:
14853 case FLASH_5720VENDOR_M_ST_M45PE10:
14854 case FLASH_5720VENDOR_A_ST_M25PE10:
14855 case FLASH_5720VENDOR_A_ST_M45PE10:
14856 case FLASH_5720VENDOR_M_ST_M25PE20:
14857 case FLASH_5720VENDOR_M_ST_M45PE20:
14858 case FLASH_5720VENDOR_A_ST_M25PE20:
14859 case FLASH_5720VENDOR_A_ST_M45PE20:
14860 case FLASH_5720VENDOR_M_ST_M25PE40:
14861 case FLASH_5720VENDOR_M_ST_M45PE40:
14862 case FLASH_5720VENDOR_A_ST_M25PE40:
14863 case FLASH_5720VENDOR_A_ST_M45PE40:
14864 case FLASH_5720VENDOR_M_ST_M25PE80:
14865 case FLASH_5720VENDOR_M_ST_M45PE80:
14866 case FLASH_5720VENDOR_A_ST_M25PE80:
14867 case FLASH_5720VENDOR_A_ST_M45PE80:
14868 case FLASH_5720VENDOR_ST_25USPT:
14869 case FLASH_5720VENDOR_ST_45USPT:
14870 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014871 tg3_flag_set(tp, NVRAM_BUFFERED);
14872 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014873
14874 switch (nvmpinstrp) {
14875 case FLASH_5720VENDOR_M_ST_M25PE20:
14876 case FLASH_5720VENDOR_M_ST_M45PE20:
14877 case FLASH_5720VENDOR_A_ST_M25PE20:
14878 case FLASH_5720VENDOR_A_ST_M45PE20:
14879 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14880 break;
14881 case FLASH_5720VENDOR_M_ST_M25PE40:
14882 case FLASH_5720VENDOR_M_ST_M45PE40:
14883 case FLASH_5720VENDOR_A_ST_M25PE40:
14884 case FLASH_5720VENDOR_A_ST_M45PE40:
14885 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14886 break;
14887 case FLASH_5720VENDOR_M_ST_M25PE80:
14888 case FLASH_5720VENDOR_M_ST_M45PE80:
14889 case FLASH_5720VENDOR_A_ST_M25PE80:
14890 case FLASH_5720VENDOR_A_ST_M45PE80:
14891 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14892 break;
14893 default:
Joe Perches41535772013-02-16 11:20:04 +000014894 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014895 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014896 break;
14897 }
14898 break;
14899 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014900 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014901 return;
14902 }
14903
14904 tg3_nvram_get_pagesize(tp, nvcfg1);
14905 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014906 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014907
Joe Perches41535772013-02-16 11:20:04 +000014908 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014909 u32 val;
14910
14911 if (tg3_nvram_read(tp, 0, &val))
14912 return;
14913
14914 if (val != TG3_EEPROM_MAGIC &&
14915 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14916 tg3_flag_set(tp, NO_NVRAM);
14917 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014918}
14919
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014921static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014922{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014923 if (tg3_flag(tp, IS_SSB_CORE)) {
14924 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14925 tg3_flag_clear(tp, NVRAM);
14926 tg3_flag_clear(tp, NVRAM_BUFFERED);
14927 tg3_flag_set(tp, NO_NVRAM);
14928 return;
14929 }
14930
Linus Torvalds1da177e2005-04-16 15:20:36 -070014931 tw32_f(GRC_EEPROM_ADDR,
14932 (EEPROM_ADDR_FSM_RESET |
14933 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14934 EEPROM_ADDR_CLKPERD_SHIFT)));
14935
Michael Chan9d57f012006-12-07 00:23:25 -080014936 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014937
14938 /* Enable seeprom accesses. */
14939 tw32_f(GRC_LOCAL_CTRL,
14940 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14941 udelay(100);
14942
Joe Perches41535772013-02-16 11:20:04 +000014943 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14944 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014945 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014946
Michael Chanec41c7d2006-01-17 02:40:55 -080014947 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014948 netdev_warn(tp->dev,
14949 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014950 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014951 return;
14952 }
Michael Chane6af3012005-04-21 17:12:05 -070014953 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014954
Matt Carlson989a9d22007-05-05 11:51:05 -070014955 tp->nvram_size = 0;
14956
Joe Perches41535772013-02-16 11:20:04 +000014957 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014958 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014959 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014960 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014961 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14962 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14963 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014964 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014965 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014966 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014967 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014968 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014969 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014970 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014971 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014972 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14973 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014974 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014975 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14976 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014977 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014978 else
14979 tg3_get_nvram_info(tp);
14980
Matt Carlson989a9d22007-05-05 11:51:05 -070014981 if (tp->nvram_size == 0)
14982 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014983
Michael Chane6af3012005-04-21 17:12:05 -070014984 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014985 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014986
14987 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014988 tg3_flag_clear(tp, NVRAM);
14989 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990
14991 tg3_get_eeprom_size(tp);
14992 }
14993}
14994
Linus Torvalds1da177e2005-04-16 15:20:36 -070014995struct subsys_tbl_ent {
14996 u16 subsys_vendor, subsys_devid;
14997 u32 phy_id;
14998};
14999
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015000static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015001 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015002 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015003 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015004 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015005 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015006 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015007 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015008 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15009 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
15010 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015011 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015012 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015013 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015014 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15015 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
15016 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015017 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015018 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015019 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015020 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015021 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015022 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015023 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015024
15025 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015026 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015027 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015028 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015029 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015030 { TG3PCI_SUBVENDOR_ID_3COM,
15031 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
15032 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015033 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015034 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015035 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015036
15037 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015038 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015039 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015040 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015041 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015042 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015043 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015044 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015045 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015046
15047 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015048 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015049 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015050 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015051 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015052 { TG3PCI_SUBVENDOR_ID_COMPAQ,
15053 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
15054 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015055 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015056 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015057 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058
15059 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015060 { TG3PCI_SUBVENDOR_ID_IBM,
15061 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015062};
15063
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015064static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015065{
15066 int i;
15067
15068 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
15069 if ((subsys_id_to_phy_id[i].subsys_vendor ==
15070 tp->pdev->subsystem_vendor) &&
15071 (subsys_id_to_phy_id[i].subsys_devid ==
15072 tp->pdev->subsystem_device))
15073 return &subsys_id_to_phy_id[i];
15074 }
15075 return NULL;
15076}
15077
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015078static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015079{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070015081
Matt Carlson79eb6902010-02-17 15:17:03 +000015082 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015083 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15084
Gary Zambranoa85feb82007-05-05 11:52:19 -070015085 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000015086 tg3_flag_set(tp, EEPROM_WRITE_PROT);
15087 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080015088
Joe Perches41535772013-02-16 11:20:04 +000015089 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080015090 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015091 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15092 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015093 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015094 val = tr32(VCPU_CFGSHDW);
15095 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000015096 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070015097 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015098 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015099 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015100 device_set_wakeup_enable(&tp->pdev->dev, true);
15101 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015102 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070015103 }
15104
Linus Torvalds1da177e2005-04-16 15:20:36 -070015105 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
15106 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
15107 u32 nic_cfg, led_cfg;
Nithin Sujir7c786062013-12-06 09:53:17 -080015108 u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
15109 u32 nic_phy_id, ver, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015110 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015111
15112 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
15113 tp->nic_sram_data_cfg = nic_cfg;
15114
15115 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
15116 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015117 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15118 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15119 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015120 (ver > 0) && (ver < 0x100))
15121 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
15122
Joe Perches41535772013-02-16 11:20:04 +000015123 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070015124 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
15125
Nithin Sujir7c786062013-12-06 09:53:17 -080015126 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15127 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15128 tg3_asic_rev(tp) == ASIC_REV_5720)
15129 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
15130
Linus Torvalds1da177e2005-04-16 15:20:36 -070015131 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
15132 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
15133 eeprom_phy_serdes = 1;
15134
15135 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
15136 if (nic_phy_id != 0) {
15137 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
15138 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
15139
15140 eeprom_phy_id = (id1 >> 16) << 10;
15141 eeprom_phy_id |= (id2 & 0xfc00) << 16;
15142 eeprom_phy_id |= (id2 & 0x03ff) << 0;
15143 } else
15144 eeprom_phy_id = 0;
15145
Michael Chan7d0c41e2005-04-21 17:06:20 -070015146 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070015147 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000015148 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015149 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000015150 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015151 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070015152 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070015153
Joe Perches63c3a662011-04-26 08:12:10 +000015154 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015155 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
15156 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070015157 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015158 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
15159
15160 switch (led_cfg) {
15161 default:
15162 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
15163 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15164 break;
15165
15166 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
15167 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15168 break;
15169
15170 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
15171 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070015172
15173 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
15174 * read on some older 5700/5701 bootcode.
15175 */
Joe Perches41535772013-02-16 11:20:04 +000015176 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15177 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070015178 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15179
Linus Torvalds1da177e2005-04-16 15:20:36 -070015180 break;
15181
15182 case SHASTA_EXT_LED_SHARED:
15183 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000015184 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
15185 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015186 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15187 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070015188
15189 if (tg3_flag(tp, 5717_PLUS) ||
15190 tg3_asic_rev(tp) == ASIC_REV_5762)
15191 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
15192 LED_CTRL_BLINK_RATE_MASK;
15193
Linus Torvalds1da177e2005-04-16 15:20:36 -070015194 break;
15195
15196 case SHASTA_EXT_LED_MAC:
15197 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
15198 break;
15199
15200 case SHASTA_EXT_LED_COMBO:
15201 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000015202 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015203 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15204 LED_CTRL_MODE_PHY_2);
15205 break;
15206
Stephen Hemminger855e1112008-04-16 16:37:28 -070015207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015208
Joe Perches41535772013-02-16 11:20:04 +000015209 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
15210 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015211 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
15212 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15213
Joe Perches41535772013-02-16 11:20:04 +000015214 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015215 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080015216
Michael Chan9d26e212006-12-07 00:21:14 -080015217 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000015218 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015219 if ((tp->pdev->subsystem_vendor ==
15220 PCI_VENDOR_ID_ARIMA) &&
15221 (tp->pdev->subsystem_device == 0x205a ||
15222 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000015223 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015224 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015225 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15226 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015228
15229 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000015230 tg3_flag_set(tp, ENABLE_ASF);
15231 if (tg3_flag(tp, 5750_PLUS))
15232 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015233 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015234
15235 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015236 tg3_flag(tp, 5750_PLUS))
15237 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015238
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015239 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070015240 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000015241 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015242
Joe Perches63c3a662011-04-26 08:12:10 +000015243 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015244 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015245 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015246 device_set_wakeup_enable(&tp->pdev->dev, true);
15247 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015248
Linus Torvalds1da177e2005-04-16 15:20:36 -070015249 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015250 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015251
15252 /* serdes signal pre-emphasis in register 0x590 set by */
15253 /* bootcode if bit 18 is set */
15254 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015255 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015256
Joe Perches63c3a662011-04-26 08:12:10 +000015257 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015258 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15259 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015260 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015261 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015262
Nithin Sujir942d1af2013-04-09 08:48:07 +000015263 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015264 u32 cfg3;
15265
15266 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015267 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15268 !tg3_flag(tp, 57765_PLUS) &&
15269 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015270 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015271 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15272 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15273 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15274 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015275 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015276
Matt Carlson14417062010-02-17 15:16:59 +000015277 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015278 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015279 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015280 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015281 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015282 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Nithin Sujir7c786062013-12-06 09:53:17 -080015283
15284 if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
15285 tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015287done:
Joe Perches63c3a662011-04-26 08:12:10 +000015288 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015289 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015290 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015291 else
15292 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015293}
15294
Michael Chanc86a8562013-01-06 12:51:08 +000015295static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15296{
15297 int i, err;
15298 u32 val2, off = offset * 8;
15299
15300 err = tg3_nvram_lock(tp);
15301 if (err)
15302 return err;
15303
15304 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15305 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15306 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15307 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15308 udelay(10);
15309
15310 for (i = 0; i < 100; i++) {
15311 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15312 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15313 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15314 break;
15315 }
15316 udelay(10);
15317 }
15318
15319 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15320
15321 tg3_nvram_unlock(tp);
15322 if (val2 & APE_OTP_STATUS_CMD_DONE)
15323 return 0;
15324
15325 return -EBUSY;
15326}
15327
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015328static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015329{
15330 int i;
15331 u32 val;
15332
15333 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15334 tw32(OTP_CTRL, cmd);
15335
15336 /* Wait for up to 1 ms for command to execute. */
15337 for (i = 0; i < 100; i++) {
15338 val = tr32(OTP_STATUS);
15339 if (val & OTP_STATUS_CMD_DONE)
15340 break;
15341 udelay(10);
15342 }
15343
15344 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15345}
15346
15347/* Read the gphy configuration from the OTP region of the chip. The gphy
15348 * configuration is a 32-bit value that straddles the alignment boundary.
15349 * We do two 32-bit reads and then shift and merge the results.
15350 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015351static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015352{
15353 u32 bhalf_otp, thalf_otp;
15354
15355 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15356
15357 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15358 return 0;
15359
15360 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15361
15362 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15363 return 0;
15364
15365 thalf_otp = tr32(OTP_READ_DATA);
15366
15367 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15368
15369 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15370 return 0;
15371
15372 bhalf_otp = tr32(OTP_READ_DATA);
15373
15374 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15375}
15376
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015377static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015378{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015379 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015380
Nithin Sujir7c786062013-12-06 09:53:17 -080015381 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
15382 if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
15383 adv |= ADVERTISED_1000baseT_Half;
15384 adv |= ADVERTISED_1000baseT_Full;
15385 }
Matt Carlsone256f8a2011-03-09 16:58:24 +000015386
15387 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15388 adv |= ADVERTISED_100baseT_Half |
15389 ADVERTISED_100baseT_Full |
15390 ADVERTISED_10baseT_Half |
15391 ADVERTISED_10baseT_Full |
15392 ADVERTISED_TP;
15393 else
15394 adv |= ADVERTISED_FIBRE;
15395
15396 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015397 tp->link_config.speed = SPEED_UNKNOWN;
15398 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015399 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015400 tp->link_config.active_speed = SPEED_UNKNOWN;
15401 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015402
15403 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015404}
15405
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015406static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015407{
15408 u32 hw_phy_id_1, hw_phy_id_2;
15409 u32 hw_phy_id, hw_phy_id_masked;
15410 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015411
Matt Carlsone256f8a2011-03-09 16:58:24 +000015412 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015413 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015414 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15415
Michael Chan8151ad52012-07-29 19:15:41 +000015416 if (tg3_flag(tp, ENABLE_APE)) {
15417 switch (tp->pci_fn) {
15418 case 0:
15419 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15420 break;
15421 case 1:
15422 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15423 break;
15424 case 2:
15425 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15426 break;
15427 case 3:
15428 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15429 break;
15430 }
15431 }
15432
Nithin Sujir942d1af2013-04-09 08:48:07 +000015433 if (!tg3_flag(tp, ENABLE_ASF) &&
15434 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15435 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15436 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15437 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15438
Joe Perches63c3a662011-04-26 08:12:10 +000015439 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015440 return tg3_phy_init(tp);
15441
Linus Torvalds1da177e2005-04-16 15:20:36 -070015442 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015443 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015444 */
15445 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015446 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015447 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015448 } else {
15449 /* Now read the physical PHY_ID from the chip and verify
15450 * that it is sane. If it doesn't look good, we fall back
15451 * to either the hard-coded table based PHY_ID and failing
15452 * that the value found in the eeprom area.
15453 */
15454 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15455 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15456
15457 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15458 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15459 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15460
Matt Carlson79eb6902010-02-17 15:17:03 +000015461 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015462 }
15463
Matt Carlson79eb6902010-02-17 15:17:03 +000015464 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015465 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015466 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015467 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015468 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015469 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015470 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015471 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015472 /* Do nothing, phy ID already set up in
15473 * tg3_get_eeprom_hw_cfg().
15474 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015475 } else {
15476 struct subsys_tbl_ent *p;
15477
15478 /* No eeprom signature? Try the hardcoded
15479 * subsys device table.
15480 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015481 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015482 if (p) {
15483 tp->phy_id = p->phy_id;
15484 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15485 /* For now we saw the IDs 0xbc050cd0,
15486 * 0xbc050f80 and 0xbc050c30 on devices
15487 * connected to an BCM4785 and there are
15488 * probably more. Just assume that the phy is
15489 * supported when it is connected to a SSB core
15490 * for now.
15491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015492 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015494
Linus Torvalds1da177e2005-04-16 15:20:36 -070015495 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015496 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015497 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015498 }
15499 }
15500
Matt Carlsona6b68da2010-12-06 08:28:52 +000015501 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015502 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15503 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015504 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015505 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15506 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15507 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15508 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015509 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015510 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15511
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015512 tp->eee.supported = SUPPORTED_100baseT_Full |
15513 SUPPORTED_1000baseT_Full;
15514 tp->eee.advertised = ADVERTISED_100baseT_Full |
15515 ADVERTISED_1000baseT_Full;
15516 tp->eee.eee_enabled = 1;
15517 tp->eee.tx_lpi_enabled = 1;
15518 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15519 }
15520
Matt Carlsone256f8a2011-03-09 16:58:24 +000015521 tg3_phy_init_link_config(tp);
15522
Nithin Sujir942d1af2013-04-09 08:48:07 +000015523 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15524 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015525 !tg3_flag(tp, ENABLE_APE) &&
15526 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015527 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015528
15529 tg3_readphy(tp, MII_BMSR, &bmsr);
15530 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15531 (bmsr & BMSR_LSTATUS))
15532 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015533
Linus Torvalds1da177e2005-04-16 15:20:36 -070015534 err = tg3_phy_reset(tp);
15535 if (err)
15536 return err;
15537
Matt Carlson42b64a42011-05-19 12:12:49 +000015538 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015539
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015540 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015541 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15542 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015543
15544 tg3_writephy(tp, MII_BMCR,
15545 BMCR_ANENABLE | BMCR_ANRESTART);
15546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015547 }
15548
15549skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015550 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015551 err = tg3_init_5401phy_dsp(tp);
15552 if (err)
15553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015554
Linus Torvalds1da177e2005-04-16 15:20:36 -070015555 err = tg3_init_5401phy_dsp(tp);
15556 }
15557
Linus Torvalds1da177e2005-04-16 15:20:36 -070015558 return err;
15559}
15560
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015561static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015562{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015563 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015564 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015565 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015566 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015567
Matt Carlson535a4902011-07-20 10:20:56 +000015568 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015569 if (!vpd_data)
15570 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015571
Matt Carlson535a4902011-07-20 10:20:56 +000015572 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015573 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015574 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015575
15576 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15577 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15578 i += PCI_VPD_LRDT_TAG_SIZE;
15579
Matt Carlson535a4902011-07-20 10:20:56 +000015580 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015581 goto out_not_found;
15582
Matt Carlson184b8902010-04-05 10:19:25 +000015583 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15584 PCI_VPD_RO_KEYWORD_MFR_ID);
15585 if (j > 0) {
15586 len = pci_vpd_info_field_size(&vpd_data[j]);
15587
15588 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15589 if (j + len > block_end || len != 4 ||
15590 memcmp(&vpd_data[j], "1028", 4))
15591 goto partno;
15592
15593 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15594 PCI_VPD_RO_KEYWORD_VENDOR0);
15595 if (j < 0)
15596 goto partno;
15597
15598 len = pci_vpd_info_field_size(&vpd_data[j]);
15599
15600 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15601 if (j + len > block_end)
15602 goto partno;
15603
Kees Cook715230a2013-03-27 06:40:50 +000015604 if (len >= sizeof(tp->fw_ver))
15605 len = sizeof(tp->fw_ver) - 1;
15606 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15607 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15608 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015609 }
15610
15611partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015612 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15613 PCI_VPD_RO_KEYWORD_PARTNO);
15614 if (i < 0)
15615 goto out_not_found;
15616
15617 len = pci_vpd_info_field_size(&vpd_data[i]);
15618
15619 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15620 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015621 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015622 goto out_not_found;
15623
15624 memcpy(tp->board_part_number, &vpd_data[i], len);
15625
Linus Torvalds1da177e2005-04-16 15:20:36 -070015626out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015627 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015628 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015629 return;
15630
15631out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015632 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015633 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15634 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015635 strcpy(tp->board_part_number, "BCM5717");
15636 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15637 strcpy(tp->board_part_number, "BCM5718");
15638 else
15639 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015640 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015641 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15642 strcpy(tp->board_part_number, "BCM57780");
15643 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15644 strcpy(tp->board_part_number, "BCM57760");
15645 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15646 strcpy(tp->board_part_number, "BCM57790");
15647 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15648 strcpy(tp->board_part_number, "BCM57788");
15649 else
15650 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015651 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015652 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15653 strcpy(tp->board_part_number, "BCM57761");
15654 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15655 strcpy(tp->board_part_number, "BCM57765");
15656 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15657 strcpy(tp->board_part_number, "BCM57781");
15658 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15659 strcpy(tp->board_part_number, "BCM57785");
15660 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15661 strcpy(tp->board_part_number, "BCM57791");
15662 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15663 strcpy(tp->board_part_number, "BCM57795");
15664 else
15665 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015666 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015667 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15668 strcpy(tp->board_part_number, "BCM57762");
15669 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15670 strcpy(tp->board_part_number, "BCM57766");
15671 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15672 strcpy(tp->board_part_number, "BCM57782");
15673 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15674 strcpy(tp->board_part_number, "BCM57786");
15675 else
15676 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015677 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015678 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015679 } else {
15680nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015681 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015683}
15684
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015685static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015686{
15687 u32 val;
15688
Matt Carlsone4f34112009-02-25 14:25:00 +000015689 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015690 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015691 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015692 val != 0)
15693 return 0;
15694
15695 return 1;
15696}
15697
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015698static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015699{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015700 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015701 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015702 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015703
15704 if (tg3_nvram_read(tp, 0xc, &offset) ||
15705 tg3_nvram_read(tp, 0x4, &start))
15706 return;
15707
15708 offset = tg3_nvram_logical_addr(tp, offset);
15709
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015710 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015711 return;
15712
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015713 if ((val & 0xfc000000) == 0x0c000000) {
15714 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015715 return;
15716
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015717 if (val == 0)
15718 newver = true;
15719 }
15720
Matt Carlson75f99362010-04-05 10:19:24 +000015721 dst_off = strlen(tp->fw_ver);
15722
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015723 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015724 if (TG3_VER_SIZE - dst_off < 16 ||
15725 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015726 return;
15727
15728 offset = offset + ver_offset - start;
15729 for (i = 0; i < 16; i += 4) {
15730 __be32 v;
15731 if (tg3_nvram_read_be32(tp, offset + i, &v))
15732 return;
15733
Matt Carlson75f99362010-04-05 10:19:24 +000015734 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015735 }
15736 } else {
15737 u32 major, minor;
15738
15739 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15740 return;
15741
15742 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15743 TG3_NVM_BCVER_MAJSFT;
15744 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015745 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15746 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015747 }
15748}
15749
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015750static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015751{
15752 u32 val, major, minor;
15753
15754 /* Use native endian representation */
15755 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15756 return;
15757
15758 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15759 TG3_NVM_HWSB_CFG1_MAJSFT;
15760 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15761 TG3_NVM_HWSB_CFG1_MINSFT;
15762
15763 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15764}
15765
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015766static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015767{
15768 u32 offset, major, minor, build;
15769
Matt Carlson75f99362010-04-05 10:19:24 +000015770 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015771
15772 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15773 return;
15774
15775 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15776 case TG3_EEPROM_SB_REVISION_0:
15777 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15778 break;
15779 case TG3_EEPROM_SB_REVISION_2:
15780 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15781 break;
15782 case TG3_EEPROM_SB_REVISION_3:
15783 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15784 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015785 case TG3_EEPROM_SB_REVISION_4:
15786 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15787 break;
15788 case TG3_EEPROM_SB_REVISION_5:
15789 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15790 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015791 case TG3_EEPROM_SB_REVISION_6:
15792 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15793 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015794 default:
15795 return;
15796 }
15797
Matt Carlsone4f34112009-02-25 14:25:00 +000015798 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015799 return;
15800
15801 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15802 TG3_EEPROM_SB_EDH_BLD_SHFT;
15803 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15804 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15805 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15806
15807 if (minor > 99 || build > 26)
15808 return;
15809
Matt Carlson75f99362010-04-05 10:19:24 +000015810 offset = strlen(tp->fw_ver);
15811 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15812 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015813
15814 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015815 offset = strlen(tp->fw_ver);
15816 if (offset < TG3_VER_SIZE - 1)
15817 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015818 }
15819}
15820
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015821static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015822{
15823 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015824 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015825
15826 for (offset = TG3_NVM_DIR_START;
15827 offset < TG3_NVM_DIR_END;
15828 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015829 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015830 return;
15831
15832 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15833 break;
15834 }
15835
15836 if (offset == TG3_NVM_DIR_END)
15837 return;
15838
Joe Perches63c3a662011-04-26 08:12:10 +000015839 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015840 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015841 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015842 return;
15843
Matt Carlsone4f34112009-02-25 14:25:00 +000015844 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015845 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015846 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015847 return;
15848
15849 offset += val - start;
15850
Matt Carlsonacd9c112009-02-25 14:26:33 +000015851 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015852
Matt Carlsonacd9c112009-02-25 14:26:33 +000015853 tp->fw_ver[vlen++] = ',';
15854 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015855
15856 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015857 __be32 v;
15858 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015859 return;
15860
Al Virob9fc7dc2007-12-17 22:59:57 -080015861 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015862
Matt Carlsonacd9c112009-02-25 14:26:33 +000015863 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15864 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015865 break;
15866 }
15867
Matt Carlsonacd9c112009-02-25 14:26:33 +000015868 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15869 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015870 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015871}
15872
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015873static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015874{
Matt Carlson7fd76442009-02-25 14:27:20 +000015875 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015876
15877 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15878 if (apedata != APE_SEG_SIG_MAGIC)
15879 return;
15880
15881 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15882 if (!(apedata & APE_FW_STATUS_READY))
15883 return;
15884
Michael Chan165f4d12012-07-16 16:23:59 +000015885 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15886 tg3_flag_set(tp, APE_HAS_NCSI);
15887}
15888
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015889static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015890{
15891 int vlen;
15892 u32 apedata;
15893 char *fwtype;
15894
Matt Carlson7fd76442009-02-25 14:27:20 +000015895 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15896
Michael Chan165f4d12012-07-16 16:23:59 +000015897 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015898 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015899 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15900 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015901 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015902 fwtype = "DASH";
15903
Matt Carlson7fd76442009-02-25 14:27:20 +000015904 vlen = strlen(tp->fw_ver);
15905
Matt Carlsonecc79642010-08-02 11:26:01 +000015906 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15907 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015908 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15909 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15910 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15911 (apedata & APE_FW_VERSION_BLDMSK));
15912}
15913
Michael Chanc86a8562013-01-06 12:51:08 +000015914static void tg3_read_otp_ver(struct tg3 *tp)
15915{
15916 u32 val, val2;
15917
Joe Perches41535772013-02-16 11:20:04 +000015918 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015919 return;
15920
15921 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15922 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15923 TG3_OTP_MAGIC0_VALID(val)) {
15924 u64 val64 = (u64) val << 32 | val2;
15925 u32 ver = 0;
15926 int i, vlen;
15927
15928 for (i = 0; i < 7; i++) {
15929 if ((val64 & 0xff) == 0)
15930 break;
15931 ver = val64 & 0xff;
15932 val64 >>= 8;
15933 }
15934 vlen = strlen(tp->fw_ver);
15935 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15936 }
15937}
15938
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015939static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015940{
15941 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015942 bool vpd_vers = false;
15943
15944 if (tp->fw_ver[0] != 0)
15945 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015946
Joe Perches63c3a662011-04-26 08:12:10 +000015947 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015948 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015949 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015950 return;
15951 }
15952
Matt Carlsonacd9c112009-02-25 14:26:33 +000015953 if (tg3_nvram_read(tp, 0, &val))
15954 return;
15955
15956 if (val == TG3_EEPROM_MAGIC)
15957 tg3_read_bc_ver(tp);
15958 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15959 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015960 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15961 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015962
Michael Chan165f4d12012-07-16 16:23:59 +000015963 if (tg3_flag(tp, ENABLE_ASF)) {
15964 if (tg3_flag(tp, ENABLE_APE)) {
15965 tg3_probe_ncsi(tp);
15966 if (!vpd_vers)
15967 tg3_read_dash_ver(tp);
15968 } else if (!vpd_vers) {
15969 tg3_read_mgmtfw_ver(tp);
15970 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015971 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015972
15973 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015974}
15975
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015976static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15977{
Joe Perches63c3a662011-04-26 08:12:10 +000015978 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015979 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015980 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015981 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015982 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015983 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015984}
15985
Benoit Taine9baa3c32014-08-08 15:56:03 +020015986static const struct pci_device_id tg3_write_reorder_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080015987 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15988 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15989 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15990 { },
15991};
15992
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015993static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015994{
15995 struct pci_dev *peer;
15996 unsigned int func, devnr = tp->pdev->devfn & ~7;
15997
15998 for (func = 0; func < 8; func++) {
15999 peer = pci_get_slot(tp->pdev->bus, devnr | func);
16000 if (peer && peer != tp->pdev)
16001 break;
16002 pci_dev_put(peer);
16003 }
16004 /* 5704 can be configured in single-port mode, set peer to
16005 * tp->pdev in that case.
16006 */
16007 if (!peer) {
16008 peer = tp->pdev;
16009 return peer;
16010 }
16011
16012 /*
16013 * We don't need to keep the refcount elevated; there's no way
16014 * to remove one half of this device without removing the other
16015 */
16016 pci_dev_put(peer);
16017
16018 return peer;
16019}
16020
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016021static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000016022{
16023 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000016024 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000016025 u32 reg;
16026
16027 /* All devices that use the alternate
16028 * ASIC REV location have a CPMU.
16029 */
16030 tg3_flag_set(tp, CPMU_PRESENT);
16031
16032 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016033 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016034 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16035 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016036 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016037 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
16038 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016039 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16040 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016041 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
16042 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000016043 reg = TG3PCI_GEN2_PRODID_ASICREV;
16044 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
16045 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
16046 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
16047 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
16048 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
16049 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
16050 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
16051 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
16052 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
16053 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
16054 reg = TG3PCI_GEN15_PRODID_ASICREV;
16055 else
16056 reg = TG3PCI_PRODID_ASICREV;
16057
16058 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
16059 }
16060
16061 /* Wrong chip ID in 5752 A0. This code can be removed later
16062 * as A0 is not in production.
16063 */
Joe Perches41535772013-02-16 11:20:04 +000016064 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000016065 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
16066
Joe Perches41535772013-02-16 11:20:04 +000016067 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000016068 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
16069
Joe Perches41535772013-02-16 11:20:04 +000016070 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16071 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16072 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000016073 tg3_flag_set(tp, 5717_PLUS);
16074
Joe Perches41535772013-02-16 11:20:04 +000016075 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
16076 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000016077 tg3_flag_set(tp, 57765_CLASS);
16078
Michael Chanc65a17f2013-01-06 12:51:07 +000016079 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016080 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000016081 tg3_flag_set(tp, 57765_PLUS);
16082
16083 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000016084 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16085 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16086 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16087 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16088 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16089 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016090 tg3_flag(tp, 57765_PLUS))
16091 tg3_flag_set(tp, 5755_PLUS);
16092
Joe Perches41535772013-02-16 11:20:04 +000016093 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
16094 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000016095 tg3_flag_set(tp, 5780_CLASS);
16096
Joe Perches41535772013-02-16 11:20:04 +000016097 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16098 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16099 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016100 tg3_flag(tp, 5755_PLUS) ||
16101 tg3_flag(tp, 5780_CLASS))
16102 tg3_flag_set(tp, 5750_PLUS);
16103
Joe Perches41535772013-02-16 11:20:04 +000016104 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016105 tg3_flag(tp, 5750_PLUS))
16106 tg3_flag_set(tp, 5705_PLUS);
16107}
16108
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016109static bool tg3_10_100_only_device(struct tg3 *tp,
16110 const struct pci_device_id *ent)
16111{
16112 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
16113
Joe Perches41535772013-02-16 11:20:04 +000016114 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
16115 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016116 (tp->phy_flags & TG3_PHYFLG_IS_FET))
16117 return true;
16118
16119 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000016120 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016121 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
16122 return true;
16123 } else {
16124 return true;
16125 }
16126 }
16127
16128 return false;
16129}
16130
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000016131static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016132{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016133 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016134 u32 pci_state_reg, grc_misc_cfg;
16135 u32 val;
16136 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016137 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016138
Linus Torvalds1da177e2005-04-16 15:20:36 -070016139 /* Force memory write invalidate off. If we leave it on,
16140 * then on 5700_BX chips we have to enable a workaround.
16141 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
16142 * to match the cacheline size. The Broadcom driver have this
16143 * workaround but turns MWI off all the times so never uses
16144 * it. This seems to suggest that the workaround is insufficient.
16145 */
16146 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16147 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
16148 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16149
Matt Carlson16821282011-07-13 09:27:28 +000016150 /* Important! -- Make sure register accesses are byteswapped
16151 * correctly. Also, for those chips that require it, make
16152 * sure that indirect register accesses are enabled before
16153 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016154 */
16155 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16156 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000016157 tp->misc_host_ctrl |= (misc_ctrl_reg &
16158 MISC_HOST_CTRL_CHIPREV);
16159 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16160 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016161
Matt Carlson42b123b2012-02-13 15:20:13 +000016162 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070016163
Michael Chan68929142005-08-09 20:17:14 -070016164 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
16165 * we need to disable memory and use config. cycles
16166 * only to access all registers. The 5702/03 chips
16167 * can mistakenly decode the special cycles from the
16168 * ICH chipsets as memory write cycles, causing corruption
16169 * of register and memory space. Only certain ICH bridges
16170 * will drive special cycles with non-zero data during the
16171 * address phase which can fall within the 5703's address
16172 * range. This is not an ICH bug as the PCI spec allows
16173 * non-zero address during special cycles. However, only
16174 * these ICH bridges are known to drive non-zero addresses
16175 * during special cycles.
16176 *
16177 * Since special cycles do not cross PCI bridges, we only
16178 * enable this workaround if the 5703 is on the secondary
16179 * bus of these ICH bridges.
16180 */
Joe Perches41535772013-02-16 11:20:04 +000016181 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
16182 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070016183 static struct tg3_dev_id {
16184 u32 vendor;
16185 u32 device;
16186 u32 rev;
16187 } ich_chipsets[] = {
16188 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
16189 PCI_ANY_ID },
16190 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
16191 PCI_ANY_ID },
16192 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
16193 0xa },
16194 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
16195 PCI_ANY_ID },
16196 { },
16197 };
16198 struct tg3_dev_id *pci_id = &ich_chipsets[0];
16199 struct pci_dev *bridge = NULL;
16200
16201 while (pci_id->vendor != 0) {
16202 bridge = pci_get_device(pci_id->vendor, pci_id->device,
16203 bridge);
16204 if (!bridge) {
16205 pci_id++;
16206 continue;
16207 }
16208 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070016209 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070016210 continue;
16211 }
16212 if (bridge->subordinate &&
16213 (bridge->subordinate->number ==
16214 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016215 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070016216 pci_dev_put(bridge);
16217 break;
16218 }
16219 }
16220 }
16221
Joe Perches41535772013-02-16 11:20:04 +000016222 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070016223 static struct tg3_dev_id {
16224 u32 vendor;
16225 u32 device;
16226 } bridge_chipsets[] = {
16227 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
16228 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
16229 { },
16230 };
16231 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
16232 struct pci_dev *bridge = NULL;
16233
16234 while (pci_id->vendor != 0) {
16235 bridge = pci_get_device(pci_id->vendor,
16236 pci_id->device,
16237 bridge);
16238 if (!bridge) {
16239 pci_id++;
16240 continue;
16241 }
16242 if (bridge->subordinate &&
16243 (bridge->subordinate->number <=
16244 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016245 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070016246 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016247 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070016248 pci_dev_put(bridge);
16249 break;
16250 }
16251 }
16252 }
16253
Michael Chan4a29cc22006-03-19 13:21:12 -080016254 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16255 * DMA addresses > 40-bit. This bridge may have other additional
16256 * 57xx devices behind it in some 4-port NIC designs for example.
16257 * Any tg3 device found behind the bridge will also need the 40-bit
16258 * DMA workaround.
16259 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016260 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016261 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016262 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016263 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016264 struct pci_dev *bridge = NULL;
16265
16266 do {
16267 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16268 PCI_DEVICE_ID_SERVERWORKS_EPB,
16269 bridge);
16270 if (bridge && bridge->subordinate &&
16271 (bridge->subordinate->number <=
16272 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016273 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016274 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016275 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016276 pci_dev_put(bridge);
16277 break;
16278 }
16279 } while (bridge);
16280 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016281
Joe Perches41535772013-02-16 11:20:04 +000016282 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16283 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016284 tp->pdev_peer = tg3_find_peer(tp);
16285
Matt Carlson507399f2009-11-13 13:03:37 +000016286 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016287 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016288 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016289 else if (tg3_flag(tp, 57765_PLUS))
16290 tg3_flag_set(tp, HW_TSO_3);
16291 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016292 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016293 tg3_flag_set(tp, HW_TSO_2);
16294 else if (tg3_flag(tp, 5750_PLUS)) {
16295 tg3_flag_set(tp, HW_TSO_1);
16296 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016297 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16298 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016299 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016300 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16301 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16302 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016303 tg3_flag_set(tp, FW_TSO);
16304 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016305 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016306 tp->fw_needed = FIRMWARE_TG3TSO5;
16307 else
16308 tp->fw_needed = FIRMWARE_TG3TSO;
16309 }
16310
Matt Carlsondabc5c62011-05-19 12:12:52 +000016311 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016312 if (tg3_flag(tp, HW_TSO_1) ||
16313 tg3_flag(tp, HW_TSO_2) ||
16314 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016315 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016316 /* For firmware TSO, assume ASF is disabled.
16317 * We'll disable TSO later if we discover ASF
16318 * is enabled in tg3_get_eeprom_hw_cfg().
16319 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016320 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016321 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016322 tg3_flag_clear(tp, TSO_CAPABLE);
16323 tg3_flag_clear(tp, TSO_BUG);
16324 tp->fw_needed = NULL;
16325 }
16326
Joe Perches41535772013-02-16 11:20:04 +000016327 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016328 tp->fw_needed = FIRMWARE_TG3;
16329
Nithin Sujirc4dab502013-03-06 17:02:34 +000016330 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16331 tp->fw_needed = FIRMWARE_TG357766;
16332
Matt Carlson507399f2009-11-13 13:03:37 +000016333 tp->irq_max = 1;
16334
Joe Perches63c3a662011-04-26 08:12:10 +000016335 if (tg3_flag(tp, 5750_PLUS)) {
16336 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016337 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16338 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16339 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16340 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016341 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016342 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016343
Joe Perches63c3a662011-04-26 08:12:10 +000016344 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016345 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016346 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016348
Joe Perches63c3a662011-04-26 08:12:10 +000016349 if (tg3_flag(tp, 57765_PLUS)) {
16350 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016351 tp->irq_max = TG3_IRQ_MAX_VECS;
16352 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016353 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016354
Michael Chan91024262012-09-28 07:12:38 +000016355 tp->txq_max = 1;
16356 tp->rxq_max = 1;
16357 if (tp->irq_max > 1) {
16358 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16359 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16360
Joe Perches41535772013-02-16 11:20:04 +000016361 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16362 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016363 tp->txq_max = tp->irq_max - 1;
16364 }
16365
Matt Carlsonb7abee62012-06-07 12:56:54 +000016366 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016367 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016368 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016369
Joe Perches41535772013-02-16 11:20:04 +000016370 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016371 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016372
Joe Perches41535772013-02-16 11:20:04 +000016373 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16374 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16375 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16376 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016377 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016378
Joe Perches63c3a662011-04-26 08:12:10 +000016379 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016380 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016381 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016382
Joe Perches63c3a662011-04-26 08:12:10 +000016383 if (!tg3_flag(tp, 5705_PLUS) ||
16384 tg3_flag(tp, 5780_CLASS) ||
16385 tg3_flag(tp, USE_JUMBO_BDFLAG))
16386 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016387
Matt Carlson52f44902008-11-21 17:17:04 -080016388 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16389 &pci_state_reg);
16390
Jon Mason708ebb32011-06-27 12:56:50 +000016391 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016392 u16 lnkctl;
16393
Joe Perches63c3a662011-04-26 08:12:10 +000016394 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016395
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016396 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016397 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016398 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016399 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016400 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016401 }
Joe Perches41535772013-02-16 11:20:04 +000016402 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16403 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16404 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16405 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016406 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016407 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016408 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016409 }
Joe Perches41535772013-02-16 11:20:04 +000016410 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000016411 /* BCM5785 devices are effectively PCIe devices, and should
16412 * follow PCIe codepaths, but do not have a PCIe capabilities
16413 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016414 */
Joe Perches63c3a662011-04-26 08:12:10 +000016415 tg3_flag_set(tp, PCI_EXPRESS);
16416 } else if (!tg3_flag(tp, 5705_PLUS) ||
16417 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016418 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16419 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016420 dev_err(&tp->pdev->dev,
16421 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016422 return -EIO;
16423 }
16424
16425 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016426 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016428
Michael Chan399de502005-10-03 14:02:39 -070016429 /* If we have an AMD 762 or VIA K8T800 chipset, write
16430 * reordering to the mailbox registers done by the host
16431 * controller can cause major troubles. We read back from
16432 * every mailbox register write to force the writes to be
16433 * posted to the chip in order.
16434 */
Matt Carlson41434702011-03-09 16:58:22 +000016435 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016436 !tg3_flag(tp, PCI_EXPRESS))
16437 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016438
Matt Carlson69fc4052008-12-21 20:19:57 -080016439 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16440 &tp->pci_cacheline_sz);
16441 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16442 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016443 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016444 tp->pci_lat_timer < 64) {
16445 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016446 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16447 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016448 }
16449
Matt Carlson16821282011-07-13 09:27:28 +000016450 /* Important! -- It is critical that the PCI-X hw workaround
16451 * situation is decided before the first MMIO register access.
16452 */
Joe Perches41535772013-02-16 11:20:04 +000016453 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016454 /* 5700 BX chips need to have their TX producer index
16455 * mailboxes written twice to workaround a bug.
16456 */
Joe Perches63c3a662011-04-26 08:12:10 +000016457 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016458
Matt Carlson52f44902008-11-21 17:17:04 -080016459 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016460 *
16461 * The workaround is to use indirect register accesses
16462 * for all chip writes not to mailbox registers.
16463 */
Joe Perches63c3a662011-04-26 08:12:10 +000016464 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016465 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016466
Joe Perches63c3a662011-04-26 08:12:10 +000016467 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016468
16469 /* The chip can have it's power management PCI config
16470 * space registers clobbered due to this bug.
16471 * So explicitly force the chip into D0 here.
16472 */
Matt Carlson9974a352007-10-07 23:27:28 -070016473 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016474 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016475 &pm_reg);
16476 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16477 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016478 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016479 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016480 pm_reg);
16481
16482 /* Also, force SERR#/PERR# in PCI command. */
16483 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16484 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16485 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16486 }
16487 }
16488
Linus Torvalds1da177e2005-04-16 15:20:36 -070016489 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016490 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016491 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016492 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016493
16494 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016495 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016496 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16497 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16498 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16499 }
16500
Michael Chan1ee582d2005-08-09 20:16:46 -070016501 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016502 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016503 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016504 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016505 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016506 tp->write32_tx_mbox = tg3_write32;
16507 tp->write32_rx_mbox = tg3_write32;
16508
16509 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016510 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016511 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016512 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016513 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016514 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016515 /*
16516 * Back to back register writes can cause problems on these
16517 * chips, the workaround is to read back all reg writes
16518 * except those to mailbox regs.
16519 *
16520 * See tg3_write_indirect_reg32().
16521 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016522 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016523 }
16524
Joe Perches63c3a662011-04-26 08:12:10 +000016525 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016526 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016527 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016528 tp->write32_rx_mbox = tg3_write_flush_reg32;
16529 }
Michael Chan20094932005-08-09 20:16:32 -070016530
Joe Perches63c3a662011-04-26 08:12:10 +000016531 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016532 tp->read32 = tg3_read_indirect_reg32;
16533 tp->write32 = tg3_write_indirect_reg32;
16534 tp->read32_mbox = tg3_read_indirect_mbox;
16535 tp->write32_mbox = tg3_write_indirect_mbox;
16536 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16537 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16538
16539 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016540 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016541
16542 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16543 pci_cmd &= ~PCI_COMMAND_MEMORY;
16544 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16545 }
Joe Perches41535772013-02-16 11:20:04 +000016546 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016547 tp->read32_mbox = tg3_read32_mbox_5906;
16548 tp->write32_mbox = tg3_write32_mbox_5906;
16549 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16550 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16551 }
Michael Chan68929142005-08-09 20:17:14 -070016552
Michael Chanbbadf502006-04-06 21:46:34 -070016553 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016554 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016555 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16556 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016557 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016558
Matt Carlson16821282011-07-13 09:27:28 +000016559 /* The memory arbiter has to be enabled in order for SRAM accesses
16560 * to succeed. Normally on powerup the tg3 chip firmware will make
16561 * sure it is enabled, but other entities such as system netboot
16562 * code might disable it.
16563 */
16564 val = tr32(MEMARB_MODE);
16565 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16566
Matt Carlson9dc5e342011-11-04 09:15:02 +000016567 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016568 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016569 tg3_flag(tp, 5780_CLASS)) {
16570 if (tg3_flag(tp, PCIX_MODE)) {
16571 pci_read_config_dword(tp->pdev,
16572 tp->pcix_cap + PCI_X_STATUS,
16573 &val);
16574 tp->pci_fn = val & 0x7;
16575 }
Joe Perches41535772013-02-16 11:20:04 +000016576 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16577 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16578 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016579 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016580 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16581 val = tr32(TG3_CPMU_STATUS);
16582
Joe Perches41535772013-02-16 11:20:04 +000016583 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016584 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16585 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016586 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16587 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016588 }
16589
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016590 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16591 tp->write32_tx_mbox = tg3_write_flush_reg32;
16592 tp->write32_rx_mbox = tg3_write_flush_reg32;
16593 }
16594
Michael Chan7d0c41e2005-04-21 17:06:20 -070016595 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016596 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016597 * determined before calling tg3_set_power_state() so that
16598 * we know whether or not to switch out of Vaux power.
16599 * When the flag is set, it means that GPIO1 is used for eeprom
16600 * write protect and also implies that it is a LOM where GPIOs
16601 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016602 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016603 tg3_get_eeprom_hw_cfg(tp);
16604
Matt Carlson1caf13e2013-03-06 17:02:29 +000016605 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016606 tg3_flag_clear(tp, TSO_CAPABLE);
16607 tg3_flag_clear(tp, TSO_BUG);
16608 tp->fw_needed = NULL;
16609 }
16610
Joe Perches63c3a662011-04-26 08:12:10 +000016611 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016612 /* Allow reads and writes to the
16613 * APE register and memory space.
16614 */
16615 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016616 PCISTATE_ALLOW_APE_SHMEM_WR |
16617 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016618 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16619 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016620
16621 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016622 }
16623
Matt Carlson16821282011-07-13 09:27:28 +000016624 /* Set up tp->grc_local_ctrl before calling
16625 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16626 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016627 * It is also used as eeprom write protect on LOMs.
16628 */
16629 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016630 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016631 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016632 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16633 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016634 /* Unused GPIO3 must be driven as output on 5752 because there
16635 * are no pull-up resistors on unused GPIO pins.
16636 */
Joe Perches41535772013-02-16 11:20:04 +000016637 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016638 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016639
Joe Perches41535772013-02-16 11:20:04 +000016640 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16641 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016642 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016643 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16644
Matt Carlson8d519ab2009-04-20 06:58:01 +000016645 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16646 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016647 /* Turn off the debug UART. */
16648 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016649 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016650 /* Keep VMain power. */
16651 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16652 GRC_LCLCTRL_GPIO_OUTPUT0;
16653 }
16654
Joe Perches41535772013-02-16 11:20:04 +000016655 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016656 tp->grc_local_ctrl |=
16657 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16658
Matt Carlson16821282011-07-13 09:27:28 +000016659 /* Switch out of Vaux if it is a NIC */
16660 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016661
Linus Torvalds1da177e2005-04-16 15:20:36 -070016662 /* Derive initial jumbo mode from MTU assigned in
16663 * ether_setup() via the alloc_etherdev() call
16664 */
Joe Perches63c3a662011-04-26 08:12:10 +000016665 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16666 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016667
16668 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016669 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16670 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16671 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16672 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016673 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016674 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016675 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016676 }
16677
Joe Perches41535772013-02-16 11:20:04 +000016678 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016679 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016680
Linus Torvalds1da177e2005-04-16 15:20:36 -070016681 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016682 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16683 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16684 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16685 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016686 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16687 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16688 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016689
Joe Perches41535772013-02-16 11:20:04 +000016690 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16691 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016692 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016693 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016694 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016695
Joe Perches63c3a662011-04-26 08:12:10 +000016696 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016697 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016698 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16699 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016700 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016701 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16702 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16703 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16704 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016705 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16706 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016707 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016708 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016709 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016710 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016711 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016713
Joe Perches41535772013-02-16 11:20:04 +000016714 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16715 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016716 tp->phy_otp = tg3_read_otp_phycfg(tp);
16717 if (tp->phy_otp == 0)
16718 tp->phy_otp = TG3_OTP_DEFAULT;
16719 }
16720
Joe Perches63c3a662011-04-26 08:12:10 +000016721 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016722 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16723 else
16724 tp->mi_mode = MAC_MI_MODE_BASE;
16725
Linus Torvalds1da177e2005-04-16 15:20:36 -070016726 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016727 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16728 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016729 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16730
Matt Carlson4d958472011-04-20 07:57:35 +000016731 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016732 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080016733 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000016734 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16735 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016736 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16737 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16738 }
16739
Joe Perches41535772013-02-16 11:20:04 +000016740 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16741 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016742 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016743
Matt Carlson158d7ab2008-05-29 01:37:54 -070016744 err = tg3_mdio_init(tp);
16745 if (err)
16746 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016747
16748 /* Initialize data/descriptor byte/word swapping. */
16749 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016750 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16751 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016752 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16753 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16754 GRC_MODE_B2HRX_ENABLE |
16755 GRC_MODE_HTX2B_ENABLE |
16756 GRC_MODE_HOST_STACKUP);
16757 else
16758 val &= GRC_MODE_HOST_STACKUP;
16759
Linus Torvalds1da177e2005-04-16 15:20:36 -070016760 tw32(GRC_MODE, val | tp->grc_mode);
16761
16762 tg3_switch_clocks(tp);
16763
16764 /* Clear this out for sanity. */
16765 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16766
Nat Gurumoorthy388d3332013-12-09 10:43:21 -080016767 /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
16768 tw32(TG3PCI_REG_BASE_ADDR, 0);
16769
Linus Torvalds1da177e2005-04-16 15:20:36 -070016770 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16771 &pci_state_reg);
16772 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016773 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016774 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16775 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16776 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16777 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016778 void __iomem *sram_base;
16779
16780 /* Write some dummy words into the SRAM status block
16781 * area, see if it reads back correctly. If the return
16782 * value is bad, force enable the PCIX workaround.
16783 */
16784 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16785
16786 writel(0x00000000, sram_base);
16787 writel(0x00000000, sram_base + 4);
16788 writel(0xffffffff, sram_base + 4);
16789 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016790 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016791 }
16792 }
16793
16794 udelay(50);
16795 tg3_nvram_init(tp);
16796
Nithin Sujirc4dab502013-03-06 17:02:34 +000016797 /* If the device has an NVRAM, no need to load patch firmware */
16798 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16799 !tg3_flag(tp, NO_NVRAM))
16800 tp->fw_needed = NULL;
16801
Linus Torvalds1da177e2005-04-16 15:20:36 -070016802 grc_misc_cfg = tr32(GRC_MISC_CFG);
16803 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16804
Joe Perches41535772013-02-16 11:20:04 +000016805 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016806 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16807 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016808 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016809
Joe Perches63c3a662011-04-26 08:12:10 +000016810 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016811 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016812 tg3_flag_set(tp, TAGGED_STATUS);
16813 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016814 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16815 HOSTCC_MODE_CLRTICK_TXBD);
16816
16817 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16818 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16819 tp->misc_host_ctrl);
16820 }
16821
Matt Carlson3bda1252008-08-15 14:08:22 -070016822 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016823 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016824 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016825 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016826 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016827
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016828 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016829 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016830
16831 err = tg3_phy_probe(tp);
16832 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016833 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016834 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016835 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016836 }
16837
Matt Carlson184b8902010-04-05 10:19:25 +000016838 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016839 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016840
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016841 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16842 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016843 } else {
Joe Perches41535772013-02-16 11:20:04 +000016844 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016845 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016846 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016847 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016848 }
16849
16850 /* 5700 {AX,BX} chips have a broken status block link
16851 * change bit implementation, so we must use the
16852 * status register in those cases.
16853 */
Joe Perches41535772013-02-16 11:20:04 +000016854 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016855 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016856 else
Joe Perches63c3a662011-04-26 08:12:10 +000016857 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016858
16859 /* The led_ctrl is set during tg3_phy_probe, here we might
16860 * have to force the link status polling mechanism based
16861 * upon subsystem IDs.
16862 */
16863 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016864 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016865 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16866 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016867 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016868 }
16869
16870 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016871 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016872 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016873 else
Joe Perches63c3a662011-04-26 08:12:10 +000016874 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016875
Nithin Sujir1743b832014-01-03 10:09:14 -080016876 if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
16877 tg3_flag_set(tp, POLL_CPMU_LINK);
16878
Eric Dumazet9205fd92011-11-18 06:47:01 +000016879 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016880 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016881 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016882 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016883 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016884#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016885 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016886#endif
16887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016888
Matt Carlson2c49a442010-09-30 10:34:35 +000016889 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16890 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016891 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16892
Matt Carlson2c49a442010-09-30 10:34:35 +000016893 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016894
16895 /* Increment the rx prod index on the rx std ring by at most
16896 * 8 for these chips to workaround hw errata.
16897 */
Joe Perches41535772013-02-16 11:20:04 +000016898 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16899 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16900 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016901 tp->rx_std_max_post = 8;
16902
Joe Perches63c3a662011-04-26 08:12:10 +000016903 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016904 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16905 PCIE_PWR_MGMT_L1_THRESH_MSK;
16906
Linus Torvalds1da177e2005-04-16 15:20:36 -070016907 return err;
16908}
16909
David S. Miller49b6e95f2007-03-29 01:38:42 -070016910#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016911static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016912{
16913 struct net_device *dev = tp->dev;
16914 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016915 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016916 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016917 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016918
David S. Miller49b6e95f2007-03-29 01:38:42 -070016919 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016920 if (addr && len == ETH_ALEN) {
16921 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016923 }
16924 return -ENODEV;
16925}
16926
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016927static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016928{
16929 struct net_device *dev = tp->dev;
16930
Joe Perchesd458cdf2013-10-01 19:04:40 -070016931 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016932 return 0;
16933}
16934#endif
16935
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016936static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016937{
16938 struct net_device *dev = tp->dev;
16939 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016940 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016941 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016942
David S. Miller49b6e95f2007-03-29 01:38:42 -070016943#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016944 if (!tg3_get_macaddr_sparc(tp))
16945 return 0;
16946#endif
16947
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016948 if (tg3_flag(tp, IS_SSB_CORE)) {
16949 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16950 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16951 return 0;
16952 }
16953
Linus Torvalds1da177e2005-04-16 15:20:36 -070016954 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016955 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016956 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016957 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16958 mac_offset = 0xcc;
16959 if (tg3_nvram_lock(tp))
16960 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16961 else
16962 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016963 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016964 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016965 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016966 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016967 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016968 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016969 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016970
16971 /* First try to get it from MAC address mailbox. */
16972 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16973 if ((hi >> 16) == 0x484b) {
16974 dev->dev_addr[0] = (hi >> 8) & 0xff;
16975 dev->dev_addr[1] = (hi >> 0) & 0xff;
16976
16977 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16978 dev->dev_addr[2] = (lo >> 24) & 0xff;
16979 dev->dev_addr[3] = (lo >> 16) & 0xff;
16980 dev->dev_addr[4] = (lo >> 8) & 0xff;
16981 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016982
Michael Chan008652b2006-03-27 23:14:53 -080016983 /* Some old bootcode may report a 0 MAC address in SRAM */
16984 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16985 }
16986 if (!addr_ok) {
16987 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016988 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016989 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016990 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016991 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16992 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016993 }
16994 /* Finally just fetch it out of the MAC control regs. */
16995 else {
16996 hi = tr32(MAC_ADDR_0_HIGH);
16997 lo = tr32(MAC_ADDR_0_LOW);
16998
16999 dev->dev_addr[5] = lo & 0xff;
17000 dev->dev_addr[4] = (lo >> 8) & 0xff;
17001 dev->dev_addr[3] = (lo >> 16) & 0xff;
17002 dev->dev_addr[2] = (lo >> 24) & 0xff;
17003 dev->dev_addr[1] = hi & 0xff;
17004 dev->dev_addr[0] = (hi >> 8) & 0xff;
17005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017006 }
17007
17008 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070017009#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070017010 if (!tg3_get_default_macaddr_sparc(tp))
17011 return 0;
17012#endif
17013 return -EINVAL;
17014 }
17015 return 0;
17016}
17017
David S. Miller59e6b432005-05-18 22:50:10 -070017018#define BOUNDARY_SINGLE_CACHELINE 1
17019#define BOUNDARY_MULTI_CACHELINE 2
17020
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017021static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070017022{
17023 int cacheline_size;
17024 u8 byte;
17025 int goal;
17026
17027 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
17028 if (byte == 0)
17029 cacheline_size = 1024;
17030 else
17031 cacheline_size = (int) byte * 4;
17032
17033 /* On 5703 and later chips, the boundary bits have no
17034 * effect.
17035 */
Joe Perches41535772013-02-16 11:20:04 +000017036 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17037 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017038 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070017039 goto out;
17040
17041#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
17042 goal = BOUNDARY_MULTI_CACHELINE;
17043#else
17044#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
17045 goal = BOUNDARY_SINGLE_CACHELINE;
17046#else
17047 goal = 0;
17048#endif
17049#endif
17050
Joe Perches63c3a662011-04-26 08:12:10 +000017051 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017052 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
17053 goto out;
17054 }
17055
David S. Miller59e6b432005-05-18 22:50:10 -070017056 if (!goal)
17057 goto out;
17058
17059 /* PCI controllers on most RISC systems tend to disconnect
17060 * when a device tries to burst across a cache-line boundary.
17061 * Therefore, letting tg3 do so just wastes PCI bandwidth.
17062 *
17063 * Unfortunately, for PCI-E there are only limited
17064 * write-side controls for this, and thus for reads
17065 * we will still get the disconnects. We'll also waste
17066 * these PCI cycles for both read and write for chips
17067 * other than 5700 and 5701 which do not implement the
17068 * boundary bits.
17069 */
Joe Perches63c3a662011-04-26 08:12:10 +000017070 if (tg3_flag(tp, PCIX_MODE) && !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 case 128:
17076 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17077 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
17078 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
17079 } else {
17080 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17081 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17082 }
17083 break;
17084
17085 case 256:
17086 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
17087 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
17088 break;
17089
17090 default:
17091 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17092 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17093 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017094 }
Joe Perches63c3a662011-04-26 08:12:10 +000017095 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017096 switch (cacheline_size) {
17097 case 16:
17098 case 32:
17099 case 64:
17100 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17101 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17102 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
17103 break;
17104 }
17105 /* fallthrough */
17106 case 128:
17107 default:
17108 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17109 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
17110 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017111 }
David S. Miller59e6b432005-05-18 22:50:10 -070017112 } else {
17113 switch (cacheline_size) {
17114 case 16:
17115 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17116 val |= (DMA_RWCTRL_READ_BNDRY_16 |
17117 DMA_RWCTRL_WRITE_BNDRY_16);
17118 break;
17119 }
17120 /* fallthrough */
17121 case 32:
17122 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17123 val |= (DMA_RWCTRL_READ_BNDRY_32 |
17124 DMA_RWCTRL_WRITE_BNDRY_32);
17125 break;
17126 }
17127 /* fallthrough */
17128 case 64:
17129 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17130 val |= (DMA_RWCTRL_READ_BNDRY_64 |
17131 DMA_RWCTRL_WRITE_BNDRY_64);
17132 break;
17133 }
17134 /* fallthrough */
17135 case 128:
17136 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17137 val |= (DMA_RWCTRL_READ_BNDRY_128 |
17138 DMA_RWCTRL_WRITE_BNDRY_128);
17139 break;
17140 }
17141 /* fallthrough */
17142 case 256:
17143 val |= (DMA_RWCTRL_READ_BNDRY_256 |
17144 DMA_RWCTRL_WRITE_BNDRY_256);
17145 break;
17146 case 512:
17147 val |= (DMA_RWCTRL_READ_BNDRY_512 |
17148 DMA_RWCTRL_WRITE_BNDRY_512);
17149 break;
17150 case 1024:
17151 default:
17152 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
17153 DMA_RWCTRL_WRITE_BNDRY_1024);
17154 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017155 }
David S. Miller59e6b432005-05-18 22:50:10 -070017156 }
17157
17158out:
17159 return val;
17160}
17161
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017162static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000017163 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017164{
17165 struct tg3_internal_buffer_desc test_desc;
17166 u32 sram_dma_descs;
17167 int i, ret;
17168
17169 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
17170
17171 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
17172 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
17173 tw32(RDMAC_STATUS, 0);
17174 tw32(WDMAC_STATUS, 0);
17175
17176 tw32(BUFMGR_MODE, 0);
17177 tw32(FTQ_RESET, 0);
17178
17179 test_desc.addr_hi = ((u64) buf_dma) >> 32;
17180 test_desc.addr_lo = buf_dma & 0xffffffff;
17181 test_desc.nic_mbuf = 0x00002100;
17182 test_desc.len = size;
17183
17184 /*
17185 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
17186 * the *second* time the tg3 driver was getting loaded after an
17187 * initial scan.
17188 *
17189 * Broadcom tells me:
17190 * ...the DMA engine is connected to the GRC block and a DMA
17191 * reset may affect the GRC block in some unpredictable way...
17192 * The behavior of resets to individual blocks has not been tested.
17193 *
17194 * Broadcom noted the GRC reset will also reset all sub-components.
17195 */
17196 if (to_device) {
17197 test_desc.cqid_sqid = (13 << 8) | 2;
17198
17199 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
17200 udelay(40);
17201 } else {
17202 test_desc.cqid_sqid = (16 << 8) | 7;
17203
17204 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
17205 udelay(40);
17206 }
17207 test_desc.flags = 0x00000005;
17208
17209 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
17210 u32 val;
17211
17212 val = *(((u32 *)&test_desc) + i);
17213 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
17214 sram_dma_descs + (i * sizeof(u32)));
17215 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
17216 }
17217 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
17218
Matt Carlson859a588792010-04-05 10:19:28 +000017219 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017220 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000017221 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070017222 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017223
17224 ret = -ENODEV;
17225 for (i = 0; i < 40; i++) {
17226 u32 val;
17227
17228 if (to_device)
17229 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
17230 else
17231 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
17232 if ((val & 0xffff) == sram_dma_descs) {
17233 ret = 0;
17234 break;
17235 }
17236
17237 udelay(100);
17238 }
17239
17240 return ret;
17241}
17242
David S. Millerded73402005-05-23 13:59:47 -070017243#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017244
Benoit Taine9baa3c32014-08-08 15:56:03 +020017245static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080017246 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
17247 { },
17248};
17249
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017250static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017251{
17252 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070017253 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017254 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017255
Matt Carlson4bae65c2010-11-24 08:31:52 +000017256 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
17257 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017258 if (!buf) {
17259 ret = -ENOMEM;
17260 goto out_nofree;
17261 }
17262
17263 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17264 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17265
David S. Miller59e6b432005-05-18 22:50:10 -070017266 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017267
Joe Perches63c3a662011-04-26 08:12:10 +000017268 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017269 goto out;
17270
Joe Perches63c3a662011-04-26 08:12:10 +000017271 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017272 /* DMA read watermark not used on PCIE */
17273 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017274 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017275 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17276 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017277 tp->dma_rwctrl |= 0x003f0000;
17278 else
17279 tp->dma_rwctrl |= 0x003f000f;
17280 } else {
Joe Perches41535772013-02-16 11:20:04 +000017281 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17282 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017283 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017284 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017285
Michael Chan4a29cc22006-03-19 13:21:12 -080017286 /* If the 5704 is behind the EPB bridge, we can
17287 * do the less restrictive ONE_DMA workaround for
17288 * better performance.
17289 */
Joe Perches63c3a662011-04-26 08:12:10 +000017290 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017291 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017292 tp->dma_rwctrl |= 0x8000;
17293 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017294 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17295
Joe Perches41535772013-02-16 11:20:04 +000017296 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017297 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017298 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017299 tp->dma_rwctrl |=
17300 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17301 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17302 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017303 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017304 /* 5780 always in PCIX mode */
17305 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017306 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017307 /* 5714 always in PCIX mode */
17308 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017309 } else {
17310 tp->dma_rwctrl |= 0x001b000f;
17311 }
17312 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017313 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17314 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017315
Joe Perches41535772013-02-16 11:20:04 +000017316 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17317 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017318 tp->dma_rwctrl &= 0xfffffff0;
17319
Joe Perches41535772013-02-16 11:20:04 +000017320 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17321 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017322 /* Remove this if it causes problems for some boards. */
17323 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17324
17325 /* On 5700/5701 chips, we need to set this bit.
17326 * Otherwise the chip will issue cacheline transactions
17327 * to streamable DMA memory with not all the byte
17328 * enables turned on. This is an error on several
17329 * RISC PCI controllers, in particular sparc64.
17330 *
17331 * On 5703/5704 chips, this bit has been reassigned
17332 * a different meaning. In particular, it is used
17333 * on those chips to enable a PCI-X workaround.
17334 */
17335 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17336 }
17337
17338 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17339
Linus Torvalds1da177e2005-04-16 15:20:36 -070017340
Joe Perches41535772013-02-16 11:20:04 +000017341 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17342 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017343 goto out;
17344
David S. Miller59e6b432005-05-18 22:50:10 -070017345 /* It is best to perform DMA test with maximum write burst size
17346 * to expose the 5700/5701 write DMA bug.
17347 */
17348 saved_dma_rwctrl = tp->dma_rwctrl;
17349 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17350 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17351
Linus Torvalds1da177e2005-04-16 15:20:36 -070017352 while (1) {
17353 u32 *p = buf, i;
17354
17355 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17356 p[i] = i;
17357
17358 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017359 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017360 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017361 dev_err(&tp->pdev->dev,
17362 "%s: Buffer write failed. err = %d\n",
17363 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017364 break;
17365 }
17366
Linus Torvalds1da177e2005-04-16 15:20:36 -070017367 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017368 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017369 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017370 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17371 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017372 break;
17373 }
17374
17375 /* Verify it. */
17376 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17377 if (p[i] == i)
17378 continue;
17379
David S. Miller59e6b432005-05-18 22:50:10 -070017380 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17381 DMA_RWCTRL_WRITE_BNDRY_16) {
17382 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017383 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17384 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17385 break;
17386 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017387 dev_err(&tp->pdev->dev,
17388 "%s: Buffer corrupted on read back! "
17389 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017390 ret = -ENODEV;
17391 goto out;
17392 }
17393 }
17394
17395 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17396 /* Success. */
17397 ret = 0;
17398 break;
17399 }
17400 }
David S. Miller59e6b432005-05-18 22:50:10 -070017401 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17402 DMA_RWCTRL_WRITE_BNDRY_16) {
17403 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017404 * now look for chipsets that are known to expose the
17405 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017406 */
Matt Carlson41434702011-03-09 16:58:22 +000017407 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017408 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17409 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017410 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017411 /* Safe to use the calculated DMA boundary. */
17412 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017413 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017414
David S. Miller59e6b432005-05-18 22:50:10 -070017415 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017417
17418out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017419 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017420out_nofree:
17421 return ret;
17422}
17423
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017424static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017425{
Joe Perches63c3a662011-04-26 08:12:10 +000017426 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017427 tp->bufmgr_config.mbuf_read_dma_low_water =
17428 DEFAULT_MB_RDMA_LOW_WATER_5705;
17429 tp->bufmgr_config.mbuf_mac_rx_low_water =
17430 DEFAULT_MB_MACRX_LOW_WATER_57765;
17431 tp->bufmgr_config.mbuf_high_water =
17432 DEFAULT_MB_HIGH_WATER_57765;
17433
17434 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17435 DEFAULT_MB_RDMA_LOW_WATER_5705;
17436 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17437 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17438 tp->bufmgr_config.mbuf_high_water_jumbo =
17439 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017440 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017441 tp->bufmgr_config.mbuf_read_dma_low_water =
17442 DEFAULT_MB_RDMA_LOW_WATER_5705;
17443 tp->bufmgr_config.mbuf_mac_rx_low_water =
17444 DEFAULT_MB_MACRX_LOW_WATER_5705;
17445 tp->bufmgr_config.mbuf_high_water =
17446 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017447 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017448 tp->bufmgr_config.mbuf_mac_rx_low_water =
17449 DEFAULT_MB_MACRX_LOW_WATER_5906;
17450 tp->bufmgr_config.mbuf_high_water =
17451 DEFAULT_MB_HIGH_WATER_5906;
17452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017453
Michael Chanfdfec1722005-07-25 12:31:48 -070017454 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17455 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17456 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17457 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17458 tp->bufmgr_config.mbuf_high_water_jumbo =
17459 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17460 } else {
17461 tp->bufmgr_config.mbuf_read_dma_low_water =
17462 DEFAULT_MB_RDMA_LOW_WATER;
17463 tp->bufmgr_config.mbuf_mac_rx_low_water =
17464 DEFAULT_MB_MACRX_LOW_WATER;
17465 tp->bufmgr_config.mbuf_high_water =
17466 DEFAULT_MB_HIGH_WATER;
17467
17468 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17469 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17470 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17471 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17472 tp->bufmgr_config.mbuf_high_water_jumbo =
17473 DEFAULT_MB_HIGH_WATER_JUMBO;
17474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017475
17476 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17477 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17478}
17479
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017480static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017481{
Matt Carlson79eb6902010-02-17 15:17:03 +000017482 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17483 case TG3_PHY_ID_BCM5400: return "5400";
17484 case TG3_PHY_ID_BCM5401: return "5401";
17485 case TG3_PHY_ID_BCM5411: return "5411";
17486 case TG3_PHY_ID_BCM5701: return "5701";
17487 case TG3_PHY_ID_BCM5703: return "5703";
17488 case TG3_PHY_ID_BCM5704: return "5704";
17489 case TG3_PHY_ID_BCM5705: return "5705";
17490 case TG3_PHY_ID_BCM5750: return "5750";
17491 case TG3_PHY_ID_BCM5752: return "5752";
17492 case TG3_PHY_ID_BCM5714: return "5714";
17493 case TG3_PHY_ID_BCM5780: return "5780";
17494 case TG3_PHY_ID_BCM5755: return "5755";
17495 case TG3_PHY_ID_BCM5787: return "5787";
17496 case TG3_PHY_ID_BCM5784: return "5784";
17497 case TG3_PHY_ID_BCM5756: return "5722/5756";
17498 case TG3_PHY_ID_BCM5906: return "5906";
17499 case TG3_PHY_ID_BCM5761: return "5761";
17500 case TG3_PHY_ID_BCM5718C: return "5718C";
17501 case TG3_PHY_ID_BCM5718S: return "5718S";
17502 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017503 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017504 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017505 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017506 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017507 case 0: return "serdes";
17508 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017510}
17511
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017512static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017513{
Joe Perches63c3a662011-04-26 08:12:10 +000017514 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017515 strcpy(str, "PCI Express");
17516 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017517 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017518 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17519
17520 strcpy(str, "PCIX:");
17521
17522 if ((clock_ctrl == 7) ||
17523 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17524 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17525 strcat(str, "133MHz");
17526 else if (clock_ctrl == 0)
17527 strcat(str, "33MHz");
17528 else if (clock_ctrl == 2)
17529 strcat(str, "50MHz");
17530 else if (clock_ctrl == 4)
17531 strcat(str, "66MHz");
17532 else if (clock_ctrl == 6)
17533 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017534 } else {
17535 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017536 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017537 strcat(str, "66MHz");
17538 else
17539 strcat(str, "33MHz");
17540 }
Joe Perches63c3a662011-04-26 08:12:10 +000017541 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017542 strcat(str, ":32-bit");
17543 else
17544 strcat(str, ":64-bit");
17545 return str;
17546}
17547
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017548static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017549{
17550 struct ethtool_coalesce *ec = &tp->coal;
17551
17552 memset(ec, 0, sizeof(*ec));
17553 ec->cmd = ETHTOOL_GCOALESCE;
17554 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17555 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17556 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17557 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17558 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17559 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17560 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17561 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17562 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17563
17564 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17565 HOSTCC_MODE_CLRTICK_TXBD)) {
17566 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17567 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17568 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17569 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17570 }
Michael Chand244c892005-07-05 14:42:33 -070017571
Joe Perches63c3a662011-04-26 08:12:10 +000017572 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017573 ec->rx_coalesce_usecs_irq = 0;
17574 ec->tx_coalesce_usecs_irq = 0;
17575 ec->stats_block_coalesce_usecs = 0;
17576 }
David S. Miller15f98502005-05-18 22:49:26 -070017577}
17578
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017579static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017580 const struct pci_device_id *ent)
17581{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017582 struct net_device *dev;
17583 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017584 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017585 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017586 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017587 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017588 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017589
Joe Perches05dbe002010-02-17 19:44:19 +000017590 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017591
17592 err = pci_enable_device(pdev);
17593 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017594 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017595 return err;
17596 }
17597
Linus Torvalds1da177e2005-04-16 15:20:36 -070017598 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17599 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017600 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017601 goto err_out_disable_pdev;
17602 }
17603
17604 pci_set_master(pdev);
17605
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017606 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017607 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017608 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017609 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017610 }
17611
Linus Torvalds1da177e2005-04-16 15:20:36 -070017612 SET_NETDEV_DEV(dev, &pdev->dev);
17613
Linus Torvalds1da177e2005-04-16 15:20:36 -070017614 tp = netdev_priv(dev);
17615 tp->pdev = pdev;
17616 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017617 tp->rx_mode = TG3_DEF_RX_MODE;
17618 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017619 tp->irq_sync = 1;
Ivan Vecera0486a062014-09-01 14:21:57 +020017620 tp->pcierr_recovery = false;
Matt Carlson8ef21422008-05-02 16:47:53 -070017621
Linus Torvalds1da177e2005-04-16 15:20:36 -070017622 if (tg3_debug > 0)
17623 tp->msg_enable = tg3_debug;
17624 else
17625 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17626
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017627 if (pdev_is_ssb_gige_core(pdev)) {
17628 tg3_flag_set(tp, IS_SSB_CORE);
17629 if (ssb_gige_must_flush_posted_writes(pdev))
17630 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17631 if (ssb_gige_one_dma_at_once(pdev))
17632 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017633 if (ssb_gige_have_roboswitch(pdev)) {
17634 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017635 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017636 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017637 if (ssb_gige_is_rgmii(pdev))
17638 tg3_flag_set(tp, RGMII_MODE);
17639 }
17640
Linus Torvalds1da177e2005-04-16 15:20:36 -070017641 /* The word/byte swap controls here control register access byte
17642 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17643 * setting below.
17644 */
17645 tp->misc_host_ctrl =
17646 MISC_HOST_CTRL_MASK_PCI_INT |
17647 MISC_HOST_CTRL_WORD_SWAP |
17648 MISC_HOST_CTRL_INDIR_ACCESS |
17649 MISC_HOST_CTRL_PCISTATE_RW;
17650
17651 /* The NONFRM (non-frame) byte/word swap controls take effect
17652 * on descriptor entries, anything which isn't packet data.
17653 *
17654 * The StrongARM chips on the board (one for tx, one for rx)
17655 * are running in big-endian mode.
17656 */
17657 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17658 GRC_MODE_WSWAP_NONFRM_DATA);
17659#ifdef __BIG_ENDIAN
17660 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17661#endif
17662 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017663 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017664 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017665
Matt Carlsond5fe4882008-11-21 17:20:32 -080017666 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017667 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017668 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017669 err = -ENOMEM;
17670 goto err_out_free_dev;
17671 }
17672
Matt Carlsonc9cab242011-07-13 09:27:27 +000017673 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17674 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17675 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17676 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17677 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017678 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017679 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17680 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017681 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017682 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17683 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017684 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17685 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017686 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17687 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017688 tg3_flag_set(tp, ENABLE_APE);
17689 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17690 if (!tp->aperegs) {
17691 dev_err(&pdev->dev,
17692 "Cannot map APE registers, aborting\n");
17693 err = -ENOMEM;
17694 goto err_out_iounmap;
17695 }
17696 }
17697
Linus Torvalds1da177e2005-04-16 15:20:36 -070017698 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17699 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017700
Linus Torvalds1da177e2005-04-16 15:20:36 -070017701 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017702 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017703 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017704 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017705
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017706 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017707 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017708 dev_err(&pdev->dev,
17709 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017710 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017711 }
17712
Michael Chan4a29cc22006-03-19 13:21:12 -080017713 /* The EPB bridge inside 5714, 5715, and 5780 and any
17714 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017715 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17716 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17717 * do DMA address check in tg3_start_xmit().
17718 */
Joe Perches63c3a662011-04-26 08:12:10 +000017719 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017720 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017721 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017722 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017723#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017724 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017725#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017726 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017727 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017728
17729 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017730 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017731 err = pci_set_dma_mask(pdev, dma_mask);
17732 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017733 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017734 err = pci_set_consistent_dma_mask(pdev,
17735 persist_dma_mask);
17736 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017737 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17738 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017739 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017740 }
17741 }
17742 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017743 if (err || dma_mask == DMA_BIT_MASK(32)) {
17744 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017745 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017746 dev_err(&pdev->dev,
17747 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017748 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017749 }
17750 }
17751
Michael Chanfdfec1722005-07-25 12:31:48 -070017752 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017753
Matt Carlson0da06062011-05-19 12:12:53 +000017754 /* 5700 B0 chips do not support checksumming correctly due
17755 * to hardware bugs.
17756 */
Joe Perches41535772013-02-16 11:20:04 +000017757 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017758 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17759
17760 if (tg3_flag(tp, 5755_PLUS))
17761 features |= NETIF_F_IPV6_CSUM;
17762 }
17763
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017764 /* TSO is on by default on chips that support hardware TSO.
17765 * Firmware TSO on older chips gives lower performance, so it
17766 * is off by default, but can be enabled using ethtool.
17767 */
Joe Perches63c3a662011-04-26 08:12:10 +000017768 if ((tg3_flag(tp, HW_TSO_1) ||
17769 tg3_flag(tp, HW_TSO_2) ||
17770 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017771 (features & NETIF_F_IP_CSUM))
17772 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017773 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017774 if (features & NETIF_F_IPV6_CSUM)
17775 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017776 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017777 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17778 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17779 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17780 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17781 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017782 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017784
Vlad Yasevich51dfe7b2014-03-24 17:52:12 -040017785 dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
17786 NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlsond542fe22011-05-19 16:02:43 +000017787 dev->vlan_features |= features;
17788
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017789 /*
17790 * Add loopback capability only for a subset of devices that support
17791 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17792 * loopback for the remaining devices.
17793 */
Joe Perches41535772013-02-16 11:20:04 +000017794 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017795 !tg3_flag(tp, CPMU_PRESENT))
17796 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017797 features |= NETIF_F_LOOPBACK;
17798
Matt Carlson0da06062011-05-19 12:12:53 +000017799 dev->hw_features |= features;
Michael Chane565eec2014-01-03 10:09:12 -080017800 dev->priv_flags |= IFF_UNICAST_FLT;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017801
Joe Perches41535772013-02-16 11:20:04 +000017802 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017803 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017804 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017805 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017806 tp->rx_pending = 63;
17807 }
17808
Linus Torvalds1da177e2005-04-16 15:20:36 -070017809 err = tg3_get_device_address(tp);
17810 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017811 dev_err(&pdev->dev,
17812 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017813 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017814 }
17815
Matt Carlson78f90dc2009-11-13 13:03:42 +000017816 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17817 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17818 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017819 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017820 struct tg3_napi *tnapi = &tp->napi[i];
17821
17822 tnapi->tp = tp;
17823 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17824
17825 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017826 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017827 intmbx += 0x8;
17828 else
17829 intmbx += 0x4;
17830
17831 tnapi->consmbox = rcvmbx;
17832 tnapi->prodmbox = sndmbx;
17833
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017834 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017835 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017836 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017837 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017838
Joe Perches63c3a662011-04-26 08:12:10 +000017839 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017840 break;
17841
17842 /*
17843 * If we support MSIX, we'll be using RSS. If we're using
17844 * RSS, the first vector only handles link interrupts and the
17845 * remaining vectors handle rx and tx interrupts. Reuse the
17846 * mailbox values for the next iteration. The values we setup
17847 * above are still useful for the single vectored mode.
17848 */
17849 if (!i)
17850 continue;
17851
17852 rcvmbx += 0x8;
17853
17854 if (sndmbx & 0x4)
17855 sndmbx -= 0x4;
17856 else
17857 sndmbx += 0xc;
17858 }
17859
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017860 /*
17861 * Reset chip in case UNDI or EFI driver did not shutdown
17862 * DMA self test will enable WDMAC and we'll see (spurious)
17863 * pending DMA on the PCI bus at that point.
17864 */
17865 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17866 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017867 tg3_full_lock(tp, 0);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017868 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17869 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017870 tg3_full_unlock(tp);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017871 }
17872
17873 err = tg3_test_dma(tp);
17874 if (err) {
17875 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
17876 goto err_out_apeunmap;
17877 }
17878
Matt Carlsonc88864d2007-11-12 21:07:01 -080017879 tg3_init_coal(tp);
17880
Michael Chanc49a1562006-12-17 17:07:29 -080017881 pci_set_drvdata(pdev, dev);
17882
Joe Perches41535772013-02-16 11:20:04 +000017883 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17884 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17885 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017886 tg3_flag_set(tp, PTP_CAPABLE);
17887
Matt Carlson21f76382012-02-22 12:35:21 +000017888 tg3_timer_init(tp);
17889
Michael Chan402e1392013-02-14 12:13:41 +000017890 tg3_carrier_off(tp);
17891
Linus Torvalds1da177e2005-04-16 15:20:36 -070017892 err = register_netdev(dev);
17893 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017894 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017895 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017896 }
17897
Ivan Vecera20d14a52015-01-08 16:13:07 +010017898 if (tg3_flag(tp, PTP_CAPABLE)) {
17899 tg3_ptp_init(tp);
17900 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
17901 &tp->pdev->dev);
17902 if (IS_ERR(tp->ptp_clock))
17903 tp->ptp_clock = NULL;
17904 }
17905
Joe Perches05dbe002010-02-17 19:44:19 +000017906 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17907 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017908 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017909 tg3_bus_string(tp, str),
17910 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017911
Andrew Lunn22209432016-01-06 20:11:13 +010017912 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017913 char *ethtype;
17914
17915 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17916 ethtype = "10/100Base-TX";
17917 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17918 ethtype = "1000Base-SX";
17919 else
17920 ethtype = "10/100/1000Base-T";
17921
Matt Carlson5129c3a2010-04-05 10:19:23 +000017922 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017923 "(WireSpeed[%d], EEE[%d])\n",
17924 tg3_phy_string(tp), ethtype,
17925 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17926 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017927 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017928
Joe Perches05dbe002010-02-17 19:44:19 +000017929 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017930 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017931 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017932 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017933 tg3_flag(tp, ENABLE_ASF) != 0,
17934 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017935 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17936 tp->dma_rwctrl,
17937 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17938 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017939
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017940 pci_save_state(pdev);
17941
Linus Torvalds1da177e2005-04-16 15:20:36 -070017942 return 0;
17943
Matt Carlson0d3031d2007-10-10 18:02:43 -070017944err_out_apeunmap:
17945 if (tp->aperegs) {
17946 iounmap(tp->aperegs);
17947 tp->aperegs = NULL;
17948 }
17949
Linus Torvalds1da177e2005-04-16 15:20:36 -070017950err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017951 if (tp->regs) {
17952 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017953 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017955
17956err_out_free_dev:
17957 free_netdev(dev);
17958
17959err_out_free_res:
17960 pci_release_regions(pdev);
17961
17962err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080017963 if (pci_is_enabled(pdev))
17964 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017965 return err;
17966}
17967
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017968static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017969{
17970 struct net_device *dev = pci_get_drvdata(pdev);
17971
17972 if (dev) {
17973 struct tg3 *tp = netdev_priv(dev);
17974
Ivan Vecera20d14a52015-01-08 16:13:07 +010017975 tg3_ptp_fini(tp);
17976
Jesper Juhle3c55302012-04-09 22:50:15 +020017977 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017978
Matt Carlsondb219972011-11-04 09:15:03 +000017979 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017980
David S. Miller1805b2f2011-10-24 18:18:09 -040017981 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017982 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017983 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017984 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017985
Linus Torvalds1da177e2005-04-16 15:20:36 -070017986 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017987 if (tp->aperegs) {
17988 iounmap(tp->aperegs);
17989 tp->aperegs = NULL;
17990 }
Michael Chan68929142005-08-09 20:17:14 -070017991 if (tp->regs) {
17992 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017993 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017995 free_netdev(dev);
17996 pci_release_regions(pdev);
17997 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017998 }
17999}
18000
Eric Dumazetaa6027c2011-01-01 05:22:46 +000018001#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018002static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018003{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018004 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018005 struct net_device *dev = pci_get_drvdata(pdev);
18006 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018007 int err = 0;
18008
18009 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018010
18011 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018012 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018013
Matt Carlsondb219972011-11-04 09:15:03 +000018014 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018015 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018016 tg3_netif_stop(tp);
18017
Matt Carlson21f76382012-02-22 12:35:21 +000018018 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018019
David S. Millerf47c11e2005-06-24 20:18:35 -070018020 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018021 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070018022 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018023
18024 netif_device_detach(dev);
18025
David S. Millerf47c11e2005-06-24 20:18:35 -070018026 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070018027 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000018028 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070018029 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018030
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018031 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018032 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018033 int err2;
18034
David S. Millerf47c11e2005-06-24 20:18:35 -070018035 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018036
Joe Perches63c3a662011-04-26 08:12:10 +000018037 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018038 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018039 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070018040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018041
Matt Carlson21f76382012-02-22 12:35:21 +000018042 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018043
18044 netif_device_attach(dev);
18045 tg3_netif_start(tp);
18046
Michael Chanb9ec6c12006-07-25 16:37:27 -070018047out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018048 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018049
18050 if (!err2)
18051 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018052 }
18053
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018054unlock:
18055 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018056 return err;
18057}
18058
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018059static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018060{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018061 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018062 struct net_device *dev = pci_get_drvdata(pdev);
18063 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018064 int err = 0;
18065
18066 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018067
18068 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018069 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018070
Linus Torvalds1da177e2005-04-16 15:20:36 -070018071 netif_device_attach(dev);
18072
David S. Millerf47c11e2005-06-24 20:18:35 -070018073 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018074
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018075 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
18076
Joe Perches63c3a662011-04-26 08:12:10 +000018077 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000018078 err = tg3_restart_hw(tp,
18079 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070018080 if (err)
18081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018082
Matt Carlson21f76382012-02-22 12:35:21 +000018083 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018084
Linus Torvalds1da177e2005-04-16 15:20:36 -070018085 tg3_netif_start(tp);
18086
Michael Chanb9ec6c12006-07-25 16:37:27 -070018087out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018088 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018089
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018090 if (!err)
18091 tg3_phy_start(tp);
18092
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018093unlock:
18094 rtnl_unlock();
Michael Chanb9ec6c12006-07-25 16:37:27 -070018095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018096}
Fabio Estevam42df36a2013-04-16 09:28:29 +000018097#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018098
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018099static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
18100
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018101static void tg3_shutdown(struct pci_dev *pdev)
18102{
18103 struct net_device *dev = pci_get_drvdata(pdev);
18104 struct tg3 *tp = netdev_priv(dev);
18105
18106 rtnl_lock();
18107 netif_device_detach(dev);
18108
18109 if (netif_running(dev))
18110 dev_close(dev);
18111
18112 if (system_state == SYSTEM_POWER_OFF)
18113 tg3_power_down(tp);
18114
18115 rtnl_unlock();
18116}
18117
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018118/**
18119 * tg3_io_error_detected - called when PCI error is detected
18120 * @pdev: Pointer to PCI device
18121 * @state: The current pci connection state
18122 *
18123 * This function is called after a PCI bus error affecting
18124 * this device has been detected.
18125 */
18126static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
18127 pci_channel_state_t state)
18128{
18129 struct net_device *netdev = pci_get_drvdata(pdev);
18130 struct tg3 *tp = netdev_priv(netdev);
18131 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
18132
18133 netdev_info(netdev, "PCI I/O error detected\n");
18134
18135 rtnl_lock();
18136
Gavin Shandfc8f372015-04-24 15:22:23 +100018137 /* We needn't recover from permanent error */
18138 if (state == pci_channel_io_frozen)
18139 tp->pcierr_recovery = true;
Ivan Vecera0486a062014-09-01 14:21:57 +020018140
Gavin Shand8af4df2013-07-24 17:25:08 +080018141 /* We probably don't have netdev yet */
18142 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018143 goto done;
18144
18145 tg3_phy_stop(tp);
18146
18147 tg3_netif_stop(tp);
18148
Matt Carlson21f76382012-02-22 12:35:21 +000018149 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018150
18151 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000018152 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018153
18154 netif_device_detach(netdev);
18155
18156 /* Clean up software state, even if MMIO is blocked */
18157 tg3_full_lock(tp, 0);
18158 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
18159 tg3_full_unlock(tp);
18160
18161done:
Michael Chan72bb72b2013-06-17 13:47:25 -070018162 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018163 if (netdev) {
18164 tg3_napi_enable(tp);
18165 dev_close(netdev);
18166 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018167 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070018168 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018169 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070018170 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018171
18172 rtnl_unlock();
18173
18174 return err;
18175}
18176
18177/**
18178 * tg3_io_slot_reset - called after the pci bus has been reset.
18179 * @pdev: Pointer to PCI device
18180 *
18181 * Restart the card from scratch, as if from a cold-boot.
18182 * At this point, the card has exprienced a hard reset,
18183 * followed by fixups by BIOS, and has its config space
18184 * set up identically to what it was at cold boot.
18185 */
18186static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
18187{
18188 struct net_device *netdev = pci_get_drvdata(pdev);
18189 struct tg3 *tp = netdev_priv(netdev);
18190 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
18191 int err;
18192
18193 rtnl_lock();
18194
18195 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018196 dev_err(&pdev->dev,
18197 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018198 goto done;
18199 }
18200
18201 pci_set_master(pdev);
18202 pci_restore_state(pdev);
18203 pci_save_state(pdev);
18204
Daniel Borkmann68293092013-08-13 11:45:13 -070018205 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018206 rc = PCI_ERS_RESULT_RECOVERED;
18207 goto done;
18208 }
18209
18210 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000018211 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018212 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018213
18214 rc = PCI_ERS_RESULT_RECOVERED;
18215
18216done:
Daniel Borkmann68293092013-08-13 11:45:13 -070018217 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070018218 tg3_napi_enable(tp);
18219 dev_close(netdev);
18220 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018221 rtnl_unlock();
18222
18223 return rc;
18224}
18225
18226/**
18227 * tg3_io_resume - called when traffic can start flowing again.
18228 * @pdev: Pointer to PCI device
18229 *
18230 * This callback is called when the error recovery driver tells
18231 * us that its OK to resume normal operation.
18232 */
18233static void tg3_io_resume(struct pci_dev *pdev)
18234{
18235 struct net_device *netdev = pci_get_drvdata(pdev);
18236 struct tg3 *tp = netdev_priv(netdev);
18237 int err;
18238
18239 rtnl_lock();
18240
18241 if (!netif_running(netdev))
18242 goto done;
18243
18244 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018245 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000018246 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018247 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018248 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018249 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018250 netdev_err(netdev, "Cannot restart hardware after reset.\n");
18251 goto done;
18252 }
18253
18254 netif_device_attach(netdev);
18255
Matt Carlson21f76382012-02-22 12:35:21 +000018256 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018257
18258 tg3_netif_start(tp);
18259
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018260 tg3_full_unlock(tp);
18261
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018262 tg3_phy_start(tp);
18263
18264done:
Ivan Vecera0486a062014-09-01 14:21:57 +020018265 tp->pcierr_recovery = false;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018266 rtnl_unlock();
18267}
18268
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070018269static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018270 .error_detected = tg3_io_error_detected,
18271 .slot_reset = tg3_io_slot_reset,
18272 .resume = tg3_io_resume
18273};
18274
Linus Torvalds1da177e2005-04-16 15:20:36 -070018275static struct pci_driver tg3_driver = {
18276 .name = DRV_MODULE_NAME,
18277 .id_table = tg3_pci_tbl,
18278 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018279 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018280 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018281 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018282 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018283};
18284
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018285module_pci_driver(tg3_driver);