blob: 04e7d0d0e5b138e9e123c8b4ddfd59a191bf5da4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Michael Chande750e42014-05-11 20:22:55 -07007 * Copyright (C) 2005-2014 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000028#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Michael Chane565eec2014-01-03 10:09:12 -080039#include <linux/if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000047#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000048#include <linux/hwmon.h>
49#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030052#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000056#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Matt Carlsonbe947302012-12-03 19:36:57 +000058#include <uapi/linux/net_tstamp.h>
59#include <linux/ptp_clock_kernel.h>
60
David S. Miller49b6e95f2007-03-29 01:38:42 -070061#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070063#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
Matt Carlson63532392008-11-03 16:49:57 -080066#define BAR_0 0
67#define BAR_2 2
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include "tg3.h"
70
Joe Perches63c3a662011-04-26 08:12:10 +000071/* Functions & macros to verify TG3_FLAGS types */
72
73static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 return test_bit(flag, bits);
76}
77
78static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 set_bit(flag, bits);
81}
82
83static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
84{
85 clear_bit(flag, bits);
86}
87
88#define tg3_flag(tp, flag) \
89 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
90#define tg3_flag_set(tp, flag) \
91 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
92#define tg3_flag_clear(tp, flag) \
93 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000096#define TG3_MAJ_NUM 3
Michael Chande750e42014-05-11 20:22:55 -070097#define TG3_MIN_NUM 137
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chande750e42014-05-11 20:22:55 -0700100#define DRV_MODULE_RELDATE "May 11, 2014"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000102#define RESET_KIND_SHUTDOWN 0
103#define RESET_KIND_INIT 1
104#define RESET_KIND_SUSPEND 2
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_MODE 0
107#define TG3_DEF_TX_MODE 0
108#define TG3_DEF_MSG_ENABLE \
109 (NETIF_MSG_DRV | \
110 NETIF_MSG_PROBE | \
111 NETIF_MSG_LINK | \
112 NETIF_MSG_TIMER | \
113 NETIF_MSG_IFDOWN | \
114 NETIF_MSG_IFUP | \
115 NETIF_MSG_RX_ERR | \
116 NETIF_MSG_TX_ERR)
117
Matt Carlson520b2752011-06-13 13:39:02 +0000118#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* length of time before we decide the hardware is borked,
121 * and dev->tx_timeout() should be called to fix the problem
122 */
Joe Perches63c3a662011-04-26 08:12:10 +0000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_TX_TIMEOUT (5 * HZ)
125
126/* hardware minimum and maximum for a single frame's data payload */
127#define TG3_MIN_MTU 60
128#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* These numbers seem to be hard coded in the NIC firmware somehow.
132 * You can't change the ring sizes, but you can change where you place
133 * them in the NIC onboard memory.
134 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000135#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000137 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000139#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000140 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000141 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define TG3_DEF_RX_JUMBO_RING_PENDING 100
143
144/* Do not place this n-ring entries value into the tp struct itself,
145 * we really want to expose these constants to GCC so that modulo et
146 * al. operations are done with shifts and masks instead of with
147 * hw multiply/modulo instructions. Another solution would be to
148 * replace things like '% foo' with '& (foo - 1)'.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define TG3_TX_RING_SIZE 512
152#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
153
Matt Carlson2c49a442010-09-30 10:34:35 +0000154#define TG3_RX_STD_RING_BYTES(tp) \
155 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
156#define TG3_RX_JMB_RING_BYTES(tp) \
157 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
158#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000159 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
161 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
163
Matt Carlson287be122009-08-28 13:58:46 +0000164#define TG3_DMA_BYTE_ENAB 64
165
166#define TG3_RX_STD_DMA_SZ 1536
167#define TG3_RX_JMB_DMA_SZ 9046
168
169#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
170
171#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
172#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Matt Carlson2c49a442010-09-30 10:34:35 +0000174#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
175 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000176
Matt Carlson2c49a442010-09-30 10:34:35 +0000177#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
178 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000179
Matt Carlsond2757fc2010-04-12 06:58:27 +0000180/* Due to a hardware bug, the 5701 can only DMA to memory addresses
181 * that are at least dword aligned when used in PCIX mode. The driver
182 * works around this bug by double copying the packet. This workaround
183 * is built into the normal double copy length check for efficiency.
184 *
185 * However, the double copy is only necessary on those architectures
186 * where unaligned memory accesses are inefficient. For those architectures
187 * where unaligned memory accesses incur little penalty, we can reintegrate
188 * the 5701 in the normal rx path. Doing so saves a device structure
189 * dereference by hardcoding the double copy threshold in place.
190 */
191#define TG3_RX_COPY_THRESHOLD 256
192#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
193 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
194#else
195 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
196#endif
197
Matt Carlson81389f52011-08-31 11:44:49 +0000198#if (NET_IP_ALIGN != 0)
199#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
200#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000201#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000202#endif
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000205#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000206#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000207#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Matt Carlsonad829262008-11-21 17:16:16 -0800209#define TG3_RAW_IP_ALIGN 2
210
Michael Chane565eec2014-01-03 10:09:12 -0800211#define TG3_MAX_UCAST_ADDR(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 3)
212#define TG3_UCAST_ADDR_IDX(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 1)
213
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000214#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000215#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000216
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800217#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000218#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800219#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
220#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
221
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500222static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000223 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
226MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
227MODULE_LICENSE("GPL");
228MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800229MODULE_FIRMWARE(FIRMWARE_TG3);
230MODULE_FIRMWARE(FIRMWARE_TG3TSO);
231MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
234module_param(tg3_debug, int, 0);
235MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
236
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000237#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
238#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
239
Benoit Taine9baa3c32014-08-08 15:56:03 +0200240static const struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
260 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
261 TG3_DRV_DATA_FLAG_5705_10_100},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
263 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
264 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
267 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
268 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
275 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
281 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000289 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
290 PCI_VENDOR_ID_LENOVO,
291 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
292 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
295 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
311 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000312 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
313 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000314 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
315 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
316 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
317 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
318 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
319 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
323 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
333 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
335 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000340 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
341 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
342 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Nithin Sujir68273712013-09-20 16:46:56 -0700343 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
344 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
345 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
346 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
347 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700348 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
349 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
350 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
351 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
352 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
353 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
354 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000355 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700356 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
359MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
360
Andreas Mohr50da8592006-08-14 23:54:30 -0700361static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000363} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 { "rx_octets" },
365 { "rx_fragments" },
366 { "rx_ucast_packets" },
367 { "rx_mcast_packets" },
368 { "rx_bcast_packets" },
369 { "rx_fcs_errors" },
370 { "rx_align_errors" },
371 { "rx_xon_pause_rcvd" },
372 { "rx_xoff_pause_rcvd" },
373 { "rx_mac_ctrl_rcvd" },
374 { "rx_xoff_entered" },
375 { "rx_frame_too_long_errors" },
376 { "rx_jabbers" },
377 { "rx_undersize_packets" },
378 { "rx_in_length_errors" },
379 { "rx_out_length_errors" },
380 { "rx_64_or_less_octet_packets" },
381 { "rx_65_to_127_octet_packets" },
382 { "rx_128_to_255_octet_packets" },
383 { "rx_256_to_511_octet_packets" },
384 { "rx_512_to_1023_octet_packets" },
385 { "rx_1024_to_1522_octet_packets" },
386 { "rx_1523_to_2047_octet_packets" },
387 { "rx_2048_to_4095_octet_packets" },
388 { "rx_4096_to_8191_octet_packets" },
389 { "rx_8192_to_9022_octet_packets" },
390
391 { "tx_octets" },
392 { "tx_collisions" },
393
394 { "tx_xon_sent" },
395 { "tx_xoff_sent" },
396 { "tx_flow_control" },
397 { "tx_mac_errors" },
398 { "tx_single_collisions" },
399 { "tx_mult_collisions" },
400 { "tx_deferred" },
401 { "tx_excessive_collisions" },
402 { "tx_late_collisions" },
403 { "tx_collide_2times" },
404 { "tx_collide_3times" },
405 { "tx_collide_4times" },
406 { "tx_collide_5times" },
407 { "tx_collide_6times" },
408 { "tx_collide_7times" },
409 { "tx_collide_8times" },
410 { "tx_collide_9times" },
411 { "tx_collide_10times" },
412 { "tx_collide_11times" },
413 { "tx_collide_12times" },
414 { "tx_collide_13times" },
415 { "tx_collide_14times" },
416 { "tx_collide_15times" },
417 { "tx_ucast_packets" },
418 { "tx_mcast_packets" },
419 { "tx_bcast_packets" },
420 { "tx_carrier_sense_errors" },
421 { "tx_discards" },
422 { "tx_errors" },
423
424 { "dma_writeq_full" },
425 { "dma_write_prioq_full" },
426 { "rxbds_empty" },
427 { "rx_discards" },
428 { "rx_errors" },
429 { "rx_threshold_hit" },
430
431 { "dma_readq_full" },
432 { "dma_read_prioq_full" },
433 { "tx_comp_queue_full" },
434
435 { "ring_set_send_prod_index" },
436 { "ring_status_update" },
437 { "nic_irqs" },
438 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000439 { "nic_tx_threshold_hit" },
440
441 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442};
443
Matt Carlson48fa55a2011-04-13 11:05:06 +0000444#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000445#define TG3_NVRAM_TEST 0
446#define TG3_LINK_TEST 1
447#define TG3_REGISTER_TEST 2
448#define TG3_MEMORY_TEST 3
449#define TG3_MAC_LOOPB_TEST 4
450#define TG3_PHY_LOOPB_TEST 5
451#define TG3_EXT_LOOPB_TEST 6
452#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000453
454
Andreas Mohr50da8592006-08-14 23:54:30 -0700455static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700456 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000457} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000458 [TG3_NVRAM_TEST] = { "nvram test (online) " },
459 [TG3_LINK_TEST] = { "link test (online) " },
460 [TG3_REGISTER_TEST] = { "register test (offline)" },
461 [TG3_MEMORY_TEST] = { "memory test (offline)" },
462 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
463 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
464 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
465 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700466};
467
Matt Carlson48fa55a2011-04-13 11:05:06 +0000468#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
469
470
Michael Chanb401e9e2005-12-19 16:27:04 -0800471static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
472{
473 writel(val, tp->regs + off);
474}
475
476static u32 tg3_read32(struct tg3 *tp, u32 off)
477{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000478 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800479}
480
Matt Carlson0d3031d2007-10-10 18:02:43 -0700481static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
482{
483 writel(val, tp->aperegs + off);
484}
485
486static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
487{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000488 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
492{
Michael Chan68929142005-08-09 20:17:14 -0700493 unsigned long flags;
494
495 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700496 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
497 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700498 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700499}
500
501static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
502{
503 writel(val, tp->regs + off);
504 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Michael Chan68929142005-08-09 20:17:14 -0700507static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
508{
509 unsigned long flags;
510 u32 val;
511
512 spin_lock_irqsave(&tp->indirect_lock, flags);
513 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
514 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
515 spin_unlock_irqrestore(&tp->indirect_lock, flags);
516 return val;
517}
518
519static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
520{
521 unsigned long flags;
522
523 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
524 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
525 TG3_64BIT_REG_LOW, val);
526 return;
527 }
Matt Carlson66711e62009-11-13 13:03:49 +0000528 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700529 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
530 TG3_64BIT_REG_LOW, val);
531 return;
532 }
533
534 spin_lock_irqsave(&tp->indirect_lock, flags);
535 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
536 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
537 spin_unlock_irqrestore(&tp->indirect_lock, flags);
538
539 /* In indirect mode when disabling interrupts, we also need
540 * to clear the interrupt bit in the GRC local ctrl register.
541 */
542 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
543 (val == 0x1)) {
544 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
545 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
546 }
547}
548
549static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
550{
551 unsigned long flags;
552 u32 val;
553
554 spin_lock_irqsave(&tp->indirect_lock, flags);
555 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
556 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
557 spin_unlock_irqrestore(&tp->indirect_lock, flags);
558 return val;
559}
560
Michael Chanb401e9e2005-12-19 16:27:04 -0800561/* usec_wait specifies the wait time in usec when writing to certain registers
562 * where it is unsafe to read back the register without some delay.
563 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
564 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
565 */
566static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Joe Perches63c3a662011-04-26 08:12:10 +0000568 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800569 /* Non-posted methods */
570 tp->write32(tp, off, val);
571 else {
572 /* Posted method */
573 tg3_write32(tp, off, val);
574 if (usec_wait)
575 udelay(usec_wait);
576 tp->read32(tp, off);
577 }
578 /* Wait again after the read for the posted method to guarantee that
579 * the wait time is met.
580 */
581 if (usec_wait)
582 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Michael Chan09ee9292005-08-09 20:17:00 -0700585static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
586{
587 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000588 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
589 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
590 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700591 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700592}
593
Michael Chan20094932005-08-09 20:16:32 -0700594static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 void __iomem *mbox = tp->regs + off;
597 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000598 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000600 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
601 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 readl(mbox);
603}
604
Michael Chanb5d37722006-09-27 16:06:21 -0700605static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
606{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000607 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700608}
609
610static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
611{
612 writel(val, tp->regs + off + GRCMBOX_BASE);
613}
614
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000615#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700616#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000617#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
618#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
619#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700620
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000621#define tw32(reg, val) tp->write32(tp, reg, val)
622#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
623#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
624#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
627{
Michael Chan68929142005-08-09 20:17:14 -0700628 unsigned long flags;
629
Joe Perches41535772013-02-16 11:20:04 +0000630 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700631 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
632 return;
633
Michael Chan68929142005-08-09 20:17:14 -0700634 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000635 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700636 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
637 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Michael Chanbbadf502006-04-06 21:46:34 -0700639 /* Always leave this as zero. */
640 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
641 } else {
642 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
643 tw32_f(TG3PCI_MEM_WIN_DATA, val);
644
645 /* Always leave this as zero. */
646 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
647 }
Michael Chan68929142005-08-09 20:17:14 -0700648 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
652{
Michael Chan68929142005-08-09 20:17:14 -0700653 unsigned long flags;
654
Joe Perches41535772013-02-16 11:20:04 +0000655 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700656 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
657 *val = 0;
658 return;
659 }
660
Michael Chan68929142005-08-09 20:17:14 -0700661 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000662 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700663 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
664 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Michael Chanbbadf502006-04-06 21:46:34 -0700666 /* Always leave this as zero. */
667 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
668 } else {
669 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
670 *val = tr32(TG3PCI_MEM_WIN_DATA);
671
672 /* Always leave this as zero. */
673 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
674 }
Michael Chan68929142005-08-09 20:17:14 -0700675 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Matt Carlson0d3031d2007-10-10 18:02:43 -0700678static void tg3_ape_lock_init(struct tg3 *tp)
679{
680 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000681 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000682
Joe Perches41535772013-02-16 11:20:04 +0000683 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000684 regbase = TG3_APE_LOCK_GRANT;
685 else
686 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700687
688 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000689 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
690 switch (i) {
691 case TG3_APE_LOCK_PHY0:
692 case TG3_APE_LOCK_PHY1:
693 case TG3_APE_LOCK_PHY2:
694 case TG3_APE_LOCK_PHY3:
695 bit = APE_LOCK_GRANT_DRIVER;
696 break;
697 default:
698 if (!tp->pci_fn)
699 bit = APE_LOCK_GRANT_DRIVER;
700 else
701 bit = 1 << tp->pci_fn;
702 }
703 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000704 }
705
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706}
707
708static int tg3_ape_lock(struct tg3 *tp, int locknum)
709{
710 int i, off;
711 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700713
Joe Perches63c3a662011-04-26 08:12:10 +0000714 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700715 return 0;
716
717 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000718 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000719 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000720 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000721 case TG3_APE_LOCK_GRC:
722 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000723 if (!tp->pci_fn)
724 bit = APE_LOCK_REQ_DRIVER;
725 else
726 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000727 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000728 case TG3_APE_LOCK_PHY0:
729 case TG3_APE_LOCK_PHY1:
730 case TG3_APE_LOCK_PHY2:
731 case TG3_APE_LOCK_PHY3:
732 bit = APE_LOCK_REQ_DRIVER;
733 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000734 default:
735 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700736 }
737
Joe Perches41535772013-02-16 11:20:04 +0000738 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000739 req = TG3_APE_LOCK_REQ;
740 gnt = TG3_APE_LOCK_GRANT;
741 } else {
742 req = TG3_APE_PER_LOCK_REQ;
743 gnt = TG3_APE_PER_LOCK_GRANT;
744 }
745
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746 off = 4 * locknum;
747
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000748 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700749
750 /* Wait for up to 1 millisecond to acquire lock. */
751 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000752 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000753 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700754 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800755 if (pci_channel_offline(tp->pdev))
756 break;
757
Matt Carlson0d3031d2007-10-10 18:02:43 -0700758 udelay(10);
759 }
760
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000761 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700762 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000763 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700764 ret = -EBUSY;
765 }
766
767 return ret;
768}
769
770static void tg3_ape_unlock(struct tg3 *tp, int locknum)
771{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000772 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700773
Joe Perches63c3a662011-04-26 08:12:10 +0000774 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700775 return;
776
777 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000778 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000779 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000780 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000781 case TG3_APE_LOCK_GRC:
782 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000783 if (!tp->pci_fn)
784 bit = APE_LOCK_GRANT_DRIVER;
785 else
786 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000787 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000788 case TG3_APE_LOCK_PHY0:
789 case TG3_APE_LOCK_PHY1:
790 case TG3_APE_LOCK_PHY2:
791 case TG3_APE_LOCK_PHY3:
792 bit = APE_LOCK_GRANT_DRIVER;
793 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000794 default:
795 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700796 }
797
Joe Perches41535772013-02-16 11:20:04 +0000798 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000799 gnt = TG3_APE_LOCK_GRANT;
800 else
801 gnt = TG3_APE_PER_LOCK_GRANT;
802
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000803 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700804}
805
Matt Carlsonb65a3722012-07-16 16:24:00 +0000806static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000807{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000808 u32 apedata;
809
Matt Carlsonb65a3722012-07-16 16:24:00 +0000810 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000811 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000812 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000813
814 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000815 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
816 break;
817
Matt Carlsonb65a3722012-07-16 16:24:00 +0000818 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
819
820 udelay(10);
821 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000822 }
823
Matt Carlsonb65a3722012-07-16 16:24:00 +0000824 return timeout_us ? 0 : -EBUSY;
825}
826
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000827static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
828{
829 u32 i, apedata;
830
831 for (i = 0; i < timeout_us / 10; i++) {
832 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
833
834 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
835 break;
836
837 udelay(10);
838 }
839
840 return i == timeout_us / 10;
841}
842
Michael Chan86449942012-10-02 20:31:14 -0700843static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
844 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000845{
846 int err;
847 u32 i, bufoff, msgoff, maxlen, apedata;
848
849 if (!tg3_flag(tp, APE_HAS_NCSI))
850 return 0;
851
852 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
853 if (apedata != APE_SEG_SIG_MAGIC)
854 return -ENODEV;
855
856 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
857 if (!(apedata & APE_FW_STATUS_READY))
858 return -EAGAIN;
859
860 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
861 TG3_APE_SHMEM_BASE;
862 msgoff = bufoff + 2 * sizeof(u32);
863 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
864
865 while (len) {
866 u32 length;
867
868 /* Cap xfer sizes to scratchpad limits. */
869 length = (len > maxlen) ? maxlen : len;
870 len -= length;
871
872 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
873 if (!(apedata & APE_FW_STATUS_READY))
874 return -EAGAIN;
875
876 /* Wait for up to 1 msec for APE to service previous event. */
877 err = tg3_ape_event_lock(tp, 1000);
878 if (err)
879 return err;
880
881 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
882 APE_EVENT_STATUS_SCRTCHPD_READ |
883 APE_EVENT_STATUS_EVENT_PENDING;
884 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
885
886 tg3_ape_write32(tp, bufoff, base_off);
887 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
888
889 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
890 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
891
892 base_off += length;
893
894 if (tg3_ape_wait_for_event(tp, 30000))
895 return -EAGAIN;
896
897 for (i = 0; length; i += 4, length -= 4) {
898 u32 val = tg3_ape_read32(tp, msgoff + i);
899 memcpy(data, &val, sizeof(u32));
900 data++;
901 }
902 }
903
904 return 0;
905}
906
Matt Carlsonb65a3722012-07-16 16:24:00 +0000907static int tg3_ape_send_event(struct tg3 *tp, u32 event)
908{
909 int err;
910 u32 apedata;
911
912 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
913 if (apedata != APE_SEG_SIG_MAGIC)
914 return -EAGAIN;
915
916 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
917 if (!(apedata & APE_FW_STATUS_READY))
918 return -EAGAIN;
919
920 /* Wait for up to 1 millisecond for APE to service previous event. */
921 err = tg3_ape_event_lock(tp, 1000);
922 if (err)
923 return err;
924
925 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
926 event | APE_EVENT_STATUS_EVENT_PENDING);
927
928 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
929 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
930
931 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000932}
933
934static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
935{
936 u32 event;
937 u32 apedata;
938
939 if (!tg3_flag(tp, ENABLE_APE))
940 return;
941
942 switch (kind) {
943 case RESET_KIND_INIT:
944 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
945 APE_HOST_SEG_SIG_MAGIC);
946 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
947 APE_HOST_SEG_LEN_MAGIC);
948 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
949 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
950 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
951 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
952 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
953 APE_HOST_BEHAV_NO_PHYLOCK);
954 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
955 TG3_APE_HOST_DRVR_STATE_START);
956
957 event = APE_EVENT_STATUS_STATE_START;
958 break;
959 case RESET_KIND_SHUTDOWN:
960 /* With the interface we are currently using,
961 * APE does not track driver state. Wiping
962 * out the HOST SEGMENT SIGNATURE forces
963 * the APE to assume OS absent status.
964 */
965 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
966
967 if (device_may_wakeup(&tp->pdev->dev) &&
968 tg3_flag(tp, WOL_ENABLE)) {
969 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
970 TG3_APE_HOST_WOL_SPEED_AUTO);
971 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
972 } else
973 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
974
975 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
976
977 event = APE_EVENT_STATUS_STATE_UNLOAD;
978 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000979 default:
980 return;
981 }
982
983 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
984
985 tg3_ape_send_event(tp, event);
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static void tg3_disable_ints(struct tg3 *tp)
989{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000990 int i;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 tw32(TG3PCI_MISC_HOST_CTRL,
993 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000994 for (i = 0; i < tp->irq_max; i++)
995 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998static void tg3_enable_ints(struct tg3 *tp)
999{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001000 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001
Michael Chanbbe832c2005-06-24 20:20:04 -07001002 tp->irq_sync = 0;
1003 wmb();
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 tw32(TG3PCI_MISC_HOST_CTRL,
1006 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001007
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001008 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 for (i = 0; i < tp->irq_cnt; i++) {
1010 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001011
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001012 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001013 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001014 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1015
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001016 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001017 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001018
1019 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001020 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001021 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1022 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1023 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001024 tw32(HOSTCC_MODE, tp->coal_now);
1025
1026 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Matt Carlson17375d22009-08-28 14:02:18 +00001029static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001030{
Matt Carlson17375d22009-08-28 14:02:18 +00001031 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001032 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001033 unsigned int work_exists = 0;
1034
1035 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001036 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001037 if (sblk->status & SD_STATUS_LINK_CHG)
1038 work_exists = 1;
1039 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001040
1041 /* check for TX work to do */
1042 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1043 work_exists = 1;
1044
1045 /* check for RX work to do */
1046 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001047 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001048 work_exists = 1;
1049
1050 return work_exists;
1051}
1052
Matt Carlson17375d22009-08-28 14:02:18 +00001053/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001054 * similar to tg3_enable_ints, but it accurately determines whether there
1055 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001056 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Matt Carlson17375d22009-08-28 14:02:18 +00001058static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Matt Carlson17375d22009-08-28 14:02:18 +00001060 struct tg3 *tp = tnapi->tp;
1061
Matt Carlson898a56f2009-08-28 14:02:40 +00001062 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 mmiowb();
1064
David S. Millerfac9b832005-05-18 22:46:34 -07001065 /* When doing tagged status, this work check is unnecessary.
1066 * The last_tag we write above tells the chip which piece of
1067 * work we've completed.
1068 */
Joe Perches63c3a662011-04-26 08:12:10 +00001069 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001070 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001071 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static void tg3_switch_clocks(struct tg3 *tp)
1075{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001076 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 u32 orig_clock_ctrl;
1078
Joe Perches63c3a662011-04-26 08:12:10 +00001079 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001080 return;
1081
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001082 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 orig_clock_ctrl = clock_ctrl;
1085 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1086 CLOCK_CTRL_CLKRUN_OENABLE |
1087 0x1f);
1088 tp->pci_clock_ctrl = clock_ctrl;
1089
Joe Perches63c3a662011-04-26 08:12:10 +00001090 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001092 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1093 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001096 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1097 clock_ctrl |
1098 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1099 40);
1100 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1101 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1102 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001104 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107#define PHY_BUSY_LOOPS 5000
1108
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001109static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1110 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 u32 frame_val;
1113 unsigned int loops;
1114 int ret;
1115
1116 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1117 tw32_f(MAC_MI_MODE,
1118 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1119 udelay(80);
1120 }
1121
Michael Chan8151ad52012-07-29 19:15:41 +00001122 tg3_ape_lock(tp, tp->phy_ape_lock);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *val = 0x0;
1125
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001126 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 MI_COM_PHY_ADDR_MASK);
1128 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1129 MI_COM_REG_ADDR_MASK);
1130 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 tw32_f(MAC_MI_COM, frame_val);
1133
1134 loops = PHY_BUSY_LOOPS;
1135 while (loops != 0) {
1136 udelay(10);
1137 frame_val = tr32(MAC_MI_COM);
1138
1139 if ((frame_val & MI_COM_BUSY) == 0) {
1140 udelay(5);
1141 frame_val = tr32(MAC_MI_COM);
1142 break;
1143 }
1144 loops -= 1;
1145 }
1146
1147 ret = -EBUSY;
1148 if (loops != 0) {
1149 *val = frame_val & MI_COM_DATA_MASK;
1150 ret = 0;
1151 }
1152
1153 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1154 tw32_f(MAC_MI_MODE, tp->mi_mode);
1155 udelay(80);
1156 }
1157
Michael Chan8151ad52012-07-29 19:15:41 +00001158 tg3_ape_unlock(tp, tp->phy_ape_lock);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return ret;
1161}
1162
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001163static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1164{
1165 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1166}
1167
1168static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1169 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 u32 frame_val;
1172 unsigned int loops;
1173 int ret;
1174
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001175 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001176 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001177 return 0;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1180 tw32_f(MAC_MI_MODE,
1181 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1182 udelay(80);
1183 }
1184
Michael Chan8151ad52012-07-29 19:15:41 +00001185 tg3_ape_lock(tp, tp->phy_ape_lock);
1186
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001187 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 MI_COM_PHY_ADDR_MASK);
1189 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1190 MI_COM_REG_ADDR_MASK);
1191 frame_val |= (val & MI_COM_DATA_MASK);
1192 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 tw32_f(MAC_MI_COM, frame_val);
1195
1196 loops = PHY_BUSY_LOOPS;
1197 while (loops != 0) {
1198 udelay(10);
1199 frame_val = tr32(MAC_MI_COM);
1200 if ((frame_val & MI_COM_BUSY) == 0) {
1201 udelay(5);
1202 frame_val = tr32(MAC_MI_COM);
1203 break;
1204 }
1205 loops -= 1;
1206 }
1207
1208 ret = -EBUSY;
1209 if (loops != 0)
1210 ret = 0;
1211
1212 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1213 tw32_f(MAC_MI_MODE, tp->mi_mode);
1214 udelay(80);
1215 }
1216
Michael Chan8151ad52012-07-29 19:15:41 +00001217 tg3_ape_unlock(tp, tp->phy_ape_lock);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return ret;
1220}
1221
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001222static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1223{
1224 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1225}
1226
Matt Carlsonb0988c12011-04-20 07:57:39 +00001227static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1228{
1229 int err;
1230
1231 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1232 if (err)
1233 goto done;
1234
1235 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1236 if (err)
1237 goto done;
1238
1239 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1240 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1241 if (err)
1242 goto done;
1243
1244 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1245
1246done:
1247 return err;
1248}
1249
1250static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1251{
1252 int err;
1253
1254 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1255 if (err)
1256 goto done;
1257
1258 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1259 if (err)
1260 goto done;
1261
1262 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1263 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1264 if (err)
1265 goto done;
1266
1267 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1268
1269done:
1270 return err;
1271}
1272
1273static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1274{
1275 int err;
1276
1277 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1278 if (!err)
1279 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1280
1281 return err;
1282}
1283
1284static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1285{
1286 int err;
1287
1288 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1289 if (!err)
1290 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1291
1292 return err;
1293}
1294
Matt Carlson15ee95c2011-04-20 07:57:40 +00001295static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1296{
1297 int err;
1298
1299 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1300 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1301 MII_TG3_AUXCTL_SHDWSEL_MISC);
1302 if (!err)
1303 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1304
1305 return err;
1306}
1307
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001308static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1309{
1310 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1311 set |= MII_TG3_AUXCTL_MISC_WREN;
1312
1313 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1314}
1315
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001316static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1317{
1318 u32 val;
1319 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001320
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001321 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1322
1323 if (err)
1324 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001325
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001326 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001327 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1328 else
1329 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1330
1331 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1332 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1333
1334 return err;
1335}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001336
Nithin Sujir3ab71072013-09-20 16:46:55 -07001337static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1338{
1339 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1340 reg | val | MII_TG3_MISC_SHDW_WREN);
1341}
1342
Matt Carlson95e28692008-05-25 23:44:14 -07001343static int tg3_bmcr_reset(struct tg3 *tp)
1344{
1345 u32 phy_control;
1346 int limit, err;
1347
1348 /* OK, reset it, and poll the BMCR_RESET bit until it
1349 * clears or we time out.
1350 */
1351 phy_control = BMCR_RESET;
1352 err = tg3_writephy(tp, MII_BMCR, phy_control);
1353 if (err != 0)
1354 return -EBUSY;
1355
1356 limit = 5000;
1357 while (limit--) {
1358 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1359 if (err != 0)
1360 return -EBUSY;
1361
1362 if ((phy_control & BMCR_RESET) == 0) {
1363 udelay(40);
1364 break;
1365 }
1366 udelay(10);
1367 }
Roel Kluind4675b52009-02-12 16:33:27 -08001368 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001369 return -EBUSY;
1370
1371 return 0;
1372}
1373
Matt Carlson158d7ab2008-05-29 01:37:54 -07001374static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1375{
Francois Romieu3d165432009-01-19 16:56:50 -08001376 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001377 u32 val;
1378
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001379 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001380
Hauke Mehrtensead24022013-09-28 23:15:26 +02001381 if (__tg3_readphy(tp, mii_id, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001382 val = -EIO;
1383
1384 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001385
1386 return val;
1387}
1388
1389static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1390{
Francois Romieu3d165432009-01-19 16:56:50 -08001391 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001392 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001393
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001394 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001395
Hauke Mehrtensead24022013-09-28 23:15:26 +02001396 if (__tg3_writephy(tp, mii_id, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001397 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001398
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001399 spin_unlock_bh(&tp->lock);
1400
1401 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001402}
1403
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001404static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001405{
1406 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001407 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001408
Hauke Mehrtensead24022013-09-28 23:15:26 +02001409 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001410 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001411 case PHY_ID_BCM50610:
1412 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001413 val = MAC_PHYCFG2_50610_LED_MODES;
1414 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001415 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001416 val = MAC_PHYCFG2_AC131_LED_MODES;
1417 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001418 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001419 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1420 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001421 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001422 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1423 break;
1424 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001425 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001426 }
1427
1428 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1429 tw32(MAC_PHYCFG2, val);
1430
1431 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001432 val &= ~(MAC_PHYCFG1_RGMII_INT |
1433 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1434 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001435 tw32(MAC_PHYCFG1, val);
1436
1437 return;
1438 }
1439
Joe Perches63c3a662011-04-26 08:12:10 +00001440 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001441 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1442 MAC_PHYCFG2_FMODE_MASK_MASK |
1443 MAC_PHYCFG2_GMODE_MASK_MASK |
1444 MAC_PHYCFG2_ACT_MASK_MASK |
1445 MAC_PHYCFG2_QUAL_MASK_MASK |
1446 MAC_PHYCFG2_INBAND_ENABLE;
1447
1448 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001449
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001450 val = tr32(MAC_PHYCFG1);
1451 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1452 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001453 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1454 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001455 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001456 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001457 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1458 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001459 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1460 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1461 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001462
Matt Carlsona9daf362008-05-25 23:49:44 -07001463 val = tr32(MAC_EXT_RGMII_MODE);
1464 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1465 MAC_RGMII_MODE_RX_QUALITY |
1466 MAC_RGMII_MODE_RX_ACTIVITY |
1467 MAC_RGMII_MODE_RX_ENG_DET |
1468 MAC_RGMII_MODE_TX_ENABLE |
1469 MAC_RGMII_MODE_TX_LOWPWR |
1470 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001471 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1472 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001473 val |= MAC_RGMII_MODE_RX_INT_B |
1474 MAC_RGMII_MODE_RX_QUALITY |
1475 MAC_RGMII_MODE_RX_ACTIVITY |
1476 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001477 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001478 val |= MAC_RGMII_MODE_TX_ENABLE |
1479 MAC_RGMII_MODE_TX_LOWPWR |
1480 MAC_RGMII_MODE_TX_RESET;
1481 }
1482 tw32(MAC_EXT_RGMII_MODE, val);
1483}
1484
Matt Carlson158d7ab2008-05-29 01:37:54 -07001485static void tg3_mdio_start(struct tg3 *tp)
1486{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001487 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1488 tw32_f(MAC_MI_MODE, tp->mi_mode);
1489 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001490
Joe Perches63c3a662011-04-26 08:12:10 +00001491 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001492 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001493 tg3_mdio_config_5785(tp);
1494}
1495
1496static int tg3_mdio_init(struct tg3 *tp)
1497{
1498 int i;
1499 u32 reg;
1500 struct phy_device *phydev;
1501
Joe Perches63c3a662011-04-26 08:12:10 +00001502 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001503 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001504
Matt Carlson69f11c92011-07-13 09:27:30 +00001505 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001506
Joe Perches41535772013-02-16 11:20:04 +00001507 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001508 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1509 else
1510 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1511 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001512 if (is_serdes)
1513 tp->phy_addr += 7;
Hauke Mehrtensee002b62013-09-28 23:15:28 +02001514 } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
1515 int addr;
1516
1517 addr = ssb_gige_get_phyaddr(tp->pdev);
1518 if (addr < 0)
1519 return addr;
1520 tp->phy_addr = addr;
Matt Carlson882e9792009-09-01 13:21:36 +00001521 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001522 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001523
Matt Carlson158d7ab2008-05-29 01:37:54 -07001524 tg3_mdio_start(tp);
1525
Joe Perches63c3a662011-04-26 08:12:10 +00001526 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001527 return 0;
1528
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001529 tp->mdio_bus = mdiobus_alloc();
1530 if (tp->mdio_bus == NULL)
1531 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001532
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001533 tp->mdio_bus->name = "tg3 mdio bus";
1534 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001535 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001536 tp->mdio_bus->priv = tp;
1537 tp->mdio_bus->parent = &tp->pdev->dev;
1538 tp->mdio_bus->read = &tg3_mdio_read;
1539 tp->mdio_bus->write = &tg3_mdio_write;
Hauke Mehrtensead24022013-09-28 23:15:26 +02001540 tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
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 Buytenhek298cf9b2008-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
Hauke Mehrtensead24022013-09-28 23:15:26 +02001557 phydev = tp->mdio_bus->phy_map[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 Buytenhek298cf9b2008-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))
Hauke Mehrtensead24022013-09-28 23:15:26 +02001967 autoneg = tp->mdio_bus->phy_map[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);
Hauke Mehrtensead24022013-09-28 23:15:26 +02002003 struct phy_device *phydev = tp->mdio_bus->phy_map[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
Hauke Mehrtensead24022013-09-28 23:15:26 +02002092 phydev = tp->mdio_bus->phy_map[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:
Hauke Mehrtensead24022013-09-28 23:15:26 +02002119 phy_disconnect(tp->mdio_bus->phy_map[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
Hauke Mehrtensead24022013-09-28 23:15:26 +02002139 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002140
Matt Carlson80096062010-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
Hauke Mehrtensead24022013-09-28 23:15:26 +02002159 phy_stop(tp->mdio_bus->phy_map[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) {
Hauke Mehrtensead24022013-09-28 23:15:26 +02002165 phy_disconnect(tp->mdio_bus->phy_map[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 Carlson80096062010-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
Hauke Mehrtensead24022013-09-28 23:15:26 +02004049 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004050
Matt Carlson80096062010-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 Carlson80096062010-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{
7386 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7387 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
Matt Carlson2ffcc982011-05-19 12:12:44 +00007834static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007835
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007836/* Use GSO to workaround all TSO packets that meet HW bug conditions
7837 * indicated in tg3_tx_frag_set()
Michael Chan52c0fd82006-06-29 20:15:54 -07007838 */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007839static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
7840 struct netdev_queue *txq, struct sk_buff *skb)
Michael Chan52c0fd82006-06-29 20:15:54 -07007841{
7842 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007843 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007844
7845 /* Estimate the number of fragments in the worst case */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007846 if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
7847 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007848
7849 /* netif_tx_stop_queue() must be done before checking
7850 * checking tx index in tg3_tx_avail() below, because in
7851 * tg3_tx(), we update tx index before checking for
7852 * netif_tx_queue_stopped().
7853 */
7854 smp_mb();
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007855 if (tg3_tx_avail(tnapi) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007856 return NETDEV_TX_BUSY;
7857
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007858 netif_tx_wake_queue(txq);
Michael Chan52c0fd82006-06-29 20:15:54 -07007859 }
7860
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007861 segs = skb_gso_segment(skb, tp->dev->features &
7862 ~(NETIF_F_TSO | NETIF_F_TSO6));
Prashant Sreedharan40c1dea2014-06-18 18:38:13 -07007863 if (IS_ERR(segs) || !segs)
Michael Chan52c0fd82006-06-29 20:15:54 -07007864 goto tg3_tso_bug_end;
7865
7866 do {
7867 nskb = segs;
7868 segs = segs->next;
7869 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007870 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007871 } while (segs);
7872
7873tg3_tso_bug_end:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007874 dev_kfree_skb_any(skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007875
7876 return NETDEV_TX_OK;
7877}
Michael Chan52c0fd82006-06-29 20:15:54 -07007878
Michael Chand71c0dc2014-05-11 20:22:53 -07007879/* hard_start_xmit for all devices */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007880static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007881{
7882 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007883 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007884 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007885 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007886 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007887 struct tg3_napi *tnapi;
7888 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007889 unsigned int last;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007890 struct iphdr *iph = NULL;
7891 struct tcphdr *tcph = NULL;
7892 __sum16 tcp_csum = 0, ip_csum = 0;
7893 __be16 ip_tot_len = 0;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007894
Matt Carlson24f4efd2009-11-13 13:03:35 +00007895 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7896 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007897 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007898 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899
Matt Carlson84b67b22011-07-27 14:20:52 +00007900 budget = tg3_tx_avail(tnapi);
7901
Michael Chan00b70502006-06-17 21:58:45 -07007902 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007903 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007904 * interrupt. Furthermore, IRQ processing runs lockless so we have
7905 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007907 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007908 if (!netif_tx_queue_stopped(txq)) {
7909 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007910
7911 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007912 netdev_err(dev,
7913 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 return NETDEV_TX_BUSY;
7916 }
7917
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007918 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 base_flags = 0;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007920
Matt Carlsonbe98da62010-07-11 09:31:46 +00007921 mss = skb_shinfo(skb)->gso_size;
7922 if (mss) {
Matt Carlson34195c32010-07-11 09:31:42 +00007923 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924
françois romieu105dcb52014-03-29 12:26:29 +01007925 if (skb_cow_head(skb, 0))
Eric Dumazet48855432011-10-24 07:53:03 +00007926 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
Matt Carlson34195c32010-07-11 09:31:42 +00007928 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007929 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
Eric Dumazeta5a11952012-01-23 01:22:09 +00007931 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007932
Vlad Yasevich476c1882014-09-18 10:31:17 -04007933 /* HW/FW can not correctly segment packets that have been
7934 * vlan encapsulated.
7935 */
7936 if (skb->protocol == htons(ETH_P_8021Q) ||
7937 skb->protocol == htons(ETH_P_8021AD))
7938 return tg3_tso_bug(tp, tnapi, txq, skb);
7939
Eric Dumazeta5a11952012-01-23 01:22:09 +00007940 if (!skb_is_gso_v6(skb)) {
Michael Chand71c0dc2014-05-11 20:22:53 -07007941 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
7942 tg3_flag(tp, TSO_BUG))
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007943 return tg3_tso_bug(tp, tnapi, txq, skb);
Michael Chand71c0dc2014-05-11 20:22:53 -07007944
Michael Chand3f6f3a2014-05-11 20:22:54 -07007945 ip_csum = iph->check;
7946 ip_tot_len = iph->tot_len;
Matt Carlson34195c32010-07-11 09:31:42 +00007947 iph->check = 0;
7948 iph->tot_len = htons(mss + hdr_len);
7949 }
7950
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7952 TXD_FLAG_CPU_POST_DMA);
7953
Michael Chand3f6f3a2014-05-11 20:22:54 -07007954 tcph = tcp_hdr(skb);
7955 tcp_csum = tcph->check;
7956
Joe Perches63c3a662011-04-26 08:12:10 +00007957 if (tg3_flag(tp, HW_TSO_1) ||
7958 tg3_flag(tp, HW_TSO_2) ||
7959 tg3_flag(tp, HW_TSO_3)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07007960 tcph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007962 } else {
7963 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
7964 0, IPPROTO_TCP, 0);
7965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966
Joe Perches63c3a662011-04-26 08:12:10 +00007967 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007968 mss |= (hdr_len & 0xc) << 12;
7969 if (hdr_len & 0x10)
7970 base_flags |= 0x00000010;
7971 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007972 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007973 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007974 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007975 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007976 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977 int tsflags;
7978
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007979 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980 mss |= (tsflags << 11);
7981 }
7982 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007983 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984 int tsflags;
7985
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007986 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 base_flags |= tsflags << 12;
7988 }
7989 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04007990 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7991 /* HW/FW can not correctly checksum packets that have been
7992 * vlan encapsulated.
7993 */
7994 if (skb->protocol == htons(ETH_P_8021Q) ||
7995 skb->protocol == htons(ETH_P_8021AD)) {
7996 if (skb_checksum_help(skb))
7997 goto drop;
7998 } else {
7999 base_flags |= TXD_FLAG_TCPUDP_CSUM;
8000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00008002
Matt Carlson93a700a2011-08-31 11:44:54 +00008003 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
8004 !mss && skb->len > VLAN_ETH_FRAME_LEN)
8005 base_flags |= TXD_FLAG_JMB_PKT;
8006
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008007 if (skb_vlan_tag_present(skb)) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008008 base_flags |= TXD_FLAG_VLAN;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008009 vlan = skb_vlan_tag_get(skb);
Matt Carlson92cd3a12011-07-27 14:20:47 +00008010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008011
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00008012 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
8013 tg3_flag(tp, TX_TSTAMP_EN)) {
8014 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
8015 base_flags |= TXD_FLAG_HWTSTAMP;
8016 }
8017
Alexander Duyckf4188d82009-12-02 16:48:38 +00008018 len = skb_headlen(skb);
8019
8020 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00008021 if (pci_dma_mapping_error(tp->pdev, mapping))
8022 goto drop;
8023
David S. Miller90079ce2008-09-11 04:52:51 -07008024
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008025 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008026 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027
8028 would_hit_hwbug = 0;
8029
Joe Perches63c3a662011-04-26 08:12:10 +00008030 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07008031 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032
Matt Carlson84b67b22011-07-27 14:20:52 +00008033 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00008034 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008035 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008036 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008037 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008038 u32 tmp_mss = mss;
8039
8040 if (!tg3_flag(tp, HW_TSO_1) &&
8041 !tg3_flag(tp, HW_TSO_2) &&
8042 !tg3_flag(tp, HW_TSO_3))
8043 tmp_mss = 0;
8044
Matt Carlsonc5665a52012-02-13 10:20:12 +00008045 /* Now loop through additional data
8046 * fragments, and queue them.
8047 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048 last = skb_shinfo(skb)->nr_frags - 1;
8049 for (i = 0; i <= last; i++) {
8050 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8051
Eric Dumazet9e903e02011-10-18 21:00:24 +00008052 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008053 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008054 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008056 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008057 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008058 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008059 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008060 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008061
Matt Carlsonb9e45482011-11-04 09:14:59 +00008062 if (!budget ||
8063 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008064 len, base_flags |
8065 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008066 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008067 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008068 break;
8069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070 }
8071 }
8072
8073 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008074 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075
Michael Chand3f6f3a2014-05-11 20:22:54 -07008076 if (mss) {
8077 /* If it's a TSO packet, do GSO instead of
8078 * allocating and copying to a large linear SKB
8079 */
8080 if (ip_tot_len) {
8081 iph->check = ip_csum;
8082 iph->tot_len = ip_tot_len;
8083 }
8084 tcph->check = tcp_csum;
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07008085 return tg3_tso_bug(tp, tnapi, txq, skb);
Michael Chand3f6f3a2014-05-11 20:22:54 -07008086 }
8087
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 /* If the workaround fails due to memory/mapping
8089 * failure, silently drop this packet.
8090 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008091 entry = tnapi->tx_prod;
8092 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008093 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008094 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008095 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096 }
8097
Richard Cochrand515b452011-06-19 03:31:41 +00008098 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008099 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008100
Michael Chan6541b802012-03-04 14:48:14 +00008101 /* Sync BD data before updating mailbox */
8102 wmb();
8103
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008104 tnapi->tx_prod = entry;
8105 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008106 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008107
8108 /* netif_tx_stop_queue() must be done before checking
8109 * checking tx index in tg3_tx_avail() below, because in
8110 * tg3_tx(), we update tx index before checking for
8111 * netif_tx_queue_stopped().
8112 */
8113 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008114 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008115 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117
Prashant Sreedharan2c7c9ea2014-10-13 09:21:42 -07008118 if (!skb->xmit_more || netif_xmit_stopped(txq)) {
8119 /* Packets are ready, update Tx producer idx on card. */
8120 tw32_tx_mbox(tnapi->prodmbox, entry);
8121 mmiowb();
8122 }
8123
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008125
8126dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008127 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008128 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008129drop:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07008130 dev_kfree_skb_any(skb);
Eric Dumazet48855432011-10-24 07:53:03 +00008131drop_nofree:
8132 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008133 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134}
8135
Matt Carlson6e01b202011-08-19 13:58:20 +00008136static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8137{
8138 if (enable) {
8139 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8140 MAC_MODE_PORT_MODE_MASK);
8141
8142 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8143
8144 if (!tg3_flag(tp, 5705_PLUS))
8145 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8146
8147 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8148 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8149 else
8150 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8151 } else {
8152 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8153
8154 if (tg3_flag(tp, 5705_PLUS) ||
8155 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008156 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008157 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8158 }
8159
8160 tw32(MAC_MODE, tp->mac_mode);
8161 udelay(40);
8162}
8163
Matt Carlson941ec902011-08-19 13:58:23 +00008164static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008165{
Matt Carlson941ec902011-08-19 13:58:23 +00008166 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008167
8168 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008169 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008170
Matt Carlson941ec902011-08-19 13:58:23 +00008171 if (extlpbk && tg3_phy_set_extloopbk(tp))
8172 return -EIO;
8173
8174 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008175 switch (speed) {
8176 case SPEED_10:
8177 break;
8178 case SPEED_100:
8179 bmcr |= BMCR_SPEED100;
8180 break;
8181 case SPEED_1000:
8182 default:
8183 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8184 speed = SPEED_100;
8185 bmcr |= BMCR_SPEED100;
8186 } else {
8187 speed = SPEED_1000;
8188 bmcr |= BMCR_SPEED1000;
8189 }
8190 }
8191
Matt Carlson941ec902011-08-19 13:58:23 +00008192 if (extlpbk) {
8193 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8194 tg3_readphy(tp, MII_CTRL1000, &val);
8195 val |= CTL1000_AS_MASTER |
8196 CTL1000_ENABLE_MASTER;
8197 tg3_writephy(tp, MII_CTRL1000, val);
8198 } else {
8199 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8200 MII_TG3_FET_PTEST_TRIM_2;
8201 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8202 }
8203 } else
8204 bmcr |= BMCR_LOOPBACK;
8205
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008206 tg3_writephy(tp, MII_BMCR, bmcr);
8207
8208 /* The write needs to be flushed for the FETs */
8209 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8210 tg3_readphy(tp, MII_BMCR, &bmcr);
8211
8212 udelay(40);
8213
8214 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008215 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008216 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008217 MII_TG3_FET_PTEST_FRC_TX_LINK |
8218 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8219
8220 /* The write needs to be flushed for the AC131 */
8221 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8222 }
8223
8224 /* Reset to prevent losing 1st rx packet intermittently */
8225 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8226 tg3_flag(tp, 5780_CLASS)) {
8227 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8228 udelay(10);
8229 tw32_f(MAC_RX_MODE, tp->rx_mode);
8230 }
8231
8232 mac_mode = tp->mac_mode &
8233 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8234 if (speed == SPEED_1000)
8235 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8236 else
8237 mac_mode |= MAC_MODE_PORT_MODE_MII;
8238
Joe Perches41535772013-02-16 11:20:04 +00008239 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008240 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8241
8242 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8243 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8244 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8245 mac_mode |= MAC_MODE_LINK_POLARITY;
8246
8247 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8248 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8249 }
8250
8251 tw32(MAC_MODE, mac_mode);
8252 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008253
8254 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008255}
8256
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008257static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008258{
8259 struct tg3 *tp = netdev_priv(dev);
8260
8261 if (features & NETIF_F_LOOPBACK) {
8262 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8263 return;
8264
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008265 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008266 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008267 netif_carrier_on(tp->dev);
8268 spin_unlock_bh(&tp->lock);
8269 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8270 } else {
8271 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8272 return;
8273
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008274 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008275 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008276 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008277 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008278 spin_unlock_bh(&tp->lock);
8279 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8280 }
8281}
8282
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008283static netdev_features_t tg3_fix_features(struct net_device *dev,
8284 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008285{
8286 struct tg3 *tp = netdev_priv(dev);
8287
Joe Perches63c3a662011-04-26 08:12:10 +00008288 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008289 features &= ~NETIF_F_ALL_TSO;
8290
8291 return features;
8292}
8293
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008294static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008295{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008296 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008297
8298 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8299 tg3_set_loopback(dev, features);
8300
8301 return 0;
8302}
8303
Matt Carlson21f581a2009-08-28 14:00:25 +00008304static void tg3_rx_prodring_free(struct tg3 *tp,
8305 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307 int i;
8308
Matt Carlson8fea32b2010-09-15 08:59:58 +00008309 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008310 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008311 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008312 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008313 tp->rx_pkt_map_sz);
8314
Joe Perches63c3a662011-04-26 08:12:10 +00008315 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008316 for (i = tpr->rx_jmb_cons_idx;
8317 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008318 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008319 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008320 TG3_RX_JMB_MAP_SZ);
8321 }
8322 }
8323
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008324 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326
Matt Carlson2c49a442010-09-30 10:34:35 +00008327 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008328 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008329 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330
Joe Perches63c3a662011-04-26 08:12:10 +00008331 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008332 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008333 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008334 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335 }
8336}
8337
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008338/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 *
8340 * The chip has been shut down and the driver detached from
8341 * the networking, so no interrupts or new tx packets will
8342 * end up in the driver. tp->{tx,}lock are held and thus
8343 * we may not sleep.
8344 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008345static int tg3_rx_prodring_alloc(struct tg3 *tp,
8346 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347{
Matt Carlson287be122009-08-28 13:58:46 +00008348 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008350 tpr->rx_std_cons_idx = 0;
8351 tpr->rx_std_prod_idx = 0;
8352 tpr->rx_jmb_cons_idx = 0;
8353 tpr->rx_jmb_prod_idx = 0;
8354
Matt Carlson8fea32b2010-09-15 08:59:58 +00008355 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008356 memset(&tpr->rx_std_buffers[0], 0,
8357 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008358 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008359 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008360 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008361 goto done;
8362 }
8363
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008365 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366
Matt Carlson287be122009-08-28 13:58:46 +00008367 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008368 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008369 tp->dev->mtu > ETH_DATA_LEN)
8370 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8371 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008372
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373 /* Initialize invariants of the rings, we only set this
8374 * stuff once. This works because the card does not
8375 * write into the rx buffer posting rings.
8376 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008377 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 struct tg3_rx_buffer_desc *rxd;
8379
Matt Carlson21f581a2009-08-28 14:00:25 +00008380 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008381 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8383 rxd->opaque = (RXD_OPAQUE_RING_STD |
8384 (i << RXD_OPAQUE_INDEX_SHIFT));
8385 }
8386
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008387 /* Now allocate fresh SKBs for each rx ring. */
8388 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008389 unsigned int frag_size;
8390
8391 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8392 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008393 netdev_warn(tp->dev,
8394 "Using a smaller RX standard ring. Only "
8395 "%d out of %d buffers were allocated "
8396 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008397 if (i == 0)
8398 goto initfail;
8399 tp->rx_pending = i;
8400 break;
8401 }
8402 }
8403
Joe Perches63c3a662011-04-26 08:12:10 +00008404 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008405 goto done;
8406
Matt Carlson2c49a442010-09-30 10:34:35 +00008407 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008408
Joe Perches63c3a662011-04-26 08:12:10 +00008409 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008410 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411
Matt Carlson2c49a442010-09-30 10:34:35 +00008412 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008413 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414
Matt Carlson0d86df82010-02-17 15:17:00 +00008415 rxd = &tpr->rx_jmb[i].std;
8416 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8417 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8418 RXD_FLAG_JUMBO;
8419 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8420 (i << RXD_OPAQUE_INDEX_SHIFT));
8421 }
8422
8423 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008424 unsigned int frag_size;
8425
8426 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8427 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008428 netdev_warn(tp->dev,
8429 "Using a smaller RX jumbo ring. Only %d "
8430 "out of %d buffers were allocated "
8431 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008432 if (i == 0)
8433 goto initfail;
8434 tp->rx_jumbo_pending = i;
8435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436 }
8437 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008438
8439done:
Michael Chan32d8c572006-07-25 16:38:29 -07008440 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008441
8442initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008443 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008444 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445}
8446
Matt Carlson21f581a2009-08-28 14:00:25 +00008447static void tg3_rx_prodring_fini(struct tg3 *tp,
8448 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449{
Matt Carlson21f581a2009-08-28 14:00:25 +00008450 kfree(tpr->rx_std_buffers);
8451 tpr->rx_std_buffers = NULL;
8452 kfree(tpr->rx_jmb_buffers);
8453 tpr->rx_jmb_buffers = NULL;
8454 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008455 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8456 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008457 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008459 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008460 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8461 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008462 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008464}
8465
Matt Carlson21f581a2009-08-28 14:00:25 +00008466static int tg3_rx_prodring_init(struct tg3 *tp,
8467 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008468{
Matt Carlson2c49a442010-09-30 10:34:35 +00008469 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8470 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008471 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008472 return -ENOMEM;
8473
Matt Carlson4bae65c2010-11-24 08:31:52 +00008474 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8475 TG3_RX_STD_RING_BYTES(tp),
8476 &tpr->rx_std_mapping,
8477 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008478 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008479 goto err_out;
8480
Joe Perches63c3a662011-04-26 08:12:10 +00008481 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008482 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008483 GFP_KERNEL);
8484 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008485 goto err_out;
8486
Matt Carlson4bae65c2010-11-24 08:31:52 +00008487 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8488 TG3_RX_JMB_RING_BYTES(tp),
8489 &tpr->rx_jmb_mapping,
8490 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008491 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008492 goto err_out;
8493 }
8494
8495 return 0;
8496
8497err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008498 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008499 return -ENOMEM;
8500}
8501
8502/* Free up pending packets in all rx/tx rings.
8503 *
8504 * The chip has been shut down and the driver detached from
8505 * the networking, so no interrupts or new tx packets will
8506 * end up in the driver. tp->{tx,}lock is not held and we are not
8507 * in an interrupt context and thus may sleep.
8508 */
8509static void tg3_free_rings(struct tg3 *tp)
8510{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008511 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008512
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008513 for (j = 0; j < tp->irq_cnt; j++) {
8514 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008515
Matt Carlson8fea32b2010-09-15 08:59:58 +00008516 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008517
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008518 if (!tnapi->tx_buffers)
8519 continue;
8520
Matt Carlson0d681b22011-07-27 14:20:49 +00008521 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8522 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008523
Matt Carlson0d681b22011-07-27 14:20:49 +00008524 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008525 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008526
Matt Carlsonba1142e2011-11-04 09:15:00 +00008527 tg3_tx_skb_unmap(tnapi, i,
8528 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008529
8530 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008531 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008532 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008533 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008534}
8535
8536/* Initialize tx/rx rings for packet processing.
8537 *
8538 * The chip has been shut down and the driver detached from
8539 * the networking, so no interrupts or new tx packets will
8540 * end up in the driver. tp->{tx,}lock are held and thus
8541 * we may not sleep.
8542 */
8543static int tg3_init_rings(struct tg3 *tp)
8544{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008545 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008546
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008547 /* Free up all the SKBs. */
8548 tg3_free_rings(tp);
8549
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008550 for (i = 0; i < tp->irq_cnt; i++) {
8551 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008552
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008553 tnapi->last_tag = 0;
8554 tnapi->last_irq_tag = 0;
8555 tnapi->hw_status->status = 0;
8556 tnapi->hw_status->status_tag = 0;
8557 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8558
8559 tnapi->tx_prod = 0;
8560 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008561 if (tnapi->tx_ring)
8562 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008563
8564 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008565 if (tnapi->rx_rcb)
8566 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008567
Thadeu Lima de Souza Cascardoa620a6b2014-11-25 14:21:11 -02008568 if (tnapi->prodring.rx_std &&
8569 tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008570 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008571 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008572 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008573 }
Matt Carlson72334482009-08-28 14:03:01 +00008574
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008575 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008576}
8577
Michael Chan49a359e2012-09-28 07:12:37 +00008578static void tg3_mem_tx_release(struct tg3 *tp)
8579{
8580 int i;
8581
8582 for (i = 0; i < tp->irq_max; i++) {
8583 struct tg3_napi *tnapi = &tp->napi[i];
8584
8585 if (tnapi->tx_ring) {
8586 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8587 tnapi->tx_ring, tnapi->tx_desc_mapping);
8588 tnapi->tx_ring = NULL;
8589 }
8590
8591 kfree(tnapi->tx_buffers);
8592 tnapi->tx_buffers = NULL;
8593 }
8594}
8595
8596static int tg3_mem_tx_acquire(struct tg3 *tp)
8597{
8598 int i;
8599 struct tg3_napi *tnapi = &tp->napi[0];
8600
8601 /* If multivector TSS is enabled, vector 0 does not handle
8602 * tx interrupts. Don't allocate any resources for it.
8603 */
8604 if (tg3_flag(tp, ENABLE_TSS))
8605 tnapi++;
8606
8607 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8608 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8609 TG3_TX_RING_SIZE, GFP_KERNEL);
8610 if (!tnapi->tx_buffers)
8611 goto err_out;
8612
8613 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8614 TG3_TX_RING_BYTES,
8615 &tnapi->tx_desc_mapping,
8616 GFP_KERNEL);
8617 if (!tnapi->tx_ring)
8618 goto err_out;
8619 }
8620
8621 return 0;
8622
8623err_out:
8624 tg3_mem_tx_release(tp);
8625 return -ENOMEM;
8626}
8627
8628static void tg3_mem_rx_release(struct tg3 *tp)
8629{
8630 int i;
8631
8632 for (i = 0; i < tp->irq_max; i++) {
8633 struct tg3_napi *tnapi = &tp->napi[i];
8634
8635 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8636
8637 if (!tnapi->rx_rcb)
8638 continue;
8639
8640 dma_free_coherent(&tp->pdev->dev,
8641 TG3_RX_RCB_RING_BYTES(tp),
8642 tnapi->rx_rcb,
8643 tnapi->rx_rcb_mapping);
8644 tnapi->rx_rcb = NULL;
8645 }
8646}
8647
8648static int tg3_mem_rx_acquire(struct tg3 *tp)
8649{
8650 unsigned int i, limit;
8651
8652 limit = tp->rxq_cnt;
8653
8654 /* If RSS is enabled, we need a (dummy) producer ring
8655 * set on vector zero. This is the true hw prodring.
8656 */
8657 if (tg3_flag(tp, ENABLE_RSS))
8658 limit++;
8659
8660 for (i = 0; i < limit; i++) {
8661 struct tg3_napi *tnapi = &tp->napi[i];
8662
8663 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8664 goto err_out;
8665
8666 /* If multivector RSS is enabled, vector 0
8667 * does not handle rx or tx interrupts.
8668 * Don't allocate any resources for it.
8669 */
8670 if (!i && tg3_flag(tp, ENABLE_RSS))
8671 continue;
8672
Joe Perchesede23fa82013-08-26 22:45:23 -07008673 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8674 TG3_RX_RCB_RING_BYTES(tp),
8675 &tnapi->rx_rcb_mapping,
8676 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008677 if (!tnapi->rx_rcb)
8678 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008679 }
8680
8681 return 0;
8682
8683err_out:
8684 tg3_mem_rx_release(tp);
8685 return -ENOMEM;
8686}
8687
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008688/*
8689 * Must not be invoked with interrupt sources disabled and
8690 * the hardware shutdown down.
8691 */
8692static void tg3_free_consistent(struct tg3 *tp)
8693{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008694 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008695
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008696 for (i = 0; i < tp->irq_cnt; i++) {
8697 struct tg3_napi *tnapi = &tp->napi[i];
8698
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008699 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008700 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8701 tnapi->hw_status,
8702 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008703 tnapi->hw_status = NULL;
8704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008706
Michael Chan49a359e2012-09-28 07:12:37 +00008707 tg3_mem_rx_release(tp);
8708 tg3_mem_tx_release(tp);
8709
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008711 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8712 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713 tp->hw_stats = NULL;
8714 }
8715}
8716
8717/*
8718 * Must not be invoked with interrupt sources disabled and
8719 * the hardware shutdown down. Can sleep.
8720 */
8721static int tg3_alloc_consistent(struct tg3 *tp)
8722{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008723 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008724
Joe Perchesede23fa82013-08-26 22:45:23 -07008725 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8726 sizeof(struct tg3_hw_stats),
8727 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728 if (!tp->hw_stats)
8729 goto err_out;
8730
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008731 for (i = 0; i < tp->irq_cnt; i++) {
8732 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008733 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008734
Joe Perchesede23fa82013-08-26 22:45:23 -07008735 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8736 TG3_HW_STATUS_SIZE,
8737 &tnapi->status_mapping,
8738 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008739 if (!tnapi->hw_status)
8740 goto err_out;
8741
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008742 sblk = tnapi->hw_status;
8743
Michael Chan49a359e2012-09-28 07:12:37 +00008744 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008745 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008746
Michael Chan49a359e2012-09-28 07:12:37 +00008747 /*
8748 * When RSS is enabled, the status block format changes
8749 * slightly. The "rx_jumbo_consumer", "reserved",
8750 * and "rx_mini_consumer" members get mapped to the
8751 * other three rx return ring producer indexes.
8752 */
8753 switch (i) {
8754 case 1:
8755 prodptr = &sblk->idx[0].rx_producer;
8756 break;
8757 case 2:
8758 prodptr = &sblk->rx_jumbo_consumer;
8759 break;
8760 case 3:
8761 prodptr = &sblk->reserved;
8762 break;
8763 case 4:
8764 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008765 break;
8766 }
Michael Chan49a359e2012-09-28 07:12:37 +00008767 tnapi->rx_rcb_prod_idx = prodptr;
8768 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008769 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008770 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008771 }
8772
Michael Chan49a359e2012-09-28 07:12:37 +00008773 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8774 goto err_out;
8775
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776 return 0;
8777
8778err_out:
8779 tg3_free_consistent(tp);
8780 return -ENOMEM;
8781}
8782
8783#define MAX_WAIT_CNT 1000
8784
8785/* To stop a block, clear the enable bit and poll till it
8786 * clears. tp->lock is held.
8787 */
Joe Perches953c96e2013-04-09 10:18:14 +00008788static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008789{
8790 unsigned int i;
8791 u32 val;
8792
Joe Perches63c3a662011-04-26 08:12:10 +00008793 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008794 switch (ofs) {
8795 case RCVLSC_MODE:
8796 case DMAC_MODE:
8797 case MBFREE_MODE:
8798 case BUFMGR_MODE:
8799 case MEMARB_MODE:
8800 /* We can't enable/disable these bits of the
8801 * 5705/5750, just say success.
8802 */
8803 return 0;
8804
8805 default:
8806 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008808 }
8809
8810 val = tr32(ofs);
8811 val &= ~enable_bit;
8812 tw32_f(ofs, val);
8813
8814 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008815 if (pci_channel_offline(tp->pdev)) {
8816 dev_err(&tp->pdev->dev,
8817 "tg3_stop_block device offline, "
8818 "ofs=%lx enable_bit=%x\n",
8819 ofs, enable_bit);
8820 return -ENODEV;
8821 }
8822
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823 udelay(100);
8824 val = tr32(ofs);
8825 if ((val & enable_bit) == 0)
8826 break;
8827 }
8828
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008829 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008830 dev_err(&tp->pdev->dev,
8831 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8832 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833 return -ENODEV;
8834 }
8835
8836 return 0;
8837}
8838
8839/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008840static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841{
8842 int i, err;
8843
8844 tg3_disable_ints(tp);
8845
Gavin Shan6d446ec2013-06-25 15:24:32 +08008846 if (pci_channel_offline(tp->pdev)) {
8847 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8848 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8849 err = -ENODEV;
8850 goto err_no_dev;
8851 }
8852
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 tp->rx_mode &= ~RX_MODE_ENABLE;
8854 tw32_f(MAC_RX_MODE, tp->rx_mode);
8855 udelay(10);
8856
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008857 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8858 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8859 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8860 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8861 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8862 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008864 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8865 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8866 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8867 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8868 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8869 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8870 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871
8872 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8873 tw32_f(MAC_MODE, tp->mac_mode);
8874 udelay(40);
8875
8876 tp->tx_mode &= ~TX_MODE_ENABLE;
8877 tw32_f(MAC_TX_MODE, tp->tx_mode);
8878
8879 for (i = 0; i < MAX_WAIT_CNT; i++) {
8880 udelay(100);
8881 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8882 break;
8883 }
8884 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008885 dev_err(&tp->pdev->dev,
8886 "%s timed out, TX_MODE_ENABLE will not clear "
8887 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008888 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889 }
8890
Michael Chane6de8ad2005-05-05 14:42:41 -07008891 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008892 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8893 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894
8895 tw32(FTQ_RESET, 0xffffffff);
8896 tw32(FTQ_RESET, 0x00000000);
8897
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008898 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8899 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900
Gavin Shan6d446ec2013-06-25 15:24:32 +08008901err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008902 for (i = 0; i < tp->irq_cnt; i++) {
8903 struct tg3_napi *tnapi = &tp->napi[i];
8904 if (tnapi->hw_status)
8905 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908 return err;
8909}
8910
Michael Chanee6a99b2007-07-18 21:49:10 -07008911/* Save PCI command register before chip reset */
8912static void tg3_save_pci_state(struct tg3 *tp)
8913{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008914 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008915}
8916
8917/* Restore PCI state after chip reset */
8918static void tg3_restore_pci_state(struct tg3 *tp)
8919{
8920 u32 val;
8921
8922 /* Re-enable indirect register accesses. */
8923 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8924 tp->misc_host_ctrl);
8925
8926 /* Set MAX PCI retry to zero. */
8927 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008928 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008929 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008930 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008931 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008932 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008933 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008934 PCISTATE_ALLOW_APE_SHMEM_WR |
8935 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008936 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8937
Matt Carlson8a6eac92007-10-21 16:17:55 -07008938 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008939
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008940 if (!tg3_flag(tp, PCI_EXPRESS)) {
8941 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8942 tp->pci_cacheline_sz);
8943 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8944 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008945 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008946
Michael Chanee6a99b2007-07-18 21:49:10 -07008947 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008948 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008949 u16 pcix_cmd;
8950
8951 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8952 &pcix_cmd);
8953 pcix_cmd &= ~PCI_X_CMD_ERO;
8954 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8955 pcix_cmd);
8956 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008957
Joe Perches63c3a662011-04-26 08:12:10 +00008958 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008959
8960 /* Chip reset on 5780 will reset MSI enable bit,
8961 * so need to restore it.
8962 */
Joe Perches63c3a662011-04-26 08:12:10 +00008963 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008964 u16 ctrl;
8965
8966 pci_read_config_word(tp->pdev,
8967 tp->msi_cap + PCI_MSI_FLAGS,
8968 &ctrl);
8969 pci_write_config_word(tp->pdev,
8970 tp->msi_cap + PCI_MSI_FLAGS,
8971 ctrl | PCI_MSI_FLAGS_ENABLE);
8972 val = tr32(MSGINT_MODE);
8973 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8974 }
8975 }
8976}
8977
Nithin Sujirf82995b2014-01-03 10:09:13 -08008978static void tg3_override_clk(struct tg3 *tp)
8979{
8980 u32 val;
8981
8982 switch (tg3_asic_rev(tp)) {
8983 case ASIC_REV_5717:
8984 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
8985 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
8986 TG3_CPMU_MAC_ORIDE_ENABLE);
8987 break;
8988
8989 case ASIC_REV_5719:
8990 case ASIC_REV_5720:
8991 tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8992 break;
8993
8994 default:
8995 return;
8996 }
8997}
8998
8999static void tg3_restore_clk(struct tg3 *tp)
9000{
9001 u32 val;
9002
9003 switch (tg3_asic_rev(tp)) {
9004 case ASIC_REV_5717:
9005 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9006 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
9007 val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
9008 break;
9009
9010 case ASIC_REV_5719:
9011 case ASIC_REV_5720:
9012 val = tr32(TG3_CPMU_CLCK_ORIDE);
9013 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9014 break;
9015
9016 default:
9017 return;
9018 }
9019}
9020
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021/* tp->lock is held. */
9022static int tg3_chip_reset(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009023 __releases(tp->lock)
9024 __acquires(tp->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025{
9026 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07009027 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00009028 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009029
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01009030 if (!pci_device_is_present(tp->pdev))
9031 return -ENODEV;
9032
David S. Millerf49639e2006-06-09 11:58:36 -07009033 tg3_nvram_lock(tp);
9034
Matt Carlson77b483f2008-08-15 14:07:24 -07009035 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
9036
David S. Millerf49639e2006-06-09 11:58:36 -07009037 /* No matching tg3_nvram_unlock() after this because
9038 * chip reset below will undo the nvram lock.
9039 */
9040 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041
Michael Chanee6a99b2007-07-18 21:49:10 -07009042 /* GRC_MISC_CFG core clock reset will clear the memory
9043 * enable bit in PCI register 4 and the MSI enable bit
9044 * on some chips, so we save relevant registers here.
9045 */
9046 tg3_save_pci_state(tp);
9047
Joe Perches41535772013-02-16 11:20:04 +00009048 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009049 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08009050 tw32(GRC_FASTBOOT_PC, 0);
9051
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 /*
9053 * We must avoid the readl() that normally takes place.
9054 * It locks machines, causes machine checks, and other
9055 * fun things. So, temporarily disable the 5701
9056 * hardware workaround, while we do the reset.
9057 */
Michael Chan1ee582d2005-08-09 20:16:46 -07009058 write_op = tp->write32;
9059 if (write_op == tg3_write_flush_reg32)
9060 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009061
Michael Chand18edcb2007-03-24 20:57:11 -07009062 /* Prevent the irq handler from reading or writing PCI registers
9063 * during chip reset when the memory enable bit in the PCI command
9064 * register may be cleared. The chip does not generate interrupt
9065 * at this time, but the irq handler may still be called due to irq
9066 * sharing or irqpoll.
9067 */
Joe Perches63c3a662011-04-26 08:12:10 +00009068 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009069 for (i = 0; i < tp->irq_cnt; i++) {
9070 struct tg3_napi *tnapi = &tp->napi[i];
9071 if (tnapi->hw_status) {
9072 tnapi->hw_status->status = 0;
9073 tnapi->hw_status->status_tag = 0;
9074 }
9075 tnapi->last_tag = 0;
9076 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07009077 }
Michael Chand18edcb2007-03-24 20:57:11 -07009078 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00009079
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009080 tg3_full_unlock(tp);
9081
Matt Carlson4f125f42009-09-01 12:55:02 +00009082 for (i = 0; i < tp->irq_cnt; i++)
9083 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07009084
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009085 tg3_full_lock(tp, 0);
9086
Joe Perches41535772013-02-16 11:20:04 +00009087 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00009088 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9089 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
9090 }
9091
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092 /* do the reset */
9093 val = GRC_MISC_CFG_CORECLK_RESET;
9094
Joe Perches63c3a662011-04-26 08:12:10 +00009095 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00009096 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00009097 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009098 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00009099 tr32(TG3_PCIE_PHY_TSTCTL) ==
9100 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
9101 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9102
Joe Perches41535772013-02-16 11:20:04 +00009103 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 tw32(GRC_MISC_CFG, (1 << 29));
9105 val |= (1 << 29);
9106 }
9107 }
9108
Joe Perches41535772013-02-16 11:20:04 +00009109 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009110 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9111 tw32(GRC_VCPU_EXT_CTRL,
9112 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9113 }
9114
Nithin Sujirf82995b2014-01-03 10:09:13 -08009115 /* Set the clock to the highest frequency to avoid timeouts. With link
9116 * aware mode, the clock speed could be slow and bootcode does not
9117 * complete within the expected time. Override the clock to allow the
9118 * bootcode to finish sooner and then restore it.
9119 */
9120 tg3_override_clk(tp);
9121
Matt Carlsonf37500d2010-08-02 11:25:59 +00009122 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009123 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009125
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126 tw32(GRC_MISC_CFG, val);
9127
Michael Chan1ee582d2005-08-09 20:16:46 -07009128 /* restore 5701 hardware bug workaround write method */
9129 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009130
9131 /* Unfortunately, we have to delay before the PCI read back.
9132 * Some 575X chips even will not respond to a PCI cfg access
9133 * when the reset command is given to the chip.
9134 *
9135 * How do these hardware designers expect things to work
9136 * properly if the PCI write is posted for a long period
9137 * of time? It is always necessary to have some method by
9138 * which a register read back can occur to push the write
9139 * out which does the reset.
9140 *
9141 * For most tg3 variants the trick below was working.
9142 * Ho hum...
9143 */
9144 udelay(120);
9145
9146 /* Flush PCI posted writes. The normal MMIO registers
9147 * are inaccessible at this time so this is the only
9148 * way to make this reliably (actually, this is no longer
9149 * the case, see above). I tried to use indirect
9150 * register read/write but this upset some 5701 variants.
9151 */
9152 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9153
9154 udelay(120);
9155
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009156 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009157 u16 val16;
9158
Joe Perches41535772013-02-16 11:20:04 +00009159 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009160 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 u32 cfg_val;
9162
9163 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009164 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165 udelay(100);
9166
9167 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9168 pci_write_config_dword(tp->pdev, 0xc4,
9169 cfg_val | (1 << 15));
9170 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009171
Matt Carlsone7126992009-08-25 10:08:16 +00009172 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009173 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009174 /*
9175 * Older PCIe devices only support the 128 byte
9176 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009177 */
Joe Perches63c3a662011-04-26 08:12:10 +00009178 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009179 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9180 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009181
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009182 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009183 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009184 PCI_EXP_DEVSTA_CED |
9185 PCI_EXP_DEVSTA_NFED |
9186 PCI_EXP_DEVSTA_FED |
9187 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188 }
9189
Michael Chanee6a99b2007-07-18 21:49:10 -07009190 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191
Joe Perches63c3a662011-04-26 08:12:10 +00009192 tg3_flag_clear(tp, CHIP_RESETTING);
9193 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009194
Michael Chanee6a99b2007-07-18 21:49:10 -07009195 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009196 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009197 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009198 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199
Joe Perches41535772013-02-16 11:20:04 +00009200 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009201 tg3_stop_fw(tp);
9202 tw32(0x5000, 0x400);
9203 }
9204
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009205 if (tg3_flag(tp, IS_SSB_CORE)) {
9206 /*
9207 * BCM4785: In order to avoid repercussions from using
9208 * potentially defective internal ROM, stop the Rx RISC CPU,
9209 * which is not required.
9210 */
9211 tg3_stop_fw(tp);
9212 tg3_halt_cpu(tp, RX_CPU_BASE);
9213 }
9214
Nithin Sujirfb03a432013-05-21 12:57:32 +00009215 err = tg3_poll_fw(tp);
9216 if (err)
9217 return err;
9218
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 tw32(GRC_MODE, tp->grc_mode);
9220
Joe Perches41535772013-02-16 11:20:04 +00009221 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009222 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223
9224 tw32(0xc4, val | (1 << 15));
9225 }
9226
9227 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009228 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009230 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9232 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9233 }
9234
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009235 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009236 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009237 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009238 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009239 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009240 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009242 val = 0;
9243
9244 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 udelay(40);
9246
Matt Carlson77b483f2008-08-15 14:07:24 -07009247 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9248
Matt Carlson0a9140c2009-08-28 12:27:50 +00009249 tg3_mdio_start(tp);
9250
Joe Perches63c3a662011-04-26 08:12:10 +00009251 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009252 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9253 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009254 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009255 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256
9257 tw32(0x7c00, val | (1 << 25));
9258 }
9259
Nithin Sujirf82995b2014-01-03 10:09:13 -08009260 tg3_restore_clk(tp);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009261
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009263 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009264 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9265 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9266
Joe Perches63c3a662011-04-26 08:12:10 +00009267 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9269 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9270 u32 nic_cfg;
9271
9272 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9273 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009274 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009275 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009276 if (tg3_flag(tp, 5750_PLUS))
9277 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009278
9279 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9280 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9281 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9282 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9283 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284 }
9285 }
9286
9287 return 0;
9288}
9289
Matt Carlson65ec6982012-02-28 23:33:37 +00009290static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9291static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Michael Chane565eec2014-01-03 10:09:12 -08009292static void __tg3_set_rx_mode(struct net_device *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009293
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009295static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296{
9297 int err;
9298
9299 tg3_stop_fw(tp);
9300
Michael Chan944d9802005-05-29 14:57:48 -07009301 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009303 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 err = tg3_chip_reset(tp);
9305
Joe Perches953c96e2013-04-09 10:18:14 +00009306 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009307
Michael Chan944d9802005-05-29 14:57:48 -07009308 tg3_write_sig_legacy(tp, kind);
9309 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310
Matt Carlson92feeab2011-12-08 14:40:14 +00009311 if (tp->hw_stats) {
9312 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009313 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009314 tg3_get_estats(tp, &tp->estats_prev);
9315
9316 /* And make sure the next sample is new data */
9317 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9318 }
9319
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009320 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321}
9322
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323static int tg3_set_mac_addr(struct net_device *dev, void *p)
9324{
9325 struct tg3 *tp = netdev_priv(dev);
9326 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009327 int err = 0;
9328 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329
Michael Chanf9804dd2005-09-27 12:13:10 -07009330 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009331 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009332
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9334
Michael Chane75f7c92006-03-20 21:33:26 -08009335 if (!netif_running(dev))
9336 return 0;
9337
Joe Perches63c3a662011-04-26 08:12:10 +00009338 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009339 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009340
Michael Chan986e0ae2007-05-05 12:10:20 -07009341 addr0_high = tr32(MAC_ADDR_0_HIGH);
9342 addr0_low = tr32(MAC_ADDR_0_LOW);
9343 addr1_high = tr32(MAC_ADDR_1_HIGH);
9344 addr1_low = tr32(MAC_ADDR_1_LOW);
9345
9346 /* Skip MAC addr 1 if ASF is using it. */
9347 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9348 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009349 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009350 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009351 spin_lock_bh(&tp->lock);
9352 __tg3_set_mac_addr(tp, skip_mac_1);
Michael Chane565eec2014-01-03 10:09:12 -08009353 __tg3_set_rx_mode(dev);
Michael Chan986e0ae2007-05-05 12:10:20 -07009354 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355
Michael Chanb9ec6c12006-07-25 16:37:27 -07009356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357}
9358
9359/* tp->lock is held. */
9360static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9361 dma_addr_t mapping, u32 maxlen_flags,
9362 u32 nic_addr)
9363{
9364 tg3_write_mem(tp,
9365 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9366 ((u64) mapping >> 32));
9367 tg3_write_mem(tp,
9368 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9369 ((u64) mapping & 0xffffffff));
9370 tg3_write_mem(tp,
9371 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9372 maxlen_flags);
9373
Joe Perches63c3a662011-04-26 08:12:10 +00009374 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 tg3_write_mem(tp,
9376 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9377 nic_addr);
9378}
9379
Michael Chana489b6d2012-09-28 07:12:39 +00009380
9381static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009382{
Michael Chana489b6d2012-09-28 07:12:39 +00009383 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009384
Joe Perches63c3a662011-04-26 08:12:10 +00009385 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009386 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9387 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9388 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009389 } else {
9390 tw32(HOSTCC_TXCOL_TICKS, 0);
9391 tw32(HOSTCC_TXMAX_FRAMES, 0);
9392 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009393
9394 for (; i < tp->txq_cnt; i++) {
9395 u32 reg;
9396
9397 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9398 tw32(reg, ec->tx_coalesce_usecs);
9399 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9400 tw32(reg, ec->tx_max_coalesced_frames);
9401 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9402 tw32(reg, ec->tx_max_coalesced_frames_irq);
9403 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009404 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009405
Michael Chana489b6d2012-09-28 07:12:39 +00009406 for (; i < tp->irq_max - 1; i++) {
9407 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9408 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9409 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9410 }
9411}
9412
9413static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9414{
9415 int i = 0;
9416 u32 limit = tp->rxq_cnt;
9417
Joe Perches63c3a662011-04-26 08:12:10 +00009418 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009419 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9420 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9421 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009422 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009423 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009424 tw32(HOSTCC_RXCOL_TICKS, 0);
9425 tw32(HOSTCC_RXMAX_FRAMES, 0);
9426 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009427 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009428
Michael Chana489b6d2012-09-28 07:12:39 +00009429 for (; i < limit; i++) {
9430 u32 reg;
9431
9432 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9433 tw32(reg, ec->rx_coalesce_usecs);
9434 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9435 tw32(reg, ec->rx_max_coalesced_frames);
9436 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9437 tw32(reg, ec->rx_max_coalesced_frames_irq);
9438 }
9439
9440 for (; i < tp->irq_max - 1; i++) {
9441 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9442 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9443 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9444 }
9445}
9446
9447static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9448{
9449 tg3_coal_tx_init(tp, ec);
9450 tg3_coal_rx_init(tp, ec);
9451
Joe Perches63c3a662011-04-26 08:12:10 +00009452 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009453 u32 val = ec->stats_block_coalesce_usecs;
9454
Matt Carlsonb6080e12009-09-01 13:12:00 +00009455 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9456 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9457
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009458 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009459 val = 0;
9460
9461 tw32(HOSTCC_STAT_COAL_TICKS, val);
9462 }
9463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464
9465/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009466static void tg3_tx_rcbs_disable(struct tg3 *tp)
9467{
9468 u32 txrcb, limit;
9469
9470 /* Disable all transmit rings but the first. */
9471 if (!tg3_flag(tp, 5705_PLUS))
9472 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9473 else if (tg3_flag(tp, 5717_PLUS))
9474 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9475 else if (tg3_flag(tp, 57765_CLASS) ||
9476 tg3_asic_rev(tp) == ASIC_REV_5762)
9477 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9478 else
9479 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9480
9481 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9482 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9483 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9484 BDINFO_FLAGS_DISABLED);
9485}
9486
9487/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009488static void tg3_tx_rcbs_init(struct tg3 *tp)
9489{
9490 int i = 0;
9491 u32 txrcb = NIC_SRAM_SEND_RCB;
9492
9493 if (tg3_flag(tp, ENABLE_TSS))
9494 i++;
9495
9496 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9497 struct tg3_napi *tnapi = &tp->napi[i];
9498
9499 if (!tnapi->tx_ring)
9500 continue;
9501
9502 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9503 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9504 NIC_SRAM_TX_BUFFER_DESC);
9505 }
9506}
9507
9508/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009509static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9510{
9511 u32 rxrcb, limit;
9512
9513 /* Disable all receive return rings but the first. */
9514 if (tg3_flag(tp, 5717_PLUS))
9515 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9516 else if (!tg3_flag(tp, 5705_PLUS))
9517 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9518 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9519 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9520 tg3_flag(tp, 57765_CLASS))
9521 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9522 else
9523 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9524
9525 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9526 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9527 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9528 BDINFO_FLAGS_DISABLED);
9529}
9530
9531/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009532static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9533{
9534 int i = 0;
9535 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9536
9537 if (tg3_flag(tp, ENABLE_RSS))
9538 i++;
9539
9540 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9541 struct tg3_napi *tnapi = &tp->napi[i];
9542
9543 if (!tnapi->rx_rcb)
9544 continue;
9545
9546 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9547 (tp->rx_ret_ring_mask + 1) <<
9548 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9549 }
9550}
9551
9552/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009553static void tg3_rings_reset(struct tg3 *tp)
9554{
9555 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009556 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009557 struct tg3_napi *tnapi = &tp->napi[0];
9558
Nithin Sujir328947f2013-05-23 11:11:24 +00009559 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009560
Nithin Sujir328947f2013-05-23 11:11:24 +00009561 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009562
9563 /* Disable interrupts */
9564 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009565 tp->napi[0].chk_msi_cnt = 0;
9566 tp->napi[0].last_rx_cons = 0;
9567 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009568
9569 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009570 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009571 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009572 tp->napi[i].tx_prod = 0;
9573 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009574 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009575 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009576 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9577 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009578 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009579 tp->napi[i].last_rx_cons = 0;
9580 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009581 }
Joe Perches63c3a662011-04-26 08:12:10 +00009582 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009583 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009584 } else {
9585 tp->napi[0].tx_prod = 0;
9586 tp->napi[0].tx_cons = 0;
9587 tw32_mailbox(tp->napi[0].prodmbox, 0);
9588 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9589 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009590
9591 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009592 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009593 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9594 for (i = 0; i < 16; i++)
9595 tw32_tx_mbox(mbox + i * 8, 0);
9596 }
9597
Matt Carlson2d31eca2009-09-01 12:53:31 +00009598 /* Clear status block in ram. */
9599 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9600
9601 /* Set status block DMA address */
9602 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9603 ((u64) tnapi->status_mapping >> 32));
9604 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9605 ((u64) tnapi->status_mapping & 0xffffffff));
9606
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009607 stblk = HOSTCC_STATBLCK_RING1;
9608
9609 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9610 u64 mapping = (u64)tnapi->status_mapping;
9611 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9612 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009613 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009614
9615 /* Clear status block in ram. */
9616 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009617 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009618
9619 tg3_tx_rcbs_init(tp);
9620 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009621}
9622
Matt Carlsoneb07a942011-04-20 07:57:36 +00009623static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9624{
9625 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9626
Joe Perches63c3a662011-04-26 08:12:10 +00009627 if (!tg3_flag(tp, 5750_PLUS) ||
9628 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009629 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9630 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009631 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009632 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009633 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9634 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009635 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9636 else
9637 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9638
9639 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9640 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9641
9642 val = min(nic_rep_thresh, host_rep_thresh);
9643 tw32(RCVBDI_STD_THRESH, val);
9644
Joe Perches63c3a662011-04-26 08:12:10 +00009645 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009646 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9647
Joe Perches63c3a662011-04-26 08:12:10 +00009648 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009649 return;
9650
Matt Carlson513aa6e2011-11-21 15:01:18 +00009651 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009652
9653 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9654
9655 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9656 tw32(RCVBDI_JUMBO_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(JMB_REPLENISH_LWM, bdcache_maxcnt);
9660}
9661
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009662static inline u32 calc_crc(unsigned char *buf, int len)
9663{
9664 u32 reg;
9665 u32 tmp;
9666 int j, k;
9667
9668 reg = 0xffffffff;
9669
9670 for (j = 0; j < len; j++) {
9671 reg ^= buf[j];
9672
9673 for (k = 0; k < 8; k++) {
9674 tmp = reg & 0x01;
9675
9676 reg >>= 1;
9677
9678 if (tmp)
9679 reg ^= 0xedb88320;
9680 }
9681 }
9682
9683 return ~reg;
9684}
9685
9686static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9687{
9688 /* accept or reject all multicast frames */
9689 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9690 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9691 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9692 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9693}
9694
9695static void __tg3_set_rx_mode(struct net_device *dev)
9696{
9697 struct tg3 *tp = netdev_priv(dev);
9698 u32 rx_mode;
9699
9700 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9701 RX_MODE_KEEP_VLAN_TAG);
9702
9703#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9704 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9705 * flag clear.
9706 */
9707 if (!tg3_flag(tp, ENABLE_ASF))
9708 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9709#endif
9710
9711 if (dev->flags & IFF_PROMISC) {
9712 /* Promiscuous mode. */
9713 rx_mode |= RX_MODE_PROMISC;
9714 } else if (dev->flags & IFF_ALLMULTI) {
9715 /* Accept all multicast. */
9716 tg3_set_multi(tp, 1);
9717 } else if (netdev_mc_empty(dev)) {
9718 /* Reject all multicast. */
9719 tg3_set_multi(tp, 0);
9720 } else {
9721 /* Accept one or more multicast(s). */
9722 struct netdev_hw_addr *ha;
9723 u32 mc_filter[4] = { 0, };
9724 u32 regidx;
9725 u32 bit;
9726 u32 crc;
9727
9728 netdev_for_each_mc_addr(ha, dev) {
9729 crc = calc_crc(ha->addr, ETH_ALEN);
9730 bit = ~crc & 0x7f;
9731 regidx = (bit & 0x60) >> 5;
9732 bit &= 0x1f;
9733 mc_filter[regidx] |= (1 << bit);
9734 }
9735
9736 tw32(MAC_HASH_REG_0, mc_filter[0]);
9737 tw32(MAC_HASH_REG_1, mc_filter[1]);
9738 tw32(MAC_HASH_REG_2, mc_filter[2]);
9739 tw32(MAC_HASH_REG_3, mc_filter[3]);
9740 }
9741
Michael Chane565eec2014-01-03 10:09:12 -08009742 if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
9743 rx_mode |= RX_MODE_PROMISC;
9744 } else if (!(dev->flags & IFF_PROMISC)) {
9745 /* Add all entries into to the mac addr filter list */
9746 int i = 0;
9747 struct netdev_hw_addr *ha;
9748
9749 netdev_for_each_uc_addr(ha, dev) {
9750 __tg3_set_one_mac_addr(tp, ha->addr,
9751 i + TG3_UCAST_ADDR_IDX(tp));
9752 i++;
9753 }
9754 }
9755
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009756 if (rx_mode != tp->rx_mode) {
9757 tp->rx_mode = rx_mode;
9758 tw32_f(MAC_RX_MODE, rx_mode);
9759 udelay(10);
9760 }
9761}
9762
Michael Chan91024262012-09-28 07:12:38 +00009763static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009764{
9765 int i;
9766
9767 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009768 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009769}
9770
9771static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009772{
9773 int i;
9774
9775 if (!tg3_flag(tp, SUPPORT_MSIX))
9776 return;
9777
Michael Chan0b3ba052012-11-14 14:44:29 +00009778 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009779 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009780 return;
9781 }
9782
9783 /* Validate table against current IRQ count */
9784 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009785 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009786 break;
9787 }
9788
9789 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009790 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009791}
9792
Matt Carlson90415472011-12-16 13:33:23 +00009793static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009794{
9795 int i = 0;
9796 u32 reg = MAC_RSS_INDIR_TBL_0;
9797
9798 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9799 u32 val = tp->rss_ind_tbl[i];
9800 i++;
9801 for (; i % 8; i++) {
9802 val <<= 4;
9803 val |= tp->rss_ind_tbl[i];
9804 }
9805 tw32(reg, val);
9806 reg += 4;
9807 }
9808}
9809
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009810static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9811{
9812 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9813 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9814 else
9815 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9816}
9817
Matt Carlson2d31eca2009-09-01 12:53:31 +00009818/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009819static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820{
9821 u32 val, rdmac_mode;
9822 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009823 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824
9825 tg3_disable_ints(tp);
9826
9827 tg3_stop_fw(tp);
9828
9829 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9830
Joe Perches63c3a662011-04-26 08:12:10 +00009831 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009832 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009834 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9835 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9836 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009837 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009838 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9839 }
9840
Nithin Sujir400dfba2013-05-18 06:26:53 +00009841 /* Enable MAC control of LPI */
9842 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9843 tg3_setup_eee(tp);
9844
Matt Carlson603f1172010-02-12 14:47:10 +00009845 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009846 tg3_phy_reset(tp);
9847
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 err = tg3_chip_reset(tp);
9849 if (err)
9850 return err;
9851
9852 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9853
Joe Perches41535772013-02-16 11:20:04 +00009854 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009855 val = tr32(TG3_CPMU_CTRL);
9856 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9857 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009858
9859 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9860 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9861 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9862 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9863
9864 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9865 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9866 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9867 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9868
9869 val = tr32(TG3_CPMU_HST_ACC);
9870 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9871 val |= CPMU_HST_ACC_MACCLK_6_25;
9872 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009873 }
9874
Joe Perches41535772013-02-16 11:20:04 +00009875 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009876 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9877 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9878 PCIE_PWR_MGMT_L1_THRESH_4MS;
9879 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009880
9881 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9882 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9883
9884 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009885
Matt Carlsonf40386c2009-11-02 14:24:02 +00009886 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9887 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009888 }
9889
Joe Perches63c3a662011-04-26 08:12:10 +00009890 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009891 u32 grc_mode = tr32(GRC_MODE);
9892
9893 /* Access the lower 1K of PL PCIE block registers. */
9894 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9895 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9896
9897 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9898 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9899 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9900
9901 tw32(GRC_MODE, grc_mode);
9902 }
9903
Matt Carlson55086ad2011-12-14 11:09:59 +00009904 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009905 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009906 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009907
Matt Carlson5093eed2010-11-24 08:31:45 +00009908 /* Access the lower 1K of PL PCIE block registers. */
9909 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9910 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009911
Matt Carlson5093eed2010-11-24 08:31:45 +00009912 val = tr32(TG3_PCIE_TLDLPL_PORT +
9913 TG3_PCIE_PL_LO_PHYCTL5);
9914 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9915 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009916
Matt Carlson5093eed2010-11-24 08:31:45 +00009917 tw32(GRC_MODE, grc_mode);
9918 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009919
Joe Perches41535772013-02-16 11:20:04 +00009920 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009921 u32 grc_mode;
9922
9923 /* Fix transmit hangs */
9924 val = tr32(TG3_CPMU_PADRNG_CTL);
9925 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9926 tw32(TG3_CPMU_PADRNG_CTL, val);
9927
9928 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009929
9930 /* Access the lower 1K of DL PCIE block registers. */
9931 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9932 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9933
9934 val = tr32(TG3_PCIE_TLDLPL_PORT +
9935 TG3_PCIE_DL_LO_FTSMAX);
9936 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9937 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9938 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9939
9940 tw32(GRC_MODE, grc_mode);
9941 }
9942
Matt Carlsona977dbe2010-04-12 06:58:26 +00009943 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9944 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9945 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9946 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009947 }
9948
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949 /* This works around an issue with Athlon chipsets on
9950 * B3 tigon3 silicon. This bit has no effect on any
9951 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009952 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 */
Joe Perches63c3a662011-04-26 08:12:10 +00009954 if (!tg3_flag(tp, CPMU_PRESENT)) {
9955 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009956 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9957 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959
Joe Perches41535772013-02-16 11:20:04 +00009960 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009961 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 val = tr32(TG3PCI_PCISTATE);
9963 val |= PCISTATE_RETRY_SAME_DMA;
9964 tw32(TG3PCI_PCISTATE, val);
9965 }
9966
Joe Perches63c3a662011-04-26 08:12:10 +00009967 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009968 /* Allow reads and writes to the
9969 * APE register and memory space.
9970 */
9971 val = tr32(TG3PCI_PCISTATE);
9972 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009973 PCISTATE_ALLOW_APE_SHMEM_WR |
9974 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009975 tw32(TG3PCI_PCISTATE, val);
9976 }
9977
Joe Perches41535772013-02-16 11:20:04 +00009978 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009979 /* Enable some hw fixes. */
9980 val = tr32(TG3PCI_MSI_DATA);
9981 val |= (1 << 26) | (1 << 28) | (1 << 29);
9982 tw32(TG3PCI_MSI_DATA, val);
9983 }
9984
9985 /* Descriptor ring init may make accesses to the
9986 * NIC SRAM area to setup the TX descriptors, so we
9987 * can only do this after the hardware has been
9988 * successfully reset.
9989 */
Michael Chan32d8c572006-07-25 16:38:29 -07009990 err = tg3_init_rings(tp);
9991 if (err)
9992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993
Joe Perches63c3a662011-04-26 08:12:10 +00009994 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009995 val = tr32(TG3PCI_DMA_RW_CTRL) &
9996 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009997 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009998 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009999 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +000010000 tg3_asic_rev(tp) != ASIC_REV_5717 &&
10001 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +000010002 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010003 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +000010004 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
10005 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -070010006 /* This value is determined during the probe time DMA
10007 * engine test, tg3_test_dma.
10008 */
10009 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
10010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010011
10012 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
10013 GRC_MODE_4X_NIC_SEND_RINGS |
10014 GRC_MODE_NO_TX_PHDR_CSUM |
10015 GRC_MODE_NO_RX_PHDR_CSUM);
10016 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -070010017
10018 /* Pseudo-header checksum is done by hardware logic and not
10019 * the offload processers, so make the chip do the pseudo-
10020 * header checksums on receive. For transmit it is more
10021 * convenient to do the pseudo-header checksum in software
10022 * as Linux does that on transmit for us in all cases.
10023 */
10024 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010025
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000010026 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
10027 if (tp->rxptpctl)
10028 tw32(TG3_RX_PTP_CTL,
10029 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
10030
10031 if (tg3_flag(tp, PTP_CAPABLE))
10032 val |= GRC_MODE_TIME_SYNC_ENABLE;
10033
10034 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035
10036 /* Setup the timer prescalar register. Clock is always 66Mhz. */
10037 val = tr32(GRC_MISC_CFG);
10038 val &= ~0xff;
10039 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
10040 tw32(GRC_MISC_CFG, val);
10041
10042 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +000010043 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010044 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +000010045 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +000010047 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010048 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
10049 else
10050 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
10051 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
10052 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +000010053 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054 int fw_len;
10055
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080010056 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
10058 tw32(BUFMGR_MB_POOL_ADDR,
10059 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
10060 tw32(BUFMGR_MB_POOL_SIZE,
10061 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
10062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063
Michael Chan0f893dc2005-07-25 12:30:38 -070010064 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10066 tp->bufmgr_config.mbuf_read_dma_low_water);
10067 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10068 tp->bufmgr_config.mbuf_mac_rx_low_water);
10069 tw32(BUFMGR_MB_HIGH_WATER,
10070 tp->bufmgr_config.mbuf_high_water);
10071 } else {
10072 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10073 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
10074 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10075 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
10076 tw32(BUFMGR_MB_HIGH_WATER,
10077 tp->bufmgr_config.mbuf_high_water_jumbo);
10078 }
10079 tw32(BUFMGR_DMA_LOW_WATER,
10080 tp->bufmgr_config.dma_low_water);
10081 tw32(BUFMGR_DMA_HIGH_WATER,
10082 tp->bufmgr_config.dma_high_water);
10083
Matt Carlsond309a462010-09-30 10:34:31 +000010084 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +000010085 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +000010086 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +000010087 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080010088 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000010089 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10090 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +000010091 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +000010092 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093 for (i = 0; i < 2000; i++) {
10094 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
10095 break;
10096 udelay(10);
10097 }
10098 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +000010099 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100 return -ENODEV;
10101 }
10102
Joe Perches41535772013-02-16 11:20:04 +000010103 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +000010104 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -070010105
Matt Carlsoneb07a942011-04-20 07:57:36 +000010106 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107
10108 /* Initialize TG3_BDINFO's at:
10109 * RCVDBDI_STD_BD: standard eth size rx ring
10110 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
10111 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
10112 *
10113 * like so:
10114 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
10115 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
10116 * ring attribute flags
10117 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
10118 *
10119 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
10120 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
10121 *
10122 * The size of each ring is fixed in the firmware, but the location is
10123 * configurable.
10124 */
10125 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010126 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010128 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010129 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010130 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10131 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010133 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010134 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10136 BDINFO_FLAGS_DISABLED);
10137
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010138 /* Program the jumbo buffer descriptor ring control
10139 * blocks on those devices that have them.
10140 */
Joe Perches41535772013-02-16 11:20:04 +000010141 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010142 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143
Joe Perches63c3a662011-04-26 08:12:10 +000010144 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010146 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010148 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010149 val = TG3_RX_JMB_RING_SIZE(tp) <<
10150 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010152 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010153 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010154 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010155 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010156 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10157 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 } else {
10159 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10160 BDINFO_FLAGS_DISABLED);
10161 }
10162
Joe Perches63c3a662011-04-26 08:12:10 +000010163 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010164 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010165 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10166 val |= (TG3_RX_STD_DMA_SZ << 2);
10167 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010168 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010169 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010170 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010171
10172 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010173
Matt Carlson411da642009-11-13 13:03:46 +000010174 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +000010175 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176
Joe Perches63c3a662011-04-26 08:12:10 +000010177 tpr->rx_jmb_prod_idx =
10178 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +000010179 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180
Matt Carlson2d31eca2009-09-01 12:53:31 +000010181 tg3_rings_reset(tp);
10182
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010184 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185
10186 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010187 tw32(MAC_RX_MTU_SIZE,
10188 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189
10190 /* The slot time is changed by tg3_setup_phy if we
10191 * run at gigabit with half duplex.
10192 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010193 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10194 (6 << TX_LENGTHS_IPG_SHIFT) |
10195 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10196
Joe Perches41535772013-02-16 11:20:04 +000010197 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10198 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010199 val |= tr32(MAC_TX_LENGTHS) &
10200 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10201 TX_LENGTHS_CNT_DWN_VAL_MSK);
10202
10203 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010204
10205 /* Receive rules. */
10206 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10207 tw32(RCVLPC_CONFIG, 0x0181);
10208
10209 /* Calculate RDMAC_MODE setting early, we need it to determine
10210 * the RCVLPC_STATE_ENABLE mask.
10211 */
10212 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10213 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10214 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10215 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10216 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010217
Joe Perches41535772013-02-16 11:20:04 +000010218 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010219 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10220
Joe Perches41535772013-02-16 11:20:04 +000010221 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10222 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10223 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010224 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10225 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10226 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10227
Joe Perches41535772013-02-16 11:20:04 +000010228 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10229 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010230 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010231 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10233 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010234 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10236 }
10237 }
10238
Joe Perches63c3a662011-04-26 08:12:10 +000010239 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010240 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10241
Joe Perches41535772013-02-16 11:20:04 +000010242 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010243 tp->dma_limit = 0;
10244 if (tp->dev->mtu <= ETH_DATA_LEN) {
10245 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10246 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10247 }
10248 }
10249
Joe Perches63c3a662011-04-26 08:12:10 +000010250 if (tg3_flag(tp, HW_TSO_1) ||
10251 tg3_flag(tp, HW_TSO_2) ||
10252 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010253 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10254
Matt Carlson108a6c12011-05-19 12:12:47 +000010255 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010256 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10257 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010258 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010259
Joe Perches41535772013-02-16 11:20:04 +000010260 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10261 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010262 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10263
Joe Perches41535772013-02-16 11:20:04 +000010264 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10265 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10266 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10267 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010268 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010269 u32 tgtreg;
10270
Joe Perches41535772013-02-16 11:20:04 +000010271 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010272 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10273 else
10274 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10275
10276 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010277 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10278 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010279 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10280 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10281 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10282 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10283 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10284 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010285 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010286 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010287 }
10288
Joe Perches41535772013-02-16 11:20:04 +000010289 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10290 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10291 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010292 u32 tgtreg;
10293
Joe Perches41535772013-02-16 11:20:04 +000010294 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010295 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10296 else
10297 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10298
10299 val = tr32(tgtreg);
10300 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010301 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10302 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10303 }
10304
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010306 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010307 val = tr32(RCVLPC_STATS_ENABLE);
10308 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10309 tw32(RCVLPC_STATS_ENABLE, val);
10310 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010311 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312 val = tr32(RCVLPC_STATS_ENABLE);
10313 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10314 tw32(RCVLPC_STATS_ENABLE, val);
10315 } else {
10316 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10317 }
10318 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10319 tw32(SNDDATAI_STATSENAB, 0xffffff);
10320 tw32(SNDDATAI_STATSCTRL,
10321 (SNDDATAI_SCTRL_ENABLE |
10322 SNDDATAI_SCTRL_FASTUPD));
10323
10324 /* Setup host coalescing engine. */
10325 tw32(HOSTCC_MODE, 0);
10326 for (i = 0; i < 2000; i++) {
10327 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10328 break;
10329 udelay(10);
10330 }
10331
Michael Chand244c892005-07-05 14:42:33 -070010332 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010333
Joe Perches63c3a662011-04-26 08:12:10 +000010334 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010335 /* Status/statistics block address. See tg3_timer,
10336 * the tg3_periodic_fetch_stats call there, and
10337 * tg3_get_stats to see how this works for 5705/5750 chips.
10338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010339 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10340 ((u64) tp->stats_mapping >> 32));
10341 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10342 ((u64) tp->stats_mapping & 0xffffffff));
10343 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010344
Linus Torvalds1da177e2005-04-16 15:20:36 -070010345 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010346
10347 /* Clear statistics and status block memory areas */
10348 for (i = NIC_SRAM_STATS_BLK;
10349 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10350 i += sizeof(u32)) {
10351 tg3_write_mem(tp, i, 0);
10352 udelay(40);
10353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010354 }
10355
10356 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10357
10358 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10359 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010360 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010361 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10362
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010363 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10364 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010365 /* reset to prevent losing 1st rx packet intermittently */
10366 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10367 udelay(10);
10368 }
10369
Matt Carlson3bda1252008-08-15 14:08:22 -070010370 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010371 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10372 MAC_MODE_FHDE_ENABLE;
10373 if (tg3_flag(tp, ENABLE_APE))
10374 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010375 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010376 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010377 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010378 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10380 udelay(40);
10381
Michael Chan314fba32005-04-21 17:07:04 -070010382 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010383 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010384 * register to preserve the GPIO settings for LOMs. The GPIOs,
10385 * whether used as inputs or outputs, are set by boot code after
10386 * reset.
10387 */
Joe Perches63c3a662011-04-26 08:12:10 +000010388 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010389 u32 gpio_mask;
10390
Michael Chan9d26e212006-12-07 00:21:14 -080010391 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10392 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10393 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010394
Joe Perches41535772013-02-16 11:20:04 +000010395 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010396 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10397 GRC_LCLCTRL_GPIO_OUTPUT3;
10398
Joe Perches41535772013-02-16 11:20:04 +000010399 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010400 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10401
Gary Zambranoaaf84462007-05-05 11:51:45 -070010402 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010403 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10404
10405 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010406 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010407 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10408 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010410 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10411 udelay(100);
10412
Matt Carlsonc3b50032012-01-17 15:27:23 +000010413 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010414 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010415 val |= MSGINT_MODE_ENABLE;
10416 if (tp->irq_cnt > 1)
10417 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010418 if (!tg3_flag(tp, 1SHOT_MSI))
10419 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010420 tw32(MSGINT_MODE, val);
10421 }
10422
Joe Perches63c3a662011-04-26 08:12:10 +000010423 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010424 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10425 udelay(40);
10426 }
10427
10428 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10429 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10430 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10431 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10432 WDMAC_MODE_LNGREAD_ENAB);
10433
Joe Perches41535772013-02-16 11:20:04 +000010434 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10435 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010436 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010437 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10438 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010439 /* nothing */
10440 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010441 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010442 val |= WDMAC_MODE_RX_ACCEL;
10443 }
10444 }
10445
Michael Chand9ab5ad12006-03-20 22:27:35 -080010446 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010447 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010448 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010449
Joe Perches41535772013-02-16 11:20:04 +000010450 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010451 val |= WDMAC_MODE_BURST_ALL_DATA;
10452
Linus Torvalds1da177e2005-04-16 15:20:36 -070010453 tw32_f(WDMAC_MODE, val);
10454 udelay(40);
10455
Joe Perches63c3a662011-04-26 08:12:10 +000010456 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010457 u16 pcix_cmd;
10458
10459 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10460 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010461 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010462 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10463 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010464 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010465 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10466 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010467 }
Matt Carlson9974a352007-10-07 23:27:28 -070010468 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10469 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010470 }
10471
10472 tw32_f(RDMAC_MODE, rdmac_mode);
10473 udelay(40);
10474
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010475 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10476 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010477 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10478 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10479 break;
10480 }
10481 if (i < TG3_NUM_RDMA_CHANNELS) {
10482 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010483 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010484 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010485 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010486 }
10487 }
10488
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010490 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010491 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010492
Joe Perches41535772013-02-16 11:20:04 +000010493 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010494 tw32(SNDDATAC_MODE,
10495 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10496 else
10497 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10498
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10500 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010501 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010502 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010503 val |= RCVDBDI_MODE_LRG_RING_SZ;
10504 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010505 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010506 if (tg3_flag(tp, HW_TSO_1) ||
10507 tg3_flag(tp, HW_TSO_2) ||
10508 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010510 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010511 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010512 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10513 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010514 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10515
Joe Perches41535772013-02-16 11:20:04 +000010516 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010517 err = tg3_load_5701_a0_firmware_fix(tp);
10518 if (err)
10519 return err;
10520 }
10521
Nithin Sujirc4dab502013-03-06 17:02:34 +000010522 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10523 /* Ignore any errors for the firmware download. If download
10524 * fails, the device will operate with EEE disabled
10525 */
10526 tg3_load_57766_firmware(tp);
10527 }
10528
Joe Perches63c3a662011-04-26 08:12:10 +000010529 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530 err = tg3_load_tso_firmware(tp);
10531 if (err)
10532 return err;
10533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534
10535 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010536
Joe Perches63c3a662011-04-26 08:12:10 +000010537 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010538 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010539 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010540
Joe Perches41535772013-02-16 11:20:04 +000010541 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10542 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010543 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10544 tp->tx_mode &= ~val;
10545 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10546 }
10547
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548 tw32_f(MAC_TX_MODE, tp->tx_mode);
10549 udelay(100);
10550
Joe Perches63c3a662011-04-26 08:12:10 +000010551 if (tg3_flag(tp, ENABLE_RSS)) {
Eric Dumazet39648352014-11-16 06:23:08 -080010552 u32 rss_key[10];
10553
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010554 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010555
Eric Dumazet39648352014-11-16 06:23:08 -080010556 netdev_rss_key_fill(rss_key, 10 * sizeof(u32));
10557
10558 for (i = 0; i < 10 ; i++)
10559 tw32(MAC_RSS_HASH_KEY_0 + i*4, rss_key[i]);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010560 }
10561
Linus Torvalds1da177e2005-04-16 15:20:36 -070010562 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010563 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010564 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10565
Nithin Sujir378b72c2013-07-29 13:58:39 -070010566 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10567 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10568
Joe Perches63c3a662011-04-26 08:12:10 +000010569 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010570 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10571 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10572 RX_MODE_RSS_IPV6_HASH_EN |
10573 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10574 RX_MODE_RSS_IPV4_HASH_EN |
10575 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10576
Linus Torvalds1da177e2005-04-16 15:20:36 -070010577 tw32_f(MAC_RX_MODE, tp->rx_mode);
10578 udelay(10);
10579
Linus Torvalds1da177e2005-04-16 15:20:36 -070010580 tw32(MAC_LED_CTRL, tp->led_ctrl);
10581
10582 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010583 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010584 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10585 udelay(10);
10586 }
10587 tw32_f(MAC_RX_MODE, tp->rx_mode);
10588 udelay(10);
10589
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010590 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010591 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10592 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010593 /* Set drive transmission level to 1.2V */
10594 /* only if the signal pre-emphasis bit is not set */
10595 val = tr32(MAC_SERDES_CFG);
10596 val &= 0xfffff000;
10597 val |= 0x880;
10598 tw32(MAC_SERDES_CFG, val);
10599 }
Joe Perches41535772013-02-16 11:20:04 +000010600 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601 tw32(MAC_SERDES_CFG, 0x616000);
10602 }
10603
10604 /* Prevent chip from dropping frames when flow control
10605 * is enabled.
10606 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010607 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010608 val = 1;
10609 else
10610 val = 2;
10611 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010612
Joe Perches41535772013-02-16 11:20:04 +000010613 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010614 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010615 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010616 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010617 }
10618
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010619 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010620 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010621 u32 tmp;
10622
10623 tmp = tr32(SERDES_RX_CTRL);
10624 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10625 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10626 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10627 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10628 }
10629
Joe Perches63c3a662011-04-26 08:12:10 +000010630 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010631 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010632 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010633
Joe Perches953c96e2013-04-09 10:18:14 +000010634 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010635 if (err)
10636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010637
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010638 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10639 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010640 u32 tmp;
10641
10642 /* Clear CRC stats. */
10643 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10644 tg3_writephy(tp, MII_TG3_TEST1,
10645 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010646 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010648 }
10649 }
10650
10651 __tg3_set_rx_mode(tp->dev);
10652
10653 /* Initialize receive rules. */
10654 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10655 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10656 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10657 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10658
Joe Perches63c3a662011-04-26 08:12:10 +000010659 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010660 limit = 8;
10661 else
10662 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010663 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010664 limit -= 4;
10665 switch (limit) {
10666 case 16:
10667 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10668 case 15:
10669 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10670 case 14:
10671 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10672 case 13:
10673 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10674 case 12:
10675 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10676 case 11:
10677 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10678 case 10:
10679 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10680 case 9:
10681 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10682 case 8:
10683 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10684 case 7:
10685 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10686 case 6:
10687 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10688 case 5:
10689 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10690 case 4:
10691 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10692 case 3:
10693 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10694 case 2:
10695 case 1:
10696
10697 default:
10698 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010700
Joe Perches63c3a662011-04-26 08:12:10 +000010701 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010702 /* Write our heartbeat update interval to APE. */
10703 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10704 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010705
Linus Torvalds1da177e2005-04-16 15:20:36 -070010706 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10707
Linus Torvalds1da177e2005-04-16 15:20:36 -070010708 return 0;
10709}
10710
10711/* Called at device open time to get the chip ready for
10712 * packet processing. Invoked with tp->lock held.
10713 */
Joe Perches953c96e2013-04-09 10:18:14 +000010714static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010715{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010716 /* Chip may have been just powered on. If so, the boot code may still
10717 * be running initialization. Wait for it to finish to avoid races in
10718 * accessing the hardware.
10719 */
10720 tg3_enable_register_access(tp);
10721 tg3_poll_fw(tp);
10722
Linus Torvalds1da177e2005-04-16 15:20:36 -070010723 tg3_switch_clocks(tp);
10724
10725 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10726
Matt Carlson2f751b62008-08-04 23:17:34 -070010727 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728}
10729
Michael Chanaed93e02012-07-16 16:24:02 +000010730static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10731{
10732 int i;
10733
10734 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10735 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10736
10737 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10738 off += len;
10739
10740 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10741 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10742 memset(ocir, 0, TG3_OCIR_LEN);
10743 }
10744}
10745
10746/* sysfs attributes for hwmon */
10747static ssize_t tg3_show_temp(struct device *dev,
10748 struct device_attribute *devattr, char *buf)
10749{
Michael Chanaed93e02012-07-16 16:24:02 +000010750 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010751 struct tg3 *tp = dev_get_drvdata(dev);
Michael Chanaed93e02012-07-16 16:24:02 +000010752 u32 temperature;
10753
10754 spin_lock_bh(&tp->lock);
10755 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10756 sizeof(temperature));
10757 spin_unlock_bh(&tp->lock);
Jean Delvared3d11fe2015-09-01 18:07:41 +020010758 return sprintf(buf, "%u\n", temperature * 1000);
Michael Chanaed93e02012-07-16 16:24:02 +000010759}
10760
10761
10762static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10763 TG3_TEMP_SENSOR_OFFSET);
10764static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10765 TG3_TEMP_CAUTION_OFFSET);
10766static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10767 TG3_TEMP_MAX_OFFSET);
10768
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010769static struct attribute *tg3_attrs[] = {
Michael Chanaed93e02012-07-16 16:24:02 +000010770 &sensor_dev_attr_temp1_input.dev_attr.attr,
10771 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10772 &sensor_dev_attr_temp1_max.dev_attr.attr,
10773 NULL
10774};
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010775ATTRIBUTE_GROUPS(tg3);
Michael Chanaed93e02012-07-16 16:24:02 +000010776
Michael Chanaed93e02012-07-16 16:24:02 +000010777static void tg3_hwmon_close(struct tg3 *tp)
10778{
Michael Chanaed93e02012-07-16 16:24:02 +000010779 if (tp->hwmon_dev) {
10780 hwmon_device_unregister(tp->hwmon_dev);
10781 tp->hwmon_dev = NULL;
Michael Chanaed93e02012-07-16 16:24:02 +000010782 }
Michael Chanaed93e02012-07-16 16:24:02 +000010783}
10784
10785static void tg3_hwmon_open(struct tg3 *tp)
10786{
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010787 int i;
Michael Chanaed93e02012-07-16 16:24:02 +000010788 u32 size = 0;
10789 struct pci_dev *pdev = tp->pdev;
10790 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10791
10792 tg3_sd_scan_scratchpad(tp, ocirs);
10793
10794 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10795 if (!ocirs[i].src_data_length)
10796 continue;
10797
10798 size += ocirs[i].src_hdr_length;
10799 size += ocirs[i].src_data_length;
10800 }
10801
10802 if (!size)
10803 return;
10804
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010805 tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
10806 tp, tg3_groups);
Michael Chanaed93e02012-07-16 16:24:02 +000010807 if (IS_ERR(tp->hwmon_dev)) {
10808 tp->hwmon_dev = NULL;
10809 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
Michael Chanaed93e02012-07-16 16:24:02 +000010810 }
Michael Chanaed93e02012-07-16 16:24:02 +000010811}
10812
10813
Linus Torvalds1da177e2005-04-16 15:20:36 -070010814#define TG3_STAT_ADD32(PSTAT, REG) \
10815do { u32 __val = tr32(REG); \
10816 (PSTAT)->low += __val; \
10817 if ((PSTAT)->low < __val) \
10818 (PSTAT)->high += 1; \
10819} while (0)
10820
10821static void tg3_periodic_fetch_stats(struct tg3 *tp)
10822{
10823 struct tg3_hw_stats *sp = tp->hw_stats;
10824
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010825 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010826 return;
10827
10828 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10829 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10830 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10831 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10832 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10833 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10834 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10835 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10836 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10837 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10838 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10839 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10840 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010841 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010842 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10843 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10844 u32 val;
10845
10846 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010847 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010848 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010849 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010851
10852 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10853 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10854 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10855 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10856 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10857 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10858 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10859 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10860 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10861 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10862 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10863 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10864 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10865 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010866
10867 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010868 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
Nithin Sujir94962f72013-12-06 09:53:19 -080010869 tg3_asic_rev(tp) != ASIC_REV_5762 &&
Joe Perches41535772013-02-16 11:20:04 +000010870 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10871 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010872 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10873 } else {
10874 u32 val = tr32(HOSTCC_FLOW_ATTN);
10875 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10876 if (val) {
10877 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10878 sp->rx_discards.low += val;
10879 if (sp->rx_discards.low < val)
10880 sp->rx_discards.high += 1;
10881 }
10882 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10883 }
Michael Chan463d3052006-05-22 16:36:27 -070010884 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010885}
10886
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010887static void tg3_chk_missed_msi(struct tg3 *tp)
10888{
10889 u32 i;
10890
10891 for (i = 0; i < tp->irq_cnt; i++) {
10892 struct tg3_napi *tnapi = &tp->napi[i];
10893
10894 if (tg3_has_work(tnapi)) {
10895 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10896 tnapi->last_tx_cons == tnapi->tx_cons) {
10897 if (tnapi->chk_msi_cnt < 1) {
10898 tnapi->chk_msi_cnt++;
10899 return;
10900 }
Matt Carlson7f230732011-08-31 11:44:48 +000010901 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010902 }
10903 }
10904 tnapi->chk_msi_cnt = 0;
10905 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10906 tnapi->last_tx_cons = tnapi->tx_cons;
10907 }
10908}
10909
Linus Torvalds1da177e2005-04-16 15:20:36 -070010910static void tg3_timer(unsigned long __opaque)
10911{
10912 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010913
David S. Millerf47c11e2005-06-24 20:18:35 -070010914 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010915
Prashant Sreedharan4fd190a2015-01-14 11:33:49 -080010916 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING)) {
10917 spin_unlock(&tp->lock);
10918 goto restart_timer;
10919 }
10920
Joe Perches41535772013-02-16 11:20:04 +000010921 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010922 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010923 tg3_chk_missed_msi(tp);
10924
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010925 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10926 /* BCM4785: Flush posted writes from GbE to host memory. */
10927 tr32(HOSTCC_MODE);
10928 }
10929
Joe Perches63c3a662011-04-26 08:12:10 +000010930 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010931 /* All of this garbage is because when using non-tagged
10932 * IRQ status the mailbox/status_block protocol the chip
10933 * uses with the cpu is race prone.
10934 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010935 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010936 tw32(GRC_LOCAL_CTRL,
10937 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10938 } else {
10939 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010940 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010942
David S. Millerfac9b832005-05-18 22:46:34 -070010943 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010944 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010945 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010946 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010948 }
10949
Linus Torvalds1da177e2005-04-16 15:20:36 -070010950 /* This part only runs once per second. */
10951 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010952 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010953 tg3_periodic_fetch_stats(tp);
10954
Matt Carlsonb0c59432011-05-19 12:12:48 +000010955 if (tp->setlpicnt && !--tp->setlpicnt)
10956 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010957
Joe Perches63c3a662011-04-26 08:12:10 +000010958 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010959 u32 mac_stat;
10960 int phy_event;
10961
10962 mac_stat = tr32(MAC_STATUS);
10963
10964 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010965 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010966 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10967 phy_event = 1;
10968 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10969 phy_event = 1;
10970
10971 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010972 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010973 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010974 u32 mac_stat = tr32(MAC_STATUS);
10975 int need_setup = 0;
10976
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010977 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010978 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10979 need_setup = 1;
10980 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010981 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010982 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10983 MAC_STATUS_SIGNAL_DET))) {
10984 need_setup = 1;
10985 }
10986 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010987 if (!tp->serdes_counter) {
10988 tw32_f(MAC_MODE,
10989 (tp->mac_mode &
10990 ~MAC_MODE_PORT_MODE_MASK));
10991 udelay(40);
10992 tw32_f(MAC_MODE, tp->mac_mode);
10993 udelay(40);
10994 }
Joe Perches953c96e2013-04-09 10:18:14 +000010995 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010996 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010997 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010998 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010999 tg3_serdes_parallel_detect(tp);
Nithin Sujir1743b832014-01-03 10:09:14 -080011000 } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
11001 u32 cpmu = tr32(TG3_CPMU_STATUS);
11002 bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
11003 TG3_CPMU_STATUS_LINK_MASK);
11004
11005 if (link_up != tp->link_up)
11006 tg3_setup_phy(tp, false);
Matt Carlson57d8b882010-06-05 17:24:35 +000011007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011008
11009 tp->timer_counter = tp->timer_multiplier;
11010 }
11011
Michael Chan130b8e42006-09-27 16:00:40 -070011012 /* Heartbeat is only sent once every 2 seconds.
11013 *
11014 * The heartbeat is to tell the ASF firmware that the host
11015 * driver is still alive. In the event that the OS crashes,
11016 * ASF needs to reset the hardware to free up the FIFO space
11017 * that may be filled with rx packets destined for the host.
11018 * If the FIFO is full, ASF will no longer function properly.
11019 *
11020 * Unintended resets have been reported on real time kernels
11021 * where the timer doesn't run on time. Netpoll will also have
11022 * same problem.
11023 *
11024 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
11025 * to check the ring condition when the heartbeat is expiring
11026 * before doing the reset. This will prevent most unintended
11027 * resets.
11028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011029 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000011030 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070011031 tg3_wait_for_event_ack(tp);
11032
Michael Chanbbadf502006-04-06 21:46:34 -070011033 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070011034 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070011035 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011036 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
11037 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070011038
11039 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011040 }
11041 tp->asf_counter = tp->asf_multiplier;
11042 }
11043
David S. Millerf47c11e2005-06-24 20:18:35 -070011044 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011045
Michael Chanf475f162006-03-27 23:20:14 -080011046restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011047 tp->timer.expires = jiffies + tp->timer_offset;
11048 add_timer(&tp->timer);
11049}
11050
Bill Pemberton229b1ad2012-12-03 09:22:59 -050011051static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000011052{
11053 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000011054 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000011055 !tg3_flag(tp, 57765_CLASS))
11056 tp->timer_offset = HZ;
11057 else
11058 tp->timer_offset = HZ / 10;
11059
11060 BUG_ON(tp->timer_offset > HZ);
11061
11062 tp->timer_multiplier = (HZ / tp->timer_offset);
11063 tp->asf_multiplier = (HZ / tp->timer_offset) *
11064 TG3_FW_UPDATE_FREQ_SEC;
11065
11066 init_timer(&tp->timer);
11067 tp->timer.data = (unsigned long) tp;
11068 tp->timer.function = tg3_timer;
11069}
11070
11071static void tg3_timer_start(struct tg3 *tp)
11072{
11073 tp->asf_counter = tp->asf_multiplier;
11074 tp->timer_counter = tp->timer_multiplier;
11075
11076 tp->timer.expires = jiffies + tp->timer_offset;
11077 add_timer(&tp->timer);
11078}
11079
11080static void tg3_timer_stop(struct tg3 *tp)
11081{
11082 del_timer_sync(&tp->timer);
11083}
11084
11085/* Restart hardware after configuration changes, self-test, etc.
11086 * Invoked with tp->lock held.
11087 */
Joe Perches953c96e2013-04-09 10:18:14 +000011088static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000011089 __releases(tp->lock)
11090 __acquires(tp->lock)
11091{
11092 int err;
11093
11094 err = tg3_init_hw(tp, reset_phy);
11095 if (err) {
11096 netdev_err(tp->dev,
11097 "Failed to re-initialize device, aborting\n");
11098 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11099 tg3_full_unlock(tp);
11100 tg3_timer_stop(tp);
11101 tp->irq_sync = 0;
11102 tg3_napi_enable(tp);
11103 dev_close(tp->dev);
11104 tg3_full_lock(tp, 0);
11105 }
11106 return err;
11107}
11108
11109static void tg3_reset_task(struct work_struct *work)
11110{
11111 struct tg3 *tp = container_of(work, struct tg3, reset_task);
11112 int err;
11113
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011114 rtnl_lock();
Matt Carlson21f76382012-02-22 12:35:21 +000011115 tg3_full_lock(tp, 0);
11116
11117 if (!netif_running(tp->dev)) {
11118 tg3_flag_clear(tp, RESET_TASK_PENDING);
11119 tg3_full_unlock(tp);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011120 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011121 return;
11122 }
11123
11124 tg3_full_unlock(tp);
11125
11126 tg3_phy_stop(tp);
11127
11128 tg3_netif_stop(tp);
11129
11130 tg3_full_lock(tp, 1);
11131
11132 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11133 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11134 tp->write32_rx_mbox = tg3_write_flush_reg32;
11135 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11136 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11137 }
11138
11139 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011140 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011141 if (err)
11142 goto out;
11143
11144 tg3_netif_start(tp);
11145
11146out:
11147 tg3_full_unlock(tp);
11148
11149 if (!err)
11150 tg3_phy_start(tp);
11151
11152 tg3_flag_clear(tp, RESET_TASK_PENDING);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011153 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011154}
11155
Matt Carlson4f125f42009-09-01 12:55:02 +000011156static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011157{
David Howells7d12e782006-10-05 14:55:46 +010011158 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011159 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011160 char *name;
11161 struct tg3_napi *tnapi = &tp->napi[irq_num];
11162
11163 if (tp->irq_cnt == 1)
11164 name = tp->dev->name;
11165 else {
11166 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011167 if (tnapi->tx_buffers && tnapi->rx_rcb)
11168 snprintf(name, IFNAMSIZ,
11169 "%s-txrx-%d", tp->dev->name, irq_num);
11170 else if (tnapi->tx_buffers)
11171 snprintf(name, IFNAMSIZ,
11172 "%s-tx-%d", tp->dev->name, irq_num);
11173 else if (tnapi->rx_rcb)
11174 snprintf(name, IFNAMSIZ,
11175 "%s-rx-%d", tp->dev->name, irq_num);
11176 else
11177 snprintf(name, IFNAMSIZ,
11178 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011179 name[IFNAMSIZ-1] = 0;
11180 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011181
Joe Perches63c3a662011-04-26 08:12:10 +000011182 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011183 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011184 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011185 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011186 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011187 } else {
11188 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011189 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011190 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011191 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011192 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011193
11194 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011195}
11196
Michael Chan79381092005-04-21 17:13:59 -070011197static int tg3_test_interrupt(struct tg3 *tp)
11198{
Matt Carlson09943a12009-08-28 14:01:57 +000011199 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011200 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011201 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011202 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011203
Michael Chand4bc3922005-05-29 14:59:20 -070011204 if (!netif_running(dev))
11205 return -ENODEV;
11206
Michael Chan79381092005-04-21 17:13:59 -070011207 tg3_disable_ints(tp);
11208
Matt Carlson4f125f42009-09-01 12:55:02 +000011209 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011210
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011211 /*
11212 * Turn off MSI one shot mode. Otherwise this test has no
11213 * observable way to know whether the interrupt was delivered.
11214 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011215 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011216 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11217 tw32(MSGINT_MODE, val);
11218 }
11219
Matt Carlson4f125f42009-09-01 12:55:02 +000011220 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011221 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011222 if (err)
11223 return err;
11224
Matt Carlson898a56f2009-08-28 14:02:40 +000011225 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011226 tg3_enable_ints(tp);
11227
11228 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011229 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011230
11231 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011232 u32 int_mbox, misc_host_ctrl;
11233
Matt Carlson898a56f2009-08-28 14:02:40 +000011234 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011235 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11236
11237 if ((int_mbox != 0) ||
11238 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11239 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011240 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011241 }
11242
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011243 if (tg3_flag(tp, 57765_PLUS) &&
11244 tnapi->hw_status->status_tag != tnapi->last_tag)
11245 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11246
Michael Chan79381092005-04-21 17:13:59 -070011247 msleep(10);
11248 }
11249
11250 tg3_disable_ints(tp);
11251
Matt Carlson4f125f42009-09-01 12:55:02 +000011252 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011253
Matt Carlson4f125f42009-09-01 12:55:02 +000011254 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011255
11256 if (err)
11257 return err;
11258
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011259 if (intr_ok) {
11260 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011261 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011262 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11263 tw32(MSGINT_MODE, val);
11264 }
Michael Chan79381092005-04-21 17:13:59 -070011265 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011266 }
Michael Chan79381092005-04-21 17:13:59 -070011267
11268 return -EIO;
11269}
11270
11271/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11272 * successfully restored
11273 */
11274static int tg3_test_msi(struct tg3 *tp)
11275{
Michael Chan79381092005-04-21 17:13:59 -070011276 int err;
11277 u16 pci_cmd;
11278
Joe Perches63c3a662011-04-26 08:12:10 +000011279 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011280 return 0;
11281
11282 /* Turn off SERR reporting in case MSI terminates with Master
11283 * Abort.
11284 */
11285 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11286 pci_write_config_word(tp->pdev, PCI_COMMAND,
11287 pci_cmd & ~PCI_COMMAND_SERR);
11288
11289 err = tg3_test_interrupt(tp);
11290
11291 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11292
11293 if (!err)
11294 return 0;
11295
11296 /* other failures */
11297 if (err != -EIO)
11298 return err;
11299
11300 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011301 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11302 "to INTx mode. Please report this failure to the PCI "
11303 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011304
Matt Carlson4f125f42009-09-01 12:55:02 +000011305 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011306
Michael Chan79381092005-04-21 17:13:59 -070011307 pci_disable_msi(tp->pdev);
11308
Joe Perches63c3a662011-04-26 08:12:10 +000011309 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011310 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011311
Matt Carlson4f125f42009-09-01 12:55:02 +000011312 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011313 if (err)
11314 return err;
11315
11316 /* Need to reset the chip because the MSI cycle may have terminated
11317 * with Master Abort.
11318 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011319 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011320
Michael Chan944d9802005-05-29 14:57:48 -070011321 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011322 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011323
David S. Millerf47c11e2005-06-24 20:18:35 -070011324 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011325
11326 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011327 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011328
11329 return err;
11330}
11331
Matt Carlson9e9fd122009-01-19 16:57:45 -080011332static int tg3_request_firmware(struct tg3 *tp)
11333{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011334 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011335
11336 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011337 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11338 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011339 return -ENOENT;
11340 }
11341
Nithin Sujir77997ea2013-03-06 17:02:32 +000011342 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011343
11344 /* Firmware blob starts with version numbers, followed by
11345 * start address and _full_ length including BSS sections
11346 * (which must be longer than the actual data, of course
11347 */
11348
Nithin Sujir77997ea2013-03-06 17:02:32 +000011349 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11350 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011351 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11352 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011353 release_firmware(tp->fw);
11354 tp->fw = NULL;
11355 return -EINVAL;
11356 }
11357
11358 /* We no longer need firmware; we have it. */
11359 tp->fw_needed = NULL;
11360 return 0;
11361}
11362
Michael Chan91024262012-09-28 07:12:38 +000011363static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011364{
Michael Chan91024262012-09-28 07:12:38 +000011365 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011366
Michael Chan91024262012-09-28 07:12:38 +000011367 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011368 /* We want as many rx rings enabled as there are cpus.
11369 * In multiqueue MSI-X mode, the first MSI-X vector
11370 * only deals with link interrupts, etc, so we add
11371 * one to the number of vectors we are requesting.
11372 */
Michael Chan91024262012-09-28 07:12:38 +000011373 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011374 }
Matt Carlson679563f2009-09-01 12:55:46 +000011375
Michael Chan91024262012-09-28 07:12:38 +000011376 return irq_cnt;
11377}
11378
11379static bool tg3_enable_msix(struct tg3 *tp)
11380{
11381 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011382 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011383
Michael Chan09681692012-09-28 07:12:42 +000011384 tp->txq_cnt = tp->txq_req;
11385 tp->rxq_cnt = tp->rxq_req;
11386 if (!tp->rxq_cnt)
11387 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011388 if (tp->rxq_cnt > tp->rxq_max)
11389 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011390
11391 /* Disable multiple TX rings by default. Simple round-robin hardware
11392 * scheduling of the TX rings can cause starvation of rings with
11393 * small packets when other rings have TSO or jumbo packets.
11394 */
11395 if (!tp->txq_req)
11396 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011397
11398 tp->irq_cnt = tg3_irq_count(tp);
11399
Matt Carlson679563f2009-09-01 12:55:46 +000011400 for (i = 0; i < tp->irq_max; i++) {
11401 msix_ent[i].entry = i;
11402 msix_ent[i].vector = 0;
11403 }
11404
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011405 rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011406 if (rc < 0) {
11407 return false;
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011408 } else if (rc < tp->irq_cnt) {
Joe Perches05dbe002010-02-17 19:44:19 +000011409 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11410 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011411 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011412 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011413 if (tp->txq_cnt)
11414 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011415 }
11416
11417 for (i = 0; i < tp->irq_max; i++)
11418 tp->napi[i].irq_vec = msix_ent[i].vector;
11419
Michael Chan49a359e2012-09-28 07:12:37 +000011420 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011421 pci_disable_msix(tp->pdev);
11422 return false;
11423 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011424
Michael Chan91024262012-09-28 07:12:38 +000011425 if (tp->irq_cnt == 1)
11426 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011427
Michael Chan91024262012-09-28 07:12:38 +000011428 tg3_flag_set(tp, ENABLE_RSS);
11429
11430 if (tp->txq_cnt > 1)
11431 tg3_flag_set(tp, ENABLE_TSS);
11432
11433 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011434
Matt Carlson679563f2009-09-01 12:55:46 +000011435 return true;
11436}
11437
Matt Carlson07b01732009-08-28 14:01:15 +000011438static void tg3_ints_init(struct tg3 *tp)
11439{
Joe Perches63c3a662011-04-26 08:12:10 +000011440 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11441 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011442 /* All MSI supporting chips should support tagged
11443 * status. Assert that this is the case.
11444 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011445 netdev_warn(tp->dev,
11446 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011447 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011448 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011449
Joe Perches63c3a662011-04-26 08:12:10 +000011450 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11451 tg3_flag_set(tp, USING_MSIX);
11452 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11453 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011454
Joe Perches63c3a662011-04-26 08:12:10 +000011455 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011456 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011457 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011458 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011459 if (!tg3_flag(tp, 1SHOT_MSI))
11460 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011461 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11462 }
11463defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011464 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011465 tp->irq_cnt = 1;
11466 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011467 }
11468
11469 if (tp->irq_cnt == 1) {
11470 tp->txq_cnt = 1;
11471 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011472 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011473 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011474 }
Matt Carlson07b01732009-08-28 14:01:15 +000011475}
11476
11477static void tg3_ints_fini(struct tg3 *tp)
11478{
Joe Perches63c3a662011-04-26 08:12:10 +000011479 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011480 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011481 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011482 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011483 tg3_flag_clear(tp, USING_MSI);
11484 tg3_flag_clear(tp, USING_MSIX);
11485 tg3_flag_clear(tp, ENABLE_RSS);
11486 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011487}
11488
Matt Carlsonbe947302012-12-03 19:36:57 +000011489static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11490 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011491{
Michael Chand8f4cd32012-09-28 07:12:40 +000011492 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011493 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011494
Matt Carlson679563f2009-09-01 12:55:46 +000011495 /*
11496 * Setup interrupts first so we know how
11497 * many NAPI resources to allocate
11498 */
11499 tg3_ints_init(tp);
11500
Matt Carlson90415472011-12-16 13:33:23 +000011501 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011502
Linus Torvalds1da177e2005-04-16 15:20:36 -070011503 /* The placement of this call is tied
11504 * to the setup and use of Host TX descriptors.
11505 */
11506 err = tg3_alloc_consistent(tp);
11507 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011508 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011509
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011510 tg3_napi_init(tp);
11511
Matt Carlsonfed97812009-09-01 13:10:19 +000011512 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011513
Matt Carlson4f125f42009-09-01 12:55:02 +000011514 for (i = 0; i < tp->irq_cnt; i++) {
11515 struct tg3_napi *tnapi = &tp->napi[i];
11516 err = tg3_request_irq(tp, i);
11517 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011518 for (i--; i >= 0; i--) {
11519 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011520 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011521 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011522 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011523 }
11524 }
Matt Carlson07b01732009-08-28 14:01:15 +000011525
David S. Millerf47c11e2005-06-24 20:18:35 -070011526 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011527
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011528 if (init)
11529 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11530
Michael Chand8f4cd32012-09-28 07:12:40 +000011531 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011532 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011533 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011534 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011535 }
11536
David S. Millerf47c11e2005-06-24 20:18:35 -070011537 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011538
Matt Carlson07b01732009-08-28 14:01:15 +000011539 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011540 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011541
Michael Chand8f4cd32012-09-28 07:12:40 +000011542 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011543 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011544
Michael Chan79381092005-04-21 17:13:59 -070011545 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011546 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011547 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011548 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011549 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011550
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011551 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011552 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011553
Joe Perches63c3a662011-04-26 08:12:10 +000011554 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011555 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011556
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011557 tw32(PCIE_TRANSACTION_CFG,
11558 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011559 }
Michael Chan79381092005-04-21 17:13:59 -070011560 }
11561
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011562 tg3_phy_start(tp);
11563
Michael Chanaed93e02012-07-16 16:24:02 +000011564 tg3_hwmon_open(tp);
11565
David S. Millerf47c11e2005-06-24 20:18:35 -070011566 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011567
Matt Carlson21f76382012-02-22 12:35:21 +000011568 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011569 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011570 tg3_enable_ints(tp);
11571
Ivan Vecera20d14a52015-01-08 16:13:07 +010011572 tg3_ptp_resume(tp);
Matt Carlsonbe947302012-12-03 19:36:57 +000011573
David S. Millerf47c11e2005-06-24 20:18:35 -070011574 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011575
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011576 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011577
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011578 /*
11579 * Reset loopback feature if it was turned on while the device was down
11580 * make sure that it's installed properly now.
11581 */
11582 if (dev->features & NETIF_F_LOOPBACK)
11583 tg3_set_loopback(dev, dev->features);
11584
Linus Torvalds1da177e2005-04-16 15:20:36 -070011585 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011586
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011587out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011588 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11589 struct tg3_napi *tnapi = &tp->napi[i];
11590 free_irq(tnapi->irq_vec, tnapi);
11591 }
Matt Carlson07b01732009-08-28 14:01:15 +000011592
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011593out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011594 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011595 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011596 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011597
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011598out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011599 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011600
Matt Carlson07b01732009-08-28 14:01:15 +000011601 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011602}
11603
Michael Chan65138592012-09-28 07:12:41 +000011604static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011605{
Matt Carlson4f125f42009-09-01 12:55:02 +000011606 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011607
Matt Carlsondb219972011-11-04 09:15:03 +000011608 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011609 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011610
Matt Carlson21f76382012-02-22 12:35:21 +000011611 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011612
Michael Chanaed93e02012-07-16 16:24:02 +000011613 tg3_hwmon_close(tp);
11614
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011615 tg3_phy_stop(tp);
11616
David S. Millerf47c11e2005-06-24 20:18:35 -070011617 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011618
11619 tg3_disable_ints(tp);
11620
Michael Chan944d9802005-05-29 14:57:48 -070011621 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011622 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011623 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011624
David S. Millerf47c11e2005-06-24 20:18:35 -070011625 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011626
Matt Carlson4f125f42009-09-01 12:55:02 +000011627 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11628 struct tg3_napi *tnapi = &tp->napi[i];
11629 free_irq(tnapi->irq_vec, tnapi);
11630 }
Matt Carlson07b01732009-08-28 14:01:15 +000011631
11632 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011633
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011634 tg3_napi_fini(tp);
11635
Linus Torvalds1da177e2005-04-16 15:20:36 -070011636 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011637}
11638
Michael Chand8f4cd32012-09-28 07:12:40 +000011639static int tg3_open(struct net_device *dev)
11640{
11641 struct tg3 *tp = netdev_priv(dev);
11642 int err;
11643
Ivan Vecera0486a062014-09-01 14:21:57 +020011644 if (tp->pcierr_recovery) {
11645 netdev_err(dev, "Failed to open device. PCI error recovery "
11646 "in progress\n");
11647 return -EAGAIN;
11648 }
11649
Michael Chand8f4cd32012-09-28 07:12:40 +000011650 if (tp->fw_needed) {
11651 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011652 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11653 if (err) {
11654 netdev_warn(tp->dev, "EEE capability disabled\n");
11655 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11656 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11657 netdev_warn(tp->dev, "EEE capability restored\n");
11658 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11659 }
11660 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011661 if (err)
11662 return err;
11663 } else if (err) {
11664 netdev_warn(tp->dev, "TSO capability disabled\n");
11665 tg3_flag_clear(tp, TSO_CAPABLE);
11666 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11667 netdev_notice(tp->dev, "TSO capability restored\n");
11668 tg3_flag_set(tp, TSO_CAPABLE);
11669 }
11670 }
11671
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011672 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011673
11674 err = tg3_power_up(tp);
11675 if (err)
11676 return err;
11677
11678 tg3_full_lock(tp, 0);
11679
11680 tg3_disable_ints(tp);
11681 tg3_flag_clear(tp, INIT_COMPLETE);
11682
11683 tg3_full_unlock(tp);
11684
Nithin Sujir942d1af2013-04-09 08:48:07 +000011685 err = tg3_start(tp,
11686 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11687 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011688 if (err) {
11689 tg3_frob_aux_power(tp, false);
11690 pci_set_power_state(tp->pdev, PCI_D3hot);
11691 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011692
Linus Torvalds1da177e2005-04-16 15:20:36 -070011693 return err;
11694}
11695
11696static int tg3_close(struct net_device *dev)
11697{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011698 struct tg3 *tp = netdev_priv(dev);
11699
Ivan Vecera0486a062014-09-01 14:21:57 +020011700 if (tp->pcierr_recovery) {
11701 netdev_err(dev, "Failed to close device. PCI error recovery "
11702 "in progress\n");
11703 return -EAGAIN;
11704 }
11705
Michael Chan65138592012-09-28 07:12:41 +000011706 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011707
11708 /* Clear stats across close / open calls */
11709 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11710 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011711
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011712 if (pci_device_is_present(tp->pdev)) {
11713 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011714
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011715 tg3_carrier_off(tp);
11716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011717 return 0;
11718}
11719
11720static inline u64 get_stat64(tg3_stat64_t *val)
11721{
11722 return ((u64)val->high << 32) | ((u64)val->low);
11723}
11724
11725static u64 tg3_calc_crc_errors(struct tg3 *tp)
11726{
11727 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11728
11729 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011730 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11731 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011732 u32 val;
11733
11734 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11735 tg3_writephy(tp, MII_TG3_TEST1,
11736 val | MII_TG3_TEST1_CRC_EN);
11737 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11738 } else
11739 val = 0;
11740
11741 tp->phy_crc_errors += val;
11742
11743 return tp->phy_crc_errors;
11744 }
11745
11746 return get_stat64(&hw_stats->rx_fcs_errors);
11747}
11748
11749#define ESTAT_ADD(member) \
11750 estats->member = old_estats->member + \
11751 get_stat64(&hw_stats->member)
11752
11753static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11754{
11755 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11756 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11757
11758 ESTAT_ADD(rx_octets);
11759 ESTAT_ADD(rx_fragments);
11760 ESTAT_ADD(rx_ucast_packets);
11761 ESTAT_ADD(rx_mcast_packets);
11762 ESTAT_ADD(rx_bcast_packets);
11763 ESTAT_ADD(rx_fcs_errors);
11764 ESTAT_ADD(rx_align_errors);
11765 ESTAT_ADD(rx_xon_pause_rcvd);
11766 ESTAT_ADD(rx_xoff_pause_rcvd);
11767 ESTAT_ADD(rx_mac_ctrl_rcvd);
11768 ESTAT_ADD(rx_xoff_entered);
11769 ESTAT_ADD(rx_frame_too_long_errors);
11770 ESTAT_ADD(rx_jabbers);
11771 ESTAT_ADD(rx_undersize_packets);
11772 ESTAT_ADD(rx_in_length_errors);
11773 ESTAT_ADD(rx_out_length_errors);
11774 ESTAT_ADD(rx_64_or_less_octet_packets);
11775 ESTAT_ADD(rx_65_to_127_octet_packets);
11776 ESTAT_ADD(rx_128_to_255_octet_packets);
11777 ESTAT_ADD(rx_256_to_511_octet_packets);
11778 ESTAT_ADD(rx_512_to_1023_octet_packets);
11779 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11780 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11781 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11782 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11783 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11784
11785 ESTAT_ADD(tx_octets);
11786 ESTAT_ADD(tx_collisions);
11787 ESTAT_ADD(tx_xon_sent);
11788 ESTAT_ADD(tx_xoff_sent);
11789 ESTAT_ADD(tx_flow_control);
11790 ESTAT_ADD(tx_mac_errors);
11791 ESTAT_ADD(tx_single_collisions);
11792 ESTAT_ADD(tx_mult_collisions);
11793 ESTAT_ADD(tx_deferred);
11794 ESTAT_ADD(tx_excessive_collisions);
11795 ESTAT_ADD(tx_late_collisions);
11796 ESTAT_ADD(tx_collide_2times);
11797 ESTAT_ADD(tx_collide_3times);
11798 ESTAT_ADD(tx_collide_4times);
11799 ESTAT_ADD(tx_collide_5times);
11800 ESTAT_ADD(tx_collide_6times);
11801 ESTAT_ADD(tx_collide_7times);
11802 ESTAT_ADD(tx_collide_8times);
11803 ESTAT_ADD(tx_collide_9times);
11804 ESTAT_ADD(tx_collide_10times);
11805 ESTAT_ADD(tx_collide_11times);
11806 ESTAT_ADD(tx_collide_12times);
11807 ESTAT_ADD(tx_collide_13times);
11808 ESTAT_ADD(tx_collide_14times);
11809 ESTAT_ADD(tx_collide_15times);
11810 ESTAT_ADD(tx_ucast_packets);
11811 ESTAT_ADD(tx_mcast_packets);
11812 ESTAT_ADD(tx_bcast_packets);
11813 ESTAT_ADD(tx_carrier_sense_errors);
11814 ESTAT_ADD(tx_discards);
11815 ESTAT_ADD(tx_errors);
11816
11817 ESTAT_ADD(dma_writeq_full);
11818 ESTAT_ADD(dma_write_prioq_full);
11819 ESTAT_ADD(rxbds_empty);
11820 ESTAT_ADD(rx_discards);
11821 ESTAT_ADD(rx_errors);
11822 ESTAT_ADD(rx_threshold_hit);
11823
11824 ESTAT_ADD(dma_readq_full);
11825 ESTAT_ADD(dma_read_prioq_full);
11826 ESTAT_ADD(tx_comp_queue_full);
11827
11828 ESTAT_ADD(ring_set_send_prod_index);
11829 ESTAT_ADD(ring_status_update);
11830 ESTAT_ADD(nic_irqs);
11831 ESTAT_ADD(nic_avoided_irqs);
11832 ESTAT_ADD(nic_tx_threshold_hit);
11833
Matt Carlson4452d092011-05-19 12:12:51 +000011834 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011835}
11836
Matt Carlson65ec6982012-02-28 23:33:37 +000011837static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011838{
Eric Dumazet511d2222010-07-07 20:44:24 +000011839 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011840 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11841
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842 stats->rx_packets = old_stats->rx_packets +
11843 get_stat64(&hw_stats->rx_ucast_packets) +
11844 get_stat64(&hw_stats->rx_mcast_packets) +
11845 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011846
Linus Torvalds1da177e2005-04-16 15:20:36 -070011847 stats->tx_packets = old_stats->tx_packets +
11848 get_stat64(&hw_stats->tx_ucast_packets) +
11849 get_stat64(&hw_stats->tx_mcast_packets) +
11850 get_stat64(&hw_stats->tx_bcast_packets);
11851
11852 stats->rx_bytes = old_stats->rx_bytes +
11853 get_stat64(&hw_stats->rx_octets);
11854 stats->tx_bytes = old_stats->tx_bytes +
11855 get_stat64(&hw_stats->tx_octets);
11856
11857 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011858 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011859 stats->tx_errors = old_stats->tx_errors +
11860 get_stat64(&hw_stats->tx_errors) +
11861 get_stat64(&hw_stats->tx_mac_errors) +
11862 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11863 get_stat64(&hw_stats->tx_discards);
11864
11865 stats->multicast = old_stats->multicast +
11866 get_stat64(&hw_stats->rx_mcast_packets);
11867 stats->collisions = old_stats->collisions +
11868 get_stat64(&hw_stats->tx_collisions);
11869
11870 stats->rx_length_errors = old_stats->rx_length_errors +
11871 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11872 get_stat64(&hw_stats->rx_undersize_packets);
11873
Linus Torvalds1da177e2005-04-16 15:20:36 -070011874 stats->rx_frame_errors = old_stats->rx_frame_errors +
11875 get_stat64(&hw_stats->rx_align_errors);
11876 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11877 get_stat64(&hw_stats->tx_discards);
11878 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11879 get_stat64(&hw_stats->tx_carrier_sense_errors);
11880
11881 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011882 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011883
John W. Linville4f63b872005-09-12 14:43:18 -070011884 stats->rx_missed_errors = old_stats->rx_missed_errors +
11885 get_stat64(&hw_stats->rx_discards);
11886
Eric Dumazetb0057c52010-10-10 19:55:52 +000011887 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011888 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889}
11890
Linus Torvalds1da177e2005-04-16 15:20:36 -070011891static int tg3_get_regs_len(struct net_device *dev)
11892{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011893 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011894}
11895
11896static void tg3_get_regs(struct net_device *dev,
11897 struct ethtool_regs *regs, void *_p)
11898{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011899 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011900
11901 regs->version = 0;
11902
Matt Carlson97bd8e42011-04-13 11:05:04 +000011903 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011904
Matt Carlson80096062010-08-02 11:26:06 +000011905 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011906 return;
11907
David S. Millerf47c11e2005-06-24 20:18:35 -070011908 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011909
Matt Carlson97bd8e42011-04-13 11:05:04 +000011910 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011911
David S. Millerf47c11e2005-06-24 20:18:35 -070011912 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011913}
11914
11915static int tg3_get_eeprom_len(struct net_device *dev)
11916{
11917 struct tg3 *tp = netdev_priv(dev);
11918
11919 return tp->nvram_size;
11920}
11921
Linus Torvalds1da177e2005-04-16 15:20:36 -070011922static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11923{
11924 struct tg3 *tp = netdev_priv(dev);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011925 int ret, cpmu_restore = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926 u8 *pd;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011927 u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011928 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929
Joe Perches63c3a662011-04-26 08:12:10 +000011930 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011931 return -EINVAL;
11932
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933 offset = eeprom->offset;
11934 len = eeprom->len;
11935 eeprom->len = 0;
11936
11937 eeprom->magic = TG3_EEPROM_MAGIC;
11938
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011939 /* Override clock, link aware and link idle modes */
11940 if (tg3_flag(tp, CPMU_PRESENT)) {
11941 cpmu_val = tr32(TG3_CPMU_CTRL);
11942 if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
11943 CPMU_CTRL_LINK_IDLE_MODE)) {
11944 tw32(TG3_CPMU_CTRL, cpmu_val &
11945 ~(CPMU_CTRL_LINK_AWARE_MODE |
11946 CPMU_CTRL_LINK_IDLE_MODE));
11947 cpmu_restore = 1;
11948 }
11949 }
11950 tg3_override_clk(tp);
11951
Linus Torvalds1da177e2005-04-16 15:20:36 -070011952 if (offset & 3) {
11953 /* adjustments to start on required 4 byte boundary */
11954 b_offset = offset & 3;
11955 b_count = 4 - b_offset;
11956 if (b_count > len) {
11957 /* i.e. offset=1 len=2 */
11958 b_count = len;
11959 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011960 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011961 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011962 goto eeprom_done;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011963 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011964 len -= b_count;
11965 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011966 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011967 }
11968
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011969 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011970 pd = &data[eeprom->len];
11971 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011972 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011973 if (ret) {
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011974 if (i)
11975 i -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976 eeprom->len += i;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011977 goto eeprom_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011979 memcpy(pd + i, &val, 4);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011980 if (need_resched()) {
11981 if (signal_pending(current)) {
11982 eeprom->len += i;
11983 ret = -EINTR;
11984 goto eeprom_done;
11985 }
11986 cond_resched();
11987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011988 }
11989 eeprom->len += i;
11990
11991 if (len & 3) {
11992 /* read last bytes not ending on 4 byte boundary */
11993 pd = &data[eeprom->len];
11994 b_count = len & 3;
11995 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011996 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011997 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011998 goto eeprom_done;
Al Virob9fc7dc2007-12-17 22:59:57 -080011999 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012000 eeprom->len += b_count;
12001 }
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012002 ret = 0;
12003
12004eeprom_done:
12005 /* Restore clock, link aware and link idle modes */
12006 tg3_restore_clk(tp);
12007 if (cpmu_restore)
12008 tw32(TG3_CPMU_CTRL, cpmu_val);
12009
12010 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012011}
12012
Linus Torvalds1da177e2005-04-16 15:20:36 -070012013static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
12014{
12015 struct tg3 *tp = netdev_priv(dev);
12016 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080012017 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012018 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012019 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020
Joe Perches63c3a662011-04-26 08:12:10 +000012021 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000012022 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012023 return -EINVAL;
12024
12025 offset = eeprom->offset;
12026 len = eeprom->len;
12027
12028 if ((b_offset = (offset & 3))) {
12029 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012030 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012031 if (ret)
12032 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012033 len += b_offset;
12034 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070012035 if (len < 4)
12036 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012037 }
12038
12039 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070012040 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012041 /* adjustments to end on required 4 byte boundary */
12042 odd_len = 1;
12043 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012044 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012045 if (ret)
12046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012047 }
12048
12049 buf = data;
12050 if (b_offset || odd_len) {
12051 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012052 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012053 return -ENOMEM;
12054 if (b_offset)
12055 memcpy(buf, &start, 4);
12056 if (odd_len)
12057 memcpy(buf+len-4, &end, 4);
12058 memcpy(buf + b_offset, data, eeprom->len);
12059 }
12060
12061 ret = tg3_nvram_write_block(tp, offset, len, buf);
12062
12063 if (buf != data)
12064 kfree(buf);
12065
12066 return ret;
12067}
12068
12069static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12070{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012071 struct tg3 *tp = netdev_priv(dev);
12072
Joe Perches63c3a662011-04-26 08:12:10 +000012073 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012074 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012075 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012076 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012077 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012078 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012079 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012080
Linus Torvalds1da177e2005-04-16 15:20:36 -070012081 cmd->supported = (SUPPORTED_Autoneg);
12082
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012083 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012084 cmd->supported |= (SUPPORTED_1000baseT_Half |
12085 SUPPORTED_1000baseT_Full);
12086
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012087 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012088 cmd->supported |= (SUPPORTED_100baseT_Half |
12089 SUPPORTED_100baseT_Full |
12090 SUPPORTED_10baseT_Half |
12091 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080012092 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070012093 cmd->port = PORT_TP;
12094 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012095 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070012096 cmd->port = PORT_FIBRE;
12097 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012098
Linus Torvalds1da177e2005-04-16 15:20:36 -070012099 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000012100 if (tg3_flag(tp, PAUSE_AUTONEG)) {
12101 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
12102 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12103 cmd->advertising |= ADVERTISED_Pause;
12104 } else {
12105 cmd->advertising |= ADVERTISED_Pause |
12106 ADVERTISED_Asym_Pause;
12107 }
12108 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12109 cmd->advertising |= ADVERTISED_Asym_Pause;
12110 }
12111 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012112 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000012113 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012114 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000012115 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012116 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
12117 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
12118 cmd->eth_tp_mdix = ETH_TP_MDI_X;
12119 else
12120 cmd->eth_tp_mdix = ETH_TP_MDI;
12121 }
Matt Carlson64c22182010-10-14 10:37:44 +000012122 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000012123 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
12124 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012125 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012126 }
Matt Carlson882e9792009-09-01 13:21:36 +000012127 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012128 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012129 cmd->autoneg = tp->link_config.autoneg;
12130 cmd->maxtxpkt = 0;
12131 cmd->maxrxpkt = 0;
12132 return 0;
12133}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012134
Linus Torvalds1da177e2005-04-16 15:20:36 -070012135static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12136{
12137 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000012138 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012139
Joe Perches63c3a662011-04-26 08:12:10 +000012140 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012141 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012142 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012143 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012144 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012145 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012146 }
12147
Matt Carlson7e5856b2009-02-25 14:23:01 +000012148 if (cmd->autoneg != AUTONEG_ENABLE &&
12149 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012150 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012151
12152 if (cmd->autoneg == AUTONEG_DISABLE &&
12153 cmd->duplex != DUPLEX_FULL &&
12154 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012156
Matt Carlson7e5856b2009-02-25 14:23:01 +000012157 if (cmd->autoneg == AUTONEG_ENABLE) {
12158 u32 mask = ADVERTISED_Autoneg |
12159 ADVERTISED_Pause |
12160 ADVERTISED_Asym_Pause;
12161
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012162 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012163 mask |= ADVERTISED_1000baseT_Half |
12164 ADVERTISED_1000baseT_Full;
12165
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012166 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012167 mask |= ADVERTISED_100baseT_Half |
12168 ADVERTISED_100baseT_Full |
12169 ADVERTISED_10baseT_Half |
12170 ADVERTISED_10baseT_Full |
12171 ADVERTISED_TP;
12172 else
12173 mask |= ADVERTISED_FIBRE;
12174
12175 if (cmd->advertising & ~mask)
12176 return -EINVAL;
12177
12178 mask &= (ADVERTISED_1000baseT_Half |
12179 ADVERTISED_1000baseT_Full |
12180 ADVERTISED_100baseT_Half |
12181 ADVERTISED_100baseT_Full |
12182 ADVERTISED_10baseT_Half |
12183 ADVERTISED_10baseT_Full);
12184
12185 cmd->advertising &= mask;
12186 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012187 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012188 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012189 return -EINVAL;
12190
12191 if (cmd->duplex != DUPLEX_FULL)
12192 return -EINVAL;
12193 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012194 if (speed != SPEED_100 &&
12195 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012196 return -EINVAL;
12197 }
12198 }
12199
David S. Millerf47c11e2005-06-24 20:18:35 -070012200 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012201
12202 tp->link_config.autoneg = cmd->autoneg;
12203 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012204 tp->link_config.advertising = (cmd->advertising |
12205 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012206 tp->link_config.speed = SPEED_UNKNOWN;
12207 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012208 } else {
12209 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012210 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012211 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012212 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012213
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012214 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12215
Nithin Sujirce20f162013-04-09 08:48:04 +000012216 tg3_warn_mgmt_link_flap(tp);
12217
Linus Torvalds1da177e2005-04-16 15:20:36 -070012218 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012219 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012220
David S. Millerf47c11e2005-06-24 20:18:35 -070012221 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012222
Linus Torvalds1da177e2005-04-16 15:20:36 -070012223 return 0;
12224}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012225
Linus Torvalds1da177e2005-04-16 15:20:36 -070012226static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12227{
12228 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012229
Rick Jones68aad782011-11-07 13:29:27 +000012230 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12231 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12232 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12233 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012234}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012235
Linus Torvalds1da177e2005-04-16 15:20:36 -070012236static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12237{
12238 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012239
Joe Perches63c3a662011-04-26 08:12:10 +000012240 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012241 wol->supported = WAKE_MAGIC;
12242 else
12243 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012244 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012245 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012246 wol->wolopts = WAKE_MAGIC;
12247 memset(&wol->sopass, 0, sizeof(wol->sopass));
12248}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012249
Linus Torvalds1da177e2005-04-16 15:20:36 -070012250static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12251{
12252 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012253 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012254
Linus Torvalds1da177e2005-04-16 15:20:36 -070012255 if (wol->wolopts & ~WAKE_MAGIC)
12256 return -EINVAL;
12257 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012258 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012259 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012260
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012261 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12262
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012263 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012264 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012265 else
Joe Perches63c3a662011-04-26 08:12:10 +000012266 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012267
Linus Torvalds1da177e2005-04-16 15:20:36 -070012268 return 0;
12269}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012270
Linus Torvalds1da177e2005-04-16 15:20:36 -070012271static u32 tg3_get_msglevel(struct net_device *dev)
12272{
12273 struct tg3 *tp = netdev_priv(dev);
12274 return tp->msg_enable;
12275}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012276
Linus Torvalds1da177e2005-04-16 15:20:36 -070012277static void tg3_set_msglevel(struct net_device *dev, u32 value)
12278{
12279 struct tg3 *tp = netdev_priv(dev);
12280 tp->msg_enable = value;
12281}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012282
Linus Torvalds1da177e2005-04-16 15:20:36 -070012283static int tg3_nway_reset(struct net_device *dev)
12284{
12285 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012287
Linus Torvalds1da177e2005-04-16 15:20:36 -070012288 if (!netif_running(dev))
12289 return -EAGAIN;
12290
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012291 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012292 return -EINVAL;
12293
Nithin Sujirce20f162013-04-09 08:48:04 +000012294 tg3_warn_mgmt_link_flap(tp);
12295
Joe Perches63c3a662011-04-26 08:12:10 +000012296 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012297 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012298 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012299 r = phy_start_aneg(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012300 } else {
12301 u32 bmcr;
12302
12303 spin_lock_bh(&tp->lock);
12304 r = -EINVAL;
12305 tg3_readphy(tp, MII_BMCR, &bmcr);
12306 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12307 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012308 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012309 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12310 BMCR_ANENABLE);
12311 r = 0;
12312 }
12313 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012315
Linus Torvalds1da177e2005-04-16 15:20:36 -070012316 return r;
12317}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012318
Linus Torvalds1da177e2005-04-16 15:20:36 -070012319static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12320{
12321 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012322
Matt Carlson2c49a442010-09-30 10:34:35 +000012323 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012324 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012325 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012326 else
12327 ering->rx_jumbo_max_pending = 0;
12328
12329 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012330
12331 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012332 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012333 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12334 else
12335 ering->rx_jumbo_pending = 0;
12336
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012337 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012338}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012339
Linus Torvalds1da177e2005-04-16 15:20:36 -070012340static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12341{
12342 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012343 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012344
Matt Carlson2c49a442010-09-30 10:34:35 +000012345 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12346 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012347 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12348 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012349 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012350 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012351 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012352
Michael Chanbbe832c2005-06-24 20:20:04 -070012353 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012354 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012355 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012356 irq_sync = 1;
12357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358
Michael Chanbbe832c2005-06-24 20:20:04 -070012359 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012360
Linus Torvalds1da177e2005-04-16 15:20:36 -070012361 tp->rx_pending = ering->rx_pending;
12362
Joe Perches63c3a662011-04-26 08:12:10 +000012363 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012364 tp->rx_pending > 63)
12365 tp->rx_pending = 63;
Ivan Veceraba67b512014-04-17 14:51:08 +020012366
12367 if (tg3_flag(tp, JUMBO_RING_ENABLE))
12368 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012369
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012370 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012371 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012372
12373 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012374 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012375 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012376 if (!err)
12377 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012378 }
12379
David S. Millerf47c11e2005-06-24 20:18:35 -070012380 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012381
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012382 if (irq_sync && !err)
12383 tg3_phy_start(tp);
12384
Michael Chanb9ec6c12006-07-25 16:37:27 -070012385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012386}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012387
Linus Torvalds1da177e2005-04-16 15:20:36 -070012388static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12389{
12390 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012391
Joe Perches63c3a662011-04-26 08:12:10 +000012392 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012393
Matt Carlson4a2db502011-12-08 14:40:17 +000012394 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012395 epause->rx_pause = 1;
12396 else
12397 epause->rx_pause = 0;
12398
Matt Carlson4a2db502011-12-08 14:40:17 +000012399 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012400 epause->tx_pause = 1;
12401 else
12402 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012403}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012404
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12406{
12407 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012408 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012409
Nithin Sujirce20f162013-04-09 08:48:04 +000012410 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12411 tg3_warn_mgmt_link_flap(tp);
12412
Joe Perches63c3a662011-04-26 08:12:10 +000012413 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012414 u32 newadv;
12415 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012416
Hauke Mehrtensead24022013-09-28 23:15:26 +020012417 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012418
Matt Carlson27121682010-02-17 15:16:57 +000012419 if (!(phydev->supported & SUPPORTED_Pause) ||
12420 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012421 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012422 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012423
Matt Carlson27121682010-02-17 15:16:57 +000012424 tp->link_config.flowctrl = 0;
12425 if (epause->rx_pause) {
12426 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012427
Matt Carlson27121682010-02-17 15:16:57 +000012428 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012429 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012430 newadv = ADVERTISED_Pause;
12431 } else
12432 newadv = ADVERTISED_Pause |
12433 ADVERTISED_Asym_Pause;
12434 } else if (epause->tx_pause) {
12435 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12436 newadv = ADVERTISED_Asym_Pause;
12437 } else
12438 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012439
Matt Carlson27121682010-02-17 15:16:57 +000012440 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012441 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012442 else
Joe Perches63c3a662011-04-26 08:12:10 +000012443 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012444
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012445 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012446 u32 oldadv = phydev->advertising &
12447 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12448 if (oldadv != newadv) {
12449 phydev->advertising &=
12450 ~(ADVERTISED_Pause |
12451 ADVERTISED_Asym_Pause);
12452 phydev->advertising |= newadv;
12453 if (phydev->autoneg) {
12454 /*
12455 * Always renegotiate the link to
12456 * inform our link partner of our
12457 * flow control settings, even if the
12458 * flow control is forced. Let
12459 * tg3_adjust_link() do the final
12460 * flow control setup.
12461 */
12462 return phy_start_aneg(phydev);
12463 }
12464 }
12465
12466 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012467 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012468 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012469 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012470 ~(ADVERTISED_Pause |
12471 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012472 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012473 }
12474 } else {
12475 int irq_sync = 0;
12476
12477 if (netif_running(dev)) {
12478 tg3_netif_stop(tp);
12479 irq_sync = 1;
12480 }
12481
12482 tg3_full_lock(tp, irq_sync);
12483
12484 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012485 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012486 else
Joe Perches63c3a662011-04-26 08:12:10 +000012487 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012488 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012489 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012490 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012491 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012492 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012493 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012494 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012495 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012496
12497 if (netif_running(dev)) {
12498 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012499 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012500 if (!err)
12501 tg3_netif_start(tp);
12502 }
12503
12504 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012506
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012507 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12508
Michael Chanb9ec6c12006-07-25 16:37:27 -070012509 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012510}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012511
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012512static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012513{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012514 switch (sset) {
12515 case ETH_SS_TEST:
12516 return TG3_NUM_TEST;
12517 case ETH_SS_STATS:
12518 return TG3_NUM_STATS;
12519 default:
12520 return -EOPNOTSUPP;
12521 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012522}
12523
Matt Carlson90415472011-12-16 13:33:23 +000012524static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12525 u32 *rules __always_unused)
12526{
12527 struct tg3 *tp = netdev_priv(dev);
12528
12529 if (!tg3_flag(tp, SUPPORT_MSIX))
12530 return -EOPNOTSUPP;
12531
12532 switch (info->cmd) {
12533 case ETHTOOL_GRXRINGS:
12534 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012535 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012536 else {
12537 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012538 if (info->data > TG3_RSS_MAX_NUM_QS)
12539 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012540 }
12541
12542 /* The first interrupt vector only
12543 * handles link interrupts.
12544 */
12545 info->data -= 1;
12546 return 0;
12547
12548 default:
12549 return -EOPNOTSUPP;
12550 }
12551}
12552
12553static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12554{
12555 u32 size = 0;
12556 struct tg3 *tp = netdev_priv(dev);
12557
12558 if (tg3_flag(tp, SUPPORT_MSIX))
12559 size = TG3_RSS_INDIR_TBL_SIZE;
12560
12561 return size;
12562}
12563
Eyal Perry892311f2014-12-02 18:12:10 +020012564static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012565{
12566 struct tg3 *tp = netdev_priv(dev);
12567 int i;
12568
Eyal Perry892311f2014-12-02 18:12:10 +020012569 if (hfunc)
12570 *hfunc = ETH_RSS_HASH_TOP;
12571 if (!indir)
12572 return 0;
12573
Matt Carlson90415472011-12-16 13:33:23 +000012574 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12575 indir[i] = tp->rss_ind_tbl[i];
12576
12577 return 0;
12578}
12579
Eyal Perry892311f2014-12-02 18:12:10 +020012580static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
12581 const u8 hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012582{
12583 struct tg3 *tp = netdev_priv(dev);
12584 size_t i;
12585
Eyal Perry892311f2014-12-02 18:12:10 +020012586 /* We require at least one supported parameter to be changed and no
12587 * change in any of the unsupported parameters
12588 */
12589 if (key ||
12590 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
12591 return -EOPNOTSUPP;
12592
12593 if (!indir)
12594 return 0;
12595
Matt Carlson90415472011-12-16 13:33:23 +000012596 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12597 tp->rss_ind_tbl[i] = indir[i];
12598
12599 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12600 return 0;
12601
12602 /* It is legal to write the indirection
12603 * table while the device is running.
12604 */
12605 tg3_full_lock(tp, 0);
12606 tg3_rss_write_indir_tbl(tp);
12607 tg3_full_unlock(tp);
12608
12609 return 0;
12610}
12611
Michael Chan09681692012-09-28 07:12:42 +000012612static void tg3_get_channels(struct net_device *dev,
12613 struct ethtool_channels *channel)
12614{
12615 struct tg3 *tp = netdev_priv(dev);
12616 u32 deflt_qs = netif_get_num_default_rss_queues();
12617
12618 channel->max_rx = tp->rxq_max;
12619 channel->max_tx = tp->txq_max;
12620
12621 if (netif_running(dev)) {
12622 channel->rx_count = tp->rxq_cnt;
12623 channel->tx_count = tp->txq_cnt;
12624 } else {
12625 if (tp->rxq_req)
12626 channel->rx_count = tp->rxq_req;
12627 else
12628 channel->rx_count = min(deflt_qs, tp->rxq_max);
12629
12630 if (tp->txq_req)
12631 channel->tx_count = tp->txq_req;
12632 else
12633 channel->tx_count = min(deflt_qs, tp->txq_max);
12634 }
12635}
12636
12637static int tg3_set_channels(struct net_device *dev,
12638 struct ethtool_channels *channel)
12639{
12640 struct tg3 *tp = netdev_priv(dev);
12641
12642 if (!tg3_flag(tp, SUPPORT_MSIX))
12643 return -EOPNOTSUPP;
12644
12645 if (channel->rx_count > tp->rxq_max ||
12646 channel->tx_count > tp->txq_max)
12647 return -EINVAL;
12648
12649 tp->rxq_req = channel->rx_count;
12650 tp->txq_req = channel->tx_count;
12651
12652 if (!netif_running(dev))
12653 return 0;
12654
12655 tg3_stop(tp);
12656
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012657 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012658
Matt Carlsonbe947302012-12-03 19:36:57 +000012659 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012660
12661 return 0;
12662}
12663
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012664static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012665{
12666 switch (stringset) {
12667 case ETH_SS_STATS:
12668 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12669 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012670 case ETH_SS_TEST:
12671 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012673 default:
12674 WARN_ON(1); /* we need a WARN() */
12675 break;
12676 }
12677}
12678
stephen hemminger81b87092011-04-04 08:43:50 +000012679static int tg3_set_phys_id(struct net_device *dev,
12680 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012681{
12682 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012683
12684 if (!netif_running(tp->dev))
12685 return -EAGAIN;
12686
stephen hemminger81b87092011-04-04 08:43:50 +000012687 switch (state) {
12688 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012689 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012690
stephen hemminger81b87092011-04-04 08:43:50 +000012691 case ETHTOOL_ID_ON:
12692 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12693 LED_CTRL_1000MBPS_ON |
12694 LED_CTRL_100MBPS_ON |
12695 LED_CTRL_10MBPS_ON |
12696 LED_CTRL_TRAFFIC_OVERRIDE |
12697 LED_CTRL_TRAFFIC_BLINK |
12698 LED_CTRL_TRAFFIC_LED);
12699 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012700
stephen hemminger81b87092011-04-04 08:43:50 +000012701 case ETHTOOL_ID_OFF:
12702 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12703 LED_CTRL_TRAFFIC_OVERRIDE);
12704 break;
Michael Chan4009a932005-09-05 17:52:54 -070012705
stephen hemminger81b87092011-04-04 08:43:50 +000012706 case ETHTOOL_ID_INACTIVE:
12707 tw32(MAC_LED_CTRL, tp->led_ctrl);
12708 break;
Michael Chan4009a932005-09-05 17:52:54 -070012709 }
stephen hemminger81b87092011-04-04 08:43:50 +000012710
Michael Chan4009a932005-09-05 17:52:54 -070012711 return 0;
12712}
12713
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012714static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012715 struct ethtool_stats *estats, u64 *tmp_stats)
12716{
12717 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012718
Matt Carlsonb546e462012-02-13 15:20:09 +000012719 if (tp->hw_stats)
12720 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12721 else
12722 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012723}
12724
Matt Carlson535a4902011-07-20 10:20:56 +000012725static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012726{
12727 int i;
12728 __be32 *buf;
12729 u32 offset = 0, len = 0;
12730 u32 magic, val;
12731
Joe Perches63c3a662011-04-26 08:12:10 +000012732 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012733 return NULL;
12734
12735 if (magic == TG3_EEPROM_MAGIC) {
12736 for (offset = TG3_NVM_DIR_START;
12737 offset < TG3_NVM_DIR_END;
12738 offset += TG3_NVM_DIRENT_SIZE) {
12739 if (tg3_nvram_read(tp, offset, &val))
12740 return NULL;
12741
12742 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12743 TG3_NVM_DIRTYPE_EXTVPD)
12744 break;
12745 }
12746
12747 if (offset != TG3_NVM_DIR_END) {
12748 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12749 if (tg3_nvram_read(tp, offset + 4, &offset))
12750 return NULL;
12751
12752 offset = tg3_nvram_logical_addr(tp, offset);
12753 }
12754 }
12755
12756 if (!offset || !len) {
12757 offset = TG3_NVM_VPD_OFF;
12758 len = TG3_NVM_VPD_LEN;
12759 }
12760
12761 buf = kmalloc(len, GFP_KERNEL);
12762 if (buf == NULL)
12763 return NULL;
12764
12765 if (magic == TG3_EEPROM_MAGIC) {
12766 for (i = 0; i < len; i += 4) {
12767 /* The data is in little-endian format in NVRAM.
12768 * Use the big-endian read routines to preserve
12769 * the byte order as it exists in NVRAM.
12770 */
12771 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12772 goto error;
12773 }
12774 } else {
12775 u8 *ptr;
12776 ssize_t cnt;
12777 unsigned int pos = 0;
12778
12779 ptr = (u8 *)&buf[0];
12780 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12781 cnt = pci_read_vpd(tp->pdev, pos,
12782 len - pos, ptr);
12783 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12784 cnt = 0;
12785 else if (cnt < 0)
12786 goto error;
12787 }
12788 if (pos != len)
12789 goto error;
12790 }
12791
Matt Carlson535a4902011-07-20 10:20:56 +000012792 *vpdlen = len;
12793
Matt Carlsonc3e94502011-04-13 11:05:08 +000012794 return buf;
12795
12796error:
12797 kfree(buf);
12798 return NULL;
12799}
12800
Michael Chan566f86a2005-05-29 14:56:58 -070012801#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012802#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12803#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12804#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012805#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12806#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012807#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012808#define NVRAM_SELFBOOT_HW_SIZE 0x20
12809#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012810
12811static int tg3_test_nvram(struct tg3 *tp)
12812{
Matt Carlson535a4902011-07-20 10:20:56 +000012813 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012814 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012815 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012816
Joe Perches63c3a662011-04-26 08:12:10 +000012817 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012818 return 0;
12819
Matt Carlsone4f34112009-02-25 14:25:00 +000012820 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012821 return -EIO;
12822
Michael Chan1b277772006-03-20 22:27:48 -080012823 if (magic == TG3_EEPROM_MAGIC)
12824 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012825 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012826 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12827 TG3_EEPROM_SB_FORMAT_1) {
12828 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12829 case TG3_EEPROM_SB_REVISION_0:
12830 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12831 break;
12832 case TG3_EEPROM_SB_REVISION_2:
12833 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12834 break;
12835 case TG3_EEPROM_SB_REVISION_3:
12836 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12837 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012838 case TG3_EEPROM_SB_REVISION_4:
12839 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12840 break;
12841 case TG3_EEPROM_SB_REVISION_5:
12842 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12843 break;
12844 case TG3_EEPROM_SB_REVISION_6:
12845 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12846 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012847 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012848 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012849 }
12850 } else
Michael Chan1b277772006-03-20 22:27:48 -080012851 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012852 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12853 size = NVRAM_SELFBOOT_HW_SIZE;
12854 else
Michael Chan1b277772006-03-20 22:27:48 -080012855 return -EIO;
12856
12857 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012858 if (buf == NULL)
12859 return -ENOMEM;
12860
Michael Chan1b277772006-03-20 22:27:48 -080012861 err = -EIO;
12862 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012863 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12864 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012865 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012866 }
Michael Chan1b277772006-03-20 22:27:48 -080012867 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012868 goto out;
12869
Michael Chan1b277772006-03-20 22:27:48 -080012870 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012871 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012872 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012873 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012874 u8 *buf8 = (u8 *) buf, csum8 = 0;
12875
Al Virob9fc7dc2007-12-17 22:59:57 -080012876 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012877 TG3_EEPROM_SB_REVISION_2) {
12878 /* For rev 2, the csum doesn't include the MBA. */
12879 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12880 csum8 += buf8[i];
12881 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12882 csum8 += buf8[i];
12883 } else {
12884 for (i = 0; i < size; i++)
12885 csum8 += buf8[i];
12886 }
Michael Chan1b277772006-03-20 22:27:48 -080012887
Adrian Bunkad96b482006-04-05 22:21:04 -070012888 if (csum8 == 0) {
12889 err = 0;
12890 goto out;
12891 }
12892
12893 err = -EIO;
12894 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012895 }
Michael Chan566f86a2005-05-29 14:56:58 -070012896
Al Virob9fc7dc2007-12-17 22:59:57 -080012897 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012898 TG3_EEPROM_MAGIC_HW) {
12899 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012900 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012901 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012902
12903 /* Separate the parity bits and the data bytes. */
12904 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12905 if ((i == 0) || (i == 8)) {
12906 int l;
12907 u8 msk;
12908
12909 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12910 parity[k++] = buf8[i] & msk;
12911 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012912 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012913 int l;
12914 u8 msk;
12915
12916 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12917 parity[k++] = buf8[i] & msk;
12918 i++;
12919
12920 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12921 parity[k++] = buf8[i] & msk;
12922 i++;
12923 }
12924 data[j++] = buf8[i];
12925 }
12926
12927 err = -EIO;
12928 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12929 u8 hw8 = hweight8(data[i]);
12930
12931 if ((hw8 & 0x1) && parity[i])
12932 goto out;
12933 else if (!(hw8 & 0x1) && !parity[i])
12934 goto out;
12935 }
12936 err = 0;
12937 goto out;
12938 }
12939
Matt Carlson01c3a392011-03-09 16:58:20 +000012940 err = -EIO;
12941
Michael Chan566f86a2005-05-29 14:56:58 -070012942 /* Bootstrap checksum at offset 0x10 */
12943 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012944 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012945 goto out;
12946
12947 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12948 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012949 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012950 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012951
Matt Carlsonc3e94502011-04-13 11:05:08 +000012952 kfree(buf);
12953
Matt Carlson535a4902011-07-20 10:20:56 +000012954 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012955 if (!buf)
12956 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012957
Matt Carlson535a4902011-07-20 10:20:56 +000012958 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012959 if (i > 0) {
12960 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12961 if (j < 0)
12962 goto out;
12963
Matt Carlson535a4902011-07-20 10:20:56 +000012964 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012965 goto out;
12966
12967 i += PCI_VPD_LRDT_TAG_SIZE;
12968 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12969 PCI_VPD_RO_KEYWORD_CHKSUM);
12970 if (j > 0) {
12971 u8 csum8 = 0;
12972
12973 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12974
12975 for (i = 0; i <= j; i++)
12976 csum8 += ((u8 *)buf)[i];
12977
12978 if (csum8)
12979 goto out;
12980 }
12981 }
12982
Michael Chan566f86a2005-05-29 14:56:58 -070012983 err = 0;
12984
12985out:
12986 kfree(buf);
12987 return err;
12988}
12989
Michael Chanca430072005-05-29 14:57:23 -070012990#define TG3_SERDES_TIMEOUT_SEC 2
12991#define TG3_COPPER_TIMEOUT_SEC 6
12992
12993static int tg3_test_link(struct tg3 *tp)
12994{
12995 int i, max;
12996
12997 if (!netif_running(tp->dev))
12998 return -ENODEV;
12999
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013000 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070013001 max = TG3_SERDES_TIMEOUT_SEC;
13002 else
13003 max = TG3_COPPER_TIMEOUT_SEC;
13004
13005 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000013006 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070013007 return 0;
13008
13009 if (msleep_interruptible(1000))
13010 break;
13011 }
13012
13013 return -EIO;
13014}
13015
Michael Chana71116d2005-05-29 14:58:11 -070013016/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080013017static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070013018{
Michael Chanb16250e2006-09-27 16:10:14 -070013019 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070013020 u32 offset, read_mask, write_mask, val, save_val, read_val;
13021 static struct {
13022 u16 offset;
13023 u16 flags;
13024#define TG3_FL_5705 0x1
13025#define TG3_FL_NOT_5705 0x2
13026#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070013027#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070013028 u32 read_mask;
13029 u32 write_mask;
13030 } reg_tbl[] = {
13031 /* MAC Control Registers */
13032 { MAC_MODE, TG3_FL_NOT_5705,
13033 0x00000000, 0x00ef6f8c },
13034 { MAC_MODE, TG3_FL_5705,
13035 0x00000000, 0x01ef6b8c },
13036 { MAC_STATUS, TG3_FL_NOT_5705,
13037 0x03800107, 0x00000000 },
13038 { MAC_STATUS, TG3_FL_5705,
13039 0x03800100, 0x00000000 },
13040 { MAC_ADDR_0_HIGH, 0x0000,
13041 0x00000000, 0x0000ffff },
13042 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013043 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070013044 { MAC_RX_MTU_SIZE, 0x0000,
13045 0x00000000, 0x0000ffff },
13046 { MAC_TX_MODE, 0x0000,
13047 0x00000000, 0x00000070 },
13048 { MAC_TX_LENGTHS, 0x0000,
13049 0x00000000, 0x00003fff },
13050 { MAC_RX_MODE, TG3_FL_NOT_5705,
13051 0x00000000, 0x000007fc },
13052 { MAC_RX_MODE, TG3_FL_5705,
13053 0x00000000, 0x000007dc },
13054 { MAC_HASH_REG_0, 0x0000,
13055 0x00000000, 0xffffffff },
13056 { MAC_HASH_REG_1, 0x0000,
13057 0x00000000, 0xffffffff },
13058 { MAC_HASH_REG_2, 0x0000,
13059 0x00000000, 0xffffffff },
13060 { MAC_HASH_REG_3, 0x0000,
13061 0x00000000, 0xffffffff },
13062
13063 /* Receive Data and Receive BD Initiator Control Registers. */
13064 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
13065 0x00000000, 0xffffffff },
13066 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
13067 0x00000000, 0xffffffff },
13068 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
13069 0x00000000, 0x00000003 },
13070 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
13071 0x00000000, 0xffffffff },
13072 { RCVDBDI_STD_BD+0, 0x0000,
13073 0x00000000, 0xffffffff },
13074 { RCVDBDI_STD_BD+4, 0x0000,
13075 0x00000000, 0xffffffff },
13076 { RCVDBDI_STD_BD+8, 0x0000,
13077 0x00000000, 0xffff0002 },
13078 { RCVDBDI_STD_BD+0xc, 0x0000,
13079 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013080
Michael Chana71116d2005-05-29 14:58:11 -070013081 /* Receive BD Initiator Control Registers. */
13082 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
13083 0x00000000, 0xffffffff },
13084 { RCVBDI_STD_THRESH, TG3_FL_5705,
13085 0x00000000, 0x000003ff },
13086 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
13087 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013088
Michael Chana71116d2005-05-29 14:58:11 -070013089 /* Host Coalescing Control Registers. */
13090 { HOSTCC_MODE, TG3_FL_NOT_5705,
13091 0x00000000, 0x00000004 },
13092 { HOSTCC_MODE, TG3_FL_5705,
13093 0x00000000, 0x000000f6 },
13094 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
13095 0x00000000, 0xffffffff },
13096 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
13097 0x00000000, 0x000003ff },
13098 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
13099 0x00000000, 0xffffffff },
13100 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
13101 0x00000000, 0x000003ff },
13102 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
13103 0x00000000, 0xffffffff },
13104 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13105 0x00000000, 0x000000ff },
13106 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
13107 0x00000000, 0xffffffff },
13108 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13109 0x00000000, 0x000000ff },
13110 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
13111 0x00000000, 0xffffffff },
13112 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
13113 0x00000000, 0xffffffff },
13114 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13115 0x00000000, 0xffffffff },
13116 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13117 0x00000000, 0x000000ff },
13118 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13119 0x00000000, 0xffffffff },
13120 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13121 0x00000000, 0x000000ff },
13122 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
13123 0x00000000, 0xffffffff },
13124 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
13125 0x00000000, 0xffffffff },
13126 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
13127 0x00000000, 0xffffffff },
13128 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
13129 0x00000000, 0xffffffff },
13130 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
13131 0x00000000, 0xffffffff },
13132 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
13133 0xffffffff, 0x00000000 },
13134 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
13135 0xffffffff, 0x00000000 },
13136
13137 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070013138 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013139 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070013140 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013141 0x00000000, 0x007fffff },
13142 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
13143 0x00000000, 0x0000003f },
13144 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
13145 0x00000000, 0x000001ff },
13146 { BUFMGR_MB_HIGH_WATER, 0x0000,
13147 0x00000000, 0x000001ff },
13148 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
13149 0xffffffff, 0x00000000 },
13150 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
13151 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013152
Michael Chana71116d2005-05-29 14:58:11 -070013153 /* Mailbox Registers */
13154 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
13155 0x00000000, 0x000001ff },
13156 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
13157 0x00000000, 0x000001ff },
13158 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
13159 0x00000000, 0x000007ff },
13160 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
13161 0x00000000, 0x000001ff },
13162
13163 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13164 };
13165
Michael Chanb16250e2006-09-27 16:10:14 -070013166 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013167 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013168 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013169 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013170 is_5750 = 1;
13171 }
Michael Chana71116d2005-05-29 14:58:11 -070013172
13173 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13174 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13175 continue;
13176
13177 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13178 continue;
13179
Joe Perches63c3a662011-04-26 08:12:10 +000013180 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013181 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13182 continue;
13183
Michael Chanb16250e2006-09-27 16:10:14 -070013184 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13185 continue;
13186
Michael Chana71116d2005-05-29 14:58:11 -070013187 offset = (u32) reg_tbl[i].offset;
13188 read_mask = reg_tbl[i].read_mask;
13189 write_mask = reg_tbl[i].write_mask;
13190
13191 /* Save the original register content */
13192 save_val = tr32(offset);
13193
13194 /* Determine the read-only value. */
13195 read_val = save_val & read_mask;
13196
13197 /* Write zero to the register, then make sure the read-only bits
13198 * are not changed and the read/write bits are all zeros.
13199 */
13200 tw32(offset, 0);
13201
13202 val = tr32(offset);
13203
13204 /* Test the read-only and read/write bits. */
13205 if (((val & read_mask) != read_val) || (val & write_mask))
13206 goto out;
13207
13208 /* Write ones to all the bits defined by RdMask and WrMask, then
13209 * make sure the read-only bits are not changed and the
13210 * read/write bits are all ones.
13211 */
13212 tw32(offset, read_mask | write_mask);
13213
13214 val = tr32(offset);
13215
13216 /* Test the read-only bits. */
13217 if ((val & read_mask) != read_val)
13218 goto out;
13219
13220 /* Test the read/write bits. */
13221 if ((val & write_mask) != write_mask)
13222 goto out;
13223
13224 tw32(offset, save_val);
13225 }
13226
13227 return 0;
13228
13229out:
Michael Chan9f88f292006-12-07 00:22:54 -080013230 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013231 netdev_err(tp->dev,
13232 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013233 tw32(offset, save_val);
13234 return -EIO;
13235}
13236
Michael Chan7942e1d2005-05-29 14:58:36 -070013237static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13238{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013239 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013240 int i;
13241 u32 j;
13242
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013243 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013244 for (j = 0; j < len; j += 4) {
13245 u32 val;
13246
13247 tg3_write_mem(tp, offset + j, test_pattern[i]);
13248 tg3_read_mem(tp, offset + j, &val);
13249 if (val != test_pattern[i])
13250 return -EIO;
13251 }
13252 }
13253 return 0;
13254}
13255
13256static int tg3_test_memory(struct tg3 *tp)
13257{
13258 static struct mem_entry {
13259 u32 offset;
13260 u32 len;
13261 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013262 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013263 { 0x00002000, 0x1c000},
13264 { 0xffffffff, 0x00000}
13265 }, mem_tbl_5705[] = {
13266 { 0x00000100, 0x0000c},
13267 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013268 { 0x00004000, 0x00800},
13269 { 0x00006000, 0x01000},
13270 { 0x00008000, 0x02000},
13271 { 0x00010000, 0x0e000},
13272 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013273 }, mem_tbl_5755[] = {
13274 { 0x00000200, 0x00008},
13275 { 0x00004000, 0x00800},
13276 { 0x00006000, 0x00800},
13277 { 0x00008000, 0x02000},
13278 { 0x00010000, 0x0c000},
13279 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013280 }, mem_tbl_5906[] = {
13281 { 0x00000200, 0x00008},
13282 { 0x00004000, 0x00400},
13283 { 0x00006000, 0x00400},
13284 { 0x00008000, 0x01000},
13285 { 0x00010000, 0x01000},
13286 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013287 }, mem_tbl_5717[] = {
13288 { 0x00000200, 0x00008},
13289 { 0x00010000, 0x0a000},
13290 { 0x00020000, 0x13c00},
13291 { 0xffffffff, 0x00000}
13292 }, mem_tbl_57765[] = {
13293 { 0x00000200, 0x00008},
13294 { 0x00004000, 0x00800},
13295 { 0x00006000, 0x09800},
13296 { 0x00010000, 0x0a000},
13297 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013298 };
13299 struct mem_entry *mem_tbl;
13300 int err = 0;
13301 int i;
13302
Joe Perches63c3a662011-04-26 08:12:10 +000013303 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013304 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013305 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013306 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013307 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013308 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013309 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013310 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013311 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013312 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013313 mem_tbl = mem_tbl_5705;
13314 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013315 mem_tbl = mem_tbl_570x;
13316
13317 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013318 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13319 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013320 break;
13321 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013322
Michael Chan7942e1d2005-05-29 14:58:36 -070013323 return err;
13324}
13325
Matt Carlsonbb158d62011-04-25 12:42:47 +000013326#define TG3_TSO_MSS 500
13327
13328#define TG3_TSO_IP_HDR_LEN 20
13329#define TG3_TSO_TCP_HDR_LEN 20
13330#define TG3_TSO_TCP_OPT_LEN 12
13331
13332static const u8 tg3_tso_header[] = {
133330x08, 0x00,
133340x45, 0x00, 0x00, 0x00,
133350x00, 0x00, 0x40, 0x00,
133360x40, 0x06, 0x00, 0x00,
133370x0a, 0x00, 0x00, 0x01,
133380x0a, 0x00, 0x00, 0x02,
133390x0d, 0x00, 0xe0, 0x00,
133400x00, 0x00, 0x01, 0x00,
133410x00, 0x00, 0x02, 0x00,
133420x80, 0x10, 0x10, 0x00,
133430x14, 0x09, 0x00, 0x00,
133440x01, 0x01, 0x08, 0x0a,
133450x11, 0x11, 0x11, 0x11,
133460x11, 0x11, 0x11, 0x11,
13347};
Michael Chan9f40dea2005-09-05 17:53:06 -070013348
Matt Carlson28a45952011-08-19 13:58:22 +000013349static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013350{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013351 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013352 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013353 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013354 struct sk_buff *skb;
13355 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013356 dma_addr_t map;
13357 int num_pkts, tx_len, rx_len, i, err;
13358 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013359 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013360 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013361
Matt Carlsonc8873402010-02-12 14:47:11 +000013362 tnapi = &tp->napi[0];
13363 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013364 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013365 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013366 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013367 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013368 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013369 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013370 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013371
Michael Chanc76949a2005-05-29 14:58:59 -070013372 err = -EIO;
13373
Matt Carlson4852a862011-04-13 11:05:07 +000013374 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013375 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013376 if (!skb)
13377 return -ENOMEM;
13378
Michael Chanc76949a2005-05-29 14:58:59 -070013379 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013380 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13381 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013382
Matt Carlson4852a862011-04-13 11:05:07 +000013383 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013384
Matt Carlson28a45952011-08-19 13:58:22 +000013385 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013386 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13387
13388 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13389 TG3_TSO_TCP_OPT_LEN;
13390
13391 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13392 sizeof(tg3_tso_header));
13393 mss = TG3_TSO_MSS;
13394
13395 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13396 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13397
13398 /* Set the total length field in the IP header */
13399 iph->tot_len = htons((u16)(mss + hdr_len));
13400
13401 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13402 TXD_FLAG_CPU_POST_DMA);
13403
Joe Perches63c3a662011-04-26 08:12:10 +000013404 if (tg3_flag(tp, HW_TSO_1) ||
13405 tg3_flag(tp, HW_TSO_2) ||
13406 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013407 struct tcphdr *th;
13408 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13409 th = (struct tcphdr *)&tx_data[val];
13410 th->check = 0;
13411 } else
13412 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13413
Joe Perches63c3a662011-04-26 08:12:10 +000013414 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013415 mss |= (hdr_len & 0xc) << 12;
13416 if (hdr_len & 0x10)
13417 base_flags |= 0x00000010;
13418 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013419 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013420 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013421 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013422 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013423 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13424 } else {
13425 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13426 }
13427
13428 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13429 } else {
13430 num_pkts = 1;
13431 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013432
13433 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13434 tx_len > VLAN_ETH_FRAME_LEN)
13435 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013436 }
13437
13438 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013439 tx_data[i] = (u8) (i & 0xff);
13440
Alexander Duyckf4188d82009-12-02 16:48:38 +000013441 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13442 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013443 dev_kfree_skb(skb);
13444 return -EIO;
13445 }
Michael Chanc76949a2005-05-29 14:58:59 -070013446
Matt Carlson0d681b22011-07-27 14:20:49 +000013447 val = tnapi->tx_prod;
13448 tnapi->tx_buffers[val].skb = skb;
13449 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13450
Michael Chanc76949a2005-05-29 14:58:59 -070013451 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013452 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013453
13454 udelay(10);
13455
Matt Carlson898a56f2009-08-28 14:02:40 +000013456 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013457
Matt Carlson84b67b22011-07-27 14:20:52 +000013458 budget = tg3_tx_avail(tnapi);
13459 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013460 base_flags | TXD_FLAG_END, mss, 0)) {
13461 tnapi->tx_buffers[val].skb = NULL;
13462 dev_kfree_skb(skb);
13463 return -EIO;
13464 }
Michael Chanc76949a2005-05-29 14:58:59 -070013465
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013466 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013467
Michael Chan6541b802012-03-04 14:48:14 +000013468 /* Sync BD data before updating mailbox */
13469 wmb();
13470
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013471 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13472 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013473
13474 udelay(10);
13475
Matt Carlson303fc922009-11-02 14:27:34 +000013476 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13477 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013478 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013479 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013480
13481 udelay(10);
13482
Matt Carlson898a56f2009-08-28 14:02:40 +000013483 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13484 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013485 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013486 (rx_idx == (rx_start_idx + num_pkts)))
13487 break;
13488 }
13489
Matt Carlsonba1142e2011-11-04 09:15:00 +000013490 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013491 dev_kfree_skb(skb);
13492
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013493 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013494 goto out;
13495
13496 if (rx_idx != rx_start_idx + num_pkts)
13497 goto out;
13498
Matt Carlsonbb158d62011-04-25 12:42:47 +000013499 val = data_off;
13500 while (rx_idx != rx_start_idx) {
13501 desc = &rnapi->rx_rcb[rx_start_idx++];
13502 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13503 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013504
Matt Carlsonbb158d62011-04-25 12:42:47 +000013505 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13506 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013507 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013508
Matt Carlsonbb158d62011-04-25 12:42:47 +000013509 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13510 - ETH_FCS_LEN;
13511
Matt Carlson28a45952011-08-19 13:58:22 +000013512 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013513 if (rx_len != tx_len)
13514 goto out;
13515
13516 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13517 if (opaque_key != RXD_OPAQUE_RING_STD)
13518 goto out;
13519 } else {
13520 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13521 goto out;
13522 }
13523 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13524 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013525 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013526 goto out;
13527 }
13528
13529 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013530 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013531 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13532 mapping);
13533 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013534 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013535 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13536 mapping);
13537 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013538 goto out;
13539
Matt Carlsonbb158d62011-04-25 12:42:47 +000013540 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13541 PCI_DMA_FROMDEVICE);
13542
Eric Dumazet9205fd92011-11-18 06:47:01 +000013543 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013544 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013545 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013546 goto out;
13547 }
Matt Carlson4852a862011-04-13 11:05:07 +000013548 }
13549
Michael Chanc76949a2005-05-29 14:58:59 -070013550 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013551
Eric Dumazet9205fd92011-11-18 06:47:01 +000013552 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013553out:
13554 return err;
13555}
13556
Matt Carlson00c266b2011-04-25 12:42:46 +000013557#define TG3_STD_LOOPBACK_FAILED 1
13558#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013559#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013560#define TG3_LOOPBACK_FAILED \
13561 (TG3_STD_LOOPBACK_FAILED | \
13562 TG3_JMB_LOOPBACK_FAILED | \
13563 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013564
Matt Carlson941ec902011-08-19 13:58:23 +000013565static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013566{
Matt Carlson28a45952011-08-19 13:58:22 +000013567 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013568 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013569 u32 jmb_pkt_sz = 9000;
13570
13571 if (tp->dma_limit)
13572 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013573
Matt Carlsonab789042011-01-25 15:58:54 +000013574 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13575 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13576
Matt Carlson28a45952011-08-19 13:58:22 +000013577 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013578 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13579 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013580 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013581 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013582 goto done;
13583 }
13584
Joe Perches953c96e2013-04-09 10:18:14 +000013585 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013586 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013587 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13588 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013589 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013590 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013591 goto done;
13592 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013593
Joe Perches63c3a662011-04-26 08:12:10 +000013594 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013595 int i;
13596
13597 /* Reroute all rx packets to the 1st queue */
13598 for (i = MAC_RSS_INDIR_TBL_0;
13599 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13600 tw32(i, 0x0);
13601 }
13602
Matt Carlson6e01b202011-08-19 13:58:20 +000013603 /* HW errata - mac loopback fails in some cases on 5780.
13604 * Normal traffic and PHY loopback are not affected by
13605 * errata. Also, the MAC loopback test is deprecated for
13606 * all newer ASIC revisions.
13607 */
Joe Perches41535772013-02-16 11:20:04 +000013608 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013609 !tg3_flag(tp, CPMU_PRESENT)) {
13610 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013611
Matt Carlson28a45952011-08-19 13:58:22 +000013612 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013613 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013614
13615 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013616 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013617 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013618
13619 tg3_mac_loopback(tp, false);
13620 }
Matt Carlson4852a862011-04-13 11:05:07 +000013621
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013622 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013623 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013624 int i;
13625
Matt Carlson941ec902011-08-19 13:58:23 +000013626 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013627
13628 /* Wait for link */
13629 for (i = 0; i < 100; i++) {
13630 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13631 break;
13632 mdelay(1);
13633 }
13634
Matt Carlson28a45952011-08-19 13:58:22 +000013635 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013636 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013637 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013638 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013639 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013640 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013641 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013642 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013643
Matt Carlson941ec902011-08-19 13:58:23 +000013644 if (do_extlpbk) {
13645 tg3_phy_lpbk_set(tp, 0, true);
13646
13647 /* All link indications report up, but the hardware
13648 * isn't really ready for about 20 msec. Double it
13649 * to be sure.
13650 */
13651 mdelay(40);
13652
13653 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013654 data[TG3_EXT_LOOPB_TEST] |=
13655 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013656 if (tg3_flag(tp, TSO_CAPABLE) &&
13657 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013658 data[TG3_EXT_LOOPB_TEST] |=
13659 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013660 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013661 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013662 data[TG3_EXT_LOOPB_TEST] |=
13663 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013664 }
13665
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013666 /* Re-enable gphy autopowerdown. */
13667 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13668 tg3_phy_toggle_apd(tp, true);
13669 }
Matt Carlson6833c042008-11-21 17:18:59 -080013670
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013671 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13672 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013673
Matt Carlsonab789042011-01-25 15:58:54 +000013674done:
13675 tp->phy_flags |= eee_cap;
13676
Michael Chan9f40dea2005-09-05 17:53:06 -070013677 return err;
13678}
13679
Michael Chan4cafd3f2005-05-29 14:56:34 -070013680static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13681 u64 *data)
13682{
Michael Chan566f86a2005-05-29 14:56:58 -070013683 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013684 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013685
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013686 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13687 if (tg3_power_up(tp)) {
13688 etest->flags |= ETH_TEST_FL_FAILED;
13689 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13690 return;
13691 }
13692 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013693 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013694
Michael Chan566f86a2005-05-29 14:56:58 -070013695 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13696
13697 if (tg3_test_nvram(tp) != 0) {
13698 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013699 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013700 }
Matt Carlson941ec902011-08-19 13:58:23 +000013701 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013702 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013703 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013704 }
Michael Chana71116d2005-05-29 14:58:11 -070013705 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013706 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013707
Michael Chanbbe832c2005-06-24 20:20:04 -070013708 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013709 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013710 tg3_netif_stop(tp);
13711 irq_sync = 1;
13712 }
13713
13714 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013715 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013716 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013717 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013718 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013719 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013720 if (!err)
13721 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013722
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013723 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013724 tg3_phy_reset(tp);
13725
Michael Chana71116d2005-05-29 14:58:11 -070013726 if (tg3_test_registers(tp) != 0) {
13727 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013728 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013729 }
Matt Carlson28a45952011-08-19 13:58:22 +000013730
Michael Chan7942e1d2005-05-29 14:58:36 -070013731 if (tg3_test_memory(tp) != 0) {
13732 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013733 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013734 }
Matt Carlson28a45952011-08-19 13:58:22 +000013735
Matt Carlson941ec902011-08-19 13:58:23 +000013736 if (doextlpbk)
13737 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13738
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013739 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013740 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013741
David S. Millerf47c11e2005-06-24 20:18:35 -070013742 tg3_full_unlock(tp);
13743
Michael Chand4bc3922005-05-29 14:59:20 -070013744 if (tg3_test_interrupt(tp) != 0) {
13745 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013746 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013747 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013748
13749 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013750
Michael Chana71116d2005-05-29 14:58:11 -070013751 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13752 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013753 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013754 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013755 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013756 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013757 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013758
13759 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013760
13761 if (irq_sync && !err2)
13762 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013763 }
Matt Carlson80096062010-08-02 11:26:06 +000013764 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013765 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013766
Michael Chan4cafd3f2005-05-29 14:56:34 -070013767}
13768
Ben Hutchings72608992013-11-18 22:59:43 +000013769static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013770{
13771 struct tg3 *tp = netdev_priv(dev);
13772 struct hwtstamp_config stmpconf;
13773
13774 if (!tg3_flag(tp, PTP_CAPABLE))
Ben Hutchings72608992013-11-18 22:59:43 +000013775 return -EOPNOTSUPP;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013776
13777 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13778 return -EFAULT;
13779
13780 if (stmpconf.flags)
13781 return -EINVAL;
13782
Ben Hutchings58b187c2013-11-14 00:40:56 +000013783 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13784 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013785 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013786
13787 switch (stmpconf.rx_filter) {
13788 case HWTSTAMP_FILTER_NONE:
13789 tp->rxptpctl = 0;
13790 break;
13791 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13792 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13793 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13794 break;
13795 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13796 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13797 TG3_RX_PTP_CTL_SYNC_EVNT;
13798 break;
13799 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13800 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13801 TG3_RX_PTP_CTL_DELAY_REQ;
13802 break;
13803 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13804 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13805 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13806 break;
13807 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13808 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13809 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13810 break;
13811 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13812 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13813 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13814 break;
13815 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13816 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13817 TG3_RX_PTP_CTL_SYNC_EVNT;
13818 break;
13819 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13820 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13821 TG3_RX_PTP_CTL_SYNC_EVNT;
13822 break;
13823 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13824 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13825 TG3_RX_PTP_CTL_SYNC_EVNT;
13826 break;
13827 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13828 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13829 TG3_RX_PTP_CTL_DELAY_REQ;
13830 break;
13831 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13832 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13833 TG3_RX_PTP_CTL_DELAY_REQ;
13834 break;
13835 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13836 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13837 TG3_RX_PTP_CTL_DELAY_REQ;
13838 break;
13839 default:
13840 return -ERANGE;
13841 }
13842
13843 if (netif_running(dev) && tp->rxptpctl)
13844 tw32(TG3_RX_PTP_CTL,
13845 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13846
Ben Hutchings58b187c2013-11-14 00:40:56 +000013847 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13848 tg3_flag_set(tp, TX_TSTAMP_EN);
13849 else
13850 tg3_flag_clear(tp, TX_TSTAMP_EN);
13851
Matt Carlson0a633ac2012-12-03 19:36:59 +000013852 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13853 -EFAULT : 0;
13854}
13855
Ben Hutchings72608992013-11-18 22:59:43 +000013856static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
13857{
13858 struct tg3 *tp = netdev_priv(dev);
13859 struct hwtstamp_config stmpconf;
13860
13861 if (!tg3_flag(tp, PTP_CAPABLE))
13862 return -EOPNOTSUPP;
13863
13864 stmpconf.flags = 0;
13865 stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
13866 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
13867
13868 switch (tp->rxptpctl) {
13869 case 0:
13870 stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
13871 break;
13872 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
13873 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
13874 break;
13875 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13876 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
13877 break;
13878 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13879 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
13880 break;
13881 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13882 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13883 break;
13884 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13885 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
13886 break;
13887 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13888 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
13889 break;
13890 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13891 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
13892 break;
13893 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13894 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
13895 break;
13896 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13897 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
13898 break;
13899 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13900 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
13901 break;
13902 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13903 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
13904 break;
13905 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13906 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
13907 break;
13908 default:
13909 WARN_ON_ONCE(1);
13910 return -ERANGE;
13911 }
13912
13913 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13914 -EFAULT : 0;
13915}
13916
Linus Torvalds1da177e2005-04-16 15:20:36 -070013917static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13918{
13919 struct mii_ioctl_data *data = if_mii(ifr);
13920 struct tg3 *tp = netdev_priv(dev);
13921 int err;
13922
Joe Perches63c3a662011-04-26 08:12:10 +000013923 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013924 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013925 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013926 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020013927 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Richard Cochran28b04112010-07-17 08:48:55 +000013928 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013929 }
13930
Matt Carlson33f401a2010-04-05 10:19:27 +000013931 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013932 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013933 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013934
13935 /* fallthru */
13936 case SIOCGMIIREG: {
13937 u32 mii_regval;
13938
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013939 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013940 break; /* We have no PHY */
13941
Matt Carlson34eea5a2011-04-20 07:57:38 +000013942 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013943 return -EAGAIN;
13944
David S. Millerf47c11e2005-06-24 20:18:35 -070013945 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013946 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13947 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013948 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013949
13950 data->val_out = mii_regval;
13951
13952 return err;
13953 }
13954
13955 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013956 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013957 break; /* We have no PHY */
13958
Matt Carlson34eea5a2011-04-20 07:57:38 +000013959 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013960 return -EAGAIN;
13961
David S. Millerf47c11e2005-06-24 20:18:35 -070013962 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013963 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13964 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013965 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013966
13967 return err;
13968
Matt Carlson0a633ac2012-12-03 19:36:59 +000013969 case SIOCSHWTSTAMP:
Ben Hutchings72608992013-11-18 22:59:43 +000013970 return tg3_hwtstamp_set(dev, ifr);
13971
13972 case SIOCGHWTSTAMP:
13973 return tg3_hwtstamp_get(dev, ifr);
Matt Carlson0a633ac2012-12-03 19:36:59 +000013974
Linus Torvalds1da177e2005-04-16 15:20:36 -070013975 default:
13976 /* do nothing */
13977 break;
13978 }
13979 return -EOPNOTSUPP;
13980}
13981
David S. Miller15f98502005-05-18 22:49:26 -070013982static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13983{
13984 struct tg3 *tp = netdev_priv(dev);
13985
13986 memcpy(ec, &tp->coal, sizeof(*ec));
13987 return 0;
13988}
13989
Michael Chand244c892005-07-05 14:42:33 -070013990static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13991{
13992 struct tg3 *tp = netdev_priv(dev);
13993 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13994 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13995
Joe Perches63c3a662011-04-26 08:12:10 +000013996 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013997 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13998 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13999 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
14000 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
14001 }
14002
14003 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
14004 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
14005 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
14006 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
14007 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
14008 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
14009 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
14010 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
14011 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
14012 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
14013 return -EINVAL;
14014
14015 /* No rx interrupts will be generated if both are zero */
14016 if ((ec->rx_coalesce_usecs == 0) &&
14017 (ec->rx_max_coalesced_frames == 0))
14018 return -EINVAL;
14019
14020 /* No tx interrupts will be generated if both are zero */
14021 if ((ec->tx_coalesce_usecs == 0) &&
14022 (ec->tx_max_coalesced_frames == 0))
14023 return -EINVAL;
14024
14025 /* Only copy relevant parameters, ignore all others. */
14026 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
14027 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
14028 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
14029 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
14030 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
14031 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
14032 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
14033 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
14034 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
14035
14036 if (netif_running(dev)) {
14037 tg3_full_lock(tp, 0);
14038 __tg3_set_coalesce(tp, &tp->coal);
14039 tg3_full_unlock(tp);
14040 }
14041 return 0;
14042}
14043
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014044static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
14045{
14046 struct tg3 *tp = netdev_priv(dev);
14047
14048 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14049 netdev_warn(tp->dev, "Board does not support EEE!\n");
14050 return -EOPNOTSUPP;
14051 }
14052
14053 if (edata->advertised != tp->eee.advertised) {
14054 netdev_warn(tp->dev,
14055 "Direct manipulation of EEE advertisement is not supported\n");
14056 return -EINVAL;
14057 }
14058
14059 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
14060 netdev_warn(tp->dev,
14061 "Maximal Tx Lpi timer supported is %#x(u)\n",
14062 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
14063 return -EINVAL;
14064 }
14065
14066 tp->eee = *edata;
14067
14068 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
14069 tg3_warn_mgmt_link_flap(tp);
14070
14071 if (netif_running(tp->dev)) {
14072 tg3_full_lock(tp, 0);
14073 tg3_setup_eee(tp);
14074 tg3_phy_reset(tp);
14075 tg3_full_unlock(tp);
14076 }
14077
14078 return 0;
14079}
14080
14081static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
14082{
14083 struct tg3 *tp = netdev_priv(dev);
14084
14085 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14086 netdev_warn(tp->dev,
14087 "Board does not support EEE!\n");
14088 return -EOPNOTSUPP;
14089 }
14090
14091 *edata = tp->eee;
14092 return 0;
14093}
14094
Jeff Garzik7282d492006-09-13 14:30:00 -040014095static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014096 .get_settings = tg3_get_settings,
14097 .set_settings = tg3_set_settings,
14098 .get_drvinfo = tg3_get_drvinfo,
14099 .get_regs_len = tg3_get_regs_len,
14100 .get_regs = tg3_get_regs,
14101 .get_wol = tg3_get_wol,
14102 .set_wol = tg3_set_wol,
14103 .get_msglevel = tg3_get_msglevel,
14104 .set_msglevel = tg3_set_msglevel,
14105 .nway_reset = tg3_nway_reset,
14106 .get_link = ethtool_op_get_link,
14107 .get_eeprom_len = tg3_get_eeprom_len,
14108 .get_eeprom = tg3_get_eeprom,
14109 .set_eeprom = tg3_set_eeprom,
14110 .get_ringparam = tg3_get_ringparam,
14111 .set_ringparam = tg3_set_ringparam,
14112 .get_pauseparam = tg3_get_pauseparam,
14113 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070014114 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000014116 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014117 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070014118 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070014119 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070014120 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000014121 .get_rxnfc = tg3_get_rxnfc,
14122 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
Ben Hutchingsfe62d002014-05-15 01:25:27 +010014123 .get_rxfh = tg3_get_rxfh,
14124 .set_rxfh = tg3_set_rxfh,
Michael Chan09681692012-09-28 07:12:42 +000014125 .get_channels = tg3_get_channels,
14126 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000014127 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014128 .get_eee = tg3_get_eee,
14129 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014130};
14131
David S. Millerb4017c52012-03-01 17:57:40 -050014132static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
14133 struct rtnl_link_stats64 *stats)
14134{
14135 struct tg3 *tp = netdev_priv(dev);
14136
David S. Millerb4017c52012-03-01 17:57:40 -050014137 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000014138 if (!tp->hw_stats) {
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014139 *stats = tp->net_stats_prev;
Michael Chan0f566b22012-07-29 19:15:44 +000014140 spin_unlock_bh(&tp->lock);
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014141 return stats;
Michael Chan0f566b22012-07-29 19:15:44 +000014142 }
14143
David S. Millerb4017c52012-03-01 17:57:40 -050014144 tg3_get_nstats(tp, stats);
14145 spin_unlock_bh(&tp->lock);
14146
14147 return stats;
14148}
14149
Matt Carlsonccd5ba92012-02-13 10:20:08 +000014150static void tg3_set_rx_mode(struct net_device *dev)
14151{
14152 struct tg3 *tp = netdev_priv(dev);
14153
14154 if (!netif_running(dev))
14155 return;
14156
14157 tg3_full_lock(tp, 0);
14158 __tg3_set_rx_mode(dev);
14159 tg3_full_unlock(tp);
14160}
14161
Matt Carlsonfaf16272012-02-13 10:20:07 +000014162static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
14163 int new_mtu)
14164{
14165 dev->mtu = new_mtu;
14166
14167 if (new_mtu > ETH_DATA_LEN) {
14168 if (tg3_flag(tp, 5780_CLASS)) {
14169 netdev_update_features(dev);
14170 tg3_flag_clear(tp, TSO_CAPABLE);
14171 } else {
14172 tg3_flag_set(tp, JUMBO_RING_ENABLE);
14173 }
14174 } else {
14175 if (tg3_flag(tp, 5780_CLASS)) {
14176 tg3_flag_set(tp, TSO_CAPABLE);
14177 netdev_update_features(dev);
14178 }
14179 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
14180 }
14181}
14182
14183static int tg3_change_mtu(struct net_device *dev, int new_mtu)
14184{
14185 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000014186 int err;
14187 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000014188
14189 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
14190 return -EINVAL;
14191
14192 if (!netif_running(dev)) {
14193 /* We'll just catch it later when the
14194 * device is up'd.
14195 */
14196 tg3_set_mtu(dev, tp, new_mtu);
14197 return 0;
14198 }
14199
14200 tg3_phy_stop(tp);
14201
14202 tg3_netif_stop(tp);
14203
Nithin Sujirc6993df2014-02-06 14:13:05 -080014204 tg3_set_mtu(dev, tp, new_mtu);
14205
Matt Carlsonfaf16272012-02-13 10:20:07 +000014206 tg3_full_lock(tp, 1);
14207
14208 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14209
Michael Chan2fae5e32012-03-04 14:48:15 +000014210 /* Reset PHY, otherwise the read DMA engine will be in a mode that
14211 * breaks all requests to 256 bytes.
14212 */
Joe Perches41535772013-02-16 11:20:04 +000014213 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000014214 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000014215
14216 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000014217
14218 if (!err)
14219 tg3_netif_start(tp);
14220
14221 tg3_full_unlock(tp);
14222
14223 if (!err)
14224 tg3_phy_start(tp);
14225
14226 return err;
14227}
14228
14229static const struct net_device_ops tg3_netdev_ops = {
14230 .ndo_open = tg3_open,
14231 .ndo_stop = tg3_close,
14232 .ndo_start_xmit = tg3_start_xmit,
14233 .ndo_get_stats64 = tg3_get_stats64,
14234 .ndo_validate_addr = eth_validate_addr,
14235 .ndo_set_rx_mode = tg3_set_rx_mode,
14236 .ndo_set_mac_address = tg3_set_mac_addr,
14237 .ndo_do_ioctl = tg3_ioctl,
14238 .ndo_tx_timeout = tg3_tx_timeout,
14239 .ndo_change_mtu = tg3_change_mtu,
14240 .ndo_fix_features = tg3_fix_features,
14241 .ndo_set_features = tg3_set_features,
14242#ifdef CONFIG_NET_POLL_CONTROLLER
14243 .ndo_poll_controller = tg3_poll_controller,
14244#endif
14245};
14246
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014247static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014248{
Michael Chan1b277772006-03-20 22:27:48 -080014249 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014250
14251 tp->nvram_size = EEPROM_CHIP_SIZE;
14252
Matt Carlsone4f34112009-02-25 14:25:00 +000014253 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014254 return;
14255
Michael Chanb16250e2006-09-27 16:10:14 -070014256 if ((magic != TG3_EEPROM_MAGIC) &&
14257 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14258 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014259 return;
14260
14261 /*
14262 * Size the chip by reading offsets at increasing powers of two.
14263 * When we encounter our validation signature, we know the addressing
14264 * has wrapped around, and thus have our chip size.
14265 */
Michael Chan1b277772006-03-20 22:27:48 -080014266 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267
14268 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014269 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270 return;
14271
Michael Chan18201802006-03-20 22:29:15 -080014272 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014273 break;
14274
14275 cursize <<= 1;
14276 }
14277
14278 tp->nvram_size = cursize;
14279}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014280
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014281static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014282{
14283 u32 val;
14284
Joe Perches63c3a662011-04-26 08:12:10 +000014285 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014286 return;
14287
14288 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014289 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014290 tg3_get_eeprom_size(tp);
14291 return;
14292 }
14293
Matt Carlson6d348f22009-02-25 14:25:52 +000014294 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014295 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014296 /* This is confusing. We want to operate on the
14297 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14298 * call will read from NVRAM and byteswap the data
14299 * according to the byteswapping settings for all
14300 * other register accesses. This ensures the data we
14301 * want will always reside in the lower 16-bits.
14302 * However, the data in NVRAM is in LE format, which
14303 * means the data from the NVRAM read will always be
14304 * opposite the endianness of the CPU. The 16-bit
14305 * byteswap then brings the data to CPU endianness.
14306 */
14307 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014308 return;
14309 }
14310 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014311 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014312}
14313
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014314static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014315{
14316 u32 nvcfg1;
14317
14318 nvcfg1 = tr32(NVRAM_CFG1);
14319 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014320 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014321 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014322 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14323 tw32(NVRAM_CFG1, nvcfg1);
14324 }
14325
Joe Perches41535772013-02-16 11:20:04 +000014326 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014327 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014328 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014329 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14330 tp->nvram_jedecnum = JEDEC_ATMEL;
14331 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014332 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014333 break;
14334 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14335 tp->nvram_jedecnum = JEDEC_ATMEL;
14336 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14337 break;
14338 case FLASH_VENDOR_ATMEL_EEPROM:
14339 tp->nvram_jedecnum = JEDEC_ATMEL;
14340 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014341 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014342 break;
14343 case FLASH_VENDOR_ST:
14344 tp->nvram_jedecnum = JEDEC_ST;
14345 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014346 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014347 break;
14348 case FLASH_VENDOR_SAIFUN:
14349 tp->nvram_jedecnum = JEDEC_SAIFUN;
14350 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14351 break;
14352 case FLASH_VENDOR_SST_SMALL:
14353 case FLASH_VENDOR_SST_LARGE:
14354 tp->nvram_jedecnum = JEDEC_SST;
14355 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14356 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357 }
Matt Carlson8590a602009-08-28 12:29:16 +000014358 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014359 tp->nvram_jedecnum = JEDEC_ATMEL;
14360 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014361 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014362 }
14363}
14364
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014365static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014366{
14367 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14368 case FLASH_5752PAGE_SIZE_256:
14369 tp->nvram_pagesize = 256;
14370 break;
14371 case FLASH_5752PAGE_SIZE_512:
14372 tp->nvram_pagesize = 512;
14373 break;
14374 case FLASH_5752PAGE_SIZE_1K:
14375 tp->nvram_pagesize = 1024;
14376 break;
14377 case FLASH_5752PAGE_SIZE_2K:
14378 tp->nvram_pagesize = 2048;
14379 break;
14380 case FLASH_5752PAGE_SIZE_4K:
14381 tp->nvram_pagesize = 4096;
14382 break;
14383 case FLASH_5752PAGE_SIZE_264:
14384 tp->nvram_pagesize = 264;
14385 break;
14386 case FLASH_5752PAGE_SIZE_528:
14387 tp->nvram_pagesize = 528;
14388 break;
14389 }
14390}
14391
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014392static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014393{
14394 u32 nvcfg1;
14395
14396 nvcfg1 = tr32(NVRAM_CFG1);
14397
Michael Chane6af3012005-04-21 17:12:05 -070014398 /* NVRAM protection for TPM */
14399 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014400 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014401
Michael Chan361b4ac2005-04-21 17:11:21 -070014402 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014403 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14404 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14405 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014406 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014407 break;
14408 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14409 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014410 tg3_flag_set(tp, NVRAM_BUFFERED);
14411 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014412 break;
14413 case FLASH_5752VENDOR_ST_M45PE10:
14414 case FLASH_5752VENDOR_ST_M45PE20:
14415 case FLASH_5752VENDOR_ST_M45PE40:
14416 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014417 tg3_flag_set(tp, NVRAM_BUFFERED);
14418 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014419 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014420 }
14421
Joe Perches63c3a662011-04-26 08:12:10 +000014422 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014423 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014424 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014425 /* For eeprom, set pagesize to maximum eeprom size */
14426 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14427
14428 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14429 tw32(NVRAM_CFG1, nvcfg1);
14430 }
14431}
14432
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014433static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014434{
Matt Carlson989a9d22007-05-05 11:51:05 -070014435 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014436
14437 nvcfg1 = tr32(NVRAM_CFG1);
14438
14439 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014440 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014441 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014442 protect = 1;
14443 }
Michael Chand3c7b882006-03-23 01:28:25 -080014444
Matt Carlson989a9d22007-05-05 11:51:05 -070014445 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14446 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014447 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14448 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14449 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14450 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14451 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014452 tg3_flag_set(tp, NVRAM_BUFFERED);
14453 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014454 tp->nvram_pagesize = 264;
14455 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14456 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14457 tp->nvram_size = (protect ? 0x3e200 :
14458 TG3_NVRAM_SIZE_512KB);
14459 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14460 tp->nvram_size = (protect ? 0x1f200 :
14461 TG3_NVRAM_SIZE_256KB);
14462 else
14463 tp->nvram_size = (protect ? 0x1f200 :
14464 TG3_NVRAM_SIZE_128KB);
14465 break;
14466 case FLASH_5752VENDOR_ST_M45PE10:
14467 case FLASH_5752VENDOR_ST_M45PE20:
14468 case FLASH_5752VENDOR_ST_M45PE40:
14469 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014470 tg3_flag_set(tp, NVRAM_BUFFERED);
14471 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014472 tp->nvram_pagesize = 256;
14473 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14474 tp->nvram_size = (protect ?
14475 TG3_NVRAM_SIZE_64KB :
14476 TG3_NVRAM_SIZE_128KB);
14477 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14478 tp->nvram_size = (protect ?
14479 TG3_NVRAM_SIZE_64KB :
14480 TG3_NVRAM_SIZE_256KB);
14481 else
14482 tp->nvram_size = (protect ?
14483 TG3_NVRAM_SIZE_128KB :
14484 TG3_NVRAM_SIZE_512KB);
14485 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014486 }
14487}
14488
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014489static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014490{
14491 u32 nvcfg1;
14492
14493 nvcfg1 = tr32(NVRAM_CFG1);
14494
14495 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014496 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14497 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14498 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14499 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14500 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014501 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014502 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014503
Matt Carlson8590a602009-08-28 12:29:16 +000014504 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14505 tw32(NVRAM_CFG1, nvcfg1);
14506 break;
14507 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14508 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14509 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14510 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14511 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014512 tg3_flag_set(tp, NVRAM_BUFFERED);
14513 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014514 tp->nvram_pagesize = 264;
14515 break;
14516 case FLASH_5752VENDOR_ST_M45PE10:
14517 case FLASH_5752VENDOR_ST_M45PE20:
14518 case FLASH_5752VENDOR_ST_M45PE40:
14519 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014520 tg3_flag_set(tp, NVRAM_BUFFERED);
14521 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014522 tp->nvram_pagesize = 256;
14523 break;
Michael Chan1b277772006-03-20 22:27:48 -080014524 }
14525}
14526
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014527static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014528{
14529 u32 nvcfg1, protect = 0;
14530
14531 nvcfg1 = tr32(NVRAM_CFG1);
14532
14533 /* NVRAM protection for TPM */
14534 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014535 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014536 protect = 1;
14537 }
14538
14539 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14540 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014541 case FLASH_5761VENDOR_ATMEL_ADB021D:
14542 case FLASH_5761VENDOR_ATMEL_ADB041D:
14543 case FLASH_5761VENDOR_ATMEL_ADB081D:
14544 case FLASH_5761VENDOR_ATMEL_ADB161D:
14545 case FLASH_5761VENDOR_ATMEL_MDB021D:
14546 case FLASH_5761VENDOR_ATMEL_MDB041D:
14547 case FLASH_5761VENDOR_ATMEL_MDB081D:
14548 case FLASH_5761VENDOR_ATMEL_MDB161D:
14549 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014550 tg3_flag_set(tp, NVRAM_BUFFERED);
14551 tg3_flag_set(tp, FLASH);
14552 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014553 tp->nvram_pagesize = 256;
14554 break;
14555 case FLASH_5761VENDOR_ST_A_M45PE20:
14556 case FLASH_5761VENDOR_ST_A_M45PE40:
14557 case FLASH_5761VENDOR_ST_A_M45PE80:
14558 case FLASH_5761VENDOR_ST_A_M45PE16:
14559 case FLASH_5761VENDOR_ST_M_M45PE20:
14560 case FLASH_5761VENDOR_ST_M_M45PE40:
14561 case FLASH_5761VENDOR_ST_M_M45PE80:
14562 case FLASH_5761VENDOR_ST_M_M45PE16:
14563 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014564 tg3_flag_set(tp, NVRAM_BUFFERED);
14565 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014566 tp->nvram_pagesize = 256;
14567 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014568 }
14569
14570 if (protect) {
14571 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14572 } else {
14573 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014574 case FLASH_5761VENDOR_ATMEL_ADB161D:
14575 case FLASH_5761VENDOR_ATMEL_MDB161D:
14576 case FLASH_5761VENDOR_ST_A_M45PE16:
14577 case FLASH_5761VENDOR_ST_M_M45PE16:
14578 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14579 break;
14580 case FLASH_5761VENDOR_ATMEL_ADB081D:
14581 case FLASH_5761VENDOR_ATMEL_MDB081D:
14582 case FLASH_5761VENDOR_ST_A_M45PE80:
14583 case FLASH_5761VENDOR_ST_M_M45PE80:
14584 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14585 break;
14586 case FLASH_5761VENDOR_ATMEL_ADB041D:
14587 case FLASH_5761VENDOR_ATMEL_MDB041D:
14588 case FLASH_5761VENDOR_ST_A_M45PE40:
14589 case FLASH_5761VENDOR_ST_M_M45PE40:
14590 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14591 break;
14592 case FLASH_5761VENDOR_ATMEL_ADB021D:
14593 case FLASH_5761VENDOR_ATMEL_MDB021D:
14594 case FLASH_5761VENDOR_ST_A_M45PE20:
14595 case FLASH_5761VENDOR_ST_M_M45PE20:
14596 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14597 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014598 }
14599 }
14600}
14601
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014602static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014603{
14604 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014605 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014606 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14607}
14608
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014609static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014610{
14611 u32 nvcfg1;
14612
14613 nvcfg1 = tr32(NVRAM_CFG1);
14614
14615 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14616 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14617 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14618 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014619 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014620 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14621
14622 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14623 tw32(NVRAM_CFG1, nvcfg1);
14624 return;
14625 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14626 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14627 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14628 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14629 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14630 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14631 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14632 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014633 tg3_flag_set(tp, NVRAM_BUFFERED);
14634 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014635
14636 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14637 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14638 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14639 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14640 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14641 break;
14642 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14643 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14644 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14645 break;
14646 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14647 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14648 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14649 break;
14650 }
14651 break;
14652 case FLASH_5752VENDOR_ST_M45PE10:
14653 case FLASH_5752VENDOR_ST_M45PE20:
14654 case FLASH_5752VENDOR_ST_M45PE40:
14655 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014656 tg3_flag_set(tp, NVRAM_BUFFERED);
14657 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014658
14659 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14660 case FLASH_5752VENDOR_ST_M45PE10:
14661 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14662 break;
14663 case FLASH_5752VENDOR_ST_M45PE20:
14664 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14665 break;
14666 case FLASH_5752VENDOR_ST_M45PE40:
14667 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14668 break;
14669 }
14670 break;
14671 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014672 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014673 return;
14674 }
14675
Matt Carlsona1b950d2009-09-01 13:20:17 +000014676 tg3_nvram_get_pagesize(tp, nvcfg1);
14677 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014678 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014679}
14680
14681
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014682static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014683{
14684 u32 nvcfg1;
14685
14686 nvcfg1 = tr32(NVRAM_CFG1);
14687
14688 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14689 case FLASH_5717VENDOR_ATMEL_EEPROM:
14690 case FLASH_5717VENDOR_MICRO_EEPROM:
14691 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014692 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014693 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14694
14695 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14696 tw32(NVRAM_CFG1, nvcfg1);
14697 return;
14698 case FLASH_5717VENDOR_ATMEL_MDB011D:
14699 case FLASH_5717VENDOR_ATMEL_ADB011B:
14700 case FLASH_5717VENDOR_ATMEL_ADB011D:
14701 case FLASH_5717VENDOR_ATMEL_MDB021D:
14702 case FLASH_5717VENDOR_ATMEL_ADB021B:
14703 case FLASH_5717VENDOR_ATMEL_ADB021D:
14704 case FLASH_5717VENDOR_ATMEL_45USPT:
14705 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014706 tg3_flag_set(tp, NVRAM_BUFFERED);
14707 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014708
14709 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14710 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014711 /* Detect size with tg3_nvram_get_size() */
14712 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014713 case FLASH_5717VENDOR_ATMEL_ADB021B:
14714 case FLASH_5717VENDOR_ATMEL_ADB021D:
14715 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14716 break;
14717 default:
14718 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14719 break;
14720 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014721 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014722 case FLASH_5717VENDOR_ST_M_M25PE10:
14723 case FLASH_5717VENDOR_ST_A_M25PE10:
14724 case FLASH_5717VENDOR_ST_M_M45PE10:
14725 case FLASH_5717VENDOR_ST_A_M45PE10:
14726 case FLASH_5717VENDOR_ST_M_M25PE20:
14727 case FLASH_5717VENDOR_ST_A_M25PE20:
14728 case FLASH_5717VENDOR_ST_M_M45PE20:
14729 case FLASH_5717VENDOR_ST_A_M45PE20:
14730 case FLASH_5717VENDOR_ST_25USPT:
14731 case FLASH_5717VENDOR_ST_45USPT:
14732 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014733 tg3_flag_set(tp, NVRAM_BUFFERED);
14734 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014735
14736 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14737 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014738 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014739 /* Detect size with tg3_nvram_get_size() */
14740 break;
14741 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014742 case FLASH_5717VENDOR_ST_A_M45PE20:
14743 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14744 break;
14745 default:
14746 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14747 break;
14748 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014749 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014750 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014751 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014752 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014753 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014754
14755 tg3_nvram_get_pagesize(tp, nvcfg1);
14756 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014757 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014758}
14759
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014760static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014761{
14762 u32 nvcfg1, nvmpinstrp;
14763
14764 nvcfg1 = tr32(NVRAM_CFG1);
14765 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14766
Joe Perches41535772013-02-16 11:20:04 +000014767 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014768 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14769 tg3_flag_set(tp, NO_NVRAM);
14770 return;
14771 }
14772
14773 switch (nvmpinstrp) {
14774 case FLASH_5762_EEPROM_HD:
14775 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014776 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014777 case FLASH_5762_EEPROM_LD:
14778 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014779 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014780 case FLASH_5720VENDOR_M_ST_M45PE20:
14781 /* This pinstrap supports multiple sizes, so force it
14782 * to read the actual size from location 0xf0.
14783 */
14784 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14785 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014786 }
14787 }
14788
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014789 switch (nvmpinstrp) {
14790 case FLASH_5720_EEPROM_HD:
14791 case FLASH_5720_EEPROM_LD:
14792 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014793 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014794
14795 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14796 tw32(NVRAM_CFG1, nvcfg1);
14797 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14798 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14799 else
14800 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14801 return;
14802 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14803 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14804 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14805 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14806 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14807 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14808 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14809 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14810 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14811 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14812 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14813 case FLASH_5720VENDOR_ATMEL_45USPT:
14814 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014815 tg3_flag_set(tp, NVRAM_BUFFERED);
14816 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014817
14818 switch (nvmpinstrp) {
14819 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14820 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14821 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14822 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14823 break;
14824 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14825 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14826 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14827 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14828 break;
14829 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14830 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14831 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14832 break;
14833 default:
Joe Perches41535772013-02-16 11:20:04 +000014834 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014835 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014836 break;
14837 }
14838 break;
14839 case FLASH_5720VENDOR_M_ST_M25PE10:
14840 case FLASH_5720VENDOR_M_ST_M45PE10:
14841 case FLASH_5720VENDOR_A_ST_M25PE10:
14842 case FLASH_5720VENDOR_A_ST_M45PE10:
14843 case FLASH_5720VENDOR_M_ST_M25PE20:
14844 case FLASH_5720VENDOR_M_ST_M45PE20:
14845 case FLASH_5720VENDOR_A_ST_M25PE20:
14846 case FLASH_5720VENDOR_A_ST_M45PE20:
14847 case FLASH_5720VENDOR_M_ST_M25PE40:
14848 case FLASH_5720VENDOR_M_ST_M45PE40:
14849 case FLASH_5720VENDOR_A_ST_M25PE40:
14850 case FLASH_5720VENDOR_A_ST_M45PE40:
14851 case FLASH_5720VENDOR_M_ST_M25PE80:
14852 case FLASH_5720VENDOR_M_ST_M45PE80:
14853 case FLASH_5720VENDOR_A_ST_M25PE80:
14854 case FLASH_5720VENDOR_A_ST_M45PE80:
14855 case FLASH_5720VENDOR_ST_25USPT:
14856 case FLASH_5720VENDOR_ST_45USPT:
14857 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014858 tg3_flag_set(tp, NVRAM_BUFFERED);
14859 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014860
14861 switch (nvmpinstrp) {
14862 case FLASH_5720VENDOR_M_ST_M25PE20:
14863 case FLASH_5720VENDOR_M_ST_M45PE20:
14864 case FLASH_5720VENDOR_A_ST_M25PE20:
14865 case FLASH_5720VENDOR_A_ST_M45PE20:
14866 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14867 break;
14868 case FLASH_5720VENDOR_M_ST_M25PE40:
14869 case FLASH_5720VENDOR_M_ST_M45PE40:
14870 case FLASH_5720VENDOR_A_ST_M25PE40:
14871 case FLASH_5720VENDOR_A_ST_M45PE40:
14872 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14873 break;
14874 case FLASH_5720VENDOR_M_ST_M25PE80:
14875 case FLASH_5720VENDOR_M_ST_M45PE80:
14876 case FLASH_5720VENDOR_A_ST_M25PE80:
14877 case FLASH_5720VENDOR_A_ST_M45PE80:
14878 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14879 break;
14880 default:
Joe Perches41535772013-02-16 11:20:04 +000014881 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014882 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014883 break;
14884 }
14885 break;
14886 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014887 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014888 return;
14889 }
14890
14891 tg3_nvram_get_pagesize(tp, nvcfg1);
14892 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014893 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014894
Joe Perches41535772013-02-16 11:20:04 +000014895 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014896 u32 val;
14897
14898 if (tg3_nvram_read(tp, 0, &val))
14899 return;
14900
14901 if (val != TG3_EEPROM_MAGIC &&
14902 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14903 tg3_flag_set(tp, NO_NVRAM);
14904 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014905}
14906
Linus Torvalds1da177e2005-04-16 15:20:36 -070014907/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014908static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014909{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014910 if (tg3_flag(tp, IS_SSB_CORE)) {
14911 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14912 tg3_flag_clear(tp, NVRAM);
14913 tg3_flag_clear(tp, NVRAM_BUFFERED);
14914 tg3_flag_set(tp, NO_NVRAM);
14915 return;
14916 }
14917
Linus Torvalds1da177e2005-04-16 15:20:36 -070014918 tw32_f(GRC_EEPROM_ADDR,
14919 (EEPROM_ADDR_FSM_RESET |
14920 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14921 EEPROM_ADDR_CLKPERD_SHIFT)));
14922
Michael Chan9d57f012006-12-07 00:23:25 -080014923 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014924
14925 /* Enable seeprom accesses. */
14926 tw32_f(GRC_LOCAL_CTRL,
14927 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14928 udelay(100);
14929
Joe Perches41535772013-02-16 11:20:04 +000014930 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14931 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014932 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014933
Michael Chanec41c7d2006-01-17 02:40:55 -080014934 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014935 netdev_warn(tp->dev,
14936 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014937 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014938 return;
14939 }
Michael Chane6af3012005-04-21 17:12:05 -070014940 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014941
Matt Carlson989a9d22007-05-05 11:51:05 -070014942 tp->nvram_size = 0;
14943
Joe Perches41535772013-02-16 11:20:04 +000014944 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014945 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014946 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014947 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014948 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14949 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14950 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014951 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014952 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014953 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014954 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014955 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014956 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014957 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014958 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014959 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14960 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014961 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014962 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14963 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014964 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014965 else
14966 tg3_get_nvram_info(tp);
14967
Matt Carlson989a9d22007-05-05 11:51:05 -070014968 if (tp->nvram_size == 0)
14969 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014970
Michael Chane6af3012005-04-21 17:12:05 -070014971 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014972 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014973
14974 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014975 tg3_flag_clear(tp, NVRAM);
14976 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014977
14978 tg3_get_eeprom_size(tp);
14979 }
14980}
14981
Linus Torvalds1da177e2005-04-16 15:20:36 -070014982struct subsys_tbl_ent {
14983 u16 subsys_vendor, subsys_devid;
14984 u32 phy_id;
14985};
14986
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014987static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014988 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014989 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014990 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014991 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014992 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014993 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014994 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014995 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14996 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14997 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014998 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014999 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015000 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015001 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15002 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
15003 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015004 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015005 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015006 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015007 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015008 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015009 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015010 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011
15012 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015013 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015014 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015015 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015016 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015017 { TG3PCI_SUBVENDOR_ID_3COM,
15018 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
15019 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015020 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015021 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015022 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015023
15024 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015025 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015026 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015027 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015028 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015029 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015030 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015031 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015032 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015033
15034 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015035 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015036 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015037 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015038 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015039 { TG3PCI_SUBVENDOR_ID_COMPAQ,
15040 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
15041 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015042 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015043 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015044 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015045
15046 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015047 { TG3PCI_SUBVENDOR_ID_IBM,
15048 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015049};
15050
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015051static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015052{
15053 int i;
15054
15055 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
15056 if ((subsys_id_to_phy_id[i].subsys_vendor ==
15057 tp->pdev->subsystem_vendor) &&
15058 (subsys_id_to_phy_id[i].subsys_devid ==
15059 tp->pdev->subsystem_device))
15060 return &subsys_id_to_phy_id[i];
15061 }
15062 return NULL;
15063}
15064
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015065static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015066{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015067 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070015068
Matt Carlson79eb6902010-02-17 15:17:03 +000015069 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015070 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15071
Gary Zambranoa85feb82007-05-05 11:52:19 -070015072 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000015073 tg3_flag_set(tp, EEPROM_WRITE_PROT);
15074 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080015075
Joe Perches41535772013-02-16 11:20:04 +000015076 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080015077 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015078 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15079 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015080 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015081 val = tr32(VCPU_CFGSHDW);
15082 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000015083 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070015084 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015085 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015086 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015087 device_set_wakeup_enable(&tp->pdev->dev, true);
15088 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015089 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070015090 }
15091
Linus Torvalds1da177e2005-04-16 15:20:36 -070015092 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
15093 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
15094 u32 nic_cfg, led_cfg;
Nithin Sujir7c786062013-12-06 09:53:17 -080015095 u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
15096 u32 nic_phy_id, ver, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015097 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015098
15099 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
15100 tp->nic_sram_data_cfg = nic_cfg;
15101
15102 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
15103 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015104 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15105 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15106 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015107 (ver > 0) && (ver < 0x100))
15108 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
15109
Joe Perches41535772013-02-16 11:20:04 +000015110 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070015111 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
15112
Nithin Sujir7c786062013-12-06 09:53:17 -080015113 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15114 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15115 tg3_asic_rev(tp) == ASIC_REV_5720)
15116 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
15117
Linus Torvalds1da177e2005-04-16 15:20:36 -070015118 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
15119 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
15120 eeprom_phy_serdes = 1;
15121
15122 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
15123 if (nic_phy_id != 0) {
15124 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
15125 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
15126
15127 eeprom_phy_id = (id1 >> 16) << 10;
15128 eeprom_phy_id |= (id2 & 0xfc00) << 16;
15129 eeprom_phy_id |= (id2 & 0x03ff) << 0;
15130 } else
15131 eeprom_phy_id = 0;
15132
Michael Chan7d0c41e2005-04-21 17:06:20 -070015133 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070015134 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000015135 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015136 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000015137 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015138 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070015139 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070015140
Joe Perches63c3a662011-04-26 08:12:10 +000015141 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015142 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
15143 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070015144 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015145 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
15146
15147 switch (led_cfg) {
15148 default:
15149 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
15150 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15151 break;
15152
15153 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
15154 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15155 break;
15156
15157 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
15158 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070015159
15160 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
15161 * read on some older 5700/5701 bootcode.
15162 */
Joe Perches41535772013-02-16 11:20:04 +000015163 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15164 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070015165 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15166
Linus Torvalds1da177e2005-04-16 15:20:36 -070015167 break;
15168
15169 case SHASTA_EXT_LED_SHARED:
15170 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000015171 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
15172 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015173 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15174 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070015175
15176 if (tg3_flag(tp, 5717_PLUS) ||
15177 tg3_asic_rev(tp) == ASIC_REV_5762)
15178 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
15179 LED_CTRL_BLINK_RATE_MASK;
15180
Linus Torvalds1da177e2005-04-16 15:20:36 -070015181 break;
15182
15183 case SHASTA_EXT_LED_MAC:
15184 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
15185 break;
15186
15187 case SHASTA_EXT_LED_COMBO:
15188 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000015189 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015190 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15191 LED_CTRL_MODE_PHY_2);
15192 break;
15193
Stephen Hemminger855e1112008-04-16 16:37:28 -070015194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015195
Joe Perches41535772013-02-16 11:20:04 +000015196 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
15197 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015198 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
15199 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15200
Joe Perches41535772013-02-16 11:20:04 +000015201 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015202 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080015203
Michael Chan9d26e212006-12-07 00:21:14 -080015204 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000015205 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015206 if ((tp->pdev->subsystem_vendor ==
15207 PCI_VENDOR_ID_ARIMA) &&
15208 (tp->pdev->subsystem_device == 0x205a ||
15209 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000015210 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015211 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015212 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15213 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015215
15216 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000015217 tg3_flag_set(tp, ENABLE_ASF);
15218 if (tg3_flag(tp, 5750_PLUS))
15219 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015220 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015221
15222 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015223 tg3_flag(tp, 5750_PLUS))
15224 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015225
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015226 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070015227 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000015228 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015229
Joe Perches63c3a662011-04-26 08:12:10 +000015230 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015231 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015232 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015233 device_set_wakeup_enable(&tp->pdev->dev, true);
15234 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015235
Linus Torvalds1da177e2005-04-16 15:20:36 -070015236 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015237 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015238
15239 /* serdes signal pre-emphasis in register 0x590 set by */
15240 /* bootcode if bit 18 is set */
15241 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015242 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015243
Joe Perches63c3a662011-04-26 08:12:10 +000015244 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015245 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15246 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015247 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015248 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015249
Nithin Sujir942d1af2013-04-09 08:48:07 +000015250 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015251 u32 cfg3;
15252
15253 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015254 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15255 !tg3_flag(tp, 57765_PLUS) &&
15256 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015257 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015258 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15259 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15260 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15261 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015262 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015263
Matt Carlson14417062010-02-17 15:16:59 +000015264 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015265 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015266 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015267 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015268 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015269 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Nithin Sujir7c786062013-12-06 09:53:17 -080015270
15271 if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
15272 tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015273 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015274done:
Joe Perches63c3a662011-04-26 08:12:10 +000015275 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015276 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015277 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015278 else
15279 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015280}
15281
Michael Chanc86a8562013-01-06 12:51:08 +000015282static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15283{
15284 int i, err;
15285 u32 val2, off = offset * 8;
15286
15287 err = tg3_nvram_lock(tp);
15288 if (err)
15289 return err;
15290
15291 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15292 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15293 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15294 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15295 udelay(10);
15296
15297 for (i = 0; i < 100; i++) {
15298 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15299 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15300 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15301 break;
15302 }
15303 udelay(10);
15304 }
15305
15306 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15307
15308 tg3_nvram_unlock(tp);
15309 if (val2 & APE_OTP_STATUS_CMD_DONE)
15310 return 0;
15311
15312 return -EBUSY;
15313}
15314
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015315static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015316{
15317 int i;
15318 u32 val;
15319
15320 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15321 tw32(OTP_CTRL, cmd);
15322
15323 /* Wait for up to 1 ms for command to execute. */
15324 for (i = 0; i < 100; i++) {
15325 val = tr32(OTP_STATUS);
15326 if (val & OTP_STATUS_CMD_DONE)
15327 break;
15328 udelay(10);
15329 }
15330
15331 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15332}
15333
15334/* Read the gphy configuration from the OTP region of the chip. The gphy
15335 * configuration is a 32-bit value that straddles the alignment boundary.
15336 * We do two 32-bit reads and then shift and merge the results.
15337 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015338static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015339{
15340 u32 bhalf_otp, thalf_otp;
15341
15342 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15343
15344 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15345 return 0;
15346
15347 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15348
15349 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15350 return 0;
15351
15352 thalf_otp = tr32(OTP_READ_DATA);
15353
15354 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15355
15356 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15357 return 0;
15358
15359 bhalf_otp = tr32(OTP_READ_DATA);
15360
15361 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15362}
15363
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015364static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015365{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015366 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015367
Nithin Sujir7c786062013-12-06 09:53:17 -080015368 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
15369 if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
15370 adv |= ADVERTISED_1000baseT_Half;
15371 adv |= ADVERTISED_1000baseT_Full;
15372 }
Matt Carlsone256f8a2011-03-09 16:58:24 +000015373
15374 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15375 adv |= ADVERTISED_100baseT_Half |
15376 ADVERTISED_100baseT_Full |
15377 ADVERTISED_10baseT_Half |
15378 ADVERTISED_10baseT_Full |
15379 ADVERTISED_TP;
15380 else
15381 adv |= ADVERTISED_FIBRE;
15382
15383 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015384 tp->link_config.speed = SPEED_UNKNOWN;
15385 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015386 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015387 tp->link_config.active_speed = SPEED_UNKNOWN;
15388 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015389
15390 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015391}
15392
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015393static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015394{
15395 u32 hw_phy_id_1, hw_phy_id_2;
15396 u32 hw_phy_id, hw_phy_id_masked;
15397 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015398
Matt Carlsone256f8a2011-03-09 16:58:24 +000015399 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015400 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015401 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15402
Michael Chan8151ad52012-07-29 19:15:41 +000015403 if (tg3_flag(tp, ENABLE_APE)) {
15404 switch (tp->pci_fn) {
15405 case 0:
15406 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15407 break;
15408 case 1:
15409 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15410 break;
15411 case 2:
15412 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15413 break;
15414 case 3:
15415 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15416 break;
15417 }
15418 }
15419
Nithin Sujir942d1af2013-04-09 08:48:07 +000015420 if (!tg3_flag(tp, ENABLE_ASF) &&
15421 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15422 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15423 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15424 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15425
Joe Perches63c3a662011-04-26 08:12:10 +000015426 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015427 return tg3_phy_init(tp);
15428
Linus Torvalds1da177e2005-04-16 15:20:36 -070015429 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015430 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015431 */
15432 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015433 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015434 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015435 } else {
15436 /* Now read the physical PHY_ID from the chip and verify
15437 * that it is sane. If it doesn't look good, we fall back
15438 * to either the hard-coded table based PHY_ID and failing
15439 * that the value found in the eeprom area.
15440 */
15441 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15442 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15443
15444 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15445 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15446 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15447
Matt Carlson79eb6902010-02-17 15:17:03 +000015448 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015449 }
15450
Matt Carlson79eb6902010-02-17 15:17:03 +000015451 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015452 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015453 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015454 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015455 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015456 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015457 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015458 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015459 /* Do nothing, phy ID already set up in
15460 * tg3_get_eeprom_hw_cfg().
15461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015462 } else {
15463 struct subsys_tbl_ent *p;
15464
15465 /* No eeprom signature? Try the hardcoded
15466 * subsys device table.
15467 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015468 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015469 if (p) {
15470 tp->phy_id = p->phy_id;
15471 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15472 /* For now we saw the IDs 0xbc050cd0,
15473 * 0xbc050f80 and 0xbc050c30 on devices
15474 * connected to an BCM4785 and there are
15475 * probably more. Just assume that the phy is
15476 * supported when it is connected to a SSB core
15477 * for now.
15478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015479 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015481
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015483 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015484 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015485 }
15486 }
15487
Matt Carlsona6b68da2010-12-06 08:28:52 +000015488 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015489 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15490 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015491 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015492 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15493 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15494 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15495 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015496 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015497 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15498
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015499 tp->eee.supported = SUPPORTED_100baseT_Full |
15500 SUPPORTED_1000baseT_Full;
15501 tp->eee.advertised = ADVERTISED_100baseT_Full |
15502 ADVERTISED_1000baseT_Full;
15503 tp->eee.eee_enabled = 1;
15504 tp->eee.tx_lpi_enabled = 1;
15505 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15506 }
15507
Matt Carlsone256f8a2011-03-09 16:58:24 +000015508 tg3_phy_init_link_config(tp);
15509
Nithin Sujir942d1af2013-04-09 08:48:07 +000015510 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15511 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015512 !tg3_flag(tp, ENABLE_APE) &&
15513 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015514 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015515
15516 tg3_readphy(tp, MII_BMSR, &bmsr);
15517 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15518 (bmsr & BMSR_LSTATUS))
15519 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015520
Linus Torvalds1da177e2005-04-16 15:20:36 -070015521 err = tg3_phy_reset(tp);
15522 if (err)
15523 return err;
15524
Matt Carlson42b64a42011-05-19 12:12:49 +000015525 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015526
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015527 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015528 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15529 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015530
15531 tg3_writephy(tp, MII_BMCR,
15532 BMCR_ANENABLE | BMCR_ANRESTART);
15533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015534 }
15535
15536skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015537 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015538 err = tg3_init_5401phy_dsp(tp);
15539 if (err)
15540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015541
Linus Torvalds1da177e2005-04-16 15:20:36 -070015542 err = tg3_init_5401phy_dsp(tp);
15543 }
15544
Linus Torvalds1da177e2005-04-16 15:20:36 -070015545 return err;
15546}
15547
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015548static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015549{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015550 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015551 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015552 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015553 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015554
Matt Carlson535a4902011-07-20 10:20:56 +000015555 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015556 if (!vpd_data)
15557 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015558
Matt Carlson535a4902011-07-20 10:20:56 +000015559 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015560 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015561 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015562
15563 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15564 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15565 i += PCI_VPD_LRDT_TAG_SIZE;
15566
Matt Carlson535a4902011-07-20 10:20:56 +000015567 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015568 goto out_not_found;
15569
Matt Carlson184b8902010-04-05 10:19:25 +000015570 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15571 PCI_VPD_RO_KEYWORD_MFR_ID);
15572 if (j > 0) {
15573 len = pci_vpd_info_field_size(&vpd_data[j]);
15574
15575 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15576 if (j + len > block_end || len != 4 ||
15577 memcmp(&vpd_data[j], "1028", 4))
15578 goto partno;
15579
15580 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15581 PCI_VPD_RO_KEYWORD_VENDOR0);
15582 if (j < 0)
15583 goto partno;
15584
15585 len = pci_vpd_info_field_size(&vpd_data[j]);
15586
15587 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15588 if (j + len > block_end)
15589 goto partno;
15590
Kees Cook715230a2013-03-27 06:40:50 +000015591 if (len >= sizeof(tp->fw_ver))
15592 len = sizeof(tp->fw_ver) - 1;
15593 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15594 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15595 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015596 }
15597
15598partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015599 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15600 PCI_VPD_RO_KEYWORD_PARTNO);
15601 if (i < 0)
15602 goto out_not_found;
15603
15604 len = pci_vpd_info_field_size(&vpd_data[i]);
15605
15606 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15607 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015608 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015609 goto out_not_found;
15610
15611 memcpy(tp->board_part_number, &vpd_data[i], len);
15612
Linus Torvalds1da177e2005-04-16 15:20:36 -070015613out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015614 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015615 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015616 return;
15617
15618out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015619 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015620 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15621 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015622 strcpy(tp->board_part_number, "BCM5717");
15623 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15624 strcpy(tp->board_part_number, "BCM5718");
15625 else
15626 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015627 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015628 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15629 strcpy(tp->board_part_number, "BCM57780");
15630 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15631 strcpy(tp->board_part_number, "BCM57760");
15632 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15633 strcpy(tp->board_part_number, "BCM57790");
15634 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15635 strcpy(tp->board_part_number, "BCM57788");
15636 else
15637 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015638 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015639 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15640 strcpy(tp->board_part_number, "BCM57761");
15641 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15642 strcpy(tp->board_part_number, "BCM57765");
15643 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15644 strcpy(tp->board_part_number, "BCM57781");
15645 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15646 strcpy(tp->board_part_number, "BCM57785");
15647 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15648 strcpy(tp->board_part_number, "BCM57791");
15649 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15650 strcpy(tp->board_part_number, "BCM57795");
15651 else
15652 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015653 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015654 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15655 strcpy(tp->board_part_number, "BCM57762");
15656 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15657 strcpy(tp->board_part_number, "BCM57766");
15658 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15659 strcpy(tp->board_part_number, "BCM57782");
15660 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15661 strcpy(tp->board_part_number, "BCM57786");
15662 else
15663 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015664 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015665 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015666 } else {
15667nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015668 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015670}
15671
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015672static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015673{
15674 u32 val;
15675
Matt Carlsone4f34112009-02-25 14:25:00 +000015676 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015677 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015678 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015679 val != 0)
15680 return 0;
15681
15682 return 1;
15683}
15684
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015685static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015686{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015687 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015688 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015689 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015690
15691 if (tg3_nvram_read(tp, 0xc, &offset) ||
15692 tg3_nvram_read(tp, 0x4, &start))
15693 return;
15694
15695 offset = tg3_nvram_logical_addr(tp, offset);
15696
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015697 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015698 return;
15699
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015700 if ((val & 0xfc000000) == 0x0c000000) {
15701 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015702 return;
15703
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015704 if (val == 0)
15705 newver = true;
15706 }
15707
Matt Carlson75f99362010-04-05 10:19:24 +000015708 dst_off = strlen(tp->fw_ver);
15709
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015710 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015711 if (TG3_VER_SIZE - dst_off < 16 ||
15712 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015713 return;
15714
15715 offset = offset + ver_offset - start;
15716 for (i = 0; i < 16; i += 4) {
15717 __be32 v;
15718 if (tg3_nvram_read_be32(tp, offset + i, &v))
15719 return;
15720
Matt Carlson75f99362010-04-05 10:19:24 +000015721 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015722 }
15723 } else {
15724 u32 major, minor;
15725
15726 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15727 return;
15728
15729 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15730 TG3_NVM_BCVER_MAJSFT;
15731 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015732 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15733 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015734 }
15735}
15736
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015737static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015738{
15739 u32 val, major, minor;
15740
15741 /* Use native endian representation */
15742 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15743 return;
15744
15745 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15746 TG3_NVM_HWSB_CFG1_MAJSFT;
15747 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15748 TG3_NVM_HWSB_CFG1_MINSFT;
15749
15750 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15751}
15752
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015753static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015754{
15755 u32 offset, major, minor, build;
15756
Matt Carlson75f99362010-04-05 10:19:24 +000015757 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015758
15759 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15760 return;
15761
15762 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15763 case TG3_EEPROM_SB_REVISION_0:
15764 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15765 break;
15766 case TG3_EEPROM_SB_REVISION_2:
15767 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15768 break;
15769 case TG3_EEPROM_SB_REVISION_3:
15770 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15771 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015772 case TG3_EEPROM_SB_REVISION_4:
15773 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15774 break;
15775 case TG3_EEPROM_SB_REVISION_5:
15776 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15777 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015778 case TG3_EEPROM_SB_REVISION_6:
15779 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15780 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015781 default:
15782 return;
15783 }
15784
Matt Carlsone4f34112009-02-25 14:25:00 +000015785 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015786 return;
15787
15788 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15789 TG3_EEPROM_SB_EDH_BLD_SHFT;
15790 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15791 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15792 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15793
15794 if (minor > 99 || build > 26)
15795 return;
15796
Matt Carlson75f99362010-04-05 10:19:24 +000015797 offset = strlen(tp->fw_ver);
15798 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15799 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015800
15801 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015802 offset = strlen(tp->fw_ver);
15803 if (offset < TG3_VER_SIZE - 1)
15804 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015805 }
15806}
15807
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015808static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015809{
15810 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015811 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015812
15813 for (offset = TG3_NVM_DIR_START;
15814 offset < TG3_NVM_DIR_END;
15815 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015816 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015817 return;
15818
15819 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15820 break;
15821 }
15822
15823 if (offset == TG3_NVM_DIR_END)
15824 return;
15825
Joe Perches63c3a662011-04-26 08:12:10 +000015826 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015827 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015828 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015829 return;
15830
Matt Carlsone4f34112009-02-25 14:25:00 +000015831 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015832 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015833 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015834 return;
15835
15836 offset += val - start;
15837
Matt Carlsonacd9c112009-02-25 14:26:33 +000015838 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015839
Matt Carlsonacd9c112009-02-25 14:26:33 +000015840 tp->fw_ver[vlen++] = ',';
15841 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015842
15843 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015844 __be32 v;
15845 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015846 return;
15847
Al Virob9fc7dc2007-12-17 22:59:57 -080015848 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015849
Matt Carlsonacd9c112009-02-25 14:26:33 +000015850 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15851 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015852 break;
15853 }
15854
Matt Carlsonacd9c112009-02-25 14:26:33 +000015855 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15856 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015857 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015858}
15859
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015860static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015861{
Matt Carlson7fd76442009-02-25 14:27:20 +000015862 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015863
15864 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15865 if (apedata != APE_SEG_SIG_MAGIC)
15866 return;
15867
15868 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15869 if (!(apedata & APE_FW_STATUS_READY))
15870 return;
15871
Michael Chan165f4d12012-07-16 16:23:59 +000015872 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15873 tg3_flag_set(tp, APE_HAS_NCSI);
15874}
15875
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015876static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015877{
15878 int vlen;
15879 u32 apedata;
15880 char *fwtype;
15881
Matt Carlson7fd76442009-02-25 14:27:20 +000015882 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15883
Michael Chan165f4d12012-07-16 16:23:59 +000015884 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015885 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015886 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15887 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015888 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015889 fwtype = "DASH";
15890
Matt Carlson7fd76442009-02-25 14:27:20 +000015891 vlen = strlen(tp->fw_ver);
15892
Matt Carlsonecc79642010-08-02 11:26:01 +000015893 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15894 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015895 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15896 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15897 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15898 (apedata & APE_FW_VERSION_BLDMSK));
15899}
15900
Michael Chanc86a8562013-01-06 12:51:08 +000015901static void tg3_read_otp_ver(struct tg3 *tp)
15902{
15903 u32 val, val2;
15904
Joe Perches41535772013-02-16 11:20:04 +000015905 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015906 return;
15907
15908 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15909 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15910 TG3_OTP_MAGIC0_VALID(val)) {
15911 u64 val64 = (u64) val << 32 | val2;
15912 u32 ver = 0;
15913 int i, vlen;
15914
15915 for (i = 0; i < 7; i++) {
15916 if ((val64 & 0xff) == 0)
15917 break;
15918 ver = val64 & 0xff;
15919 val64 >>= 8;
15920 }
15921 vlen = strlen(tp->fw_ver);
15922 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15923 }
15924}
15925
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015926static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015927{
15928 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015929 bool vpd_vers = false;
15930
15931 if (tp->fw_ver[0] != 0)
15932 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015933
Joe Perches63c3a662011-04-26 08:12:10 +000015934 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015935 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015936 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015937 return;
15938 }
15939
Matt Carlsonacd9c112009-02-25 14:26:33 +000015940 if (tg3_nvram_read(tp, 0, &val))
15941 return;
15942
15943 if (val == TG3_EEPROM_MAGIC)
15944 tg3_read_bc_ver(tp);
15945 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15946 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015947 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15948 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015949
Michael Chan165f4d12012-07-16 16:23:59 +000015950 if (tg3_flag(tp, ENABLE_ASF)) {
15951 if (tg3_flag(tp, ENABLE_APE)) {
15952 tg3_probe_ncsi(tp);
15953 if (!vpd_vers)
15954 tg3_read_dash_ver(tp);
15955 } else if (!vpd_vers) {
15956 tg3_read_mgmtfw_ver(tp);
15957 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015958 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015959
15960 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015961}
15962
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015963static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15964{
Joe Perches63c3a662011-04-26 08:12:10 +000015965 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015966 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015967 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015968 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015969 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015970 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015971}
15972
Benoit Taine9baa3c32014-08-08 15:56:03 +020015973static const struct pci_device_id tg3_write_reorder_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080015974 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15975 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15976 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15977 { },
15978};
15979
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015980static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015981{
15982 struct pci_dev *peer;
15983 unsigned int func, devnr = tp->pdev->devfn & ~7;
15984
15985 for (func = 0; func < 8; func++) {
15986 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15987 if (peer && peer != tp->pdev)
15988 break;
15989 pci_dev_put(peer);
15990 }
15991 /* 5704 can be configured in single-port mode, set peer to
15992 * tp->pdev in that case.
15993 */
15994 if (!peer) {
15995 peer = tp->pdev;
15996 return peer;
15997 }
15998
15999 /*
16000 * We don't need to keep the refcount elevated; there's no way
16001 * to remove one half of this device without removing the other
16002 */
16003 pci_dev_put(peer);
16004
16005 return peer;
16006}
16007
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016008static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000016009{
16010 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000016011 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000016012 u32 reg;
16013
16014 /* All devices that use the alternate
16015 * ASIC REV location have a CPMU.
16016 */
16017 tg3_flag_set(tp, CPMU_PRESENT);
16018
16019 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016020 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016021 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16022 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016023 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016024 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
16025 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016026 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16027 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016028 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
16029 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000016030 reg = TG3PCI_GEN2_PRODID_ASICREV;
16031 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
16032 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
16033 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
16034 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
16035 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
16036 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
16037 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
16038 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
16039 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
16040 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
16041 reg = TG3PCI_GEN15_PRODID_ASICREV;
16042 else
16043 reg = TG3PCI_PRODID_ASICREV;
16044
16045 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
16046 }
16047
16048 /* Wrong chip ID in 5752 A0. This code can be removed later
16049 * as A0 is not in production.
16050 */
Joe Perches41535772013-02-16 11:20:04 +000016051 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000016052 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
16053
Joe Perches41535772013-02-16 11:20:04 +000016054 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000016055 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
16056
Joe Perches41535772013-02-16 11:20:04 +000016057 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16058 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16059 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000016060 tg3_flag_set(tp, 5717_PLUS);
16061
Joe Perches41535772013-02-16 11:20:04 +000016062 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
16063 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000016064 tg3_flag_set(tp, 57765_CLASS);
16065
Michael Chanc65a17f2013-01-06 12:51:07 +000016066 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016067 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000016068 tg3_flag_set(tp, 57765_PLUS);
16069
16070 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000016071 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16072 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16073 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16074 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16075 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16076 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016077 tg3_flag(tp, 57765_PLUS))
16078 tg3_flag_set(tp, 5755_PLUS);
16079
Joe Perches41535772013-02-16 11:20:04 +000016080 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
16081 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000016082 tg3_flag_set(tp, 5780_CLASS);
16083
Joe Perches41535772013-02-16 11:20:04 +000016084 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16085 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16086 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016087 tg3_flag(tp, 5755_PLUS) ||
16088 tg3_flag(tp, 5780_CLASS))
16089 tg3_flag_set(tp, 5750_PLUS);
16090
Joe Perches41535772013-02-16 11:20:04 +000016091 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016092 tg3_flag(tp, 5750_PLUS))
16093 tg3_flag_set(tp, 5705_PLUS);
16094}
16095
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016096static bool tg3_10_100_only_device(struct tg3 *tp,
16097 const struct pci_device_id *ent)
16098{
16099 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
16100
Joe Perches41535772013-02-16 11:20:04 +000016101 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
16102 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016103 (tp->phy_flags & TG3_PHYFLG_IS_FET))
16104 return true;
16105
16106 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000016107 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016108 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
16109 return true;
16110 } else {
16111 return true;
16112 }
16113 }
16114
16115 return false;
16116}
16117
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000016118static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016119{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016120 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016121 u32 pci_state_reg, grc_misc_cfg;
16122 u32 val;
16123 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016124 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016125
Linus Torvalds1da177e2005-04-16 15:20:36 -070016126 /* Force memory write invalidate off. If we leave it on,
16127 * then on 5700_BX chips we have to enable a workaround.
16128 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
16129 * to match the cacheline size. The Broadcom driver have this
16130 * workaround but turns MWI off all the times so never uses
16131 * it. This seems to suggest that the workaround is insufficient.
16132 */
16133 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16134 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
16135 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16136
Matt Carlson16821282011-07-13 09:27:28 +000016137 /* Important! -- Make sure register accesses are byteswapped
16138 * correctly. Also, for those chips that require it, make
16139 * sure that indirect register accesses are enabled before
16140 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016141 */
16142 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16143 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000016144 tp->misc_host_ctrl |= (misc_ctrl_reg &
16145 MISC_HOST_CTRL_CHIPREV);
16146 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16147 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016148
Matt Carlson42b123b2012-02-13 15:20:13 +000016149 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070016150
Michael Chan68929142005-08-09 20:17:14 -070016151 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
16152 * we need to disable memory and use config. cycles
16153 * only to access all registers. The 5702/03 chips
16154 * can mistakenly decode the special cycles from the
16155 * ICH chipsets as memory write cycles, causing corruption
16156 * of register and memory space. Only certain ICH bridges
16157 * will drive special cycles with non-zero data during the
16158 * address phase which can fall within the 5703's address
16159 * range. This is not an ICH bug as the PCI spec allows
16160 * non-zero address during special cycles. However, only
16161 * these ICH bridges are known to drive non-zero addresses
16162 * during special cycles.
16163 *
16164 * Since special cycles do not cross PCI bridges, we only
16165 * enable this workaround if the 5703 is on the secondary
16166 * bus of these ICH bridges.
16167 */
Joe Perches41535772013-02-16 11:20:04 +000016168 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
16169 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070016170 static struct tg3_dev_id {
16171 u32 vendor;
16172 u32 device;
16173 u32 rev;
16174 } ich_chipsets[] = {
16175 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
16176 PCI_ANY_ID },
16177 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
16178 PCI_ANY_ID },
16179 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
16180 0xa },
16181 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
16182 PCI_ANY_ID },
16183 { },
16184 };
16185 struct tg3_dev_id *pci_id = &ich_chipsets[0];
16186 struct pci_dev *bridge = NULL;
16187
16188 while (pci_id->vendor != 0) {
16189 bridge = pci_get_device(pci_id->vendor, pci_id->device,
16190 bridge);
16191 if (!bridge) {
16192 pci_id++;
16193 continue;
16194 }
16195 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070016196 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070016197 continue;
16198 }
16199 if (bridge->subordinate &&
16200 (bridge->subordinate->number ==
16201 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016202 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070016203 pci_dev_put(bridge);
16204 break;
16205 }
16206 }
16207 }
16208
Joe Perches41535772013-02-16 11:20:04 +000016209 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070016210 static struct tg3_dev_id {
16211 u32 vendor;
16212 u32 device;
16213 } bridge_chipsets[] = {
16214 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
16215 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
16216 { },
16217 };
16218 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
16219 struct pci_dev *bridge = NULL;
16220
16221 while (pci_id->vendor != 0) {
16222 bridge = pci_get_device(pci_id->vendor,
16223 pci_id->device,
16224 bridge);
16225 if (!bridge) {
16226 pci_id++;
16227 continue;
16228 }
16229 if (bridge->subordinate &&
16230 (bridge->subordinate->number <=
16231 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016232 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070016233 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016234 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070016235 pci_dev_put(bridge);
16236 break;
16237 }
16238 }
16239 }
16240
Michael Chan4a29cc22006-03-19 13:21:12 -080016241 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16242 * DMA addresses > 40-bit. This bridge may have other additional
16243 * 57xx devices behind it in some 4-port NIC designs for example.
16244 * Any tg3 device found behind the bridge will also need the 40-bit
16245 * DMA workaround.
16246 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016247 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016248 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016249 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016250 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016251 struct pci_dev *bridge = NULL;
16252
16253 do {
16254 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16255 PCI_DEVICE_ID_SERVERWORKS_EPB,
16256 bridge);
16257 if (bridge && bridge->subordinate &&
16258 (bridge->subordinate->number <=
16259 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016260 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016261 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016262 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016263 pci_dev_put(bridge);
16264 break;
16265 }
16266 } while (bridge);
16267 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016268
Joe Perches41535772013-02-16 11:20:04 +000016269 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16270 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016271 tp->pdev_peer = tg3_find_peer(tp);
16272
Matt Carlson507399f2009-11-13 13:03:37 +000016273 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016274 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016275 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016276 else if (tg3_flag(tp, 57765_PLUS))
16277 tg3_flag_set(tp, HW_TSO_3);
16278 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016279 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016280 tg3_flag_set(tp, HW_TSO_2);
16281 else if (tg3_flag(tp, 5750_PLUS)) {
16282 tg3_flag_set(tp, HW_TSO_1);
16283 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016284 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16285 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016286 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016287 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16288 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16289 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016290 tg3_flag_set(tp, FW_TSO);
16291 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016292 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016293 tp->fw_needed = FIRMWARE_TG3TSO5;
16294 else
16295 tp->fw_needed = FIRMWARE_TG3TSO;
16296 }
16297
Matt Carlsondabc5c62011-05-19 12:12:52 +000016298 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016299 if (tg3_flag(tp, HW_TSO_1) ||
16300 tg3_flag(tp, HW_TSO_2) ||
16301 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016302 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016303 /* For firmware TSO, assume ASF is disabled.
16304 * We'll disable TSO later if we discover ASF
16305 * is enabled in tg3_get_eeprom_hw_cfg().
16306 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016307 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016308 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016309 tg3_flag_clear(tp, TSO_CAPABLE);
16310 tg3_flag_clear(tp, TSO_BUG);
16311 tp->fw_needed = NULL;
16312 }
16313
Joe Perches41535772013-02-16 11:20:04 +000016314 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016315 tp->fw_needed = FIRMWARE_TG3;
16316
Nithin Sujirc4dab502013-03-06 17:02:34 +000016317 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16318 tp->fw_needed = FIRMWARE_TG357766;
16319
Matt Carlson507399f2009-11-13 13:03:37 +000016320 tp->irq_max = 1;
16321
Joe Perches63c3a662011-04-26 08:12:10 +000016322 if (tg3_flag(tp, 5750_PLUS)) {
16323 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016324 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16325 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16326 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16327 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016328 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016329 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016330
Joe Perches63c3a662011-04-26 08:12:10 +000016331 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016332 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016333 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016335
Joe Perches63c3a662011-04-26 08:12:10 +000016336 if (tg3_flag(tp, 57765_PLUS)) {
16337 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016338 tp->irq_max = TG3_IRQ_MAX_VECS;
16339 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016340 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016341
Michael Chan91024262012-09-28 07:12:38 +000016342 tp->txq_max = 1;
16343 tp->rxq_max = 1;
16344 if (tp->irq_max > 1) {
16345 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16346 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16347
Joe Perches41535772013-02-16 11:20:04 +000016348 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16349 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016350 tp->txq_max = tp->irq_max - 1;
16351 }
16352
Matt Carlsonb7abee62012-06-07 12:56:54 +000016353 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016354 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016355 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016356
Joe Perches41535772013-02-16 11:20:04 +000016357 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016358 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016359
Joe Perches41535772013-02-16 11:20:04 +000016360 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16361 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16362 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16363 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016364 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016365
Joe Perches63c3a662011-04-26 08:12:10 +000016366 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016367 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016368 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016369
Joe Perches63c3a662011-04-26 08:12:10 +000016370 if (!tg3_flag(tp, 5705_PLUS) ||
16371 tg3_flag(tp, 5780_CLASS) ||
16372 tg3_flag(tp, USE_JUMBO_BDFLAG))
16373 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016374
Matt Carlson52f44902008-11-21 17:17:04 -080016375 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16376 &pci_state_reg);
16377
Jon Mason708ebb3a2011-06-27 12:56:50 +000016378 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016379 u16 lnkctl;
16380
Joe Perches63c3a662011-04-26 08:12:10 +000016381 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016382
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016383 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016384 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016385 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016386 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016387 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016388 }
Joe Perches41535772013-02-16 11:20:04 +000016389 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16390 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16391 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16392 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016393 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016394 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016395 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016396 }
Joe Perches41535772013-02-16 11:20:04 +000016397 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016398 /* BCM5785 devices are effectively PCIe devices, and should
16399 * follow PCIe codepaths, but do not have a PCIe capabilities
16400 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016401 */
Joe Perches63c3a662011-04-26 08:12:10 +000016402 tg3_flag_set(tp, PCI_EXPRESS);
16403 } else if (!tg3_flag(tp, 5705_PLUS) ||
16404 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016405 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16406 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016407 dev_err(&tp->pdev->dev,
16408 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016409 return -EIO;
16410 }
16411
16412 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016413 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016415
Michael Chan399de502005-10-03 14:02:39 -070016416 /* If we have an AMD 762 or VIA K8T800 chipset, write
16417 * reordering to the mailbox registers done by the host
16418 * controller can cause major troubles. We read back from
16419 * every mailbox register write to force the writes to be
16420 * posted to the chip in order.
16421 */
Matt Carlson41434702011-03-09 16:58:22 +000016422 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016423 !tg3_flag(tp, PCI_EXPRESS))
16424 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016425
Matt Carlson69fc4052008-12-21 20:19:57 -080016426 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16427 &tp->pci_cacheline_sz);
16428 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16429 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016430 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016431 tp->pci_lat_timer < 64) {
16432 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016433 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16434 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016435 }
16436
Matt Carlson16821282011-07-13 09:27:28 +000016437 /* Important! -- It is critical that the PCI-X hw workaround
16438 * situation is decided before the first MMIO register access.
16439 */
Joe Perches41535772013-02-16 11:20:04 +000016440 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016441 /* 5700 BX chips need to have their TX producer index
16442 * mailboxes written twice to workaround a bug.
16443 */
Joe Perches63c3a662011-04-26 08:12:10 +000016444 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016445
Matt Carlson52f44902008-11-21 17:17:04 -080016446 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016447 *
16448 * The workaround is to use indirect register accesses
16449 * for all chip writes not to mailbox registers.
16450 */
Joe Perches63c3a662011-04-26 08:12:10 +000016451 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016452 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016453
Joe Perches63c3a662011-04-26 08:12:10 +000016454 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016455
16456 /* The chip can have it's power management PCI config
16457 * space registers clobbered due to this bug.
16458 * So explicitly force the chip into D0 here.
16459 */
Matt Carlson9974a352007-10-07 23:27:28 -070016460 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016461 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016462 &pm_reg);
16463 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16464 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016465 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016466 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016467 pm_reg);
16468
16469 /* Also, force SERR#/PERR# in PCI command. */
16470 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16471 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16472 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16473 }
16474 }
16475
Linus Torvalds1da177e2005-04-16 15:20:36 -070016476 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016477 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016478 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016479 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016480
16481 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016482 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016483 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16484 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16485 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16486 }
16487
Michael Chan1ee582d2005-08-09 20:16:46 -070016488 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016489 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016490 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016491 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016492 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016493 tp->write32_tx_mbox = tg3_write32;
16494 tp->write32_rx_mbox = tg3_write32;
16495
16496 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016497 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016498 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016499 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016500 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016501 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016502 /*
16503 * Back to back register writes can cause problems on these
16504 * chips, the workaround is to read back all reg writes
16505 * except those to mailbox regs.
16506 *
16507 * See tg3_write_indirect_reg32().
16508 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016509 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016510 }
16511
Joe Perches63c3a662011-04-26 08:12:10 +000016512 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016513 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016514 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016515 tp->write32_rx_mbox = tg3_write_flush_reg32;
16516 }
Michael Chan20094932005-08-09 20:16:32 -070016517
Joe Perches63c3a662011-04-26 08:12:10 +000016518 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016519 tp->read32 = tg3_read_indirect_reg32;
16520 tp->write32 = tg3_write_indirect_reg32;
16521 tp->read32_mbox = tg3_read_indirect_mbox;
16522 tp->write32_mbox = tg3_write_indirect_mbox;
16523 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16524 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16525
16526 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016527 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016528
16529 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16530 pci_cmd &= ~PCI_COMMAND_MEMORY;
16531 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16532 }
Joe Perches41535772013-02-16 11:20:04 +000016533 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016534 tp->read32_mbox = tg3_read32_mbox_5906;
16535 tp->write32_mbox = tg3_write32_mbox_5906;
16536 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16537 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16538 }
Michael Chan68929142005-08-09 20:17:14 -070016539
Michael Chanbbadf502006-04-06 21:46:34 -070016540 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016541 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016542 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16543 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016544 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016545
Matt Carlson16821282011-07-13 09:27:28 +000016546 /* The memory arbiter has to be enabled in order for SRAM accesses
16547 * to succeed. Normally on powerup the tg3 chip firmware will make
16548 * sure it is enabled, but other entities such as system netboot
16549 * code might disable it.
16550 */
16551 val = tr32(MEMARB_MODE);
16552 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16553
Matt Carlson9dc5e342011-11-04 09:15:02 +000016554 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016555 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016556 tg3_flag(tp, 5780_CLASS)) {
16557 if (tg3_flag(tp, PCIX_MODE)) {
16558 pci_read_config_dword(tp->pdev,
16559 tp->pcix_cap + PCI_X_STATUS,
16560 &val);
16561 tp->pci_fn = val & 0x7;
16562 }
Joe Perches41535772013-02-16 11:20:04 +000016563 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16564 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16565 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016566 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016567 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16568 val = tr32(TG3_CPMU_STATUS);
16569
Joe Perches41535772013-02-16 11:20:04 +000016570 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016571 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16572 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016573 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16574 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016575 }
16576
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016577 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16578 tp->write32_tx_mbox = tg3_write_flush_reg32;
16579 tp->write32_rx_mbox = tg3_write_flush_reg32;
16580 }
16581
Michael Chan7d0c41e2005-04-21 17:06:20 -070016582 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016583 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016584 * determined before calling tg3_set_power_state() so that
16585 * we know whether or not to switch out of Vaux power.
16586 * When the flag is set, it means that GPIO1 is used for eeprom
16587 * write protect and also implies that it is a LOM where GPIOs
16588 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016589 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016590 tg3_get_eeprom_hw_cfg(tp);
16591
Matt Carlson1caf13e2013-03-06 17:02:29 +000016592 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016593 tg3_flag_clear(tp, TSO_CAPABLE);
16594 tg3_flag_clear(tp, TSO_BUG);
16595 tp->fw_needed = NULL;
16596 }
16597
Joe Perches63c3a662011-04-26 08:12:10 +000016598 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016599 /* Allow reads and writes to the
16600 * APE register and memory space.
16601 */
16602 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016603 PCISTATE_ALLOW_APE_SHMEM_WR |
16604 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016605 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16606 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016607
16608 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016609 }
16610
Matt Carlson16821282011-07-13 09:27:28 +000016611 /* Set up tp->grc_local_ctrl before calling
16612 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16613 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016614 * It is also used as eeprom write protect on LOMs.
16615 */
16616 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016617 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016618 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016619 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16620 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016621 /* Unused GPIO3 must be driven as output on 5752 because there
16622 * are no pull-up resistors on unused GPIO pins.
16623 */
Joe Perches41535772013-02-16 11:20:04 +000016624 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016625 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016626
Joe Perches41535772013-02-16 11:20:04 +000016627 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16628 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016629 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016630 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16631
Matt Carlson8d519ab2009-04-20 06:58:01 +000016632 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16633 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016634 /* Turn off the debug UART. */
16635 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016636 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016637 /* Keep VMain power. */
16638 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16639 GRC_LCLCTRL_GPIO_OUTPUT0;
16640 }
16641
Joe Perches41535772013-02-16 11:20:04 +000016642 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016643 tp->grc_local_ctrl |=
16644 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16645
Matt Carlson16821282011-07-13 09:27:28 +000016646 /* Switch out of Vaux if it is a NIC */
16647 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016648
Linus Torvalds1da177e2005-04-16 15:20:36 -070016649 /* Derive initial jumbo mode from MTU assigned in
16650 * ether_setup() via the alloc_etherdev() call
16651 */
Joe Perches63c3a662011-04-26 08:12:10 +000016652 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16653 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016654
16655 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016656 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16657 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16658 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16659 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016660 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016661 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016662 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016663 }
16664
Joe Perches41535772013-02-16 11:20:04 +000016665 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016666 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016667
Linus Torvalds1da177e2005-04-16 15:20:36 -070016668 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016669 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16670 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16671 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16672 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016673 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16674 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16675 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016676
Joe Perches41535772013-02-16 11:20:04 +000016677 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16678 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016679 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016680 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016681 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016682
Joe Perches63c3a662011-04-26 08:12:10 +000016683 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016684 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016685 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16686 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016687 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016688 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16689 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16690 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16691 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016692 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16693 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016694 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016695 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016696 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016697 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016698 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016700
Joe Perches41535772013-02-16 11:20:04 +000016701 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16702 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016703 tp->phy_otp = tg3_read_otp_phycfg(tp);
16704 if (tp->phy_otp == 0)
16705 tp->phy_otp = TG3_OTP_DEFAULT;
16706 }
16707
Joe Perches63c3a662011-04-26 08:12:10 +000016708 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016709 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16710 else
16711 tp->mi_mode = MAC_MI_MODE_BASE;
16712
Linus Torvalds1da177e2005-04-16 15:20:36 -070016713 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016714 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16715 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016716 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16717
Matt Carlson4d958472011-04-20 07:57:35 +000016718 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016719 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080016720 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000016721 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16722 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016723 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16724 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16725 }
16726
Joe Perches41535772013-02-16 11:20:04 +000016727 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16728 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016729 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016730
Matt Carlson158d7ab2008-05-29 01:37:54 -070016731 err = tg3_mdio_init(tp);
16732 if (err)
16733 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016734
16735 /* Initialize data/descriptor byte/word swapping. */
16736 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016737 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16738 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016739 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16740 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16741 GRC_MODE_B2HRX_ENABLE |
16742 GRC_MODE_HTX2B_ENABLE |
16743 GRC_MODE_HOST_STACKUP);
16744 else
16745 val &= GRC_MODE_HOST_STACKUP;
16746
Linus Torvalds1da177e2005-04-16 15:20:36 -070016747 tw32(GRC_MODE, val | tp->grc_mode);
16748
16749 tg3_switch_clocks(tp);
16750
16751 /* Clear this out for sanity. */
16752 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16753
Nat Gurumoorthy388d3332013-12-09 10:43:21 -080016754 /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
16755 tw32(TG3PCI_REG_BASE_ADDR, 0);
16756
Linus Torvalds1da177e2005-04-16 15:20:36 -070016757 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16758 &pci_state_reg);
16759 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016760 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016761 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16762 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16763 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16764 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016765 void __iomem *sram_base;
16766
16767 /* Write some dummy words into the SRAM status block
16768 * area, see if it reads back correctly. If the return
16769 * value is bad, force enable the PCIX workaround.
16770 */
16771 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16772
16773 writel(0x00000000, sram_base);
16774 writel(0x00000000, sram_base + 4);
16775 writel(0xffffffff, sram_base + 4);
16776 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016777 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016778 }
16779 }
16780
16781 udelay(50);
16782 tg3_nvram_init(tp);
16783
Nithin Sujirc4dab502013-03-06 17:02:34 +000016784 /* If the device has an NVRAM, no need to load patch firmware */
16785 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16786 !tg3_flag(tp, NO_NVRAM))
16787 tp->fw_needed = NULL;
16788
Linus Torvalds1da177e2005-04-16 15:20:36 -070016789 grc_misc_cfg = tr32(GRC_MISC_CFG);
16790 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16791
Joe Perches41535772013-02-16 11:20:04 +000016792 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016793 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16794 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016795 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016796
Joe Perches63c3a662011-04-26 08:12:10 +000016797 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016798 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016799 tg3_flag_set(tp, TAGGED_STATUS);
16800 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016801 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16802 HOSTCC_MODE_CLRTICK_TXBD);
16803
16804 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16805 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16806 tp->misc_host_ctrl);
16807 }
16808
Matt Carlson3bda1252008-08-15 14:08:22 -070016809 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016810 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016811 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016812 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016813 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016814
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016815 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016816 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016817
16818 err = tg3_phy_probe(tp);
16819 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016820 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016821 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016822 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016823 }
16824
Matt Carlson184b8902010-04-05 10:19:25 +000016825 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016826 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016827
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016828 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16829 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016830 } else {
Joe Perches41535772013-02-16 11:20:04 +000016831 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016832 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016833 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016834 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016835 }
16836
16837 /* 5700 {AX,BX} chips have a broken status block link
16838 * change bit implementation, so we must use the
16839 * status register in those cases.
16840 */
Joe Perches41535772013-02-16 11:20:04 +000016841 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016842 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016843 else
Joe Perches63c3a662011-04-26 08:12:10 +000016844 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016845
16846 /* The led_ctrl is set during tg3_phy_probe, here we might
16847 * have to force the link status polling mechanism based
16848 * upon subsystem IDs.
16849 */
16850 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016851 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016852 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16853 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016854 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016855 }
16856
16857 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016858 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016859 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016860 else
Joe Perches63c3a662011-04-26 08:12:10 +000016861 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016862
Nithin Sujir1743b832014-01-03 10:09:14 -080016863 if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
16864 tg3_flag_set(tp, POLL_CPMU_LINK);
16865
Eric Dumazet9205fd92011-11-18 06:47:01 +000016866 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016867 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016868 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016869 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016870 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016871#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016872 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016873#endif
16874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016875
Matt Carlson2c49a442010-09-30 10:34:35 +000016876 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16877 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016878 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16879
Matt Carlson2c49a442010-09-30 10:34:35 +000016880 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016881
16882 /* Increment the rx prod index on the rx std ring by at most
16883 * 8 for these chips to workaround hw errata.
16884 */
Joe Perches41535772013-02-16 11:20:04 +000016885 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16886 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16887 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016888 tp->rx_std_max_post = 8;
16889
Joe Perches63c3a662011-04-26 08:12:10 +000016890 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016891 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16892 PCIE_PWR_MGMT_L1_THRESH_MSK;
16893
Linus Torvalds1da177e2005-04-16 15:20:36 -070016894 return err;
16895}
16896
David S. Miller49b6e95f2007-03-29 01:38:42 -070016897#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016898static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016899{
16900 struct net_device *dev = tp->dev;
16901 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016902 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016903 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016904 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016905
David S. Miller49b6e95f2007-03-29 01:38:42 -070016906 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016907 if (addr && len == ETH_ALEN) {
16908 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016910 }
16911 return -ENODEV;
16912}
16913
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016914static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016915{
16916 struct net_device *dev = tp->dev;
16917
Joe Perchesd458cdf2013-10-01 19:04:40 -070016918 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016919 return 0;
16920}
16921#endif
16922
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016923static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016924{
16925 struct net_device *dev = tp->dev;
16926 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016927 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016928 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016929
David S. Miller49b6e95f2007-03-29 01:38:42 -070016930#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016931 if (!tg3_get_macaddr_sparc(tp))
16932 return 0;
16933#endif
16934
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016935 if (tg3_flag(tp, IS_SSB_CORE)) {
16936 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16937 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16938 return 0;
16939 }
16940
Linus Torvalds1da177e2005-04-16 15:20:36 -070016941 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016942 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016943 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016944 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16945 mac_offset = 0xcc;
16946 if (tg3_nvram_lock(tp))
16947 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16948 else
16949 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016950 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016951 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016952 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016953 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016954 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016955 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016956 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016957
16958 /* First try to get it from MAC address mailbox. */
16959 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16960 if ((hi >> 16) == 0x484b) {
16961 dev->dev_addr[0] = (hi >> 8) & 0xff;
16962 dev->dev_addr[1] = (hi >> 0) & 0xff;
16963
16964 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16965 dev->dev_addr[2] = (lo >> 24) & 0xff;
16966 dev->dev_addr[3] = (lo >> 16) & 0xff;
16967 dev->dev_addr[4] = (lo >> 8) & 0xff;
16968 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016969
Michael Chan008652b2006-03-27 23:14:53 -080016970 /* Some old bootcode may report a 0 MAC address in SRAM */
16971 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16972 }
16973 if (!addr_ok) {
16974 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016975 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016976 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016977 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016978 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16979 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016980 }
16981 /* Finally just fetch it out of the MAC control regs. */
16982 else {
16983 hi = tr32(MAC_ADDR_0_HIGH);
16984 lo = tr32(MAC_ADDR_0_LOW);
16985
16986 dev->dev_addr[5] = lo & 0xff;
16987 dev->dev_addr[4] = (lo >> 8) & 0xff;
16988 dev->dev_addr[3] = (lo >> 16) & 0xff;
16989 dev->dev_addr[2] = (lo >> 24) & 0xff;
16990 dev->dev_addr[1] = hi & 0xff;
16991 dev->dev_addr[0] = (hi >> 8) & 0xff;
16992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016993 }
16994
16995 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016996#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016997 if (!tg3_get_default_macaddr_sparc(tp))
16998 return 0;
16999#endif
17000 return -EINVAL;
17001 }
17002 return 0;
17003}
17004
David S. Miller59e6b432005-05-18 22:50:10 -070017005#define BOUNDARY_SINGLE_CACHELINE 1
17006#define BOUNDARY_MULTI_CACHELINE 2
17007
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017008static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070017009{
17010 int cacheline_size;
17011 u8 byte;
17012 int goal;
17013
17014 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
17015 if (byte == 0)
17016 cacheline_size = 1024;
17017 else
17018 cacheline_size = (int) byte * 4;
17019
17020 /* On 5703 and later chips, the boundary bits have no
17021 * effect.
17022 */
Joe Perches41535772013-02-16 11:20:04 +000017023 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17024 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017025 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070017026 goto out;
17027
17028#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
17029 goal = BOUNDARY_MULTI_CACHELINE;
17030#else
17031#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
17032 goal = BOUNDARY_SINGLE_CACHELINE;
17033#else
17034 goal = 0;
17035#endif
17036#endif
17037
Joe Perches63c3a662011-04-26 08:12:10 +000017038 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017039 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
17040 goto out;
17041 }
17042
David S. Miller59e6b432005-05-18 22:50:10 -070017043 if (!goal)
17044 goto out;
17045
17046 /* PCI controllers on most RISC systems tend to disconnect
17047 * when a device tries to burst across a cache-line boundary.
17048 * Therefore, letting tg3 do so just wastes PCI bandwidth.
17049 *
17050 * Unfortunately, for PCI-E there are only limited
17051 * write-side controls for this, and thus for reads
17052 * we will still get the disconnects. We'll also waste
17053 * these PCI cycles for both read and write for chips
17054 * other than 5700 and 5701 which do not implement the
17055 * boundary bits.
17056 */
Joe Perches63c3a662011-04-26 08:12:10 +000017057 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017058 switch (cacheline_size) {
17059 case 16:
17060 case 32:
17061 case 64:
17062 case 128:
17063 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17064 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
17065 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
17066 } else {
17067 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17068 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17069 }
17070 break;
17071
17072 case 256:
17073 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
17074 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
17075 break;
17076
17077 default:
17078 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17079 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17080 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017081 }
Joe Perches63c3a662011-04-26 08:12:10 +000017082 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017083 switch (cacheline_size) {
17084 case 16:
17085 case 32:
17086 case 64:
17087 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17088 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17089 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
17090 break;
17091 }
17092 /* fallthrough */
17093 case 128:
17094 default:
17095 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17096 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
17097 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017098 }
David S. Miller59e6b432005-05-18 22:50:10 -070017099 } else {
17100 switch (cacheline_size) {
17101 case 16:
17102 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17103 val |= (DMA_RWCTRL_READ_BNDRY_16 |
17104 DMA_RWCTRL_WRITE_BNDRY_16);
17105 break;
17106 }
17107 /* fallthrough */
17108 case 32:
17109 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17110 val |= (DMA_RWCTRL_READ_BNDRY_32 |
17111 DMA_RWCTRL_WRITE_BNDRY_32);
17112 break;
17113 }
17114 /* fallthrough */
17115 case 64:
17116 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17117 val |= (DMA_RWCTRL_READ_BNDRY_64 |
17118 DMA_RWCTRL_WRITE_BNDRY_64);
17119 break;
17120 }
17121 /* fallthrough */
17122 case 128:
17123 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17124 val |= (DMA_RWCTRL_READ_BNDRY_128 |
17125 DMA_RWCTRL_WRITE_BNDRY_128);
17126 break;
17127 }
17128 /* fallthrough */
17129 case 256:
17130 val |= (DMA_RWCTRL_READ_BNDRY_256 |
17131 DMA_RWCTRL_WRITE_BNDRY_256);
17132 break;
17133 case 512:
17134 val |= (DMA_RWCTRL_READ_BNDRY_512 |
17135 DMA_RWCTRL_WRITE_BNDRY_512);
17136 break;
17137 case 1024:
17138 default:
17139 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
17140 DMA_RWCTRL_WRITE_BNDRY_1024);
17141 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017142 }
David S. Miller59e6b432005-05-18 22:50:10 -070017143 }
17144
17145out:
17146 return val;
17147}
17148
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017149static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000017150 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017151{
17152 struct tg3_internal_buffer_desc test_desc;
17153 u32 sram_dma_descs;
17154 int i, ret;
17155
17156 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
17157
17158 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
17159 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
17160 tw32(RDMAC_STATUS, 0);
17161 tw32(WDMAC_STATUS, 0);
17162
17163 tw32(BUFMGR_MODE, 0);
17164 tw32(FTQ_RESET, 0);
17165
17166 test_desc.addr_hi = ((u64) buf_dma) >> 32;
17167 test_desc.addr_lo = buf_dma & 0xffffffff;
17168 test_desc.nic_mbuf = 0x00002100;
17169 test_desc.len = size;
17170
17171 /*
17172 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
17173 * the *second* time the tg3 driver was getting loaded after an
17174 * initial scan.
17175 *
17176 * Broadcom tells me:
17177 * ...the DMA engine is connected to the GRC block and a DMA
17178 * reset may affect the GRC block in some unpredictable way...
17179 * The behavior of resets to individual blocks has not been tested.
17180 *
17181 * Broadcom noted the GRC reset will also reset all sub-components.
17182 */
17183 if (to_device) {
17184 test_desc.cqid_sqid = (13 << 8) | 2;
17185
17186 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
17187 udelay(40);
17188 } else {
17189 test_desc.cqid_sqid = (16 << 8) | 7;
17190
17191 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
17192 udelay(40);
17193 }
17194 test_desc.flags = 0x00000005;
17195
17196 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
17197 u32 val;
17198
17199 val = *(((u32 *)&test_desc) + i);
17200 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
17201 sram_dma_descs + (i * sizeof(u32)));
17202 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
17203 }
17204 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
17205
Matt Carlson859a588792010-04-05 10:19:28 +000017206 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017207 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000017208 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070017209 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017210
17211 ret = -ENODEV;
17212 for (i = 0; i < 40; i++) {
17213 u32 val;
17214
17215 if (to_device)
17216 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
17217 else
17218 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
17219 if ((val & 0xffff) == sram_dma_descs) {
17220 ret = 0;
17221 break;
17222 }
17223
17224 udelay(100);
17225 }
17226
17227 return ret;
17228}
17229
David S. Millerded73402005-05-23 13:59:47 -070017230#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017231
Benoit Taine9baa3c32014-08-08 15:56:03 +020017232static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080017233 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
17234 { },
17235};
17236
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017237static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017238{
17239 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070017240 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017241 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017242
Matt Carlson4bae65c2010-11-24 08:31:52 +000017243 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
17244 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017245 if (!buf) {
17246 ret = -ENOMEM;
17247 goto out_nofree;
17248 }
17249
17250 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17251 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17252
David S. Miller59e6b432005-05-18 22:50:10 -070017253 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017254
Joe Perches63c3a662011-04-26 08:12:10 +000017255 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017256 goto out;
17257
Joe Perches63c3a662011-04-26 08:12:10 +000017258 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017259 /* DMA read watermark not used on PCIE */
17260 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017261 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017262 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17263 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017264 tp->dma_rwctrl |= 0x003f0000;
17265 else
17266 tp->dma_rwctrl |= 0x003f000f;
17267 } else {
Joe Perches41535772013-02-16 11:20:04 +000017268 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17269 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017270 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017271 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017272
Michael Chan4a29cc22006-03-19 13:21:12 -080017273 /* If the 5704 is behind the EPB bridge, we can
17274 * do the less restrictive ONE_DMA workaround for
17275 * better performance.
17276 */
Joe Perches63c3a662011-04-26 08:12:10 +000017277 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017278 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017279 tp->dma_rwctrl |= 0x8000;
17280 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017281 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17282
Joe Perches41535772013-02-16 11:20:04 +000017283 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017284 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017285 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017286 tp->dma_rwctrl |=
17287 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17288 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17289 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017290 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017291 /* 5780 always in PCIX mode */
17292 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017293 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017294 /* 5714 always in PCIX mode */
17295 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017296 } else {
17297 tp->dma_rwctrl |= 0x001b000f;
17298 }
17299 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017300 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17301 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017302
Joe Perches41535772013-02-16 11:20:04 +000017303 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17304 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017305 tp->dma_rwctrl &= 0xfffffff0;
17306
Joe Perches41535772013-02-16 11:20:04 +000017307 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17308 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017309 /* Remove this if it causes problems for some boards. */
17310 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17311
17312 /* On 5700/5701 chips, we need to set this bit.
17313 * Otherwise the chip will issue cacheline transactions
17314 * to streamable DMA memory with not all the byte
17315 * enables turned on. This is an error on several
17316 * RISC PCI controllers, in particular sparc64.
17317 *
17318 * On 5703/5704 chips, this bit has been reassigned
17319 * a different meaning. In particular, it is used
17320 * on those chips to enable a PCI-X workaround.
17321 */
17322 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17323 }
17324
17325 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17326
Linus Torvalds1da177e2005-04-16 15:20:36 -070017327
Joe Perches41535772013-02-16 11:20:04 +000017328 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17329 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017330 goto out;
17331
David S. Miller59e6b432005-05-18 22:50:10 -070017332 /* It is best to perform DMA test with maximum write burst size
17333 * to expose the 5700/5701 write DMA bug.
17334 */
17335 saved_dma_rwctrl = tp->dma_rwctrl;
17336 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17337 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17338
Linus Torvalds1da177e2005-04-16 15:20:36 -070017339 while (1) {
17340 u32 *p = buf, i;
17341
17342 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17343 p[i] = i;
17344
17345 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017346 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017347 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017348 dev_err(&tp->pdev->dev,
17349 "%s: Buffer write failed. err = %d\n",
17350 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017351 break;
17352 }
17353
Linus Torvalds1da177e2005-04-16 15:20:36 -070017354 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017355 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017356 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017357 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17358 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017359 break;
17360 }
17361
17362 /* Verify it. */
17363 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17364 if (p[i] == i)
17365 continue;
17366
David S. Miller59e6b432005-05-18 22:50:10 -070017367 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17368 DMA_RWCTRL_WRITE_BNDRY_16) {
17369 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017370 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17371 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17372 break;
17373 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017374 dev_err(&tp->pdev->dev,
17375 "%s: Buffer corrupted on read back! "
17376 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017377 ret = -ENODEV;
17378 goto out;
17379 }
17380 }
17381
17382 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17383 /* Success. */
17384 ret = 0;
17385 break;
17386 }
17387 }
David S. Miller59e6b432005-05-18 22:50:10 -070017388 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17389 DMA_RWCTRL_WRITE_BNDRY_16) {
17390 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017391 * now look for chipsets that are known to expose the
17392 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017393 */
Matt Carlson41434702011-03-09 16:58:22 +000017394 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017395 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17396 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017397 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017398 /* Safe to use the calculated DMA boundary. */
17399 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017400 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017401
David S. Miller59e6b432005-05-18 22:50:10 -070017402 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017404
17405out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017406 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017407out_nofree:
17408 return ret;
17409}
17410
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017411static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017412{
Joe Perches63c3a662011-04-26 08:12:10 +000017413 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017414 tp->bufmgr_config.mbuf_read_dma_low_water =
17415 DEFAULT_MB_RDMA_LOW_WATER_5705;
17416 tp->bufmgr_config.mbuf_mac_rx_low_water =
17417 DEFAULT_MB_MACRX_LOW_WATER_57765;
17418 tp->bufmgr_config.mbuf_high_water =
17419 DEFAULT_MB_HIGH_WATER_57765;
17420
17421 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17422 DEFAULT_MB_RDMA_LOW_WATER_5705;
17423 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17424 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17425 tp->bufmgr_config.mbuf_high_water_jumbo =
17426 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017427 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017428 tp->bufmgr_config.mbuf_read_dma_low_water =
17429 DEFAULT_MB_RDMA_LOW_WATER_5705;
17430 tp->bufmgr_config.mbuf_mac_rx_low_water =
17431 DEFAULT_MB_MACRX_LOW_WATER_5705;
17432 tp->bufmgr_config.mbuf_high_water =
17433 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017434 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017435 tp->bufmgr_config.mbuf_mac_rx_low_water =
17436 DEFAULT_MB_MACRX_LOW_WATER_5906;
17437 tp->bufmgr_config.mbuf_high_water =
17438 DEFAULT_MB_HIGH_WATER_5906;
17439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017440
Michael Chanfdfec1722005-07-25 12:31:48 -070017441 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17442 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17443 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17444 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17445 tp->bufmgr_config.mbuf_high_water_jumbo =
17446 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17447 } else {
17448 tp->bufmgr_config.mbuf_read_dma_low_water =
17449 DEFAULT_MB_RDMA_LOW_WATER;
17450 tp->bufmgr_config.mbuf_mac_rx_low_water =
17451 DEFAULT_MB_MACRX_LOW_WATER;
17452 tp->bufmgr_config.mbuf_high_water =
17453 DEFAULT_MB_HIGH_WATER;
17454
17455 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17456 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17457 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17458 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17459 tp->bufmgr_config.mbuf_high_water_jumbo =
17460 DEFAULT_MB_HIGH_WATER_JUMBO;
17461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017462
17463 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17464 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17465}
17466
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017467static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017468{
Matt Carlson79eb6902010-02-17 15:17:03 +000017469 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17470 case TG3_PHY_ID_BCM5400: return "5400";
17471 case TG3_PHY_ID_BCM5401: return "5401";
17472 case TG3_PHY_ID_BCM5411: return "5411";
17473 case TG3_PHY_ID_BCM5701: return "5701";
17474 case TG3_PHY_ID_BCM5703: return "5703";
17475 case TG3_PHY_ID_BCM5704: return "5704";
17476 case TG3_PHY_ID_BCM5705: return "5705";
17477 case TG3_PHY_ID_BCM5750: return "5750";
17478 case TG3_PHY_ID_BCM5752: return "5752";
17479 case TG3_PHY_ID_BCM5714: return "5714";
17480 case TG3_PHY_ID_BCM5780: return "5780";
17481 case TG3_PHY_ID_BCM5755: return "5755";
17482 case TG3_PHY_ID_BCM5787: return "5787";
17483 case TG3_PHY_ID_BCM5784: return "5784";
17484 case TG3_PHY_ID_BCM5756: return "5722/5756";
17485 case TG3_PHY_ID_BCM5906: return "5906";
17486 case TG3_PHY_ID_BCM5761: return "5761";
17487 case TG3_PHY_ID_BCM5718C: return "5718C";
17488 case TG3_PHY_ID_BCM5718S: return "5718S";
17489 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017490 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017491 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017492 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017493 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017494 case 0: return "serdes";
17495 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017497}
17498
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017499static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017500{
Joe Perches63c3a662011-04-26 08:12:10 +000017501 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017502 strcpy(str, "PCI Express");
17503 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017504 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017505 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17506
17507 strcpy(str, "PCIX:");
17508
17509 if ((clock_ctrl == 7) ||
17510 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17511 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17512 strcat(str, "133MHz");
17513 else if (clock_ctrl == 0)
17514 strcat(str, "33MHz");
17515 else if (clock_ctrl == 2)
17516 strcat(str, "50MHz");
17517 else if (clock_ctrl == 4)
17518 strcat(str, "66MHz");
17519 else if (clock_ctrl == 6)
17520 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017521 } else {
17522 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017523 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017524 strcat(str, "66MHz");
17525 else
17526 strcat(str, "33MHz");
17527 }
Joe Perches63c3a662011-04-26 08:12:10 +000017528 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017529 strcat(str, ":32-bit");
17530 else
17531 strcat(str, ":64-bit");
17532 return str;
17533}
17534
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017535static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017536{
17537 struct ethtool_coalesce *ec = &tp->coal;
17538
17539 memset(ec, 0, sizeof(*ec));
17540 ec->cmd = ETHTOOL_GCOALESCE;
17541 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17542 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17543 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17544 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17545 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17546 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17547 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17548 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17549 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17550
17551 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17552 HOSTCC_MODE_CLRTICK_TXBD)) {
17553 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17554 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17555 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17556 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17557 }
Michael Chand244c892005-07-05 14:42:33 -070017558
Joe Perches63c3a662011-04-26 08:12:10 +000017559 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017560 ec->rx_coalesce_usecs_irq = 0;
17561 ec->tx_coalesce_usecs_irq = 0;
17562 ec->stats_block_coalesce_usecs = 0;
17563 }
David S. Miller15f98502005-05-18 22:49:26 -070017564}
17565
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017566static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017567 const struct pci_device_id *ent)
17568{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017569 struct net_device *dev;
17570 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017571 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017572 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017573 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017574 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017575 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017576
Joe Perches05dbe002010-02-17 19:44:19 +000017577 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017578
17579 err = pci_enable_device(pdev);
17580 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017581 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017582 return err;
17583 }
17584
Linus Torvalds1da177e2005-04-16 15:20:36 -070017585 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17586 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017587 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017588 goto err_out_disable_pdev;
17589 }
17590
17591 pci_set_master(pdev);
17592
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017593 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017594 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017595 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017596 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017597 }
17598
Linus Torvalds1da177e2005-04-16 15:20:36 -070017599 SET_NETDEV_DEV(dev, &pdev->dev);
17600
Linus Torvalds1da177e2005-04-16 15:20:36 -070017601 tp = netdev_priv(dev);
17602 tp->pdev = pdev;
17603 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017604 tp->rx_mode = TG3_DEF_RX_MODE;
17605 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017606 tp->irq_sync = 1;
Ivan Vecera0486a062014-09-01 14:21:57 +020017607 tp->pcierr_recovery = false;
Matt Carlson8ef21422008-05-02 16:47:53 -070017608
Linus Torvalds1da177e2005-04-16 15:20:36 -070017609 if (tg3_debug > 0)
17610 tp->msg_enable = tg3_debug;
17611 else
17612 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17613
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017614 if (pdev_is_ssb_gige_core(pdev)) {
17615 tg3_flag_set(tp, IS_SSB_CORE);
17616 if (ssb_gige_must_flush_posted_writes(pdev))
17617 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17618 if (ssb_gige_one_dma_at_once(pdev))
17619 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017620 if (ssb_gige_have_roboswitch(pdev)) {
17621 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017622 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017623 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017624 if (ssb_gige_is_rgmii(pdev))
17625 tg3_flag_set(tp, RGMII_MODE);
17626 }
17627
Linus Torvalds1da177e2005-04-16 15:20:36 -070017628 /* The word/byte swap controls here control register access byte
17629 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17630 * setting below.
17631 */
17632 tp->misc_host_ctrl =
17633 MISC_HOST_CTRL_MASK_PCI_INT |
17634 MISC_HOST_CTRL_WORD_SWAP |
17635 MISC_HOST_CTRL_INDIR_ACCESS |
17636 MISC_HOST_CTRL_PCISTATE_RW;
17637
17638 /* The NONFRM (non-frame) byte/word swap controls take effect
17639 * on descriptor entries, anything which isn't packet data.
17640 *
17641 * The StrongARM chips on the board (one for tx, one for rx)
17642 * are running in big-endian mode.
17643 */
17644 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17645 GRC_MODE_WSWAP_NONFRM_DATA);
17646#ifdef __BIG_ENDIAN
17647 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17648#endif
17649 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017650 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017651 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017652
Matt Carlsond5fe4882008-11-21 17:20:32 -080017653 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017654 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017655 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017656 err = -ENOMEM;
17657 goto err_out_free_dev;
17658 }
17659
Matt Carlsonc9cab242011-07-13 09:27:27 +000017660 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17661 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17662 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17663 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17664 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017665 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017666 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17667 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017668 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017669 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17670 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017671 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17672 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017673 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17674 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017675 tg3_flag_set(tp, ENABLE_APE);
17676 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17677 if (!tp->aperegs) {
17678 dev_err(&pdev->dev,
17679 "Cannot map APE registers, aborting\n");
17680 err = -ENOMEM;
17681 goto err_out_iounmap;
17682 }
17683 }
17684
Linus Torvalds1da177e2005-04-16 15:20:36 -070017685 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17686 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017687
Linus Torvalds1da177e2005-04-16 15:20:36 -070017688 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017689 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017690 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017691 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017692
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017693 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017694 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017695 dev_err(&pdev->dev,
17696 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017697 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017698 }
17699
Michael Chan4a29cc22006-03-19 13:21:12 -080017700 /* The EPB bridge inside 5714, 5715, and 5780 and any
17701 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017702 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17703 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17704 * do DMA address check in tg3_start_xmit().
17705 */
Joe Perches63c3a662011-04-26 08:12:10 +000017706 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017707 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017708 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017709 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017710#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017711 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017712#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017713 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017714 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017715
17716 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017717 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017718 err = pci_set_dma_mask(pdev, dma_mask);
17719 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017720 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017721 err = pci_set_consistent_dma_mask(pdev,
17722 persist_dma_mask);
17723 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017724 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17725 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017726 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017727 }
17728 }
17729 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017730 if (err || dma_mask == DMA_BIT_MASK(32)) {
17731 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017732 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017733 dev_err(&pdev->dev,
17734 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017735 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017736 }
17737 }
17738
Michael Chanfdfec1722005-07-25 12:31:48 -070017739 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017740
Matt Carlson0da06062011-05-19 12:12:53 +000017741 /* 5700 B0 chips do not support checksumming correctly due
17742 * to hardware bugs.
17743 */
Joe Perches41535772013-02-16 11:20:04 +000017744 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017745 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17746
17747 if (tg3_flag(tp, 5755_PLUS))
17748 features |= NETIF_F_IPV6_CSUM;
17749 }
17750
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017751 /* TSO is on by default on chips that support hardware TSO.
17752 * Firmware TSO on older chips gives lower performance, so it
17753 * is off by default, but can be enabled using ethtool.
17754 */
Joe Perches63c3a662011-04-26 08:12:10 +000017755 if ((tg3_flag(tp, HW_TSO_1) ||
17756 tg3_flag(tp, HW_TSO_2) ||
17757 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017758 (features & NETIF_F_IP_CSUM))
17759 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017760 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017761 if (features & NETIF_F_IPV6_CSUM)
17762 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017763 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017764 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17765 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17766 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17767 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17768 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017769 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017771
Vlad Yasevich51dfe7b2014-03-24 17:52:12 -040017772 dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
17773 NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlsond542fe22011-05-19 16:02:43 +000017774 dev->vlan_features |= features;
17775
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017776 /*
17777 * Add loopback capability only for a subset of devices that support
17778 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17779 * loopback for the remaining devices.
17780 */
Joe Perches41535772013-02-16 11:20:04 +000017781 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017782 !tg3_flag(tp, CPMU_PRESENT))
17783 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017784 features |= NETIF_F_LOOPBACK;
17785
Matt Carlson0da06062011-05-19 12:12:53 +000017786 dev->hw_features |= features;
Michael Chane565eec2014-01-03 10:09:12 -080017787 dev->priv_flags |= IFF_UNICAST_FLT;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017788
Joe Perches41535772013-02-16 11:20:04 +000017789 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017790 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017791 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017792 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017793 tp->rx_pending = 63;
17794 }
17795
Linus Torvalds1da177e2005-04-16 15:20:36 -070017796 err = tg3_get_device_address(tp);
17797 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017798 dev_err(&pdev->dev,
17799 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017800 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017801 }
17802
Matt Carlson78f90dc2009-11-13 13:03:42 +000017803 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17804 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17805 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017806 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017807 struct tg3_napi *tnapi = &tp->napi[i];
17808
17809 tnapi->tp = tp;
17810 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17811
17812 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017813 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017814 intmbx += 0x8;
17815 else
17816 intmbx += 0x4;
17817
17818 tnapi->consmbox = rcvmbx;
17819 tnapi->prodmbox = sndmbx;
17820
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017821 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017822 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017823 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017824 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017825
Joe Perches63c3a662011-04-26 08:12:10 +000017826 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017827 break;
17828
17829 /*
17830 * If we support MSIX, we'll be using RSS. If we're using
17831 * RSS, the first vector only handles link interrupts and the
17832 * remaining vectors handle rx and tx interrupts. Reuse the
17833 * mailbox values for the next iteration. The values we setup
17834 * above are still useful for the single vectored mode.
17835 */
17836 if (!i)
17837 continue;
17838
17839 rcvmbx += 0x8;
17840
17841 if (sndmbx & 0x4)
17842 sndmbx -= 0x4;
17843 else
17844 sndmbx += 0xc;
17845 }
17846
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017847 /*
17848 * Reset chip in case UNDI or EFI driver did not shutdown
17849 * DMA self test will enable WDMAC and we'll see (spurious)
17850 * pending DMA on the PCI bus at that point.
17851 */
17852 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17853 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017854 tg3_full_lock(tp, 0);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017855 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17856 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017857 tg3_full_unlock(tp);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017858 }
17859
17860 err = tg3_test_dma(tp);
17861 if (err) {
17862 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
17863 goto err_out_apeunmap;
17864 }
17865
Matt Carlsonc88864d2007-11-12 21:07:01 -080017866 tg3_init_coal(tp);
17867
Michael Chanc49a1562006-12-17 17:07:29 -080017868 pci_set_drvdata(pdev, dev);
17869
Joe Perches41535772013-02-16 11:20:04 +000017870 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17871 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17872 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017873 tg3_flag_set(tp, PTP_CAPABLE);
17874
Matt Carlson21f76382012-02-22 12:35:21 +000017875 tg3_timer_init(tp);
17876
Michael Chan402e1392013-02-14 12:13:41 +000017877 tg3_carrier_off(tp);
17878
Linus Torvalds1da177e2005-04-16 15:20:36 -070017879 err = register_netdev(dev);
17880 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017881 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017882 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017883 }
17884
Ivan Vecera20d14a52015-01-08 16:13:07 +010017885 if (tg3_flag(tp, PTP_CAPABLE)) {
17886 tg3_ptp_init(tp);
17887 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
17888 &tp->pdev->dev);
17889 if (IS_ERR(tp->ptp_clock))
17890 tp->ptp_clock = NULL;
17891 }
17892
Joe Perches05dbe002010-02-17 19:44:19 +000017893 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17894 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017895 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017896 tg3_bus_string(tp, str),
17897 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017898
Andrew Lunn22209432016-01-06 20:11:13 +010017899 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017900 char *ethtype;
17901
17902 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17903 ethtype = "10/100Base-TX";
17904 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17905 ethtype = "1000Base-SX";
17906 else
17907 ethtype = "10/100/1000Base-T";
17908
Matt Carlson5129c3a2010-04-05 10:19:23 +000017909 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017910 "(WireSpeed[%d], EEE[%d])\n",
17911 tg3_phy_string(tp), ethtype,
17912 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17913 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017914 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017915
Joe Perches05dbe002010-02-17 19:44:19 +000017916 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017917 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017918 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017919 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017920 tg3_flag(tp, ENABLE_ASF) != 0,
17921 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017922 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17923 tp->dma_rwctrl,
17924 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17925 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017926
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017927 pci_save_state(pdev);
17928
Linus Torvalds1da177e2005-04-16 15:20:36 -070017929 return 0;
17930
Matt Carlson0d3031d2007-10-10 18:02:43 -070017931err_out_apeunmap:
17932 if (tp->aperegs) {
17933 iounmap(tp->aperegs);
17934 tp->aperegs = NULL;
17935 }
17936
Linus Torvalds1da177e2005-04-16 15:20:36 -070017937err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017938 if (tp->regs) {
17939 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017940 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017942
17943err_out_free_dev:
17944 free_netdev(dev);
17945
17946err_out_free_res:
17947 pci_release_regions(pdev);
17948
17949err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080017950 if (pci_is_enabled(pdev))
17951 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017952 return err;
17953}
17954
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017955static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017956{
17957 struct net_device *dev = pci_get_drvdata(pdev);
17958
17959 if (dev) {
17960 struct tg3 *tp = netdev_priv(dev);
17961
Ivan Vecera20d14a52015-01-08 16:13:07 +010017962 tg3_ptp_fini(tp);
17963
Jesper Juhle3c55302012-04-09 22:50:15 +020017964 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017965
Matt Carlsondb219972011-11-04 09:15:03 +000017966 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017967
David S. Miller1805b2f2011-10-24 18:18:09 -040017968 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017969 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017970 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017971 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017972
Linus Torvalds1da177e2005-04-16 15:20:36 -070017973 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017974 if (tp->aperegs) {
17975 iounmap(tp->aperegs);
17976 tp->aperegs = NULL;
17977 }
Michael Chan68929142005-08-09 20:17:14 -070017978 if (tp->regs) {
17979 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017980 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017982 free_netdev(dev);
17983 pci_release_regions(pdev);
17984 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017985 }
17986}
17987
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017988#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017989static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017990{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017991 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017992 struct net_device *dev = pci_get_drvdata(pdev);
17993 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017994 int err = 0;
17995
17996 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070017997
17998 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017999 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018000
Matt Carlsondb219972011-11-04 09:15:03 +000018001 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018002 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018003 tg3_netif_stop(tp);
18004
Matt Carlson21f76382012-02-22 12:35:21 +000018005 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018006
David S. Millerf47c11e2005-06-24 20:18:35 -070018007 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018008 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070018009 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018010
18011 netif_device_detach(dev);
18012
David S. Millerf47c11e2005-06-24 20:18:35 -070018013 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070018014 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000018015 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070018016 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018017
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018018 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018019 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018020 int err2;
18021
David S. Millerf47c11e2005-06-24 20:18:35 -070018022 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018023
Joe Perches63c3a662011-04-26 08:12:10 +000018024 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018025 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018026 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070018027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018028
Matt Carlson21f76382012-02-22 12:35:21 +000018029 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018030
18031 netif_device_attach(dev);
18032 tg3_netif_start(tp);
18033
Michael Chanb9ec6c12006-07-25 16:37:27 -070018034out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018035 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018036
18037 if (!err2)
18038 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018039 }
18040
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018041unlock:
18042 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018043 return err;
18044}
18045
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018046static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018047{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018048 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018049 struct net_device *dev = pci_get_drvdata(pdev);
18050 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018051 int err = 0;
18052
18053 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018054
18055 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018056 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018057
Linus Torvalds1da177e2005-04-16 15:20:36 -070018058 netif_device_attach(dev);
18059
David S. Millerf47c11e2005-06-24 20:18:35 -070018060 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018061
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018062 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
18063
Joe Perches63c3a662011-04-26 08:12:10 +000018064 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000018065 err = tg3_restart_hw(tp,
18066 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070018067 if (err)
18068 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018069
Matt Carlson21f76382012-02-22 12:35:21 +000018070 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018071
Linus Torvalds1da177e2005-04-16 15:20:36 -070018072 tg3_netif_start(tp);
18073
Michael Chanb9ec6c12006-07-25 16:37:27 -070018074out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018075 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018076
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018077 if (!err)
18078 tg3_phy_start(tp);
18079
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018080unlock:
18081 rtnl_unlock();
Michael Chanb9ec6c12006-07-25 16:37:27 -070018082 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018083}
Fabio Estevam42df36a2013-04-16 09:28:29 +000018084#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018085
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018086static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
18087
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018088static void tg3_shutdown(struct pci_dev *pdev)
18089{
18090 struct net_device *dev = pci_get_drvdata(pdev);
18091 struct tg3 *tp = netdev_priv(dev);
18092
18093 rtnl_lock();
18094 netif_device_detach(dev);
18095
18096 if (netif_running(dev))
18097 dev_close(dev);
18098
18099 if (system_state == SYSTEM_POWER_OFF)
18100 tg3_power_down(tp);
18101
18102 rtnl_unlock();
18103}
18104
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018105/**
18106 * tg3_io_error_detected - called when PCI error is detected
18107 * @pdev: Pointer to PCI device
18108 * @state: The current pci connection state
18109 *
18110 * This function is called after a PCI bus error affecting
18111 * this device has been detected.
18112 */
18113static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
18114 pci_channel_state_t state)
18115{
18116 struct net_device *netdev = pci_get_drvdata(pdev);
18117 struct tg3 *tp = netdev_priv(netdev);
18118 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
18119
18120 netdev_info(netdev, "PCI I/O error detected\n");
18121
18122 rtnl_lock();
18123
Gavin Shandfc8f372015-04-24 15:22:23 +100018124 /* We needn't recover from permanent error */
18125 if (state == pci_channel_io_frozen)
18126 tp->pcierr_recovery = true;
Ivan Vecera0486a062014-09-01 14:21:57 +020018127
Gavin Shand8af4df2013-07-24 17:25:08 +080018128 /* We probably don't have netdev yet */
18129 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018130 goto done;
18131
18132 tg3_phy_stop(tp);
18133
18134 tg3_netif_stop(tp);
18135
Matt Carlson21f76382012-02-22 12:35:21 +000018136 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018137
18138 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000018139 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018140
18141 netif_device_detach(netdev);
18142
18143 /* Clean up software state, even if MMIO is blocked */
18144 tg3_full_lock(tp, 0);
18145 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
18146 tg3_full_unlock(tp);
18147
18148done:
Michael Chan72bb72b2013-06-17 13:47:25 -070018149 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018150 if (netdev) {
18151 tg3_napi_enable(tp);
18152 dev_close(netdev);
18153 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018154 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070018155 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018156 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070018157 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018158
18159 rtnl_unlock();
18160
18161 return err;
18162}
18163
18164/**
18165 * tg3_io_slot_reset - called after the pci bus has been reset.
18166 * @pdev: Pointer to PCI device
18167 *
18168 * Restart the card from scratch, as if from a cold-boot.
18169 * At this point, the card has exprienced a hard reset,
18170 * followed by fixups by BIOS, and has its config space
18171 * set up identically to what it was at cold boot.
18172 */
18173static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
18174{
18175 struct net_device *netdev = pci_get_drvdata(pdev);
18176 struct tg3 *tp = netdev_priv(netdev);
18177 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
18178 int err;
18179
18180 rtnl_lock();
18181
18182 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018183 dev_err(&pdev->dev,
18184 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018185 goto done;
18186 }
18187
18188 pci_set_master(pdev);
18189 pci_restore_state(pdev);
18190 pci_save_state(pdev);
18191
Daniel Borkmann68293092013-08-13 11:45:13 -070018192 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018193 rc = PCI_ERS_RESULT_RECOVERED;
18194 goto done;
18195 }
18196
18197 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000018198 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018199 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018200
18201 rc = PCI_ERS_RESULT_RECOVERED;
18202
18203done:
Daniel Borkmann68293092013-08-13 11:45:13 -070018204 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070018205 tg3_napi_enable(tp);
18206 dev_close(netdev);
18207 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018208 rtnl_unlock();
18209
18210 return rc;
18211}
18212
18213/**
18214 * tg3_io_resume - called when traffic can start flowing again.
18215 * @pdev: Pointer to PCI device
18216 *
18217 * This callback is called when the error recovery driver tells
18218 * us that its OK to resume normal operation.
18219 */
18220static void tg3_io_resume(struct pci_dev *pdev)
18221{
18222 struct net_device *netdev = pci_get_drvdata(pdev);
18223 struct tg3 *tp = netdev_priv(netdev);
18224 int err;
18225
18226 rtnl_lock();
18227
18228 if (!netif_running(netdev))
18229 goto done;
18230
18231 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018232 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000018233 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018234 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018235 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018236 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018237 netdev_err(netdev, "Cannot restart hardware after reset.\n");
18238 goto done;
18239 }
18240
18241 netif_device_attach(netdev);
18242
Matt Carlson21f76382012-02-22 12:35:21 +000018243 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018244
18245 tg3_netif_start(tp);
18246
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018247 tg3_full_unlock(tp);
18248
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018249 tg3_phy_start(tp);
18250
18251done:
Ivan Vecera0486a062014-09-01 14:21:57 +020018252 tp->pcierr_recovery = false;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018253 rtnl_unlock();
18254}
18255
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070018256static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018257 .error_detected = tg3_io_error_detected,
18258 .slot_reset = tg3_io_slot_reset,
18259 .resume = tg3_io_resume
18260};
18261
Linus Torvalds1da177e2005-04-16 15:20:36 -070018262static struct pci_driver tg3_driver = {
18263 .name = DRV_MODULE_NAME,
18264 .id_table = tg3_pci_tbl,
18265 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018266 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018267 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018268 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018269 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018270};
18271
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018272module_pci_driver(tg3_driver);